#42 Checking ListView for Levels

This commit is contained in:
reverend 2021-10-01 22:38:44 +02:00
parent b1cd8fd0cc
commit b607335299
4 changed files with 18 additions and 5 deletions

View File

@ -82,6 +82,9 @@ class Explorer(models.Model):
choices=EXPLORER_LEVELS choices=EXPLORER_LEVELS
) )
def get_places_eligible_to_see(self):
return Place.objects.all().filter(level__lte=self.level)
def __str__(self): def __str__(self):
return self.user.username return self.user.username

View File

@ -4,6 +4,7 @@
from django.views import View from django.views import View
from django.views.generic.edit import CreateView from django.views.generic.edit import CreateView
from django.views.generic.detail import SingleObjectMixin from django.views.generic.detail import SingleObjectMixin
from django.views.generic import ListView
from django.contrib import messages from django.contrib import messages
from django.contrib.auth.mixins import UserPassesTestMixin, LoginRequiredMixin from django.contrib.auth.mixins import UserPassesTestMixin, LoginRequiredMixin
@ -113,3 +114,10 @@ class PlaceAssetDeleteView(IsAuthenticatedMixin, IsPlaceSubmitterMixin, SingleOb
self.get_object().delete() self.get_object().delete()
messages.success(self.request, self.success_message) messages.success(self.request, self.success_message)
return redirect_referer_or(request, reverse('place_detail', kwargs={'pk': place_id})) 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()

View File

@ -6,7 +6,6 @@ from django.db.models.functions import Lower
from django.views import View from django.views import View
from django.views.generic.edit import CreateView, UpdateView, DeleteView from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.views.generic.detail import SingleObjectMixin from django.views.generic.detail import SingleObjectMixin
from django.views.generic import ListView
from django.contrib import messages from django.contrib import messages
from django.contrib.messages.views import SuccessMessageMixin 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 django.urls import reverse_lazy, reverse
from lostplaces.models import Place, PlaceImage 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.views.place_image_views import MultiplePlaceImageUploadMixin
from lostplaces.forms import PlaceForm, PlaceImageForm, TagSubmitForm from lostplaces.forms import PlaceForm, PlaceImageForm, TagSubmitForm
from lostplaces.common import redirect_referer_or from lostplaces.common import redirect_referer_or
from taggit.models import Tag from taggit.models import Tag
class PlaceListView(IsAuthenticatedMixin, ListView): class PlaceListView(IsAuthenticatedMixin, LevelCapPlaceListView):
paginate_by = 5 paginate_by = 5
model = Place
template_name = 'place/place_list.html' template_name = 'place/place_list.html'
ordering = [Lower('name')] ordering = [Lower('name')]

View File

@ -31,7 +31,7 @@ class SignUpView(SuccessMessageMixin, CreateView):
class HomeView(IsAuthenticatedMixin, View): class HomeView(IsAuthenticatedMixin, View):
def get(self, request, *args, **kwargs): 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 = { context = {
'place_list': place_list, 'place_list': place_list,
'mapping_config': { 'mapping_config': {