Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extending category #552

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 4 additions & 76 deletions zinnia/models/category.py
Original file line number Diff line number Diff line change
@@ -1,81 +1,9 @@
"""Category model for Zinnia"""
from django.db import models
from django.urls import reverse
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _
from zinnia.models_bases import load_model_class
from zinnia.settings import CATEGORY_BASE_MODEL

from mptt.managers import TreeManager
from mptt.models import MPTTModel
from mptt.models import TreeForeignKey

from zinnia.managers import EntryRelatedPublishedManager
from zinnia.managers import entries_published


@python_2_unicode_compatible
class Category(MPTTModel):
class Category(load_model_class(CATEGORY_BASE_MODEL)):
"""
Simple model for categorizing entries.
The final Category model based on inheritence.
"""

title = models.CharField(
_('title'), max_length=255)

slug = models.SlugField(
_('slug'), unique=True, max_length=255,
help_text=_("Used to build the category's URL."))

description = models.TextField(
_('description'), blank=True)

parent = TreeForeignKey(
'self',
related_name='children',
null=True, blank=True,
on_delete=models.SET_NULL,
verbose_name=_('parent category'))

objects = TreeManager()
published = EntryRelatedPublishedManager()

def entries_published(self):
"""
Returns category's published entries.
"""
return entries_published(self.entries)

@property
def tree_path(self):
"""
Returns category's tree path
by concatening the slug of his ancestors.
"""
if self.parent_id:
return '/'.join(
[ancestor.slug for ancestor in self.get_ancestors()] +
[self.slug])
return self.slug

def get_absolute_url(self):
"""
Builds and returns the category's URL
based on his tree path.
"""
return reverse('zinnia:category_detail', args=(self.tree_path,))

def __str__(self):
return self.title

class Meta:
"""
Category's meta informations.
"""
ordering = ['title']
verbose_name = _('category')
verbose_name_plural = _('categories')

class MPTTMeta:
"""
Category MPTT's meta informations.
"""
order_insertion_by = ['title']
82 changes: 82 additions & 0 deletions zinnia/models_bases/category.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
"""Category model for Zinnia"""
from django.db import models
from django.urls import reverse
from django.utils.encoding import python_2_unicode_compatible
from django.utils.translation import ugettext_lazy as _

from mptt.managers import TreeManager
from mptt.models import MPTTModel
from mptt.models import TreeForeignKey

from zinnia.managers import EntryRelatedPublishedManager
from zinnia.managers import entries_published


@python_2_unicode_compatible
class AbstractCategory(MPTTModel):
"""
Simple model for categorizing entries.
"""

title = models.CharField(
_('title'), max_length=255)

slug = models.SlugField(
_('slug'), unique=True, max_length=255,
help_text=_("Used to build the category's URL."))

description = models.TextField(
_('description'), blank=True)

parent = TreeForeignKey(
'self',
related_name='children',
null=True, blank=True,
on_delete=models.SET_NULL,
verbose_name=_('parent category'))

objects = TreeManager()
published = EntryRelatedPublishedManager()

def entries_published(self):
"""
Returns category's published entries.
"""
return entries_published(self.entries)

@property
def tree_path(self):
"""
Returns category's tree path
by concatening the slug of his ancestors.
"""
if self.parent_id:
return '/'.join(
[ancestor.slug for ancestor in self.get_ancestors()] +
[self.slug])
return self.slug

def get_absolute_url(self):
"""
Builds and returns the category's URL
based on his tree path.
"""
return reverse('zinnia:category_detail', args=(self.tree_path,))

def __str__(self):
return self.title

class Meta:
"""
Category's meta informations.
"""
ordering = ['title']
verbose_name = _('category')
verbose_name_plural = _('categories')
abstract = True

class MPTTMeta:
"""
Category MPTT's meta informations.
"""
order_insertion_by = ['title']
2 changes: 2 additions & 0 deletions zinnia/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

ENTRY_BASE_MODEL = getattr(settings, 'ZINNIA_ENTRY_BASE_MODEL',
'zinnia.models_bases.entry.AbstractEntry')
CATEGORY_BASE_MODEL = getattr(settings, 'ZINNIA_CATEGORY_BASE_MODEL',
'zinnia.models_bases.category.AbstractCategory')

ENTRY_DETAIL_TEMPLATES = getattr(
settings, 'ZINNIA_ENTRY_DETAIL_TEMPLATES', [])
Expand Down