summaryrefslogtreecommitdiff
path: root/app/links/models.py
blob: 0d1755866b36380a30a4527d8f223302afd1941d (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
from django.db import models
from django.urls import reverse
from django.contrib.sitemaps import Sitemap

from taggit.managers import TaggableManager

from taxonomy.models import TaggedItems
from utils.util import render_images, markdown_to_html


class Link(models.Model):
    title = models.CharField(max_length=200)
    link_url = models.URLField(max_length=400)
    pub_date = models.DateTimeField('Date published')
    slug = models.SlugField(unique_for_date='pub_date')
    tags = TaggableManager(through=TaggedItems, blank=True, help_text='Topics Covered')
    body_html = models.TextField(blank=True)
    body_markdown = models.TextField()
    PUB_STATUS = (
        (0, 'Draft'),
        (1, 'Published'),
    )
    status = models.IntegerField(choices=PUB_STATUS, default=0)

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

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse("links:detail", kwargs={"year": self.pub_date.year, "month": self.pub_date.strftime("%m"), "slug": self.slug})

    def save(self):
        md = render_images(self.body_markdown)
        self.body_html = markdown_to_html(md)
        super(Link, self).save()


class LinkedSitemap(Sitemap):
    changefreq = "weekly"
    priority = 0.8
    protocol = "https"

    def items(self):
        return Link.objects.filter(status=1)

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