3 Commits

Author SHA1 Message Date
1df19b8a74 Assigning Visitors to Sessions and Images 2022-12-25 12:20:38 +01:00
8c06ce09fa Representation of an image in the admin view 2022-12-25 12:20:13 +01:00
b0d9c46eb2 #2 Handling image upload 2022-12-25 12:19:54 +01:00
6 changed files with 62 additions and 13 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
django_web_galleries/media/* django_web_galleries/media/*
django_web_galleries/static/*

View File

@@ -16,7 +16,20 @@ Including another URLconf
from django.contrib import admin from django.contrib import admin
from django.urls import path, include from django.urls import path, include
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('', include('web_galleries.urls')) path('', include('web_galleries.urls'))
] ]
if settings.DEBUG:
urlpatterns += static(
settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT
)
urlpatterns += static(
settings.STATIC_URL,
document_root=settings.STATIC_ROOT
)

View File

@@ -6,4 +6,7 @@ from .models import Image
class ImageUploadForm(forms.ModelForm): class ImageUploadForm(forms.ModelForm):
class Meta: class Meta:
model = Image model = Image
fields = ['description', 'image_file', 'private'] fields = ['description', 'image_file', 'private', 'title']
labels = {
'private': 'Make this image private'
}

View File

@@ -1,7 +1,12 @@
import uuid
from django.db import models from django.db import models
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
def get_uuid():
return str(uuid.uuid4())
class Visitor(models.Model): class Visitor(models.Model):
""" """
Stores information about a visitor of this application. Stores information about a visitor of this application.
@@ -10,7 +15,8 @@ class Visitor(models.Model):
""" """
session_id = models.CharField( session_id = models.CharField(
_('A cookie set by this application to identify a visitor by session'), _('A cookie set by this application to identify a visitor by session'),
max_length=50 max_length=50,
default=get_uuid
) )
name = models.CharField( name = models.CharField(
_('Human readable, self assigned name of the visitor'), _('Human readable, self assigned name of the visitor'),
@@ -67,6 +73,13 @@ 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
like description, uploader and affiliation to galleries. like description, uploader and affiliation to galleries.
""" """
title = models.CharField(
_('Title of the image'),
max_length=100,
null=True,
blank=True
)
description = models.TextField( description = models.TextField(
_('An optional description of the Image'), _('An optional description of the Image'),
null=True null=True
@@ -105,3 +118,11 @@ class Image(models.Model):
related_name='images' related_name='images'
) )
def __str__(self):
if self.title:
return self.title
elif self.description:
return self.description[:100]
else:
return super.__str__()

View File

@@ -1,6 +1,4 @@
from django.urls import path from django.urls import path
from django.conf import settings
from django.conf.urls.static import static
from .views import ( from .views import (
ImageUploadView, ImageUploadView,
@@ -11,9 +9,3 @@ urlpatterns = [
path('', PublicImageListView.as_view(), name='home'), path('', PublicImageListView.as_view(), name='home'),
path('upload/', ImageUploadView.as_view(), name='upload_image') path('upload/', ImageUploadView.as_view(), name='upload_image')
] ]
if settings.DEBUG:
urlpatterns += static(
settings.MEDIA_URL,
document_root=settings.MEDIA_ROOT
)

View File

@@ -6,9 +6,26 @@ from django.shortcuts import render, redirect
from django.urls import reverse from django.urls import reverse
from .forms import ImageUploadForm from .forms import ImageUploadForm
from .models import Image from .models import (
Image,
Visitor
)
class ImageUploadView(View): class VisitorSessionMixin(View):
def get_visitor(self):
if self.request.session:
if 'visitor_session' in self.request.session:
if Visitor.objects.get(session_id=self.request.session['visitor_session']).exists():
return Visitor.objects.get(session_id=self.request.session['visitor_session'])
else:
visitor = Visitor.objects.create()
self.request.session['visitor_session'] = visitor.session_id
return visitor
else:
return None
class ImageUploadView(VisitorSessionMixin, View):
def get(self, request): def get(self, request):
form = ImageUploadForm() form = ImageUploadForm()
return render( return render(
@@ -31,6 +48,8 @@ class ImageUploadView(View):
if image.private: if image.private:
image.access_code = secrets.token_hex(32) image.access_code = secrets.token_hex(32)
image.uploaded_by = self.get_visitor()
print(image.uploaded_by)
image.save() image.save()
return redirect( return redirect(
reverse('home') reverse('home')