2024-01-04 11:54:56 +11:00
|
|
|
"""newsletter models"""
|
|
|
|
|
|
|
|
from django.db import models
|
|
|
|
from django.utils import timezone
|
|
|
|
|
|
|
|
from .utils import Announcement, Category
|
|
|
|
|
|
|
|
|
|
|
|
class Newsletter(models.Model):
|
|
|
|
"""a newsletter"""
|
|
|
|
|
2024-01-09 18:11:22 +11:00
|
|
|
name = models.CharField(max_length=100)
|
2024-01-26 11:00:26 +11:00
|
|
|
author_name = models.CharField(max_length=100)
|
2024-01-04 11:54:56 +11:00
|
|
|
category = models.CharField(choices=Category.choices, max_length=4)
|
|
|
|
url = models.URLField(max_length=400, unique=True)
|
2024-01-21 15:39:19 +11:00
|
|
|
feed = models.URLField(max_length=1000, unique=True, blank=True, null=True)
|
2024-01-09 18:11:22 +11:00
|
|
|
description = models.TextField(null=True, blank=True, max_length=250)
|
2024-01-04 11:54:56 +11:00
|
|
|
activitypub_account_name = models.CharField(max_length=200, blank=True, null=True)
|
2024-01-07 16:58:56 +11:00
|
|
|
contact_email = models.EmailField(blank=True, null=True)
|
2024-01-21 15:39:19 +11:00
|
|
|
|
2024-01-04 11:54:56 +11:00
|
|
|
announced = models.BooleanField(default=False)
|
|
|
|
approved = models.BooleanField(default=False)
|
2024-01-21 15:39:19 +11:00
|
|
|
active = models.BooleanField(null=True, default=True)
|
|
|
|
failing = models.BooleanField(default=False, blank=True, null=True)
|
|
|
|
|
|
|
|
updateddate = models.DateTimeField()
|
2024-01-26 11:00:26 +11:00
|
|
|
pubdate = models.DateTimeField(null=True, default=None)
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
"""display for admin dropdowns"""
|
|
|
|
return self.name
|
2024-01-04 11:54:56 +11:00
|
|
|
|
|
|
|
def announce(self):
|
|
|
|
"""create a event announcement"""
|
|
|
|
|
|
|
|
category = Category(self.category).label
|
2024-01-26 11:00:26 +11:00
|
|
|
name = self.author_name
|
2024-01-04 11:54:56 +11:00
|
|
|
if self.activitypub_account_name:
|
2024-01-26 11:00:26 +11:00
|
|
|
name = f"{self.author_name} ({self.activitypub_account_name})"
|
2024-01-04 11:54:56 +11:00
|
|
|
|
2024-01-26 11:00:26 +11:00
|
|
|
status = f"{self.name} is a newsletter about {category} from {name}. Check it out:\n\n{self.url}"
|
2024-01-04 11:54:56 +11:00
|
|
|
|
|
|
|
Announcement.objects.create(status=status)
|
|
|
|
self.announced = True
|
|
|
|
super().save()
|
|
|
|
|
|
|
|
def save(self, *args, **kwargs):
|
2024-01-21 15:39:19 +11:00
|
|
|
if not self.updateddate:
|
|
|
|
self.updateddate = timezone.now()
|
2024-01-04 11:54:56 +11:00
|
|
|
super().save(*args, **kwargs)
|
2024-01-21 15:39:19 +11:00
|
|
|
|
|
|
|
def set_failing(self):
|
|
|
|
"""set the newsletter feed as failing"""
|
|
|
|
|
|
|
|
self.failing = True
|
|
|
|
super().save()
|
|
|
|
|
|
|
|
def set_success(self, updateddate):
|
|
|
|
"""
|
|
|
|
set failing to false
|
|
|
|
set the updateddate to a datetime
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
self.failing = False
|
|
|
|
self.updateddate = updateddate
|
|
|
|
super().save()
|
|
|
|
|
|
|
|
|
|
|
|
class Edition(models.Model):
|
|
|
|
"""A newsletter edition"""
|
|
|
|
|
|
|
|
title = models.CharField(max_length=2000)
|
|
|
|
author_name = models.CharField(max_length=1000, null=True, blank=True)
|
|
|
|
url = models.URLField(max_length=2000, unique=True)
|
|
|
|
description = models.TextField(null=True, blank=True)
|
|
|
|
updateddate = models.DateTimeField()
|
|
|
|
newsletter = models.ForeignKey(
|
|
|
|
Newsletter, on_delete=models.CASCADE, related_name="editions"
|
|
|
|
)
|
2024-01-26 11:00:26 +11:00
|
|
|
pubdate = models.DateTimeField(null=True, default=timezone.now)
|
2024-01-21 15:39:19 +11:00
|
|
|
guid = models.CharField(max_length=2000)
|
2024-01-26 11:00:26 +11:00
|
|
|
|
|
|
|
def announce(self):
|
|
|
|
"""queue an edition announcement"""
|
|
|
|
|
|
|
|
author = self.newsletter.activitypub_account_name or self.author_name
|
|
|
|
|
|
|
|
if self.newsletter.activitypub_account_name:
|
|
|
|
author = f"{self.newsletter.activitypub_account_name} - "
|
|
|
|
elif self.author_name:
|
|
|
|
author = f"{self.author_name} - "
|
|
|
|
else:
|
|
|
|
author = ""
|
|
|
|
|
|
|
|
status = f"📬 {self.title} ({author}{self.newsletter.name})\n\n{self.url}"
|
|
|
|
|
|
|
|
Announcement.objects.create(status=status)
|