2020-07-31 14:55:26 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
''' Django views. '''
|
2020-07-29 21:28:12 +02:00
|
|
|
from django.shortcuts import render, redirect, get_object_or_404
|
2020-07-29 00:15:12 +02:00
|
|
|
from django.urls import reverse_lazy
|
2020-08-10 19:54:17 +02:00
|
|
|
from django.views.generic.edit import CreateView, UpdateView, DeleteView
|
2020-08-27 10:31:14 +02:00
|
|
|
from django.views.generic.detail import SingleObjectMixin
|
2020-08-12 21:53:49 +02:00
|
|
|
from django.views.generic import ListView
|
2020-07-29 20:22:04 +02:00
|
|
|
from django.views import View
|
2020-07-31 00:27:01 +02:00
|
|
|
from django.http import Http404
|
2020-08-12 17:18:31 +02:00
|
|
|
from django.contrib import messages
|
2020-08-12 18:59:21 +02:00
|
|
|
from django.contrib.auth.mixins import UserPassesTestMixin, LoginRequiredMixin
|
2020-07-31 00:27:01 +02:00
|
|
|
|
2020-08-12 20:33:09 +02:00
|
|
|
from django.contrib.messages.views import SuccessMessageMixin
|
|
|
|
|
2020-08-06 16:57:43 +02:00
|
|
|
from .forms import (
|
|
|
|
ExplorerCreationForm,
|
|
|
|
PlaceForm,
|
2020-08-11 10:34:39 +02:00
|
|
|
PlaceImageCreateForm
|
2020-08-06 16:57:43 +02:00
|
|
|
)
|
2020-08-26 21:36:10 +02:00
|
|
|
from .models import Place, PlaceImage, Voucher, PhotoAlbum
|
2020-07-26 22:19:37 +02:00
|
|
|
|
|
|
|
# Create your views here.
|
|
|
|
|
2020-08-12 19:07:07 +02:00
|
|
|
# BaseView that checks if user is logged in.
|
2020-08-12 18:59:21 +02:00
|
|
|
class IsAuthenticated(LoginRequiredMixin, View):
|
|
|
|
redirect_field_name = 'redirect_to'
|
2020-08-27 15:46:45 +02:00
|
|
|
login_required_message = 'Please login to proceed'
|
2020-08-27 10:31:14 +02:00
|
|
|
|
|
|
|
def handle_no_permission(self):
|
2020-08-27 15:46:45 +02:00
|
|
|
messages.error(self.request, self.login_required_message)
|
2020-08-27 10:31:14 +02:00
|
|
|
return super().handle_no_permission()
|
2020-08-12 18:59:21 +02:00
|
|
|
|
2020-08-12 19:07:07 +02:00
|
|
|
# BaseView that checks if logged in user is submitter of place.
|
2020-08-27 10:31:14 +02:00
|
|
|
class IsPlaceSubmitter(UserPassesTestMixin, View):
|
|
|
|
place_submitter_error_message = None
|
|
|
|
|
|
|
|
def get_place(self):
|
|
|
|
pass
|
|
|
|
|
2020-08-12 19:07:07 +02:00
|
|
|
def test_func(self):
|
|
|
|
""" Check if user is eligible to modify place. """
|
2020-08-27 10:31:14 +02:00
|
|
|
|
|
|
|
if not hasattr(self.request, 'user'):
|
|
|
|
return False
|
|
|
|
|
2020-08-12 19:07:07 +02:00
|
|
|
if self.request.user.is_superuser:
|
|
|
|
return True
|
|
|
|
|
|
|
|
# Check if currently logged in user was the submitter
|
2020-08-27 10:31:14 +02:00
|
|
|
place_obj = self.get_place()
|
2020-08-12 19:07:07 +02:00
|
|
|
|
2020-08-27 10:31:14 +02:00
|
|
|
if place_obj and hasattr(place_obj, 'submitted_by') and self.request.user == place_obj.submitted_by:
|
2020-08-12 19:07:07 +02:00
|
|
|
return True
|
|
|
|
|
2020-08-27 10:31:14 +02:00
|
|
|
if self.place_submitter_error_message:
|
|
|
|
messages.error(self.request, self.place_submitter_error_message)
|
2020-08-12 19:07:07 +02:00
|
|
|
return False
|
|
|
|
|
2020-08-12 20:33:09 +02:00
|
|
|
class SignUpView(SuccessMessageMixin, CreateView):
|
2020-07-29 00:15:12 +02:00
|
|
|
form_class = ExplorerCreationForm
|
|
|
|
success_url = reverse_lazy('login')
|
|
|
|
template_name = 'signup.html'
|
2020-08-12 20:56:37 +02:00
|
|
|
success_message = 'User created.'
|
2020-07-29 00:15:12 +02:00
|
|
|
|
2020-08-12 21:53:49 +02:00
|
|
|
class PlaceListView(IsAuthenticated, ListView):
|
2020-08-20 23:33:33 +02:00
|
|
|
paginate_by = 5
|
2020-08-12 21:53:49 +02:00
|
|
|
model = Place
|
|
|
|
template_name = 'place/place_list.html'
|
2020-08-22 02:59:04 +02:00
|
|
|
ordering = ['name']
|
2020-07-26 23:25:07 +02:00
|
|
|
|
2020-08-22 09:24:50 +02:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
2020-08-22 09:58:57 +02:00
|
|
|
context['place_map_center'] = Place.average_latlon(context['place_list'])
|
2020-08-22 09:24:50 +02:00
|
|
|
return context
|
|
|
|
|
2020-08-12 19:24:04 +02:00
|
|
|
class PlaceDetailView(IsAuthenticated, View):
|
2020-08-12 20:11:25 +02:00
|
|
|
def get(self, request, pk):
|
2020-08-22 09:19:01 +02:00
|
|
|
place = Place.objects.get(pk=pk)
|
2020-08-12 19:24:04 +02:00
|
|
|
context = {
|
2020-08-22 09:19:01 +02:00
|
|
|
'place': place,
|
|
|
|
'place_list': [ place ],
|
|
|
|
'place_map_center': [ place.latitude, place.longitude ]
|
2020-08-12 19:24:04 +02:00
|
|
|
}
|
|
|
|
return render(request, 'place/place_detail.html', context)
|
2020-07-26 22:19:37 +02:00
|
|
|
|
2020-08-03 19:14:37 +02:00
|
|
|
class HomeView(View):
|
2020-08-05 18:57:09 +02:00
|
|
|
def get(self, request, *args, **kwargs):
|
2020-08-12 22:00:45 +02:00
|
|
|
place_list = Place.objects.all().order_by('-submitted_when')[:10]
|
2020-08-21 10:31:30 +02:00
|
|
|
place_map_center = Place.average_latlon(place_list)
|
2020-08-05 18:57:09 +02:00
|
|
|
context = {
|
2020-08-20 21:38:01 +02:00
|
|
|
'place_list': place_list,
|
|
|
|
'place_map_center': place_map_center
|
2020-08-05 18:57:09 +02:00
|
|
|
}
|
|
|
|
return render(request, 'home.html', context)
|
2020-08-03 19:14:37 +02:00
|
|
|
|
2020-08-27 10:31:14 +02:00
|
|
|
class PlaceUpdateView(IsAuthenticated, IsPlaceSubmitter, SuccessMessageMixin, UpdateView):
|
2020-08-10 20:00:54 +02:00
|
|
|
template_name = 'place/place_update.html'
|
2020-07-31 00:27:01 +02:00
|
|
|
model = Place
|
|
|
|
form_class = PlaceForm
|
2020-08-12 20:52:17 +02:00
|
|
|
success_message = 'Successfully updated place.'
|
2020-08-27 10:31:14 +02:00
|
|
|
place_submitter_error_message = 'You do no have permissions to alter this place'
|
2020-07-31 00:27:01 +02:00
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
return reverse_lazy('place_detail', kwargs={'pk':self.get_object().pk})
|
|
|
|
|
2020-08-27 10:31:14 +02:00
|
|
|
def get_place(self):
|
|
|
|
return self.get_object()
|
|
|
|
|
2020-08-12 18:59:21 +02:00
|
|
|
class PlaceCreateView(IsAuthenticated, View):
|
2020-08-01 19:08:29 +02:00
|
|
|
|
2020-07-29 20:22:04 +02:00
|
|
|
def get(self, request, *args, **kwargs):
|
2020-07-30 13:09:00 +02:00
|
|
|
place_image_form = PlaceImageCreateForm()
|
2020-07-31 00:27:01 +02:00
|
|
|
place_form = PlaceForm()
|
2020-07-30 14:27:17 +02:00
|
|
|
|
2020-07-30 10:51:05 +02:00
|
|
|
context = {
|
2020-07-30 13:09:00 +02:00
|
|
|
'place_form': place_form,
|
|
|
|
'place_image_form': place_image_form
|
2020-07-30 10:51:05 +02:00
|
|
|
}
|
2020-08-10 20:00:54 +02:00
|
|
|
return render(request, 'place/place_create.html', context)
|
2020-07-30 10:51:05 +02:00
|
|
|
|
2020-07-29 20:22:04 +02:00
|
|
|
def post(self, request, *args, **kwargs):
|
|
|
|
place_form = PlaceForm(request.POST)
|
2020-07-30 13:09:00 +02:00
|
|
|
|
2020-07-30 14:15:28 +02:00
|
|
|
if place_form.is_valid():
|
|
|
|
submitter = request.user
|
2020-07-30 14:26:52 +02:00
|
|
|
place = place_form.save(commit=False)
|
2020-07-29 20:22:04 +02:00
|
|
|
# Save logged in user as "submitted_by"
|
2020-07-30 14:26:52 +02:00
|
|
|
place.submitted_by = submitter
|
|
|
|
place.save()
|
2020-07-30 14:15:28 +02:00
|
|
|
|
|
|
|
if request.FILES:
|
2020-07-30 14:26:52 +02:00
|
|
|
self._apply_multipart_image_upload(
|
2020-07-30 22:33:41 +02:00
|
|
|
files=request.FILES.getlist('filename'),
|
|
|
|
place=place,
|
|
|
|
submitter=submitter
|
2020-07-30 14:26:52 +02:00
|
|
|
)
|
2020-07-30 14:15:28 +02:00
|
|
|
|
2020-07-30 10:51:05 +02:00
|
|
|
kwargs_to_pass = {
|
2020-07-30 14:26:52 +02:00
|
|
|
'pk': place.pk
|
2020-07-30 10:51:05 +02:00
|
|
|
}
|
2020-08-12 20:35:49 +02:00
|
|
|
|
|
|
|
messages.success(
|
|
|
|
self.request, 'Successfully created place.')
|
2020-07-30 10:51:05 +02:00
|
|
|
return redirect(reverse_lazy('place_detail', kwargs=kwargs_to_pass))
|
2020-08-12 20:35:49 +02:00
|
|
|
|
2020-07-29 20:22:04 +02:00
|
|
|
else:
|
2020-07-30 10:51:05 +02:00
|
|
|
context = {
|
|
|
|
'form': form_place
|
|
|
|
}
|
2020-08-12 20:35:49 +02:00
|
|
|
|
|
|
|
# Usually the browser should have checked the form before sending.
|
|
|
|
messages.error(
|
|
|
|
self.request, 'Please fill in all required fields.')
|
2020-08-10 20:00:54 +02:00
|
|
|
return render(request, 'place/place_create.html', context)
|
2020-07-30 14:26:52 +02:00
|
|
|
|
2020-08-04 22:10:31 +02:00
|
|
|
def _apply_multipart_image_upload(self, files, place, submitter):
|
2020-08-09 11:40:23 +02:00
|
|
|
for image in files:
|
2020-07-30 14:27:17 +02:00
|
|
|
place_image = PlaceImage.objects.create(
|
|
|
|
filename=image,
|
|
|
|
place=place,
|
|
|
|
submitted_by=submitter
|
|
|
|
)
|
|
|
|
place_image.save()
|
2020-08-06 16:57:43 +02:00
|
|
|
|
2020-08-27 10:31:14 +02:00
|
|
|
class PlaceDeleteView(IsAuthenticated, IsPlaceSubmitter, DeleteView):
|
2020-08-10 20:00:54 +02:00
|
|
|
template_name = 'place/place_delete.html'
|
2020-08-10 19:54:17 +02:00
|
|
|
model = Place
|
2020-08-12 20:52:17 +02:00
|
|
|
success_message = 'Successfully deleted place.'
|
2020-08-12 20:56:37 +02:00
|
|
|
success_url = reverse_lazy('place_list')
|
2020-08-12 21:07:13 +02:00
|
|
|
success_message = 'Place deleted'
|
2020-08-27 10:31:14 +02:00
|
|
|
place_submitter_error_message = 'You do no have permission to delete this place'
|
2020-08-12 21:07:13 +02:00
|
|
|
|
|
|
|
def delete(self, request, *args, **kwargs):
|
|
|
|
messages.success(self.request, self.success_message)
|
|
|
|
return super().delete(request, *args, **kwargs)
|
2020-08-26 21:36:10 +02:00
|
|
|
|
2020-08-27 10:31:14 +02:00
|
|
|
def get_place(self):
|
|
|
|
return self.get_object()
|
|
|
|
|
2020-08-26 21:36:10 +02:00
|
|
|
class AlbumCreateView(IsAuthenticated, View):
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
url = request.GET['url']
|
|
|
|
place_id = request.GET['place_id']
|
|
|
|
place = Place.objects.get(pk=place_id)
|
|
|
|
photo_album = PhotoAlbum()
|
|
|
|
photo_album.url = url
|
|
|
|
photo_album.place = place
|
|
|
|
photo_album.submitted_by = request.user
|
|
|
|
photo_album.save()
|
|
|
|
print(photo_album)
|
|
|
|
return redirect(reverse_lazy('place_detail', kwargs={'pk': place_id}))
|
|
|
|
|
2020-08-27 10:31:14 +02:00
|
|
|
class PhotoAlbumCreateView(IsAuthenticated, SuccessMessageMixin, CreateView):
|
2020-08-26 21:36:10 +02:00
|
|
|
model = PhotoAlbum
|
|
|
|
fields = ['url', 'label']
|
|
|
|
template_name = 'photo_album/photo_album_create.html'
|
2020-08-27 10:31:14 +02:00
|
|
|
success_message = 'Photo Album submitted'
|
2020-08-26 21:36:10 +02:00
|
|
|
|
|
|
|
def get(self, request, place_id, *args, **kwargs):
|
|
|
|
self.place = Place.objects.get(pk=place_id)
|
|
|
|
return super().get(request, *args, **kwargs)
|
|
|
|
|
|
|
|
def post(self, request, place_id, *args, **kwargs):
|
|
|
|
self.place = Place.objects.get(pk=place_id)
|
|
|
|
response = super().post(request, *args, **kwargs)
|
|
|
|
self.object.place = self.place
|
|
|
|
self.object.submitted_by = request.user
|
|
|
|
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):
|
2020-08-27 10:31:14 +02:00
|
|
|
return reverse_lazy('place_detail', kwargs={'pk': self.place.id})
|
|
|
|
|
|
|
|
class PhotoAlbumDeleteView(IsAuthenticated, IsPlaceSubmitter, SingleObjectMixin, View):
|
|
|
|
model = PhotoAlbum
|
|
|
|
pk_url_kwarg = 'pk'
|
|
|
|
success_message = 'Photo Album deleted'
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
if self.get_object().submitted_by == self.request.user:
|
|
|
|
return True
|
|
|
|
|
|
|
|
messages.error(self.request, 'You do not have permissions to alter this photo album')
|
|
|
|
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)
|
|
|
|
return redirect(reverse_lazy('place_detail', kwargs={'pk': place_id}))
|