Assigning Visitors to Sessions and Images

This commit is contained in:
reverend 2022-12-25 12:20:38 +01:00
parent 8c06ce09fa
commit 1df19b8a74

View File

@ -6,9 +6,26 @@ from django.shortcuts import render, redirect
from django.urls import reverse
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):
form = ImageUploadForm()
return render(
@ -31,6 +48,8 @@ class ImageUploadView(View):
if image.private:
image.access_code = secrets.token_hex(32)
image.uploaded_by = self.get_visitor()
print(image.uploaded_by)
image.save()
return redirect(
reverse('home')