summaryrefslogtreecommitdiff
path: root/app/links/models.py
blob: 0a82366cea3af6809d7e5a5622747f122f43a10b (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
from django.core.urlresolvers import reverse
from django.utils.encoding import force_text
from django.db import models
from django.contrib.sitemaps import Sitemap
from taggit.managers import TaggableManager

from utils.widgets import markdown_to_html


class Link(models.Model):
    title = models.CharField(max_length=400)
    url = models.CharField(max_length=400)
    description = models.TextField(blank=True, null=True)
    body_markdown = models.TextField(blank=True, null=True)
    pub_date = models.DateTimeField()
    PUB_STATUS = (
        (0, 'Private'),
        (1, 'Public'),
    )
    status = models.IntegerField(choices=PUB_STATUS, default=0)
    tags = TaggableManager(blank=True)

    class Meta:
        ordering = ['-pub_date']

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse("links:detail", kwargs={"slug": self.pk})

    def render_description(self):
        return markdown_to_html(self.description)

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

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

    def admin_link(self):
        return force_text('<a href="%s">Visit Site</a>' % (self.url))
    admin_link.allow_tags = True
    admin_link.short_description = 'Link'


class LinkSitemap(Sitemap):
    changefreq = "never"
    priority = 0.4

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

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