2021-12-22 18:03:21 +00:00
|
|
|
"""Category models."""
|
2024-04-15 17:43:22 +00:00
|
|
|
|
2021-12-05 14:34:46 +00:00
|
|
|
from functools import reduce
|
|
|
|
|
|
2024-01-23 11:01:07 +00:00
|
|
|
from django.contrib.contenttypes.fields import GenericForeignKey
|
2021-12-05 14:34:46 +00:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2017-03-30 01:43:55 +00:00
|
|
|
from django.core.files.images import get_image_dimensions
|
2009-07-03 00:23:08 +00:00
|
|
|
from django.db import models
|
2021-12-05 14:34:46 +00:00
|
|
|
from django.urls import reverse
|
2021-12-16 11:26:40 +00:00
|
|
|
from django.utils.encoding import force_str
|
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
2009-07-03 00:23:08 +00:00
|
|
|
|
2012-02-07 18:03:31 +00:00
|
|
|
from .base import CategoryBase
|
2021-12-05 14:34:46 +00:00
|
|
|
from .settings import (
|
|
|
|
|
RELATION_MODELS,
|
|
|
|
|
RELATIONS,
|
2023-08-17 12:12:42 +00:00
|
|
|
THUMBNAIL_STORAGE_ALIAS,
|
2021-12-05 14:34:46 +00:00
|
|
|
THUMBNAIL_UPLOAD_PATH,
|
|
|
|
|
)
|
2012-02-06 16:51:12 +00:00
|
|
|
|
2023-08-17 12:12:42 +00:00
|
|
|
# Determine storage method based on Django version
|
|
|
|
|
try: # Django 4.2+
|
|
|
|
|
from django.core.files.storage import storages
|
|
|
|
|
|
|
|
|
|
STORAGE = storages[THUMBNAIL_STORAGE_ALIAS]
|
|
|
|
|
except ImportError:
|
|
|
|
|
from django.core.files.storage import get_storage_class
|
|
|
|
|
|
2024-05-22 07:31:56 +00:00
|
|
|
from .settings import THUMBNAIL_STORAGE
|
|
|
|
|
|
2023-08-17 12:12:42 +00:00
|
|
|
STORAGE = get_storage_class(THUMBNAIL_STORAGE)()
|
2012-02-06 16:51:12 +00:00
|
|
|
|
2012-07-12 23:24:55 +00:00
|
|
|
|
2012-02-06 16:51:12 +00:00
|
|
|
class Category(CategoryBase):
|
2021-12-22 18:03:21 +00:00
|
|
|
"""A basic category model."""
|
|
|
|
|
|
2011-05-11 13:01:33 +00:00
|
|
|
thumbnail = models.FileField(
|
2012-07-12 23:24:55 +00:00
|
|
|
upload_to=THUMBNAIL_UPLOAD_PATH,
|
2021-12-05 14:34:46 +00:00
|
|
|
null=True,
|
|
|
|
|
blank=True,
|
2023-08-17 12:12:42 +00:00
|
|
|
storage=STORAGE,
|
2021-12-05 14:34:46 +00:00
|
|
|
)
|
2011-05-11 13:01:33 +00:00
|
|
|
thumbnail_width = models.IntegerField(blank=True, null=True)
|
|
|
|
|
thumbnail_height = models.IntegerField(blank=True, null=True)
|
2012-02-06 16:51:12 +00:00
|
|
|
order = models.IntegerField(default=0)
|
2010-04-22 18:31:38 +00:00
|
|
|
alternate_title = models.CharField(
|
2021-12-05 14:34:46 +00:00
|
|
|
blank=True, default="", max_length=100, help_text="An alternative title to use on pages with this category."
|
|
|
|
|
)
|
2012-01-03 13:58:23 +00:00
|
|
|
alternate_url = models.CharField(
|
2012-07-12 23:24:55 +00:00
|
|
|
blank=True,
|
|
|
|
|
max_length=200,
|
2021-12-05 14:34:46 +00:00
|
|
|
help_text="An alternative URL to use instead of the one derived from " "the category hierarchy.",
|
|
|
|
|
)
|
2010-04-22 18:31:38 +00:00
|
|
|
description = models.TextField(blank=True, null=True)
|
|
|
|
|
meta_keywords = models.CharField(
|
2021-12-05 14:34:46 +00:00
|
|
|
blank=True, default="", max_length=255, help_text="Comma-separated keywords for search engines."
|
|
|
|
|
)
|
2010-04-22 18:31:38 +00:00
|
|
|
meta_extra = models.TextField(
|
2021-12-05 14:34:46 +00:00
|
|
|
blank=True, default="", help_text="(Advanced) Any additional HTML to be placed verbatim " "in the <head>"
|
|
|
|
|
)
|
2012-07-12 23:24:55 +00:00
|
|
|
|
2010-12-12 18:15:03 +00:00
|
|
|
@property
|
|
|
|
|
def short_title(self):
|
2021-12-22 18:03:21 +00:00
|
|
|
"""Return the name."""
|
2010-12-12 18:15:03 +00:00
|
|
|
return self.name
|
2012-07-12 23:24:55 +00:00
|
|
|
|
2009-07-03 00:23:08 +00:00
|
|
|
def get_absolute_url(self):
|
2021-12-22 18:03:21 +00:00
|
|
|
"""Return a path."""
|
2017-12-26 17:27:42 +00:00
|
|
|
from django.urls import NoReverseMatch
|
2015-06-09 20:17:21 +00:00
|
|
|
|
2011-08-01 13:09:34 +00:00
|
|
|
if self.alternate_url:
|
|
|
|
|
return self.alternate_url
|
2015-06-09 20:17:21 +00:00
|
|
|
try:
|
2021-12-05 14:34:46 +00:00
|
|
|
prefix = reverse("categories_tree_list")
|
2015-06-09 20:17:21 +00:00
|
|
|
except NoReverseMatch:
|
2021-12-05 14:34:46 +00:00
|
|
|
prefix = "/"
|
|
|
|
|
ancestors = list(self.get_ancestors()) + [
|
|
|
|
|
self,
|
|
|
|
|
]
|
2021-12-16 11:26:40 +00:00
|
|
|
return prefix + "/".join([force_str(i.slug) for i in ancestors]) + "/"
|
2012-07-12 23:24:55 +00:00
|
|
|
|
2011-05-11 11:08:06 +00:00
|
|
|
if RELATION_MODELS:
|
2021-12-05 14:34:46 +00:00
|
|
|
|
2011-05-11 11:08:06 +00:00
|
|
|
def get_related_content_type(self, content_type):
|
|
|
|
|
"""
|
2021-12-22 18:03:21 +00:00
|
|
|
Get all related items of the specified content type.
|
2011-05-11 11:08:06 +00:00
|
|
|
"""
|
2021-12-05 14:34:46 +00:00
|
|
|
return self.categoryrelation_set.filter(content_type__name=content_type)
|
2012-07-12 23:24:55 +00:00
|
|
|
|
2011-05-11 11:08:06 +00:00
|
|
|
def get_relation_type(self, relation_type):
|
|
|
|
|
"""
|
2021-12-22 18:03:21 +00:00
|
|
|
Get all relations of the specified relation type.
|
2011-05-11 11:08:06 +00:00
|
|
|
"""
|
2011-05-12 10:55:08 +00:00
|
|
|
return self.categoryrelation_set.filter(relation_type=relation_type)
|
2012-07-12 23:24:55 +00:00
|
|
|
|
2011-05-11 13:01:33 +00:00
|
|
|
def save(self, *args, **kwargs):
|
2021-12-22 18:03:21 +00:00
|
|
|
"""Save the category."""
|
2011-05-11 13:01:33 +00:00
|
|
|
if self.thumbnail:
|
2017-03-30 01:43:55 +00:00
|
|
|
width, height = get_image_dimensions(self.thumbnail.file)
|
2011-05-11 13:01:33 +00:00
|
|
|
else:
|
|
|
|
|
width, height = None, None
|
2012-07-12 23:24:55 +00:00
|
|
|
|
2011-05-11 13:01:33 +00:00
|
|
|
self.thumbnail_width = width
|
|
|
|
|
self.thumbnail_height = height
|
2012-07-12 23:24:55 +00:00
|
|
|
|
2011-08-29 10:39:00 +00:00
|
|
|
super(Category, self).save(*args, **kwargs)
|
2012-07-12 23:24:55 +00:00
|
|
|
|
2012-02-06 16:51:12 +00:00
|
|
|
class Meta(CategoryBase.Meta):
|
2021-12-05 14:34:46 +00:00
|
|
|
verbose_name = _("category")
|
|
|
|
|
verbose_name_plural = _("categories")
|
2012-07-12 23:24:55 +00:00
|
|
|
|
2010-10-24 00:31:17 +00:00
|
|
|
class MPTTMeta:
|
2021-12-05 14:34:46 +00:00
|
|
|
order_insertion_by = ("order", "name")
|
2012-02-06 16:51:12 +00:00
|
|
|
|
2012-07-12 23:24:55 +00:00
|
|
|
|
2012-02-07 18:03:31 +00:00
|
|
|
if RELATIONS:
|
2012-07-12 23:24:55 +00:00
|
|
|
CATEGORY_RELATION_LIMITS = reduce(lambda x, y: x | y, RELATIONS)
|
2012-02-07 18:03:31 +00:00
|
|
|
else:
|
|
|
|
|
CATEGORY_RELATION_LIMITS = []
|
|
|
|
|
|
2012-07-12 23:24:55 +00:00
|
|
|
|
2012-02-06 16:51:12 +00:00
|
|
|
class CategoryRelationManager(models.Manager):
|
2021-12-22 18:03:21 +00:00
|
|
|
"""Custom access functions for category relations."""
|
|
|
|
|
|
2012-02-06 16:51:12 +00:00
|
|
|
def get_content_type(self, content_type):
|
2012-02-07 18:03:31 +00:00
|
|
|
"""
|
|
|
|
|
Get all the items of the given content type related to this item.
|
|
|
|
|
"""
|
2014-09-12 14:58:27 +00:00
|
|
|
qs = self.get_queryset()
|
2012-02-06 16:51:12 +00:00
|
|
|
return qs.filter(content_type__name=content_type)
|
2012-07-12 23:24:55 +00:00
|
|
|
|
2012-02-06 16:51:12 +00:00
|
|
|
def get_relation_type(self, relation_type):
|
2012-02-07 18:03:31 +00:00
|
|
|
"""
|
|
|
|
|
Get all the items of the given relationship type related to this item.
|
|
|
|
|
"""
|
2014-09-12 14:58:27 +00:00
|
|
|
qs = self.get_queryset()
|
2012-02-06 16:51:12 +00:00
|
|
|
return qs.filter(relation_type=relation_type)
|
2011-05-12 10:45:36 +00:00
|
|
|
|
2012-07-12 23:24:55 +00:00
|
|
|
|
2012-02-06 16:51:12 +00:00
|
|
|
class CategoryRelation(models.Model):
|
2021-12-22 18:03:21 +00:00
|
|
|
"""Related category item."""
|
2021-12-05 14:34:46 +00:00
|
|
|
|
|
|
|
|
category = models.ForeignKey(Category, verbose_name=_("category"), on_delete=models.CASCADE)
|
2012-02-06 16:51:12 +00:00
|
|
|
content_type = models.ForeignKey(
|
2021-12-05 14:34:46 +00:00
|
|
|
ContentType,
|
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
|
limit_choices_to=CATEGORY_RELATION_LIMITS,
|
|
|
|
|
verbose_name=_("content type"),
|
|
|
|
|
)
|
|
|
|
|
object_id = models.PositiveIntegerField(verbose_name=_("object id"))
|
|
|
|
|
content_object = GenericForeignKey("content_type", "object_id")
|
2016-02-15 21:47:21 +00:00
|
|
|
relation_type = models.CharField(
|
2021-12-05 14:34:46 +00:00
|
|
|
verbose_name=_("relation type"),
|
2016-02-15 17:53:01 +00:00
|
|
|
max_length=200,
|
2012-07-12 23:24:55 +00:00
|
|
|
blank=True,
|
2012-02-06 16:51:12 +00:00
|
|
|
null=True,
|
2021-12-05 14:34:46 +00:00
|
|
|
help_text=_("A generic text field to tag a relation, like 'leadphoto'."),
|
|
|
|
|
)
|
2012-07-12 23:24:55 +00:00
|
|
|
|
2012-02-06 16:51:12 +00:00
|
|
|
objects = CategoryRelationManager()
|
2012-07-12 23:24:55 +00:00
|
|
|
|
2012-02-06 16:51:12 +00:00
|
|
|
def __unicode__(self):
|
2016-02-10 17:47:30 +00:00
|
|
|
return "CategoryRelation"
|