lostplaces-backend/django_lostplaces/lostplaces/views/place_views.py

230 lines
8.1 KiB
Python
Raw Normal View History

2020-09-19 22:50:07 +02:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
2021-12-30 23:20:05 +01:00
from datetime import timedelta
2020-09-19 22:50:07 +02:00
2020-09-29 21:41:36 +02:00
from django.db.models.functions import Lower
2020-08-30 17:11:24 +02:00
from django.views import View
from django.views.generic.edit import CreateView, UpdateView, DeleteView
from django.views.generic.detail import SingleObjectMixin
from django.contrib import messages
from django.contrib.messages.views import SuccessMessageMixin
2022-09-17 18:07:29 +02:00
from django.utils.translation import gettext as _
2021-12-30 23:20:05 +01:00
from django.utils import timezone
2020-08-30 17:11:24 +02:00
2020-09-18 23:32:52 +02:00
from django.shortcuts import render, redirect, get_object_or_404
2020-12-25 13:00:30 +01:00
from django.urls import reverse_lazy, reverse
2020-08-30 17:11:24 +02:00
2021-12-30 23:20:05 +01:00
from lostplaces.models import (
Place,
PlaceImage,
PlaceVoting,
PLACE_LEVELS
2021-12-30 23:20:05 +01:00
)
2021-10-01 22:38:44 +02:00
from lostplaces.views.base_views import (
IsAuthenticatedMixin,
IsPlaceSubmitterMixin,
LevelCapPlaceListView,
IsEligibleToSeePlaceMixin
2021-10-01 22:38:44 +02:00
)
from lostplaces.views.place_image_views import MultiplePlaceImageUploadMixin
from lostplaces.forms import PlaceForm, PlaceImageForm, TagSubmitForm
2020-12-25 12:57:05 +01:00
from lostplaces.common import redirect_referer_or
2020-08-30 17:11:24 +02:00
2020-08-31 18:18:24 +02:00
from taggit.models import Tag
2021-10-01 22:38:44 +02:00
class PlaceListView(IsAuthenticatedMixin, LevelCapPlaceListView):
2020-08-30 17:11:24 +02:00
paginate_by = 5
template_name = 'place/place_list.html'
2020-09-29 21:41:36 +02:00
ordering = [Lower('name')]
2020-08-30 17:11:24 +02:00
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
2020-09-13 20:15:49 +02:00
context['mapping_config'] = {
'all_points': context['place_list'],
2020-09-12 12:02:25 +02:00
'map_center': Place.average_latlon(context['place_list'])
}
2020-08-30 17:11:24 +02:00
return context
class PlaceDetailView(IsAuthenticatedMixin, IsEligibleToSeePlaceMixin, View):
not_eligible_to_see_message = _('You\'r not allowed to see this place')
def get_place(self):
return get_object_or_404(Place, pk=self.kwargs['pk'])
2021-12-30 23:20:05 +01:00
def get(self, request, pk):
place = self.get_place()
2021-12-30 23:20:05 +01:00
place.calculate_place_level()
explorer = request.user.explorer
2020-08-30 17:11:24 +02:00
context = {
'place': place,
2020-09-13 20:15:49 +02:00
'mapping_config': {
'all_points': [ place ],
2020-09-12 11:42:31 +02:00
'map_center': {'latitude': place.latitude, 'longitude': place.longitude},
},
2020-09-02 00:08:00 +02:00
'tagging_config': {
2020-09-12 11:24:16 +02:00
'all_tags': Tag.objects.all(),
2020-09-02 00:08:00 +02:00
'submit_form': TagSubmitForm(),
'tagged_item': place,
'submit_url': reverse('place_tag_submit', kwargs={'tagged_id': place.id}),
2020-09-02 00:08:00 +02:00
'delete_url_name': 'place_tag_delete'
2021-12-30 23:20:05 +01:00
},
'placevoting': {
'users_vote': PlaceVoting.objects.filter(place=place, submitted_by=explorer).first(),
'all_choices': reversed(PLACE_LEVELS)
}
2020-08-30 17:11:24 +02:00
}
return render(request, 'place/place_detail.html', context)
2020-09-13 10:57:53 +02:00
class PlaceUpdateView(IsAuthenticatedMixin, IsPlaceSubmitterMixin, SuccessMessageMixin, UpdateView):
2020-08-30 17:11:24 +02:00
template_name = 'place/place_update.html'
model = Place
form_class = PlaceForm
success_message = _('Successfully updated place')
2020-10-11 21:27:27 +02:00
place_submitter_error_message = _('You are not allowed to edit this place')
2020-08-30 17:11:24 +02:00
def get_success_url(self):
return reverse_lazy('place_detail', kwargs={'pk':self.get_object().pk})
def get_place(self):
return self.get_object()
class PlaceCreateView(MultiplePlaceImageUploadMixin, IsAuthenticatedMixin, View):
2020-08-30 17:11:24 +02:00
def get(self, request, *args, **kwargs):
place_image_form = PlaceImageForm()
2020-08-30 17:11:24 +02:00
place_form = PlaceForm()
context = {
'place_form': place_form,
'place_image_form': place_image_form
}
return render(request, 'place/place_create.html', context)
def post(self, request, *args, **kwargs):
place_form = PlaceForm(request.POST)
if place_form.is_valid():
submitter = request.user.explorer
2020-08-30 17:11:24 +02:00
place = place_form.save(commit=False)
# Save logged in user as "submitted_by"
place.submitted_by = submitter
place.save()
self.handle_place_images(request, place)
2020-08-30 17:11:24 +02:00
messages.success(
2020-09-13 20:39:32 +02:00
self.request,
_('Successfully created place')
2020-09-13 20:39:32 +02:00
)
return redirect(reverse_lazy('place_detail', kwargs={'pk': place.pk}))
2020-08-30 17:11:24 +02:00
else:
# Usually the browser should have checked the form before sending.
messages.error(
2020-09-13 20:39:32 +02:00
self.request,
2020-10-11 21:27:27 +02:00
_('Please fill in all required fields.')
2020-09-13 20:39:32 +02:00
)
return render(
request=request,
template_name='place/place_create.html',
context={
'place_form': place_form,
'place_image_form': PlaceImageForm()
},
status=400
)
2020-08-30 17:11:24 +02:00
2020-09-13 10:57:53 +02:00
class PlaceDeleteView(IsAuthenticatedMixin, IsPlaceSubmitterMixin, DeleteView):
2020-08-30 17:11:24 +02:00
template_name = 'place/place_delete.html'
model = Place
success_message = _('Successfully deleted place')
2020-08-30 17:11:24 +02:00
success_url = reverse_lazy('place_list')
2020-10-11 21:27:27 +02:00
place_submitter_error_message = _('You are not allowed to delete this place')
2020-08-30 17:11:24 +02:00
def delete(self, request, *args, **kwargs):
messages.success(self.request, self.success_message)
return super().delete(request, *args, **kwargs)
def get_place(self):
return self.get_object()
2020-12-24 15:56:33 +01:00
class PlaceFavoriteView(IsAuthenticatedMixin, IsEligibleToSeePlaceMixin, View):
not_eligible_to_see_message = _('You\'r not allowed to favorite this place')
def get_place(self):
return get_object_or_404(Place, pk=self.kwargs['place_id'])
2020-12-24 15:56:33 +01:00
def get(self, request, place_id):
place = self.get_place()
2020-12-24 15:56:33 +01:00
if request.user is not None:
request.user.explorer.favorite_places.add(place)
request.user.explorer.save()
2020-12-25 12:57:05 +01:00
return redirect_referer_or(request, reverse('place_detail', kwargs={'pk': place.pk}))
2020-12-24 15:56:33 +01:00
class PlaceUnfavoriteView(IsAuthenticatedMixin, View):
2020-12-24 15:56:33 +01:00
def get(self, request, place_id):
place = get_object_or_404(Place, id=place_id)
if request.user is not None:
request.user.explorer.favorite_places.remove(place)
request.user.explorer.save()
return redirect_referer_or(request, reverse('place_detail', kwargs={'pk': place.pk}))
class PlaceVisitCreateView(IsAuthenticatedMixin, IsEligibleToSeePlaceMixin, View):
not_eligible_to_see_message = _('You\'r not allowed to visit this place :P (Now please stop trying out URL\'s)')
def get_place(self):
return get_object_or_404(Place, pk=self.kwargs['place_id'])
def get(self, request, place_id):
place = self.get_place()
if request.user is not None:
request.user.explorer.visited_places.add(place)
request.user.explorer.save()
return redirect_referer_or(request, reverse('place_detail', kwargs={'pk': place.pk}))
class PlaceVisitDeleteView(IsAuthenticatedMixin, View):
def get(self, request, place_id):
place = get_object_or_404(Place, id=place_id)
if request.user is not None:
request.user.explorer.visited_places.remove(place)
request.user.explorer.save()
return redirect_referer_or(request, reverse('place_detail', kwargs={'pk': place.pk}))
2021-12-30 23:20:05 +01:00
class PlaceVoteView(IsEligibleToSeePlaceMixin, View):
delta = timedelta(weeks=24)
def get(self, request, place_id, vote):
place = get_object_or_404(Place, id=place_id)
explorer = request.user.explorer
voting = PlaceVoting.objects.filter(
submitted_by=explorer,
place=place
).first()
if voting is None:
voting = PlaceVoting.objects.create(
submitted_by=explorer,
place=place,
vote=vote,
expires_when=timezone.now()+self.delta
)
messages.success(self.request, _('Vote submitted'))
else:
voting.expires_when=timezone.now()+self.delta
voting.vote = vote
messages.success(self.request, _('Your vote has been update'))
voting.save()
return redirect_referer_or(request, reverse('place_detail', kwargs={'pk': place.pk}))