Upload image to gallery and view images through galleries

This commit is contained in:
reverend 2022-12-28 15:57:52 +01:00
parent 582155bdd6
commit c38ab9a9b4
6 changed files with 126 additions and 13 deletions

View File

@ -31,3 +31,8 @@ class AccessCodeForm(forms.Form):
class VisitorNameForm(forms.Form): class VisitorNameForm(forms.Form):
name = forms.CharField(max_length=50) name = forms.CharField(max_length=50)
class ImageUploadForm(forms.Form):
images = forms.ImageField(
widget=forms.ClearableFileInput(attrs={'multiple': True})
)

View File

@ -72,13 +72,6 @@ class Gallery(models.Model):
def __str__(self): def __str__(self):
return self.title return self.title
def mettwurst(self):
if self.private and access_code is None:
raise ValidationErrorr('Private gallery needs an access code')
if len(self.visitors.all()) > 10:
raise ValidationError('Gallery can not have more than 10 visitors assigned')
class Image(models.Model): class Image(models.Model):
""" """
An image contains the path to the image file and several information An image contains the path to the image file and several information

View File

@ -6,10 +6,11 @@
{% if gallery.title %} {% if gallery.title %}
<h1>{{gallery.title}}</h1> <h1>{{gallery.title}}</h1>
{% endif %} {% endif %}
<a href="{% url 'gallery_upload' gallery_id=gallery.id %}">Upload image to this gallery</a>
<section class="RV-Images"> <section class="RV-Images">
<li class="RV-Images__list"> <li class="RV-Images__list">
{% for image in gallery.images.all %} {% for image in gallery.images.all %}
<a href="{% url 'image' pk=image.id %}" class="RV-Image__link RV-Image__link--detail"> <a href="{% url 'image_through_gallery' gallery_id=gallery.id image_id=image.id %}" class="RV-Image__link RV-Image__link--detail">
<img class="RV-Image__source" src="{% src image.image_file 200x200 %}"> <img class="RV-Image__source" src="{% src image.image_file 200x200 %}">
</a> </a>
{% endfor %} {% endfor %}

View File

@ -0,0 +1,14 @@
{% extends '../global.html' %}
{% block content %}
<form method="POST" enctype="multipart/form-data">
{% csrf_token %}
<h2>Upload image to "{{gallery.title}}"</h2>
<div class="RV-Fieldset">
{% include 'partials/form_input.html' with field=form.images %}
</div>
<div class="RV-Fieldset">
<button type="submit">Upload</button>
</div>
</form>
{% endblock content %}

View File

@ -10,7 +10,9 @@ from .views import (
ImageDetailView, ImageDetailView,
GainAccessToGalleryView, GainAccessToGalleryView,
VisitorSettingsView, VisitorSettingsView,
RevokeAccessToGallery RevokeAccessToGallery,
UploadToGalleryView,
ImageThroughGalleryView
) )
urlpatterns = [ urlpatterns = [
@ -24,4 +26,6 @@ urlpatterns = [
path('gallery/gain_access/<int:gallery_id>/', GainAccessToGalleryView.as_view(), name='gallery_gain_access'), path('gallery/gain_access/<int:gallery_id>/', GainAccessToGalleryView.as_view(), name='gallery_gain_access'),
path('visitor/my_profile', VisitorSettingsView.as_view(), name='visitor_profile'), path('visitor/my_profile', VisitorSettingsView.as_view(), name='visitor_profile'),
path('gallery/revoke_access/<int:gallery_id>/<int:visitor_id>', RevokeAccessToGallery.as_view(), name='gallery_revoke_access'), path('gallery/revoke_access/<int:gallery_id>/<int:visitor_id>', RevokeAccessToGallery.as_view(), name='gallery_revoke_access'),
path('gallery/upload/<int:gallery_id>', UploadToGalleryView.as_view(), name='gallery_upload'),
path('gallery/image/<int:gallery_id>/<int:image_id>', ImageThroughGalleryView.as_view(), name='image_through_gallery')
] ]

View File

@ -15,7 +15,8 @@ from .forms import (
ImageUploadForm, ImageUploadForm,
GalleryCreatingForm, GalleryCreatingForm,
AccessCodeForm, AccessCodeForm,
VisitorNameForm VisitorNameForm,
ImageUploadForm
) )
from .models import ( from .models import (
Image, Image,
@ -94,7 +95,7 @@ class ImageUploadView(VisitorSessionMixin, View):
} }
) )
class PublicImageListView(ListView): class PublicImageListView(VisitorSessionMixin, ListView):
model = Image model = Image
paginate_by = 20 paginate_by = 20
context_object_name = 'images' context_object_name = 'images'
@ -107,6 +108,7 @@ class MyImagesListView(VisitorSessionMixin, View):
def get(self, request): def get(self, request):
visitor = self.get_visitor() visitor = self.get_visitor()
uploaded_images = Image.objects.all().filter( uploaded_images = Image.objects.all().filter(
uploaded_by=self.get_visitor() uploaded_by=self.get_visitor()
) )
shared_images = Image.objects.all().filter( shared_images = Image.objects.all().filter(
@ -165,7 +167,12 @@ class GalleryCreateView(VisitorSessionMixin, View):
gallery.save() gallery.save()
return redirect( return redirect(
reverse('home') reverse(
'gallery',
kwargs={
'pk': gallery.id
}
)
) )
else: else:
form = ImageUploadForm() form = ImageUploadForm()
@ -234,6 +241,42 @@ class ImageDetailView(VisitorSessionMixin, UserPassesTestMixin, DetailView):
reverse('home') reverse('home')
) )
class ImageThroughGalleryView(VisitorSessionMixin, View):
def _redirect_gallery_no_access(self, gallery):
return redirect(
reverse(
'gallery_gain_access',
kwargs={
'gallery_id': gallery.id
}
)
)
def get(self, request, gallery_id, image_id):
gallery = get_object_or_404(Gallery, id=gallery_id)
if self.get_visitor() in gallery.visitors.all() or self.get_visitor() == gallery.created_by:
image = get_object_or_404(Image, id=image_id)
if image in gallery.images.all():
return render(
request,
'image/image_detail.html',
{
'image': image,
'visitor': self.get_visitor()
}
)
else:
return redirect(
reverse(
'gallery',
kwargs={
'pk': gallery.id
}
)
)
else:
return self._redirect_gallery_no_access(gallery)
class GainAccessToGalleryView(VisitorSessionMixin, View): class GainAccessToGalleryView(VisitorSessionMixin, View):
def _redirect_to_gallery(self, gallery): def _redirect_to_gallery(self, gallery):
return redirect( return redirect(
@ -351,3 +394,56 @@ class VisitorSettingsView(VisitorSessionMixin, View):
'visitor': self.get_visitor() 'visitor': self.get_visitor()
} }
) )
class UploadToGalleryView(VisitorSessionMixin, View):
def get(self, request, gallery_id):
upload_form = ImageUploadForm()
gallery = get_object_or_404(Gallery, id=gallery_id)
return render(
request,
'gallery/upload_image.html',
{
'form': upload_form,
'gallery': gallery
}
)
def post(self, request, gallery_id):
upload_form = ImageUploadForm(
request.POST,
request.FILES
)
if upload_form.is_valid():
gallery = get_object_or_404(Gallery, id=gallery_id)
for image in request.FILES.getlist('images'):
imageObject = Image.objects.create(
image_file=image,
uploaded_by=self.get_visitor(),
private=gallery.private,
access_code=gallery.access_code
)
imageObject.galleries.add(gallery)
imageObject.save()
gallery.save()
return redirect(
reverse(
'gallery',
kwargs={
'pk': gallery.id
}
)
)
else:
return render(
request,
'gallery/upload_image.html',
{
'form': upload_form,
'gallery': gallery
}
)