django-web-galleries/django_web_galleries/web_galleries/models.py

108 lines
3.0 KiB
Python
Raw Permalink Normal View History

2022-12-17 13:42:34 +01:00
from django.db import models
from django.core.exceptions import ValidationError
from django.utils.translation import gettext as _
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'),
max_length=50
)
name = models.CharField(
_('Human readable, self assigned name of the visitor'),
null=True,
default=None,
max_length=50,
unique=True
)
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,
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,
verbose_name=_('Visitors that a part of this gallery'),
related_name='linked_galleries'
)
def clean(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):
"""
An image contains the path to the image file and several information
like description, uploader and affiliation to galleries.
"""
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,
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,
verbose_name=_('What galleries this image is in'),
related_name='images'
)
2022-12-17 13:42:34 +01:00