diff --git a/django_lostplaces/lostplaces/models/models.py b/django_lostplaces/lostplaces/models/models.py index 5f0fd44..10f493b 100644 --- a/django_lostplaces/lostplaces/models/models.py +++ b/django_lostplaces/lostplaces/models/models.py @@ -82,6 +82,9 @@ class Explorer(models.Model): choices=EXPLORER_LEVELS ) + def get_places_eligible_to_see(self): + return Place.objects.all().filter(level__lte=self.level) + def __str__(self): return self.user.username diff --git a/django_lostplaces/lostplaces/views/base_views.py b/django_lostplaces/lostplaces/views/base_views.py index 3f07af2..df37d5b 100644 --- a/django_lostplaces/lostplaces/views/base_views.py +++ b/django_lostplaces/lostplaces/views/base_views.py @@ -4,6 +4,7 @@ from django.views import View from django.views.generic.edit import CreateView from django.views.generic.detail import SingleObjectMixin +from django.views.generic import ListView from django.contrib import messages from django.contrib.auth.mixins import UserPassesTestMixin, LoginRequiredMixin @@ -113,3 +114,10 @@ class PlaceAssetDeleteView(IsAuthenticatedMixin, IsPlaceSubmitterMixin, SingleOb self.get_object().delete() messages.success(self.request, self.success_message) return redirect_referer_or(request, reverse('place_detail', kwargs={'pk': place_id})) + + +class LevelCapPlaceListView(ListView): + model = Place + + def get_queryset(self): + return self.request.user.explorer.get_places_eligible_to_see() \ No newline at end of file diff --git a/django_lostplaces/lostplaces/views/place_views.py b/django_lostplaces/lostplaces/views/place_views.py index d391300..bdcee30 100644 --- a/django_lostplaces/lostplaces/views/place_views.py +++ b/django_lostplaces/lostplaces/views/place_views.py @@ -6,7 +6,6 @@ from django.db.models.functions import Lower from django.views import View from django.views.generic.edit import CreateView, UpdateView, DeleteView from django.views.generic.detail import SingleObjectMixin -from django.views.generic import ListView from django.contrib import messages from django.contrib.messages.views import SuccessMessageMixin @@ -16,16 +15,19 @@ from django.shortcuts import render, redirect, get_object_or_404 from django.urls import reverse_lazy, reverse from lostplaces.models import Place, PlaceImage -from lostplaces.views.base_views import IsAuthenticatedMixin, IsPlaceSubmitterMixin +from lostplaces.views.base_views import ( + IsAuthenticatedMixin, + IsPlaceSubmitterMixin, + LevelCapPlaceListView +) from lostplaces.views.place_image_views import MultiplePlaceImageUploadMixin from lostplaces.forms import PlaceForm, PlaceImageForm, TagSubmitForm from lostplaces.common import redirect_referer_or from taggit.models import Tag -class PlaceListView(IsAuthenticatedMixin, ListView): +class PlaceListView(IsAuthenticatedMixin, LevelCapPlaceListView): paginate_by = 5 - model = Place template_name = 'place/place_list.html' ordering = [Lower('name')] diff --git a/django_lostplaces/lostplaces/views/views.py b/django_lostplaces/lostplaces/views/views.py index 74d16a2..a72fd8f 100644 --- a/django_lostplaces/lostplaces/views/views.py +++ b/django_lostplaces/lostplaces/views/views.py @@ -31,7 +31,7 @@ class SignUpView(SuccessMessageMixin, CreateView): class HomeView(IsAuthenticatedMixin, View): def get(self, request, *args, **kwargs): - place_list = Place.objects.all().order_by('-submitted_when')[:10] + place_list = request.user.explorer.get_places_eligible_to_see() context = { 'place_list': place_list, 'mapping_config': {