forked from jazzband/django-categories
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
44 changed files
with
1,533 additions
and
437 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from categories.models import CategoryBase | ||
|
||
class SimpleCategory(CategoryBase): | ||
""" | ||
A simple of catgorizing example | ||
""" | ||
|
||
class Meta: | ||
verbose_name_plural = 'simple categories' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
from django.contrib import admin | ||
|
||
from categories.admin import CategoryBaseAdmin, CategoryBaseAdminForm | ||
|
||
from .models import SimpleCategory | ||
|
||
class SimpleCategoryAdminForm(CategoryBaseAdminForm): | ||
class Meta: | ||
model = SimpleCategory | ||
|
||
class SimpleCategoryAdmin(CategoryBaseAdmin): | ||
form = SimpleCategoryAdminForm | ||
|
||
admin.site.register(SimpleCategory, SimpleCategoryAdmin) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
class Category(CategoryBase): | ||
thumbnail = models.FileField( | ||
upload_to=THUMBNAIL_UPLOAD_PATH, | ||
null=True, blank=True, | ||
storage=STORAGE(),) | ||
thumbnail_width = models.IntegerField(blank=True, null=True) | ||
thumbnail_height = models.IntegerField(blank=True, null=True) | ||
order = models.IntegerField(default=0) | ||
alternate_title = models.CharField( | ||
blank=True, | ||
default="", | ||
max_length=100, | ||
help_text="An alternative title to use on pages with this category.") | ||
alternate_url = models.CharField( | ||
blank=True, | ||
max_length=200, | ||
help_text="An alternative URL to use instead of the one derived from " | ||
"the category hierarchy.") | ||
description = models.TextField(blank=True, null=True) | ||
meta_keywords = models.CharField( | ||
blank=True, | ||
default="", | ||
max_length=255, | ||
help_text="Comma-separated keywords for search engines.") | ||
meta_extra = models.TextField( | ||
blank=True, | ||
default="", | ||
help_text="(Advanced) Any additional HTML to be placed verbatim " | ||
"in the <head>") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
def save(self, *args, **kwargs): | ||
if self.thumbnail: | ||
from django.core.files.images import get_image_dimensions | ||
import django | ||
if django.VERSION[1] < 2: | ||
width, height = get_image_dimensions(self.thumbnail.file) | ||
else: | ||
width, height = get_image_dimensions(self.thumbnail.file, close=True) | ||
else: | ||
width, height = None, None | ||
|
||
self.thumbnail_width = width | ||
self.thumbnail_height = height | ||
|
||
super(Category, self).save(*args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class Meta(CategoryBase.Meta): | ||
verbose_name_plural = 'categories' | ||
|
||
class MPTTMeta: | ||
order_insertion_by = ('order', 'name') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class CategoryAdminForm(CategoryBaseAdminForm): | ||
class Meta: | ||
model = Category | ||
|
||
def clean_alternate_title(self): | ||
if self.instance is None or not self.cleaned_data['alternate_title']: | ||
return self.cleaned_data['name'] | ||
else: | ||
return self.cleaned_data['alternate_title'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class CategoryAdmin(CategoryBaseAdmin): | ||
form = CategoryAdminForm | ||
list_display = ('name', 'alternate_title', 'active') | ||
fieldsets = ( | ||
(None, { | ||
'fields': ('parent', 'name', 'thumbnail', 'active') | ||
}), | ||
('Meta Data', { | ||
'fields': ('alternate_title', 'alternate_url', 'description', | ||
'meta_keywords', 'meta_extra'), | ||
'classes': ('collapse',), | ||
}), | ||
('Advanced', { | ||
'fields': ('order', 'slug'), | ||
'classes': ('collapse',), | ||
}), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
.. _creating_custom_categories: | ||
|
||
========================== | ||
Creating Custom Categories | ||
========================== | ||
|
||
Django Categories isn't just for using a single category model. It allows you to create your own custom category-like models with as little or much customization as you need. | ||
|
||
Name only | ||
========= | ||
|
||
For many cases, you want a simple user-managed lookup table. You can do this with just a little bit of code. The resulting model will include name, slug and active fields and a hierarchical admin. | ||
|
||
#. Create a model that subclasses :py:class:`CategoryBase` | ||
|
||
.. literalinclude:: code_examples/custom_categories1.py | ||
:linenos: | ||
|
||
#. For the Django admin, create a subclass of :py:class:`CategoryBaseAdminForm`. Create an internal class called :py:class:`Meta` and assign the attribute ``model`` to your model (see line 9 in the example). | ||
|
||
.. literalinclude:: code_examples/custom_categories2.py | ||
:linenos: | ||
|
||
#. Create a subclass of CategoryBaseAdmin and assign ``form`` attribute the class created above (see line 12 in the above example). | ||
|
||
#. Register your model and custom model admin class. | ||
|
||
Name and other data | ||
=================== | ||
|
||
Sometimes you need more functionality, such as extra metadata and custom functions. The :py:class:`Category` model in this package does this. | ||
|
||
#. Create a model that subclasses :py:class:`CategoryBase` as above. | ||
|
||
#. Add new fields to the model. The :py:class:`Category` model adds these extra fields. | ||
|
||
.. literalinclude:: code_examples/custom_categories3.py | ||
:linenos: | ||
|
||
#. Add new methods to the model. For example, the :py:class:`Category` model adds several new methods, including overriding the :py:meth:`save` method. | ||
|
||
.. literalinclude:: code_examples/custom_categories4.py | ||
:linenos: | ||
|
||
#. Alter :py:class:`Meta` or :py:class:`MPTTMeta` class. Either of these inner classes can be overridden, however your :py:class:`Meta` class should inherit :py:class:`CategoryBase.Meta`. Options for :py:class:`Meta` are in the `Django-MPTT docs <http://readthedocs.org/docs/django-mptt/en/latest/models.html#model-options>`_. | ||
|
||
.. literalinclude:: code_examples/custom_categories5.py | ||
:linenos: | ||
|
||
#. For the admin, you must create a form that subclasses :py:class:`CategoryBaseAdminForm` and at least sets the ``Meta.model`` attribute. You can also alter the form fields and cleaning methods, as :py:class:`Category` does. | ||
|
||
.. literalinclude:: code_examples/custom_categories6.py | ||
:linenos: | ||
|
||
#. Next you must subclass :py:class:`CategoryBaseAdmin` and assign the ``form`` attribute the form class created above. You can alter any other attributes as necessary. | ||
|
||
.. literalinclude:: code_examples/custom_categories7.py | ||
:linenos: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.