summaryrefslogtreecommitdiff
path: root/app/figments/models.py
blob: 8b22049b981b1a2cfa608f733fa360b933a50ecf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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.util 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()])

    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