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

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)

    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_markdown = 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')

    def __str__(self):
        return self.title

    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]
        self.date_updated = timezone.now()
        super(Note, self).save()