153 lines
3.8 KiB
Python
153 lines
3.8 KiB
Python
# -*- encoding: utf-8 -*-
|
|
|
|
import logging
|
|
from uuid import uuid4
|
|
from pathlib import Path
|
|
|
|
from django.db import models
|
|
from django.utils import timezone
|
|
from django.dispatch import receiver
|
|
from django.db.utils import IntegrityError
|
|
from django.db.models.signals import pre_save
|
|
from django.utils.translation import gettext_lazy as _
|
|
from django.core.files.storage import FileSystemStorage
|
|
from django.utils.deconstruct import deconstructible
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
class OverwriteStorage(FileSystemStorage):
|
|
"""
|
|
Storage class that allows overriding files, instead of django appending random
|
|
characters to prevent conflicts.
|
|
"""
|
|
|
|
def get_available_name(self, name, max_length=None) -> str:
|
|
if self.exists(name):
|
|
(Path(self.location) / name).unlink()
|
|
|
|
return name
|
|
|
|
|
|
@deconstructible
|
|
class IconPathGenerator:
|
|
"""
|
|
Icon path generator.
|
|
"""
|
|
|
|
def __call__(self, instance, filename: str) -> Path:
|
|
return Path(instance.__class__.__name__.lower()) / str(instance.uuid) / "icon.webp"
|
|
|
|
|
|
class SavedGuilds(models.Model):
|
|
"""
|
|
|
|
"""
|
|
|
|
id = models.AutoField(
|
|
primary_key=True
|
|
)
|
|
|
|
guild_id = models.CharField(
|
|
max_length=128
|
|
)
|
|
|
|
name = models.CharField(
|
|
max_length=128
|
|
)
|
|
|
|
icon = models.CharField(
|
|
max_length=128
|
|
)
|
|
|
|
|
|
class Subscription(models.Model):
|
|
"""
|
|
Represents a stored RSS Feed.
|
|
"""
|
|
|
|
uuid = models.UUIDField(
|
|
primary_key=True,
|
|
default=uuid4,
|
|
editable=False
|
|
)
|
|
|
|
# Name attribute acts as a human readable identification and search option.
|
|
name = models.CharField(
|
|
verbose_name=_("name"),
|
|
help_text=_("Reference name for this subscription (max 32 chars)."),
|
|
max_length=32,
|
|
null=False,
|
|
blank=False
|
|
)
|
|
|
|
rss_url = models.URLField(
|
|
verbose_name=_("rss url"),
|
|
help_text=_("URL of the subscribed to RSS feed.")
|
|
)
|
|
|
|
image = models.ImageField(
|
|
verbose_name=_("image"),
|
|
help_text=_("image of the RSS feed."),
|
|
upload_to=IconPathGenerator(),
|
|
storage=OverwriteStorage(),
|
|
default="../static/images/placeholder-100x100.png",
|
|
null=True,
|
|
blank=True
|
|
)
|
|
|
|
# Discord Server ID
|
|
server = models.PositiveBigIntegerField(
|
|
verbose_name=_("server id"),
|
|
help_text=_("Identifier for the discord server that owns this subscription.")
|
|
)
|
|
|
|
targets = models.CharField(
|
|
verbose_name=_("targets"),
|
|
max_length=500,
|
|
help_text=_("Discord channels where subscription content is delivered to.")
|
|
)
|
|
|
|
creation_datetime = models.DateTimeField(
|
|
verbose_name=_("creation datetime"),
|
|
help_text=_("when this instance was created."),
|
|
default=timezone.now,
|
|
editable=False
|
|
)
|
|
|
|
extra_notes = models.CharField(
|
|
verbose_name=_("extra notes"),
|
|
max_length=250,
|
|
null=True,
|
|
blank=True,
|
|
help_text=_("Additional user written notes about this item.")
|
|
)
|
|
|
|
active = models.BooleanField(
|
|
verbose_name=_("active"),
|
|
default=True,
|
|
help_text=_("Acts as a soft delete.")
|
|
)
|
|
|
|
class Meta:
|
|
"""
|
|
Metadata for the Subscription Model.
|
|
"""
|
|
|
|
verbose_name = "subscription"
|
|
verbose_name_plural = "subscriptions"
|
|
get_latest_by = "-creation_datetime"
|
|
constraints = [
|
|
# Prevent servers from having subscriptions with duplicate names
|
|
models.UniqueConstraint(fields=["name", "server"], name="unique name & server pair")
|
|
]
|
|
|
|
def __str__(self):
|
|
return self.name
|
|
|
|
def save(self, *args, **kwargs):
|
|
new_text = "New " if self._state.adding else ""
|
|
log.debug("%sSubscription Saved %s", new_text, self.uuid)
|
|
super().save(*args, **kwargs)
|
|
|