2020-09-26 09:48:06 +02:00
|
|
|
from django.views import View
|
2020-09-26 16:08:37 +02:00
|
|
|
from django.shortcuts import get_object_or_404, redirect
|
2020-10-11 07:50:15 +02:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2020-09-26 09:48:06 +02:00
|
|
|
|
2020-09-26 16:08:37 +02:00
|
|
|
from lostplaces.views.base_views import PlaceAssetCreateView, PlaceAssetDeleteView
|
2020-09-26 09:48:06 +02:00
|
|
|
from lostplaces.models import PlaceImage, Place
|
2020-09-26 16:08:37 +02:00
|
|
|
from lostplaces.forms import PlaceImageForm
|
2020-09-26 09:48:06 +02:00
|
|
|
|
2020-09-26 16:08:37 +02:00
|
|
|
class MultiplePlaceImageUploadMixin:
|
|
|
|
|
|
|
|
def handle_place_images(self, request, place):
|
|
|
|
if request.FILES:
|
|
|
|
submitted_by = request.user.explorer
|
|
|
|
for image in request.FILES.getlist('filename'):
|
|
|
|
place_image = PlaceImage.objects.create(
|
|
|
|
filename=image,
|
|
|
|
place=place,
|
|
|
|
submitted_by=submitted_by
|
|
|
|
)
|
|
|
|
place_image.save()
|
2021-10-01 21:44:35 +02:00
|
|
|
if place.hero is None:
|
|
|
|
place.hero = place.placeimages.all()[0]
|
|
|
|
place.save()
|
|
|
|
|
2020-09-26 16:08:37 +02:00
|
|
|
class PlaceImageCreateView(MultiplePlaceImageUploadMixin, PlaceAssetCreateView):
|
2020-09-26 09:48:06 +02:00
|
|
|
model = PlaceImage
|
2020-09-26 16:08:37 +02:00
|
|
|
form_class = PlaceImageForm
|
2020-09-26 09:48:06 +02:00
|
|
|
template_name = 'place_image/place_image_create.html'
|
2020-10-11 07:50:15 +02:00
|
|
|
success_message = _('Image(s) submitted successfully')
|
2020-09-26 16:08:37 +02:00
|
|
|
commit = False
|
2020-09-26 09:48:06 +02:00
|
|
|
|
|
|
|
def post(self, request, place_id, *args, **kwargs):
|
2020-09-26 16:08:37 +02:00
|
|
|
self.place = get_object_or_404(Place, pk=place_id)
|
|
|
|
self.handle_place_images(request, self.place)
|
|
|
|
return redirect(self.get_success_url())
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
form.instance.place = self.place
|
|
|
|
form.instance.submitted_by = self.request.user.explorer
|
|
|
|
return super().form_valid(form)
|
|
|
|
|
|
|
|
class PlaceImageDeleteView(PlaceAssetDeleteView):
|
|
|
|
model = PlaceImage
|
2020-10-11 21:27:27 +02:00
|
|
|
success_message = _('Image deleted successfully')
|
2020-10-11 07:50:15 +02:00
|
|
|
permission_denied_message = _('You are not allowed to delete this image')
|