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

143 lines
3.6 KiB
Python
Raw Normal View History

import uuid
import names
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 _
def get_uuid():
return str(uuid.uuid4())
def get_visitor_name():
return names.get_first_name()
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=get_visitor_name,
max_length=50,
unique=True
)
def __str__(self):
return self.name
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,
blank=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,
blank=True,
verbose_name=_('Visitors that a part of this gallery'),
related_name='linked_galleries'
)
def __str__(self):
return self.title
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,
blank=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,
blank=True,
verbose_name=_('What galleries this image is in'),
related_name='images'
)
2022-12-17 13:42:34 +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'
)
def __str__(self):
if self.title:
return self.title
elif self.description:
return self.description[:100]
else:
return super().__str__()