import datetime import os from django.utils import timezone from django.contrib.gis.db import models from django.contrib.contenttypes.fields import GenericRelation, GenericForeignKey from django.contrib.contenttypes.models import ContentType from django.contrib.sites.models import Site from django.urls import reverse from django.apps import apps from django.conf import settings from django.contrib.sitemaps import Sitemap from utils.util import render_images, render_products, parse_video, markdown_to_html, extract_main_image def get_upload_path(self, filename): return "images/post-images/%s/%s" % (datetime.datetime.today().strftime("%Y"), filename) class GTDOutcome(models.Model): title = models.CharField(max_length=300) body_markdown = models.TextField() body_html = models.TextField(blank=True) date_goal = models.DateField(blank=True, null=True) date_ended = models.DateField(blank=True, null=True) def __str__(self): return self.title class Meta: ordering = ('-date_goal',) def get_absolute_url(self): return reverse('gtd:outcome-detail', kwargs={"pk": self.pk}) def save(self, *args, **kwargs): created = self.pk is None if not created: md = render_images(self.body_markdown) self.body_html = markdown_to_html(md) super(GTDOutcome, self).save(*args, **kwargs) class GTDProject(models.Model): PROJECT_TYPE = ( (0, 'wired'), (1, 'lbh'), (2, 'personal'), (3, 'lxf'), ) title = models.CharField(max_length=200) short_title = models.CharField(max_length=200, blank=True, null=True) body_markdown = models.TextField(null=True, blank=True) body_html = models.TextField(blank=True) date_goal = models.DateField(blank=True, null=True) date_ended = models.DateField(blank=True, null=True) project_type = models.IntegerField(choices=PROJECT_TYPE, default=2) outcome = models.ForeignKey(GTDOutcome, on_delete=models.SET_NULL, null=True, blank=True) class Meta: ordering = ('-date_goal',) get_latest_by = 'date_goal' def __str__(self): return self.title def get_absolute_url(self): return reverse('gtd:project-detail', kwargs={"pk": self.id}) @property def get_previous_admin_url(self): n = self.get_previous_by_date_goal() return reverse('admin:%s_%s_change' %(self._meta.app_label, self._meta.model_name), args=[n.id] ) @property def get_next_admin_url(self): model = apps.get_model(app_label=self._meta.app_label, model_name=self._meta.model_name) try: return reverse('admin:%s_%s_change' %(self._meta.app_label, self._meta.model_name), args=[self.get_next_by_date_goal().pk] ) except model.DoesNotExist: return '' def save(self, *args, **kwargs): created = self.pk is None if not created: md = render_images(self.body_markdown) self.body_html = markdown_to_html(md) super(GTDProject, self).save(*args, **kwargs) class GTDNote(models.Model): title = models.CharField(max_length=200) url = models.CharField(max_length=400, blank=True, null=True) body_markdown = models.TextField(null=True, blank=True) body_html = models.TextField(blank=True) date_completed = models.DateField(null=True, blank=True) date_created = models.DateTimeField(auto_now=True) ACTION = 0 REMINDER = 1 REFERENCE = 2 NOTE_TYPE = [ (ACTION, 'Action'), (REMINDER, 'Reminder'), (REFERENCE, 'Reference'), ] note_type = models.SmallIntegerField(choices=NOTE_TYPE, default=ACTION) reminder = models.BigIntegerField(help_text="In days", null=True, blank=True) project = models.ForeignKey(GTDProject, on_delete=models.SET_NULL, null=True, blank=True) NONE = 0 OPEN = 1 COMPLETED = 2 STATUS= [ (NONE, 'None'), (OPEN, 'Open'), (COMPLETED, 'Completed'), ] status = models.SmallIntegerField(choices=STATUS, default=OPEN) CALLIN = 0 ASKEDFOR = 1 COMING = 2 TESTING = 3 DONE = 4 LIVE = 5 WORK_STATUS = ( (CALLIN, 'Call In'), (ASKEDFOR, 'Asked For'), (COMING, 'Coming'), (TESTING, 'Testing'), (DONE, 'Done'), (LIVE, 'Live'), ) work_status = models.SmallIntegerField(choices=WORK_STATUS, default=CALLIN) NONE = 0 GUIDE = 1 REVIEW = 2 RAVE = 3 WORK_PLAN = ( (GUIDE, 'For Guide'), (REVIEW, 'Review'), (RAVE, 'Rave/Rant'), (NONE, 'No Plan'), ) work_plan = models.IntegerField(choices=WORK_PLAN, default=1) class Meta: ordering = ('-date_created',) get_latest_by = 'date_created' def __str__(self): return self.title def save(self, *args, **kwargs): self.body_html = markdown_to_html(self.body_markdown) super(GTDNote, self).save(*args, **kwargs) def get_absolute_url(self, *args, **kwargs): return reverse('gtd:note-edit', kwargs={"pk": self.pk}) class PostType(models.IntegerChoices): REVIEW = 0, ('review') GUIDE = 1, ('guide') HOWTO = 2, ('how-to') class TemplateType(models.IntegerChoices): STORY = 0, ('story') GALLERY = 1, ('gallery') class PostStatus(models.IntegerChoices): ASSIGNED = 0, ('Assigned') TURNEDIN = 1, ('turned in') PUBLISHED = 2, ('published') class WiredPost(models.Model): # an entry in a feed title = models.CharField(max_length=512, blank=True, null=True) body = models.TextField(blank=True, null=True) url = models.CharField(max_length=512, blank=True, null=True) edit_url = models.CharField(max_length=512, blank=True, null=True) date_last_pub = models.DateField(blank=True, null=True) guid = models.CharField(max_length=512, blank=True, null=True, db_index=True) author = models.CharField(max_length=255, blank=True, null=True) post_type = models.IntegerField(choices=PostType.choices, default=PostType.GUIDE) template_type = models.IntegerField(choices=TemplateType.choices, default=TemplateType.STORY) update_frequency = models.BigIntegerField(help_text="In days") #products = models.ManyToManyField(ProductLink, blank=True, null=True) needs_update = models.BooleanField(default=False) is_live = models.BooleanField(default=True) post_status = models.IntegerField(choices=PostStatus.choices, default=PostStatus.PUBLISHED) class Meta: ordering = ('date_last_pub',) def __str__(self): return self.title def time_since_update(self): td = datetime.date.today() - self.date_last_pub return int(td.days) #def get_needs_update(self): # if self.time_since_update() > self.update_frequency: # return True # else: # return False def days_overdue(self): if self.needs_update == True: return self.time_since_update() - self.update_frequency else: return 0 def admin_url(self): return format_html('%s' % (self.url, self.url)) admin_link.short_description = 'Link' def get_absolute_url(self): return reverse('gtd:wiredpost-detail', kwargs={"pk": self.id}) def save(self, *args, **kwargs): if self.date_last_pub: td = timezone.localdate() - self.date_last_pub if td.days > self.update_frequency and self.post_status != 1: self.needs_update = True else: self.needs_update = False super(WiredPost, self).save() class WiredNote(models.Model): title = models.CharField(max_length=400) url = models.CharField(max_length=400, blank=True, null=True) body_markdown = models.TextField(blank=True, null=True) date_created = models.DateTimeField(default=timezone.now) post = models.ForeignKey(WiredPost, on_delete=models.CASCADE, null=True, blank=True) STATUS = ( (0, 'Call In'), (1, 'Asked For'), (2, 'Coming'), (3, 'Testing'), (4, 'Done'), (5, 'Live'), ) status = models.IntegerField(choices=STATUS, default=0) PLAN = ( (0, 'For Guide'), (1, 'Review'), (2, 'Rave/Rant'), (3, 'No Plan'), ) plan = models.IntegerField(choices=PLAN, default=0) class Meta: ordering = ('date_created', 'status') def __str__(self): return self.title def get_absolute_url(self): return reverse('gtd:wirednote-edit', kwargs={"pk": self.pk}) def save(self, *args, **kwargs): super(WiredNote, self).save()