aboutsummaryrefslogtreecommitdiff
path: root/apps/notes/models.py
blob: c0b23ce43a6a414a8bb8ca716a704cdb8452dd1f (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
import uuid
from django.db import models
from django.utils import timezone
from django.utils.functional import cached_property
from django.template.defaultfilters import slugify
from django.urls import reverse
from django.contrib.postgres.fields import JSONField

from taggit.managers import TaggableManager

from django.conf import settings

from utils.util import unique_slug_generator


class Notebook(models.Model):
    unique_id = models.UUIDField(default=uuid.uuid4, editable=False)
    owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    name = models.CharField(max_length=250)
    url = models.CharField(max_length=250, null=True, blank=True)
    slug = models.SlugField(blank=True)
    date_created = models.DateTimeField(blank=True, auto_now_add=True, editable=False)
    date_updated = models.DateTimeField(blank=True, auto_now=True, editable=False)

    class Meta:
        unique_together = ("owner", "slug")

    def __str__(self):
        return self.name

    def save(self, **kwargs):
        if self._state.adding:
            self.slug = unique_slug_generator(self)
        super(Notebook, self).save()

    @cached_property
    def get_absolute_url(self):
        return reverse("notes:notebook-detail", kwargs={"user": self.owner.username, "slug": self.slug})


class Note(models.Model):
    unique_id = models.UUIDField(default=uuid.uuid4, editable=False)
    owner = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    date_created = models.DateTimeField(blank=True, auto_now_add=True, editable=False)
    date_updated = models.DateTimeField(blank=True, auto_now=True, editable=False)
    title = models.CharField(max_length=250)
    body_text = models.TextField(null=True)
    body_html = models.TextField(null=True, blank=True)
    body_qjson = JSONField(null=True, blank=True)
    url = models.CharField(max_length=250, null=True, blank=True)
    slug = models.SlugField(blank=True)
    notebook = models.ForeignKey(Notebook, null=True, blank=True, on_delete=models.SET_NULL)
    tags = TaggableManager(blank=True, help_text='Tags')
    is_public = models.BooleanField(default=False)

    class Meta:
        unique_together = ("owner", "slug")

    def __str__(self):
        return self.title

    @cached_property
    def get_absolute_url(self):
        return reverse("notes:note-detail", kwargs={"user": self.owner.username, "slug": self.slug})

    def save(self, **kwargs):
        if not self.title:
            self.title = str(self.body_text)[:50]
        if self._state.adding:
            self.slug = unique_slug_generator(self)
        super(Note, self).save()