import re from django.db import models from django.urls import reverse from django.contrib.sitemaps import Sitemap from django.conf import settings import datetime from itertools import chain from utils.util import parse_image, markdown_to_html def render_images(s): s = re.sub('', parse_image, s) return s class Topic(models.Model): name = models.CharField(max_length=60) slug = models.SlugField() pluralized_name = models.CharField(max_length=60) def __str__(self): return self.name def get_absolute_url(self): return reverse('src:list_topics', kwargs={"slug": self.slug}) @property def pub_date(self): return datetime.datetime.now() class SrcPost(models.Model): title = models.CharField(max_length=200) slug = models.SlugField(unique_for_date='pub_date') body_html = models.TextField(blank=True) body_markdown = models.TextField() pub_date = models.DateTimeField('Date published') topics = models.ManyToManyField(Topic, blank=True) 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) TEMPLATES = ( (0, 'default'), ) template_name = models.IntegerField(choices=TEMPLATES, default=0) class Meta: ordering = ('-pub_date',) get_latest_by = 'pub_date' verbose_name_plural = 'posts' def __str__(self): return self.title def get_absolute_url(self): return reverse('src:detail', kwargs={"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) super(SrcPost, self).save() def get_upload_path(self, filename): return "images/src/%s" % filename class Book(models.Model): title = models.CharField(max_length=200) image = models.FileField(blank=True, null=True, upload_to=get_upload_path) 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) PUB_STATUS = ( (0, 'Draft'), (1, 'Published'), ) status = models.IntegerField(choices=PUB_STATUS, default=0) price = models.FloatField() price_sale = models.FloatField() meta_description = models.CharField(max_length=256, null=True, blank=True) pages = models.DecimalField(max_digits=3, decimal_places=0, null=True, blank=True) DEFAULT = 'details/src_book.html' BOOK2 = 'details/src_book_2.html' TEMPLATES = ( (DEFAULT, 'Default'), (BOOK2, 'Book Two'), ) template_name = models.CharField( max_length=200, choices=TEMPLATES, default=DEFAULT ) class Meta: ordering = ('-pub_date',) get_latest_by = 'pub_date' def __str__(self): return self.title def get_absolute_url(self): return reverse('src:detail_book', kwargs={"slug": self.slug}) def get_image_url(self): img = self.image.url.split('src/')[1] return '%ssrc/%s' % (settings.IMAGES_URL, img) def save(self): md = render_images(self.body_markdown) self.body_html = markdown_to_html(md) super(Book, self).save() '''class SrcDemo(models.Model): title = models.CharField(max_length=254) slug = models.SlugField() body = models.TextField(blank=True, null=True) head = models.TextField(blank=True, null=True) DEMO_TEMPLATES = ( (0, 'Blank'), (1, 'Basic_light'), ) template = models.IntegerField(choices=DEMO_TEMPLATES, default=0) pub_date = models.DateTimeField('Date published', blank=True) class Meta: verbose_name_plural = "Demos" app_label = 'projects' ordering = ('-pub_date',) def save(self): if not self.id: self.pub_date = datetime.datetime.now() super(SrcDemo, self).save() ''' class SrcSitemap(Sitemap): changefreq = "never" priority = 0.7 protocol = "https" def items(self): return list(chain( SrcPost.objects.filter(status=1), Topic.objects.all() )) def lastmod(self, obj): return obj.pub_date