summaryrefslogtreecommitdiff
path: root/app/taxonomy/models.py
blob: 736fe1598307966de089597ed59889bc428f81d9 (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
from django.contrib.gis.db import models
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from django.utils.functional import cached_property

from utils.util import markdown_to_html

from taggit.models import TagBase, GenericTaggedItemBase


class LuxTag(TagBase):
    ''' override the default taggit model to add some color '''
    color_rgb = models.CharField(max_length=20, blank=True)

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

    @cached_property
    def get_absolute_url(self):
        return reverse("taxonomy:cat-detail", kwargs={"slug": self.slug})


class TaggedItems(GenericTaggedItemBase):
    ''' necessary with custom tag model, lets you still use TaggableManager'''
    tag = models.ForeignKey(LuxTag, related_name="%(app_label)s_%(class)s_items", on_delete=models.CASCADE)


class Category(models.Model):
    """ Generic model for Categories """
    name = models.CharField(max_length=250)
    pluralized_name = models.CharField(max_length=60, null=True, blank=True)
    description = models.CharField(max_length=300, null=True, blank=True)
    intro_markdown = models.TextField(null=True, blank=True)
    intro_html = models.TextField(null=True, blank=True)
    slug = models.SlugField(blank=True)
    color_rgb = models.CharField(max_length=20, 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)

    def __str__(self):
        return self.name
    
    class Meta:
        verbose_name = _("Category")
        verbose_name_plural = _("Categories")

    def get_absolute_url(self):
        return reverse("taxonomy:cat-detail", kwargs={"slug": self.slug})


    def save(self, *args, **kwargs):
        if self.intro_markdown:
            self.intro_html = markdown_to_html(self.intro_markdown)
        super(Category, self).save(*args, **kwargs)