summaryrefslogtreecommitdiff
path: root/app/guides/models.py
blob: 1583beee3af0cc906be7ae1820c8118ddf6c6d34 (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
from django.contrib.gis.db import models
from django.urls import reverse
from django.contrib.sitemaps import Sitemap
import datetime
from itertools import chain

from taggit.managers import TaggableManager

from taxonomy.models import TaggedItems, Category
from utils.util import render_images, markdown_to_html
from fieldnotes.models import FieldNote
from books.models import Book
from locations.models import Location
from photos.models import LuxImage
from essays.models import Essay
from jrnl.models import Entry


class Guide(models.Model):
    title = models.CharField(max_length=200)
    sub_title = models.CharField(max_length=200, blank=True)
    dek = models.TextField(blank=True)
    preamble_markdown = models.TextField(blank=True)
    preamble_html = models.TextField(blank=True)
    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)
    enable_comments = 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)
    tags = TaggableManager(through=TaggedItems, blank=True, help_text='Topics Covered')
    category = models.ForeignKey(Category, on_delete=models.CASCADE)
    featured_image = models.ForeignKey(LuxImage, on_delete=models.CASCADE, null=True, blank=True)
    has_video = models.BooleanField(blank=True, default=False)
    field_notes = models.ManyToManyField(FieldNote, blank=True)
    books = models.ManyToManyField(Book, blank=True)
    essays = models.ManyToManyField(Essay, blank=True)
    jrnl = models.ManyToManyField(Entry, blank=True)
    point = models.PointField(null=True, blank=True)
    location = models.ForeignKey(Location, on_delete=models.CASCADE, null=True, blank=True)
    afterword = models.TextField(blank=True)
    afterword_html = models.TextField(blank=True)

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

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse('guides:detail', kwargs={"category": self.category, "slug": self.slug})

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

    @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)

    def save(self):
        md = render_images(self.body_markdown)
        self.body_html = markdown_to_html(md)
        self.afterword_html = markdown_to_html(self.afterword)
        super(Guide, self).save()


class GuideSitemap(Sitemap):
    changefreq = "never"
    priority = 1.0
    protocol = "https"

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

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