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