2020-09-19 22:50:07 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2020-08-30 17:11:24 +02:00
|
|
|
from django.views import View
|
2020-08-30 17:26:43 +02:00
|
|
|
from django.views.generic.edit import CreateView
|
|
|
|
from django.views.generic.detail import SingleObjectMixin
|
2021-10-01 22:38:44 +02:00
|
|
|
from django.views.generic import ListView
|
2020-08-30 17:11:24 +02:00
|
|
|
|
|
|
|
from django.contrib import messages
|
2020-08-30 17:26:43 +02:00
|
|
|
from django.contrib.auth.mixins import UserPassesTestMixin, LoginRequiredMixin
|
|
|
|
from django.contrib.messages.views import SuccessMessageMixin
|
|
|
|
|
2020-09-18 23:32:52 +02:00
|
|
|
from django.shortcuts import redirect, get_object_or_404
|
2020-12-25 13:00:30 +01:00
|
|
|
from django.urls import reverse_lazy, reverse
|
2022-09-17 18:07:29 +02:00
|
|
|
from django.utils.translation import gettext as _
|
2020-08-30 17:26:43 +02:00
|
|
|
|
2020-09-14 17:26:17 +02:00
|
|
|
from lostplaces.models import Place
|
2020-12-25 12:57:05 +01:00
|
|
|
from lostplaces.common import redirect_referer_or
|
2020-08-30 17:11:24 +02:00
|
|
|
|
2020-09-13 10:56:18 +02:00
|
|
|
class IsAuthenticatedMixin(LoginRequiredMixin, View):
|
2020-09-09 22:52:28 +02:00
|
|
|
'''
|
2020-09-21 21:37:28 +02:00
|
|
|
A view mixin that checks wether a user is logged in or not.
|
2020-09-09 22:52:28 +02:00
|
|
|
If the user is not logged in, he gets redirected to
|
|
|
|
the login page.
|
|
|
|
'''
|
2020-09-10 18:38:26 +02:00
|
|
|
login_url = reverse_lazy('login')
|
2020-10-11 01:12:52 +02:00
|
|
|
permission_denied_message = _('Please login to proceed')
|
2020-08-30 17:11:24 +02:00
|
|
|
|
|
|
|
def handle_no_permission(self):
|
2021-10-01 23:41:34 +02:00
|
|
|
if not self.request.user.is_authenticated:
|
|
|
|
messages.error(self.request, self.permission_denied_message)
|
2020-08-30 17:11:24 +02:00
|
|
|
return super().handle_no_permission()
|
|
|
|
|
2022-10-16 10:14:04 +02:00
|
|
|
class IsSuperUserMixin(UserPassesTestMixin, View):
|
|
|
|
'''
|
|
|
|
A view mixin that checks if the user is a superuser.
|
|
|
|
Users who are not logged in or users who are no superuser get
|
|
|
|
a permission deined message.
|
|
|
|
'''
|
|
|
|
permission_denied_message = _('You are not allowed to see this page')
|
|
|
|
|
|
|
|
def test_func(self):
|
|
|
|
return self.request.user.is_superuser
|
|
|
|
|
|
|
|
def handle_no_permission(self):
|
|
|
|
messages.error(self.request, self.permission_denied_message)
|
|
|
|
return super().handle_no_permission()
|
|
|
|
|
2020-09-13 10:57:53 +02:00
|
|
|
class IsPlaceSubmitterMixin(UserPassesTestMixin, View):
|
2020-09-09 22:52:28 +02:00
|
|
|
'''
|
2020-09-21 21:37:28 +02:00
|
|
|
A view mixin that checks wether a user is the submitter
|
|
|
|
of a place, throws 403 if the user is not. The subclass
|
|
|
|
has to provide a get_place method, which returns the
|
2020-09-09 22:52:28 +02:00
|
|
|
place to check.
|
|
|
|
'''
|
2020-08-30 17:11:24 +02:00
|
|
|
place_submitter_error_message = None
|
|
|
|
|
|
|
|
def get_place(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_func(self):
|
|
|
|
""" Check if user is eligible to modify place. """
|
|
|
|
|
|
|
|
if not hasattr(self.request, 'user'):
|
|
|
|
return False
|
|
|
|
|
|
|
|
if self.request.user.is_superuser:
|
|
|
|
return True
|
|
|
|
|
|
|
|
# Check if currently logged in user was the submitter
|
|
|
|
place_obj = self.get_place()
|
|
|
|
|
2020-09-10 22:30:29 +02:00
|
|
|
if place_obj and hasattr(place_obj, 'submitted_by') and self.request.user.explorer == place_obj.submitted_by:
|
2020-08-30 17:11:24 +02:00
|
|
|
return True
|
|
|
|
|
|
|
|
if self.place_submitter_error_message:
|
|
|
|
messages.error(self.request, self.place_submitter_error_message)
|
|
|
|
return False
|
2021-10-01 23:41:34 +02:00
|
|
|
|
|
|
|
class IsEligibleToSeePlaceMixin(UserPassesTestMixin):
|
|
|
|
not_eligible_to_see_message = None
|
|
|
|
|
|
|
|
def get_place(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_func(self):
|
|
|
|
if not hasattr(self.request, 'user'):
|
|
|
|
return False
|
|
|
|
|
|
|
|
if self.request.user.explorer.is_eligible_to_see(self.get_place()):
|
|
|
|
return True
|
|
|
|
|
|
|
|
if self.not_eligible_to_see_message:
|
|
|
|
messages.error(self.request, self.not_eligible_to_see_message)
|
|
|
|
return False
|
2020-08-30 17:26:43 +02:00
|
|
|
|
2020-09-13 10:56:18 +02:00
|
|
|
class PlaceAssetCreateView(IsAuthenticatedMixin, SuccessMessageMixin, CreateView):
|
2021-05-15 23:27:33 +02:00
|
|
|
"""
|
|
|
|
Abstract View for creating a place asset (i.e. PlaceImage)
|
|
|
|
"""
|
2020-08-30 17:26:43 +02:00
|
|
|
model = None
|
|
|
|
template_name = ''
|
|
|
|
success_message = ''
|
|
|
|
|
|
|
|
def get(self, request, place_id, *args, **kwargs):
|
2020-09-18 23:32:52 +02:00
|
|
|
self.place = get_object_or_404(Place, pk=place_id)
|
2020-08-30 17:26:43 +02:00
|
|
|
return super().get(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def post(self, request, place_id, *args, **kwargs):
|
2020-09-18 23:32:52 +02:00
|
|
|
self.place = get_object_or_404(Place, pk=place_id)
|
2020-08-30 17:26:43 +02:00
|
|
|
response = super().post(request, *args, **kwargs)
|
|
|
|
self.object.place = self.place
|
2020-09-10 22:30:29 +02:00
|
|
|
self.object.submitted_by = request.user.explorer
|
2020-08-30 17:26:43 +02:00
|
|
|
self.object.save()
|
|
|
|
return response
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context['place'] = self.place
|
|
|
|
return context
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
return reverse_lazy('place_detail', kwargs={'pk': self.place.id})
|
|
|
|
|
2020-09-13 10:57:53 +02:00
|
|
|
class PlaceAssetDeleteView(IsAuthenticatedMixin, IsPlaceSubmitterMixin, SingleObjectMixin, View):
|
2020-08-30 17:26:43 +02:00
|
|
|
model = None
|
|
|
|
success_message = ''
|
|
|
|
permission_denied_message = ''
|
|
|
|
|
|
|
|
def get_place(self):
|
|
|
|
place_id = self.get_object().place.id
|
|
|
|
return Place.objects.get(pk=place_id)
|
|
|
|
|
|
|
|
def test_func(self):
|
|
|
|
can_edit_place = super().test_func()
|
|
|
|
if can_edit_place:
|
|
|
|
return True
|
|
|
|
|
2020-09-10 22:30:29 +02:00
|
|
|
if self.get_object().submitted_by == self.request.user.explorer:
|
2020-08-30 17:26:43 +02:00
|
|
|
return True
|
|
|
|
|
|
|
|
messages.error(self.request, self.permission_denied_message)
|
|
|
|
return False
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
place_id = self.get_object().place.id
|
|
|
|
self.get_object().delete()
|
|
|
|
messages.success(self.request, self.success_message)
|
2020-12-25 12:57:05 +01:00
|
|
|
return redirect_referer_or(request, reverse('place_detail', kwargs={'pk': place_id}))
|
2021-10-01 22:38:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
class LevelCapPlaceListView(ListView):
|
|
|
|
model = Place
|
|
|
|
|
|
|
|
def get_queryset(self):
|
2022-09-24 12:10:19 +02:00
|
|
|
return self.request.user.explorer.get_place_list_to_display()
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
|
|
|
context['place_list'] = context.pop('object_list')
|
|
|
|
return context
|
|
|
|
|