summaryrefslogtreecommitdiff
path: root/app/gtd/models.py
blob: 3ad29317695e6fb5904161e3ed2ca215a81dc722 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
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 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('<a target="_blank" href="%s">%s</a>' % (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()