Compare commits

...

3 Commits

Author SHA1 Message Date
5f33ec5824 Deletion of place images 2020-09-26 16:08:56 +02:00
e217d3254e Small Teaak 2020-09-26 16:08:47 +02:00
263416a401 Abstraction for PlaceImageCreation (multiple images) 2020-09-26 16:08:37 +02:00
5 changed files with 55 additions and 28 deletions

View File

@ -94,9 +94,23 @@
<ul class="LP-ImageGrid__Container"> <ul class="LP-ImageGrid__Container">
{% for place_image in place.placeimages.all %} {% for place_image in place.placeimages.all %}
<li class="LP-ImageGrid__Item"> <li class="LP-ImageGrid__Item">
<a href="{{ place_image.filename.large.url }}" class="LP-Link"><img class="LP-Image" src="{{ place_image.filename.thumbnail.url }}"></a> <a href="{{ place_image.filename.large.url }}" class="LP-Link">
<img class="LP-Image" src="{{ place_image.filename.thumbnail.url }}">
</a>
{% if user.explorer == place_image.submitted_by or user.explorer == place.submitted_by %}
<span class="LP-ImageGrid__DeleteItem" title="Bild löschen">
<a href="{% url 'place_image_delete' pk=place_image.id %}" class="LP-Link">
<img class="LP-Icon" src="{% static 'icons/cancel.svg' %}" />
</a>
</span>
{% endif %}
</li> </li>
{% endfor %} {% endfor %}
<li class="LP-ImageGrid__Item LP-ImageGrid__Item--add" title="Bild hinzufügen">
<a class="LP-Link" href="{% url 'place_image_create' place_id=place.id%}">
<img class="LP-Icon" src="{% static 'icons/plus.svg' %}" />
</a>
</li>
</ul> </ul>
</div> </div>
</section> </section>

View File

@ -14,6 +14,7 @@ from lostplaces.views import (
PhotoAlbumCreateView, PhotoAlbumCreateView,
PhotoAlbumDeleteView, PhotoAlbumDeleteView,
PlaceImageCreateView, PlaceImageCreateView,
PlaceImageDeleteView,
FlatView FlatView
) )
@ -27,6 +28,7 @@ urlpatterns = [
path('place/delete/<int:pk>/', PlaceDeleteView.as_view(), name='place_delete'), path('place/delete/<int:pk>/', PlaceDeleteView.as_view(), name='place_delete'),
path('place/', PlaceListView.as_view(), name='place_list'), path('place/', PlaceListView.as_view(), name='place_list'),
path('place_image/create/<int:place_id>', PlaceImageCreateView.as_view(), name='place_image_create'), path('place_image/create/<int:place_id>', PlaceImageCreateView.as_view(), name='place_image_create'),
path('place_image/delete/<int:pk>', PlaceImageDeleteView.as_view(), name='place_image_delete'),
path('flat/<slug:slug>/', FlatView, name='flatpage'), path('flat/<slug:slug>/', FlatView, name='flatpage'),
# POST-only URLs for tag submission # POST-only URLs for tag submission

View File

@ -60,7 +60,6 @@ class IsPlaceSubmitterMixin(UserPassesTestMixin, View):
class PlaceAssetCreateView(IsAuthenticatedMixin, SuccessMessageMixin, CreateView): class PlaceAssetCreateView(IsAuthenticatedMixin, SuccessMessageMixin, CreateView):
model = None model = None
fields = []
template_name = '' template_name = ''
success_message = '' success_message = ''

View File

@ -1,18 +1,43 @@
from django.views import View from django.views import View
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404, redirect
from lostplaces.views.base_views import PlaceAssetCreateView from lostplaces.views.base_views import PlaceAssetCreateView, PlaceAssetDeleteView
from lostplaces.models import PlaceImage, Place from lostplaces.models import PlaceImage, Place
from lostplaces.forms import PlaceImageForm
class PlaceImageCreateView(PlaceAssetCreateView): 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()
class PlaceImageCreateView(MultiplePlaceImageUploadMixin, PlaceAssetCreateView):
model = PlaceImage model = PlaceImage
fields = ['filename'] form_class = PlaceImageForm
template_name = 'place_image/place_image_create.html' template_name = 'place_image/place_image_create.html'
success_message = 'Place Images submitted' success_message = 'Place Images submitted'
commit = False
def post(self, request, place_id, *args, **kwargs): def post(self, request, place_id, *args, **kwargs):
place = get_object_or_404(Place, pk=place_id) self.place = get_object_or_404(Place, pk=place_id)
super().post(request, place_id) self.handle_place_images(request, self.place)
self.object.place = place return redirect(self.get_success_url())
self.object = self.request.user
self.object.save() 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
success_message = 'Images deleted successfully'
permission_denied_message = 'You\'r not allowed to delete this image'

View File

@ -13,7 +13,8 @@ from django.shortcuts import render, redirect, get_object_or_404
from django.urls import reverse_lazy from django.urls import reverse_lazy
from lostplaces.models import Place, PlaceImage from lostplaces.models import Place, PlaceImage
from lostplaces.views import IsAuthenticatedMixin, IsPlaceSubmitterMixin from lostplaces.views.base_views import IsAuthenticatedMixin, IsPlaceSubmitterMixin
from lostplaces.views.place_image_views import MultiplePlaceImageUploadMixin
from lostplaces.forms import PlaceForm, PlaceImageForm, TagSubmitForm from lostplaces.forms import PlaceForm, PlaceImageForm, TagSubmitForm
from taggit.models import Tag from taggit.models import Tag
@ -64,7 +65,7 @@ class PlaceUpdateView(IsAuthenticatedMixin, IsPlaceSubmitterMixin, SuccessMessag
def get_place(self): def get_place(self):
return self.get_object() return self.get_object()
class PlaceCreateView(IsAuthenticatedMixin, View): class PlaceCreateView(MultiplePlaceImageUploadMixin, IsAuthenticatedMixin, View):
def get(self, request, *args, **kwargs): def get(self, request, *args, **kwargs):
place_image_form = PlaceImageForm() place_image_form = PlaceImageForm()
@ -86,12 +87,7 @@ class PlaceCreateView(IsAuthenticatedMixin, View):
place.submitted_by = submitter place.submitted_by = submitter
place.save() place.save()
if request.FILES: self.handle_place_images(request, place)
self._apply_multipart_image_upload(
files=request.FILES.getlist('filename'),
place=place,
submitter=submitter
)
messages.success( messages.success(
self.request, self.request,
@ -107,15 +103,6 @@ class PlaceCreateView(IsAuthenticatedMixin, View):
) )
return render(request, 'place/place_create.html', context={'form': place_form}) return render(request, 'place/place_create.html', context={'form': place_form})
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()
class PlaceDeleteView(IsAuthenticatedMixin, IsPlaceSubmitterMixin, DeleteView): class PlaceDeleteView(IsAuthenticatedMixin, IsPlaceSubmitterMixin, DeleteView):
template_name = 'place/place_delete.html' template_name = 'place/place_delete.html'
model = Place model = Place