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

129 lines
3.4 KiB
Python

import uuid
from django.db import models
from django.core.exceptions import ValidationError
from django.utils.translation import gettext as _
def get_uuid():
return str(uuid.uuid4())
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,
default=get_uuid
)
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.
"""
title = models.CharField(
_('Title of the image'),
max_length=100,
null=True,
blank=True
)
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'
)
def __str__(self):
if self.title:
return self.title
elif self.description:
return self.description[:100]
else:
return super.__str__()