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