vbv/server/vbv_lernwelt/media_files/models.py

107 lines
2.9 KiB
Python

from django.db import models
from django.utils.translation import gettext_lazy as _
from wagtail.documents.models import AbstractDocument, Document
from wagtail.images.models import (
AbstractImage,
AbstractRendition,
get_upload_to,
Image,
WagtailImageField,
)
from vbv_lernwelt.core.admin import User
from vbv_lernwelt.media_files.storage_backends import (
ContentDocumentsStorage,
ContentImagesStorage,
UserDocumentsStorage,
UserImagesStorage,
)
class ContentDocument(AbstractDocument):
"""
Content documents are documents that are handled by the CMS.
"""
file = models.FileField(
upload_to="documents", verbose_name=_("file"), storage=ContentDocumentsStorage
)
display_text = models.CharField(max_length=1024, default="")
description = models.TextField(default="", blank=True)
link_display_text = models.CharField(max_length=1024, default="", blank=True)
thumbnail = models.CharField(default="", max_length=1024, blank=True)
admin_form_fields = Document.admin_form_fields + (
"display_text",
"description",
"link_display_text",
"thumbnail",
)
def has_permission(self, user: User):
# TODO: 20-11-2023 Renzo: add more advanced permission handling
if user.is_authenticated:
return True
class UserDocument(AbstractDocument):
"""
Documents that are uploaded by the user and not visible in the CMS.
Still they are inherited from the Wagtail Document model.
"""
file = models.FileField(
upload_to="documents", verbose_name=_("file"), storage=UserDocumentsStorage
)
class ContentImage(AbstractImage):
"""
Content images are images that are handled by the CMS.
"""
file = WagtailImageField(
verbose_name=_("file"),
upload_to=get_upload_to,
width_field="width",
height_field="height",
storage=ContentImagesStorage,
)
admin_form_fields = Image.admin_form_fields + (
# Then add the field names here to make them appear in the form:
# 'caption',
)
class UserImage(AbstractImage):
"""
User images are images that are uploaded by the user and not visible in the CMS.
Still they are inherited from the Wagtail Image model.
"""
file = WagtailImageField(
verbose_name=_("file"),
upload_to=get_upload_to,
width_field="width",
height_field="height",
storage=UserImagesStorage,
)
class ContentImageRendition(AbstractRendition):
image = models.ForeignKey(
ContentImage, on_delete=models.CASCADE, related_name="renditions"
)
class Meta:
unique_together = (("image", "filter_spec", "focal_point_key"),)
class UserImageRendition(AbstractRendition):
image = models.ForeignKey(
UserImage, on_delete=models.CASCADE, related_name="renditions"
)
class Meta:
unique_together = (("image", "filter_spec", "focal_point_key"),)