aboutsummaryrefslogtreecommitdiff
path: root/apps/notes/models.py
blob: 318b079e5d8ee481f11e109b8d06987d2a6eacf1 (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
from django.db import models
from django.utils import timezone
from django.template.defaultfilters import slugify
from django.urls import reverse

from taggit.managers import TaggableManager

from django.conf import settings


class Folder(models.Model):
    created_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    name = models.CharField(max_length=250)
    date_created = models.DateTimeField(blank=True, editable=False)
    date_updated = models.DateTimeField(blank=True, editable=False)

    def __str__(self):
        return self.name


class Note(models.Model):
    created_by = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    date_created = models.DateTimeField(blank=True, editable=False)
    date_updated = models.DateTimeField(blank=True, editable=False)
    title = models.CharField(max_length=250)
    body = models.TextField(null=True)
    rendered_body = models.TextField(null=True)
    url = models.CharField(max_length=250)
    slug = models.SlugField(unique=True, blank=True)
    folder = models.ForeignKey(Folder, null=True, on_delete=models.SET_NULL)
    tags = TaggableManager(blank=True, help_text='Tags')
    is_public = models.BooleanField(default=False)

    def __str__(self):
        return self.title

    def get_absolute_url(self):
        return reverse("notes:notes-detail", kwargs={"pk": self.pk})

    def save(self, **kwargs):
        # On save, update timestamps (users updated through admin.py)
        if not self.id:
            self.date_created = timezone.now()
            self.slug = slugify(self.title)[:50]
            if not self.title:
                self.title = str(self.date_created)
        self.date_updated = timezone.now()
        super(Note, self).save()