import datetime from itertools import chain from django.db import models from django.urls import reverse from django.contrib.sitemaps import Sitemap from django.contrib.syndication.views import Feed from django.db.models.signals import post_save from django.dispatch import receiver from utils.widgets import markdown_to_html from .ebook import generate_epub_file class Series(models.Model): title = models.CharField(max_length=200) slug = models.CharField(max_length=50) is_book = models.BooleanField(default=False) body_markdown = models.TextField(null=True, blank=True) body_html = models.TextField(null=True, blank=True) pub_date = models.DateTimeField(auto_now_add=True) class Meta: verbose_name_plural = 'Series' def __str__(self): return self.title class Figment(models.Model): title = models.CharField(max_length=200) slug = models.CharField(max_length=50) pub_date = models.DateTimeField(blank=True) body_markdown = models.TextField(null=True, blank=True) prologue = models.TextField(null=True, blank=True) dek = models.TextField(null=True, blank=True) body_html = models.TextField(null=True, blank=True) PUB_STATUS = ( (0, 'Draft'), (1, 'Published'), ) status = models.IntegerField(choices=PUB_STATUS, default=0) series = models.ManyToManyField(Series, related_name="series", blank=True) TEMPLATES = ( (0, 'default'), ) template_name = models.IntegerField(choices=TEMPLATES, default=0) published_link = models.CharField(max_length=300, help_text="link to online pub", blank=True, null=True) class Meta: ordering = ('-pub_date',) def __str__(self): return self.title def get_absolute_url(self): return reverse("figments:detail", kwargs={"slug": self.slug}) def get_series(self): return "\n".join([s.title for s in self.series.all()]) @classmethod def from_db(cls, db, field_names, values): # default implementation of from_db() (could be replaced # with super()) if cls._deferred: instance = cls(**zip(field_names, values)) else: instance = cls(*values) instance._state.adding = False instance._state.db = db # customization to store the original field values on the instance instance._loaded_values = dict(zip(field_names, values)) return instance def save(self, *args, **kwargs): if not self.id and not self.pub_date: self.pub_date = datetime.datetime.now() self.body_html = markdown_to_html(self.body_markdown) super(Figment, self).save() @receiver(post_save, sender=Figment) def post_save_events(sender, instance, **kwargs): if instance.body_markdown != instance._loaded_values['body_markdown']: print("you updated") generate_epub_file(instance) else: print("no update found, not buidling") class LatestFull(Feed): title = "luxagraf figments: stories less literally true." link = "/figments/" description = "Latest postings to luxagraf.net/figments" description_template = 'feeds/blog_description.html' def items(self): return Figment.objects.filter(status__exact=1).order_by('-pub_date')[:10] class FigmentSitemap(Sitemap): changefreq = "never" priority = 0.7 protocol = "https" def items(self): return list(chain(Figment.objects.filter(status__exact=1), Series.objects.all())) def lastmod(self, obj): return obj.pub_date