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

129 lines
4.6 KiB
Python
Raw Normal View History

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.views.generic import ListView
from django.contrib import messages
from django.contrib.messages.views import SuccessMessageMixin
2020-09-18 23:32:52 +02:00
from django.shortcuts import render, redirect, get_object_or_404
2020-08-30 17:11:24 +02:00
from django.urls import reverse_lazy
from lostplaces.models import Place, PlaceImage
from lostplaces.views import IsAuthenticatedMixin, IsPlaceSubmitterMixin
from lostplaces.forms import PlaceForm, PlaceImageCreateForm, TagSubmitForm
2020-08-30 17:11:24 +02:00
2020-08-31 18:18:24 +02:00
from taggit.models import Tag
2020-09-13 10:56:18 +02:00
class PlaceListView(IsAuthenticatedMixin, ListView):
2020-08-30 17:11:24 +02:00
paginate_by = 5
model = Place
template_name = 'place/place_list.html'
ordering = ['name']
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
2020-09-13 10:56:18 +02:00
class PlaceDetailView(IsAuthenticatedMixin, View):
2020-08-30 17:11:24 +02:00
def get(self, request, pk):
2020-09-18 23:32:52 +02:00
place = get_object_or_404(Place, pk=pk)
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,
2020-09-12 11:24:16 +02:00
'submit_url_name': 'place_tag_submit',
2020-09-02 00:08:00 +02:00
'delete_url_name': 'place_tag_delete'
}
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.'
place_submitter_error_message = 'You do no have permissions to alter this place'
def get_success_url(self):
return reverse_lazy('place_detail', kwargs={'pk':self.get_object().pk})
def get_place(self):
return self.get_object()
2020-09-13 10:56:18 +02:00
class PlaceCreateView(IsAuthenticatedMixin, View):
2020-08-30 17:11:24 +02:00
def get(self, request, *args, **kwargs):
place_image_form = PlaceImageCreateForm()
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()
if request.FILES:
self._apply_multipart_image_upload(
files=request.FILES.getlist('filename'),
place=place,
submitter=submitter
)
messages.success(
2020-09-13 20:39:32 +02:00
self.request,
'Successfully created place.'
)
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,
'Please fill in all required fields.'
)
return render(request, 'place/place_create.html', context={'form': form_place})
2020-08-30 17:11:24 +02:00
def _apply_multipart_image_upload(self, files, place, submitter):
for image in files:
place_image = PlaceImage.objects.create(
filename=image,
place=place,
submitted_by=submitter
)
place_image.save()
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.'
success_url = reverse_lazy('place_list')
success_message = 'Place deleted'
place_submitter_error_message = 'You do no have permission to delete this place'
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()