Browse files

Extracted a base class for categories to allow other apps to make the…

…ir own independent category-style models.

* Updated for django-mptt 0.5.2
* Fixed typo in the CategoryRelation field in that the foreign key is called 'story'
* Made the order field non-null and default to 0
* Changed the parent foreign key a TreeForeignKey (for 0.5.2)
* Changed requirements to mptt>=0.5.2
* Added a migration for model changes.
  • Loading branch information...
1 parent 84f84e1 commit fdf968fb6e0aead56b877f483bfd9d110798e2f4 @coordt coordt committed Feb 6, 2012
Showing with 147 additions and 56 deletions.
  1. +78 −0 categories/migrations/0009_changed_category_relation.py
  2. +68 −55 categories/models.py
  3. +1 −1 requirements.txt
View
78 categories/migrations/0009_changed_category_relation.py
@@ -0,0 +1,78 @@
+# encoding: utf-8
+import datetime
+from south.db import db
+from south.v2 import SchemaMigration
+from django.db import models
+
+class Migration(SchemaMigration):
+
+ def forwards(self, orm):
+
+ # Changing field 'Category.parent'
+ db.alter_column('categories_category', 'parent_id', self.gf('mptt.fields.TreeForeignKey')(null=True, to=orm['categories.Category']))
+
+ # Changing field 'Category.order'
+ db.alter_column('categories_category', 'order', self.gf('django.db.models.fields.IntegerField')())
+
+ # Deleting field 'CategoryRelation.story'
+ db.delete_column('categories_categoryrelation', 'story_id')
+
+ # Adding field 'CategoryRelation.category'
+ db.add_column('categories_categoryrelation', 'category', self.gf('django.db.models.fields.related.ForeignKey')(default=0, to=orm['categories.Category']), keep_default=False)
+
+
+ def backwards(self, orm):
+
+ # Changing field 'Category.parent'
+ db.alter_column('categories_category', 'parent_id', self.gf('django.db.models.fields.related.ForeignKey')(null=True, to=orm['categories.Category']))
+
+ # Changing field 'Category.order'
+ db.alter_column('categories_category', 'order', self.gf('django.db.models.fields.IntegerField')(null=True))
+
+ # User chose to not deal with backwards NULL issues for 'CategoryRelation.story'
+ raise RuntimeError("Cannot reverse this migration. 'CategoryRelation.story' and its values cannot be restored.")
+
+ # Deleting field 'CategoryRelation.category'
+ db.delete_column('categories_categoryrelation', 'category_id')
+
+
+ models = {
+ 'categories.category': {
+ 'Meta': {'ordering': "('tree_id', 'lft')", 'unique_together': "(('parent', 'name'),)", 'object_name': 'Category'},
+ 'active': ('django.db.models.fields.BooleanField', [], {'default': 'True'}),
+ 'alternate_title': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '100', 'blank': 'True'}),
+ 'alternate_url': ('django.db.models.fields.CharField', [], {'max_length': '200', 'blank': 'True'}),
+ 'description': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'level': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
+ 'lft': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
+ 'meta_extra': ('django.db.models.fields.TextField', [], {'default': "''", 'blank': 'True'}),
+ 'meta_keywords': ('django.db.models.fields.CharField', [], {'default': "''", 'max_length': '255', 'blank': 'True'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'order': ('django.db.models.fields.IntegerField', [], {'default': '0'}),
+ 'parent': ('mptt.fields.TreeForeignKey', [], {'blank': 'True', 'related_name': "'children'", 'null': 'True', 'to': "orm['categories.Category']"}),
+ 'rght': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}),
+ 'slug': ('django.db.models.fields.SlugField', [], {'max_length': '50', 'db_index': 'True'}),
+ 'thumbnail': ('django.db.models.fields.files.FileField', [], {'max_length': '100', 'null': 'True', 'blank': 'True'}),
+ 'thumbnail_height': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}),
+ 'thumbnail_width': ('django.db.models.fields.IntegerField', [], {'null': 'True', 'blank': 'True'}),
+ 'tree_id': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'})
+ },
+ 'categories.categoryrelation': {
+ 'Meta': {'object_name': 'CategoryRelation'},
+ 'category': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['categories.Category']"}),
+ 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'object_id': ('django.db.models.fields.PositiveIntegerField', [], {}),
+ 'relation_type': ('django.db.models.fields.CharField', [], {'max_length': "'200'", 'null': 'True', 'blank': 'True'})
+ },
+ 'contenttypes.contenttype': {
+ 'Meta': {'ordering': "('name',)", 'unique_together': "(('app_label', 'model'),)", 'object_name': 'ContentType', 'db_table': "'django_content_type'"},
+ 'app_label': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}),
+ 'model': ('django.db.models.fields.CharField', [], {'max_length': '100'}),
+ 'name': ('django.db.models.fields.CharField', [], {'max_length': '100'})
+ }
+ }
+
+ complete_apps = ['categories']
View
123 categories/models.py
@@ -7,7 +7,7 @@
from django.template.defaultfilters import slugify
from django.utils.translation import ugettext as _
-from mptt.models import MPTTModel
+from mptt.models import MPTTModel, TreeForeignKey
from .settings import (RELATION_MODELS, RELATIONS, THUMBNAIL_UPLOAD_PATH,
THUMBNAIL_STORAGE)
@@ -24,22 +24,58 @@ def active(self):
"""
return self.get_query_set().filter(active=True)
-class Category(MPTTModel):
- parent = models.ForeignKey('self',
+
+class CategoryBase(MPTTModel):
+ parent = TreeForeignKey('self',
blank=True,
null=True,
related_name="children",
help_text="Leave this blank for an Category Tree",
verbose_name='Parent')
name = models.CharField(max_length=100)
+ slug = models.SlugField()
+ active = models.BooleanField(default=True)
+
+ objects = CategoryManager()
+
+ def save(self, *args, **kwargs):
+ """
+ While you can activate an item without activating its descendants,
+ It doesn't make sense that you can deactivate an item and have its
+ decendants remain active.
+ """
+ if not self.slug:
+ self.slug = slugify(self.name)[:50]
+
+ super(CategoryBase, self).save(*args, **kwargs)
+
+ if not self.active:
+ for item in self.get_descendants():
+ if item.active != self.active:
+ item.active = self.active
+ item.save()
+
+ def __unicode__(self):
+ ancestors = self.get_ancestors()
+ return ' > '.join([force_unicode(i.name) for i in ancestors]+[self.name,])
+
+ class Meta:
+ abstract = True
+ unique_together = ('parent', 'name')
+ ordering = ('tree_id', 'lft')
+
+ class MPTTMeta:
+ order_insertion_by = 'name'
+
+
+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(blank=True, null=True)
- slug = models.SlugField()
+ order = models.IntegerField(default=0)
alternate_title = models.CharField(
blank=True,
default="",
@@ -59,9 +95,7 @@ class Category(MPTTModel):
blank=True,
default="",
help_text="(Advanced) Any additional HTML to be placed verbatim in the <head>")
- active = models.BooleanField(default=True)
- objects = CategoryManager()
@property
def short_title(self):
@@ -90,8 +124,6 @@ def get_relation_type(self, relation_type):
return self.categoryrelation_set.filter(relation_type=relation_type)
def save(self, *args, **kwargs):
- if not self.slug:
- self.slug = slugify(self.name)[:50]
if self.thumbnail:
from django.core.files.images import get_image_dimensions
import django
@@ -106,56 +138,37 @@ def save(self, *args, **kwargs):
self.thumbnail_height = height
super(Category, self).save(*args, **kwargs)
-
- # While you can activate an item without activating its descendants,
- # It doesn't make sense that you can deactivate an item and have its
- # decendants remain active.
- if not self.active:
- for item in self.get_descendants():
- if item.active != self.active:
- item.active = self.active
- item.save()
- class Meta:
+ class Meta(CategoryBase.Meta):
verbose_name_plural = 'categories'
- unique_together = ('parent', 'name')
- ordering = ('tree_id', 'lft')
class MPTTMeta:
- verbose_name_plural = 'categories'
- unique_together = ('parent', 'name')
- ordering = ('tree_id', 'lft')
- order_insertion_by = 'name'
+ order_insertion_by = ('order', 'name')
+
+category_relation_limits = reduce(lambda x,y: x|y, RELATIONS)
+class CategoryRelationManager(models.Manager):
+ def get_content_type(self, content_type):
+ qs = self.get_query_set()
+ return qs.filter(content_type__name=content_type)
- def __unicode__(self):
- ancestors = self.get_ancestors()
- return ' > '.join([force_unicode(i.name) for i in ancestors]+[self.name,])
+ def get_relation_type(self, relation_type):
+ qs = self.get_query_set()
+ return qs.filter(relation_type=relation_type)
-if RELATION_MODELS:
- category_relation_limits = reduce(lambda x,y: x|y, RELATIONS)
- class CategoryRelationManager(models.Manager):
- def get_content_type(self, content_type):
- qs = self.get_query_set()
- return qs.filter(content_type__name=content_type)
-
- def get_relation_type(self, relation_type):
- qs = self.get_query_set()
- return qs.filter(relation_type=relation_type)
+class CategoryRelation(models.Model):
+ """Related category item"""
+ category = models.ForeignKey(Category)
+ content_type = models.ForeignKey(
+ ContentType, limit_choices_to=category_relation_limits)
+ object_id = models.PositiveIntegerField()
+ content_object = generic.GenericForeignKey('content_type', 'object_id')
+ relation_type = models.CharField(_("Relation Type"),
+ max_length="200",
+ blank=True,
+ null=True,
+ help_text=_("A generic text field to tag a relation, like 'leadphoto'."))
- class CategoryRelation(models.Model):
- """Related story item"""
- story = models.ForeignKey(Category)
- content_type = models.ForeignKey(
- ContentType, limit_choices_to=category_relation_limits)
- object_id = models.PositiveIntegerField()
- content_object = generic.GenericForeignKey('content_type', 'object_id')
- relation_type = models.CharField(_("Relation Type"),
- max_length="200",
- blank=True,
- null=True,
- help_text=_("A generic text field to tag a relation, like 'leadphoto'."))
-
- objects = CategoryRelationManager()
-
- def __unicode__(self):
- return u"CategoryRelation"
+ objects = CategoryRelationManager()
+
+ def __unicode__(self):
+ return u"CategoryRelation"
View
2 requirements.txt
@@ -1 +1 @@
-django-mptt==0.4.2
+django-mptt>=0.5.2

0 comments on commit fdf968f

Please sign in to comment.