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