diff options
Diffstat (limited to 'app/essays/models.py')
-rw-r--r-- | app/essays/models.py | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/app/essays/models.py b/app/essays/models.py new file mode 100644 index 0000000..23a4cce --- /dev/null +++ b/app/essays/models.py @@ -0,0 +1,100 @@ +from django.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 +from utils.util import render_images, markdown_to_html +from sketches.models import Sketch +from books.models import Book +from photos.models import LuxImage + + +POST_TYPE = ( + (0, 'essays'), + (1, 'tools'), + (2, 'figments'), +) + + +class PostType(models.Model): + name = models.CharField(max_length=200) + dek = models.TextField(blank=True) + slug = models.SlugField() + + def __str__(self): + return self.name + + +class Essay(models.Model): + title = models.CharField(max_length=200) + sub_title = models.CharField(max_length=200, blank=True) + dek = 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) + 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) + post_type = models.IntegerField(choices=POST_TYPE, default=0) + tags = TaggableManager(through=TaggedItems, blank=True, help_text='Topics Covered') + originally_published_by = models.CharField(max_length=400, blank=True) + originally_published_by_url = models.CharField(max_length=400, blank=True) + 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(Sketch, blank=True) + books = models.ManyToManyField(Book, 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 = 'Essays' + + def __str__(self): + return self.title + + def get_absolute_url(self): + return "/%s/%s" % (self.get_post_type_display(), 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(Essay, self).save() + + +class EssaySitemap(Sitemap): + changefreq = "never" + priority = 0.7 + protocol = "https" + + def items(self): + return list(chain( + Essay.objects.all(), + )) + + def lastmod(self, obj): + return obj.pub_date |