summaryrefslogtreecommitdiff
path: root/app/figments/models.py
blob: b76d8d5c77268a87ab0858fa18f6884fe76037d6 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import datetime
from itertools import chain
from django.db import models
from django.core.urlresolvers 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