aboutsummaryrefslogtreecommitdiff
path: root/apps/notes/models.py
blob: 9dc4f1361ad8f36d796894c5fe282529095d55f3 (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
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

from django.utils.translation import ugettext_lazy as _

from taggit.managers import TaggableManager
from taggit.models import TagBase, GenericTaggedItemBase


class LuxTag(TagBase):
    color_rgb = models.CharField(max_length=20, null=True, blank=True)

    class Meta:
        verbose_name = _("Tag")
        verbose_name_plural = _("Tags")

    @cached_property
    def get_absolute_url(self):
        return reverse("notes:tags", kwargs={"slug": self.slug})


class TaggedNotes(GenericTaggedItemBase):
    tag = models.ForeignKey(LuxTag, related_name="%(app_label)s_%(class)s_items", on_delete=models.CASCADE)


class Notebook(models.Model):
    """ Notebook model for Notes """
    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)
    color_rgb = models.CharField(max_length=20, 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", "name")

    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("notebooks:detail", kwargs={"slug": self.slug})

    @cached_property
    def color_rgba(self, opacity=".5"):
        try:
            color = self.color_rgb.split('(')[1].split(')')[0]
            rgba = "rgba(%s,%s)" % (color, opacity)
        except AttributeError:
            rgba = self.color_rgb
        return rgba


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(through=TaggedNotes, blank=True, help_text='Tags')
    is_public = models.BooleanField(default=False)

    class Meta:
        unique_together = ("owner", "slug")
        ordering = ("-date_created", "-date_updated")

    def __str__(self):
        return self.title

    @cached_property
    def get_absolute_url(self):
        return reverse("notes:detail", kwargs={"slug": self.slug, 'pk': self.pk})

    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()


class Annotation(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)
    highlight_text = models.TextField(null=True)
    body_text = models.TextField(null=True)
    body_html = models.TextField(null=True, blank=True)
    body_qjson = JSONField(null=True, blank=True)
    note = models.ForeignKey(Note, null=True, blank=True, on_delete=models.SET_NULL)
    is_public = models.BooleanField(default=False)

    def __str__(self):
        return self.body_text[:30]