summaryrefslogtreecommitdiff
path: root/app/src/models.py
blob: 2cf6e23c7448a25eb662ce5df099259969ec374e (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from django.db import models
from django.contrib.sitemaps import Sitemap
from django.contrib.syndication.views import Feed
from jrnl.models import image_url_replace, extract_images
import markdown
import datetime
from itertools import chain


class Topic(models.Model):
    name = models.CharField(max_length=60)
    slug = models.SlugField()
    pluralized_name = models.CharField(max_length=60)

    def __str__(self):
        return self.name

    def get_absolute_url(self):
        return "/src/topic/%s" % (self.slug)

    @property
    def pub_date(self):
        return datetime.datetime.now()


class Entry(models.Model):
    title = models.CharField(max_length=200)
    slug = models.SlugField(unique_for_date='pub_date')
    body_html = models.TextField(blank=True)
    body_markdown = models.TextField()
    pub_date = models.DateTimeField('Date published')
    topics = models.ManyToManyField(Topic, blank=True)
    last_updated = models.DateTimeField(auto_now=True)
    enable_comments = models.BooleanField(default=False)
    has_code = models.BooleanField(default=False)
    PUB_STATUS = (
        (0, 'Draft'),
        (1, 'Published'),
    )
    status = models.IntegerField(choices=PUB_STATUS, default=0)
    meta_description = models.CharField(max_length=256, null=True, blank=True)
    TEMPLATES = (
        (0, 'default'),
    )
    template_name = models.IntegerField(choices=TEMPLATES, default=0)

    class Meta:
        ordering = ('-pub_date',)
        get_latest_by = 'pub_date'
        verbose_name_plural = 'entries'

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return "/src/%s" % self.slug

    def comment_period_open(self):
        return self.enable_comments and datetime.datetime.today() - datetime.timedelta(30) <= self.pub_date

    def get_images(self):
        return extract_images(self.body_html)

    @property
    def get_previous_published(self):
        return self.get_previous_by_pub_date(status__exact=1)

    @property
    def get_next_published(self):
        return self.get_next_by_pub_date(status__exact=1)

    'codehilite(css_class=highlight,linenums=False,guess_lang=True,use_pygments=True)'

    def save(self):
        md = image_url_replace(self.body_markdown)
        self.body_html = markdown.markdown(md, extensions=[
            'markdown.extensions.codehilite(css_class=highlight,linenums=False)',
            'markdown.extensions.fenced_code',
            'markdown.extensions.attr_list',
            'extra'
        ], safe_mode=False)
        super(Entry, self).save()


def get_upload_path(self, filename):
    return "images/src/%s" % filename


class Book(models.Model):
    title = models.CharField(max_length=200)
    image = models.FileField(blank=True, null=True, upload_to=get_upload_path)
    slug = models.SlugField(unique_for_date='pub_date')
    body_html = models.TextField(blank=True)
    body_markdown = models.TextField()
    pub_date = models.DateTimeField('Date published')
    last_updated = models.DateTimeField(auto_now=True)
    PUB_STATUS = (
        (0, 'Draft'),
        (1, 'Published'),
    )
    status = models.IntegerField(choices=PUB_STATUS, default=0)
    price = models.FloatField()
    price_sale = models.FloatField()
    meta_description = models.CharField(max_length=256, null=True, blank=True)
    DEFAULT = 'details/src_book.html'
    BOOK2 = 'details/src_book_2.html'
    TEMPLATES = (
        (DEFAULT, 'Default'),
        (BOOK2, 'Book Two'),
    )
    template_name = models.CharField(
        max_length=200,
        choices=TEMPLATES,
        default=DEFAULT
    )

    class Meta:
        ordering = ('-pub_date',)
        get_latest_by = 'pub_date'

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return "/src/books/%s" % (self.slug)

    def save(self):
        md = image_url_replace(self.body_markdown)
        self.body_html = markdown.markdown(md, extensions=[
            'markdown.extensions.codehilite(css_class=highlight,linenums=False)',
            'markdown.extensions.fenced_code',
            'markdown.extensions.attr_list',
            'extra'
        ], safe_mode=False)
        super(Book, self).save()


class SrcSitemap(Sitemap):
    changefreq = "never"
    priority = 0.7
    protocol = "https"

    def items(self):
        return list(chain(
            Entry.objects.all(),
            Book.objects.all(),
            Topic.objects.all()
        ))

    def lastmod(self, obj):
        return obj.pub_date