2022-12-25 12:20:13 +01:00
|
|
|
import uuid
|
2022-12-27 22:09:10 +01:00
|
|
|
import names
|
2022-12-25 12:20:13 +01:00
|
|
|
|
2022-12-17 13:42:34 +01:00
|
|
|
from django.db import models
|
2022-12-18 09:18:50 +01:00
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.utils.translation import gettext as _
|
|
|
|
|
2022-12-25 12:20:13 +01:00
|
|
|
def get_uuid():
|
|
|
|
return str(uuid.uuid4())
|
2022-12-27 22:09:10 +01:00
|
|
|
def get_visitor_name():
|
|
|
|
return names.get_first_name()
|
2022-12-25 12:20:13 +01:00
|
|
|
|
2022-12-18 09:18:50 +01:00
|
|
|
class Visitor(models.Model):
|
|
|
|
"""
|
|
|
|
Stores information about a visitor of this application.
|
|
|
|
No personal information is gathered, the name is optional and can be anything.
|
|
|
|
The name has to be unique, tho.
|
|
|
|
"""
|
|
|
|
session_id = models.CharField(
|
|
|
|
_('A cookie set by this application to identify a visitor by session'),
|
2022-12-25 12:20:13 +01:00
|
|
|
max_length=50,
|
|
|
|
default=get_uuid
|
2022-12-18 09:18:50 +01:00
|
|
|
)
|
|
|
|
name = models.CharField(
|
|
|
|
_('Human readable, self assigned name of the visitor'),
|
|
|
|
null=True,
|
2022-12-27 22:09:10 +01:00
|
|
|
default=get_visitor_name,
|
2022-12-18 09:18:50 +01:00
|
|
|
max_length=50,
|
|
|
|
unique=True
|
|
|
|
)
|
|
|
|
|
2022-12-27 22:09:10 +01:00
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
2022-12-18 09:18:50 +01:00
|
|
|
|
|
|
|
class Gallery(models.Model):
|
|
|
|
"""
|
|
|
|
A gallery is a collection of multiple images.
|
|
|
|
It must be
|
|
|
|
"""
|
|
|
|
title = models.CharField(
|
|
|
|
_('Optional title for this gallery'),
|
|
|
|
null=True,
|
|
|
|
max_length=100
|
|
|
|
)
|
|
|
|
private = models.BooleanField(
|
|
|
|
_('Wether the gallery is vsibile publicly or to certain users only'),
|
|
|
|
default=False,
|
|
|
|
blank=True
|
|
|
|
)
|
|
|
|
access_code = models.CharField(
|
|
|
|
_('Code to access private galleries'),
|
|
|
|
null=True,
|
2022-12-27 22:09:10 +01:00
|
|
|
blank=True,
|
2022-12-18 09:18:50 +01:00
|
|
|
max_length=32
|
|
|
|
)
|
|
|
|
|
|
|
|
created_by = models.ForeignKey(
|
|
|
|
Visitor,
|
|
|
|
verbose_name=_('Visitor who created the gallery'),
|
|
|
|
related_name='created_galleries',
|
|
|
|
on_delete=models.CASCADE
|
|
|
|
)
|
|
|
|
|
|
|
|
visitors = models.ManyToManyField(
|
|
|
|
Visitor,
|
2022-12-27 22:09:10 +01:00
|
|
|
blank=True,
|
2022-12-18 09:18:50 +01:00
|
|
|
verbose_name=_('Visitors that a part of this gallery'),
|
|
|
|
related_name='linked_galleries'
|
|
|
|
)
|
|
|
|
|
2022-12-27 22:09:10 +01:00
|
|
|
def __str__(self):
|
|
|
|
return self.title
|
|
|
|
|
|
|
|
def mettwurst(self):
|
2022-12-18 09:18:50 +01:00
|
|
|
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):
|
|
|
|
"""
|
|
|
|
An image contains the path to the image file and several information
|
|
|
|
like description, uploader and affiliation to galleries.
|
|
|
|
"""
|
2022-12-25 12:20:13 +01:00
|
|
|
|
|
|
|
title = models.CharField(
|
|
|
|
_('Title of the image'),
|
|
|
|
max_length=100,
|
|
|
|
null=True,
|
|
|
|
blank=True
|
|
|
|
)
|
2022-12-18 09:18:50 +01:00
|
|
|
description = models.TextField(
|
|
|
|
_('An optional description of the Image'),
|
|
|
|
null=True
|
|
|
|
)
|
|
|
|
image_file = models.ImageField(
|
|
|
|
_('File of the image'),
|
|
|
|
upload_to='web_galleries/uploads/'
|
|
|
|
)
|
|
|
|
|
|
|
|
private = models.BooleanField(
|
|
|
|
_('Wether this image is visible publicly or to certain users only'),
|
|
|
|
default=False,
|
|
|
|
blank=True
|
|
|
|
)
|
|
|
|
access_code = models.CharField(
|
|
|
|
_('Code to access private images'),
|
|
|
|
null=True,
|
2022-12-27 22:09:10 +01:00
|
|
|
blank=True,
|
2022-12-18 09:18:50 +01:00
|
|
|
max_length=32
|
|
|
|
)
|
|
|
|
|
|
|
|
uploaded_by = models.ForeignKey(
|
|
|
|
Visitor,
|
|
|
|
verbose_name=_('Which visitor uploaded this picture'),
|
|
|
|
null=True,
|
|
|
|
related_name='uploaded_images',
|
|
|
|
on_delete=models.SET_NULL
|
|
|
|
)
|
|
|
|
uploaded_when = models.DateTimeField(
|
|
|
|
_('When this picture was uploaded'),
|
|
|
|
auto_now_add=True
|
|
|
|
)
|
|
|
|
|
|
|
|
galleries = models.ManyToManyField(
|
|
|
|
Gallery,
|
2022-12-27 22:09:10 +01:00
|
|
|
blank=True,
|
2022-12-18 09:18:50 +01:00
|
|
|
verbose_name=_('What galleries this image is in'),
|
|
|
|
related_name='images'
|
|
|
|
)
|
2022-12-17 13:42:34 +01:00
|
|
|
|
2022-12-27 22:09:10 +01:00
|
|
|
visible_to = models.ManyToManyField(
|
|
|
|
Visitor,
|
|
|
|
blank=True,
|
|
|
|
verbose_name=_('Visitors that can see this picture if it is marked private'),
|
|
|
|
related_name='visible_images'
|
|
|
|
)
|
|
|
|
|
2022-12-25 12:20:13 +01:00
|
|
|
def __str__(self):
|
|
|
|
if self.title:
|
|
|
|
return self.title
|
|
|
|
elif self.description:
|
|
|
|
return self.description[:100]
|
|
|
|
else:
|
2022-12-27 22:09:10 +01:00
|
|
|
return super().__str__()
|
|
|
|
|
2022-12-25 12:20:13 +01:00
|
|
|
|