import datetime from django.contrib.gis.db import models from django.dispatch import receiver from django.db.models.signals import post_save from django.urls import reverse from django.apps import apps from django.utils import timezone from django.utils.html import format_html from taxonomy.models import Category from utils.util import render_images, markdown_to_html 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): LBH = 1 LXF = 2 PERSONAL = 3 BUS = 4 PROJECT_TYPE = ( (LBH, 'lbh'), (LXF, 'lxf'), (PERSONAL, 'personal'), (BUS, 'bus'), ) 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=PERSONAL) 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 NoteType(models.IntegerChoices): ACTION = 0, ('Action') REMINDER = 1, ('Reminder') REFERENCE = 2, ('Reference') SOMEDAY = 3, ('Someday') @classmethod def string_to_int(cls, the_string): """Convert the string value to an int value, or return None.""" for num, string in cls.choices: print(num, string) if string == the_string: return num return None 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) note_type = models.SmallIntegerField(choices=NoteType.choices, default=NoteType.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=NONE) class Meta: ordering = ('-date_created',) get_latest_by = 'date_created' def __str__(self): return self.title def get_note_type(self): return self.get_note_type_display() 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') INPROGRESS = 3, ('in progress') class WiredUpdate(models.Model): name = models.CharField(max_length=20, blank=True, null=True) date = models.DateField(blank=True, null=True) class Meta: ordering = ('date',) def __str__(self): return self.name 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") needs_update = models.BooleanField(default=False) is_live = models.BooleanField(default=True) is_priority = models.BooleanField(default=True) post_status = models.IntegerField(choices=PostStatus.choices, default=PostStatus.PUBLISHED) category = models.ForeignKey(Category, on_delete=models.SET_NULL, null=True, blank=True) update_schedule = models.ManyToManyField(WiredUpdate, blank=True) 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 set_needs_update(self): """ figures out if a post needs to be update based on either update frequency or month-based schedule if there is one """ self.needs_update = False if self.date_last_pub and not self.update_schedule.exists(): td = datetime.date.today() - self.date_last_pub if self.post_status == 0 or self.post_status == 2: if td.days > self.update_frequency: self.needs_update = True self.save() if self.update_schedule.exists(): for m in self.update_schedule.all(): td = m.date - datetime.date.today() if td.days <= 30: self.needs_update = True self.save() return '' def days_overdue(self): """ calculates how overdue something is in day based on either how often it should be updated or the update schedule if there is one """ if self.needs_update and not self.update_schedule.exists(): return self.time_since_update() - self.update_frequency elif self.needs_update and self.update_schedule.exists(): for m in self.update_schedule.all(): if m.date > datetime.date.today(): td = m.date - datetime.date.today() if td.days <= 30: return td.days 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): super(WiredPost, self).save() @receiver(post_save, sender=WiredPost) def post_save_events(sender, update_fields, created, instance, **kwargs): post_save.disconnect(post_save_events, sender=WiredPost) instance.set_needs_update() instance.save() post_save.connect(post_save_events, sender=WiredPost) 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()