From 21abe382ad825e1383dfb1299efc1201ddd1e1c0 Mon Sep 17 00:00:00 2001 From: Stephen Burrows Date: Wed, 1 Jun 2011 17:47:28 -0400 Subject: [PATCH] Implemented the solution proposed to #130 - removed TreeModel and made a SlugTreeEntity subclass of TreeEntity to handle Node and Template. Correctly subclassed SlugTreeEntity.Meta so that Node and Template actually have the unique constraints they are meant to. Minor correction to TreeEntity.get_path. --- docs/models/entities.rst | 4 +- philo/admin/base.py | 6 +- philo/admin/pages.py | 4 +- ...003_auto__del_field_navigationitem_slug.py | 74 +++++++++ philo/contrib/shipherd/models.py | 9 +- ...parent__add_unique_template_slug_parent.py | 144 ++++++++++++++++++ philo/models/base.py | 76 +++++---- philo/models/nodes.py | 6 +- philo/models/pages.py | 6 +- 9 files changed, 271 insertions(+), 58 deletions(-) create mode 100644 philo/contrib/shipherd/migrations/0003_auto__del_field_navigationitem_slug.py create mode 100644 philo/migrations/0016_auto__add_unique_node_slug_parent__add_unique_template_slug_parent.py diff --git a/docs/models/entities.rst b/docs/models/entities.rst index a394c6f..b39a253 100644 --- a/docs/models/entities.rst +++ b/docs/models/entities.rst @@ -41,7 +41,7 @@ Entities .. autoclass:: Entity :members: -.. autoclass:: TreeManager +.. autoclass:: TreeEntityManager :members: .. autoclass:: TreeEntity @@ -50,6 +50,6 @@ Entities .. attribute:: objects - An instance of :class:`TreeManager`. + An instance of :class:`TreeEntityManager`. .. automethod:: get_path \ No newline at end of file diff --git a/philo/admin/base.py b/philo/admin/base.py index 3a9458e..81916ab 100644 --- a/philo/admin/base.py +++ b/philo/admin/base.py @@ -136,11 +136,7 @@ class EntityAdmin(admin.ModelAdmin): return db_field.formfield(**kwargs) -class TreeAdmin(MPTTModelAdmin): - pass - - -class TreeEntityAdmin(EntityAdmin, TreeAdmin): +class TreeEntityAdmin(EntityAdmin, MPTTModelAdmin): pass diff --git a/philo/admin/pages.py b/philo/admin/pages.py index fd8665b..3e8f0f1 100644 --- a/philo/admin/pages.py +++ b/philo/admin/pages.py @@ -2,7 +2,7 @@ from django import forms from django.conf import settings from django.contrib import admin -from philo.admin.base import COLLAPSE_CLASSES, TreeAdmin +from philo.admin.base import COLLAPSE_CLASSES, TreeEntityAdmin from philo.admin.forms.containers import * from philo.admin.nodes import ViewAdmin from philo.models.pages import Page, Template, Contentlet, ContentReference @@ -55,7 +55,7 @@ class PageAdmin(ViewAdmin): return super(PageAdmin, self).response_add(request, obj, post_url_continue) -class TemplateAdmin(TreeAdmin): +class TemplateAdmin(TreeEntityAdmin): prepopulated_fields = {'slug': ('name',)} fieldsets = ( (None, { diff --git a/philo/contrib/shipherd/migrations/0003_auto__del_field_navigationitem_slug.py b/philo/contrib/shipherd/migrations/0003_auto__del_field_navigationitem_slug.py new file mode 100644 index 0000000..5d7d5e3 --- /dev/null +++ b/philo/contrib/shipherd/migrations/0003_auto__del_field_navigationitem_slug.py @@ -0,0 +1,74 @@ +# 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): + + # Deleting field 'NavigationItem.slug' + db.delete_column('shipherd_navigationitem', 'slug') + + + def backwards(self, orm): + + # User chose to not deal with backwards NULL issues for 'NavigationItem.slug' + raise RuntimeError("Cannot reverse this migration. 'NavigationItem.slug' and its values cannot be restored.") + + + models = { + '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'}) + }, + 'philo.attribute': { + 'Meta': {'unique_together': "(('key', 'entity_content_type', 'entity_object_id'), ('value_content_type', 'value_object_id'))", 'object_name': 'Attribute'}, + 'entity_content_type': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'attribute_entity_set'", 'to': "orm['contenttypes.ContentType']"}), + 'entity_object_id': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'key': ('django.db.models.fields.CharField', [], {'max_length': '255', 'db_index': 'True'}), + 'value_content_type': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'attribute_value_set'", 'null': 'True', 'to': "orm['contenttypes.ContentType']"}), + 'value_object_id': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True', 'null': 'True', 'blank': 'True'}) + }, + 'philo.node': { + 'Meta': {'object_name': 'Node'}, + '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'}), + 'parent': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'children'", 'null': 'True', 'to': "orm['philo.Node']"}), + 'rght': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}), + 'slug': ('django.db.models.fields.SlugField', [], {'max_length': '255', 'db_index': 'True'}), + 'tree_id': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}), + 'view_content_type': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'node_view_set'", 'null': 'True', 'to': "orm['contenttypes.ContentType']"}), + 'view_object_id': ('django.db.models.fields.PositiveIntegerField', [], {'null': 'True', 'blank': 'True'}) + }, + 'shipherd.navigation': { + 'Meta': {'unique_together': "(('node', 'key'),)", 'object_name': 'Navigation'}, + 'depth': ('django.db.models.fields.PositiveSmallIntegerField', [], {'default': '3'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'key': ('django.db.models.fields.CharField', [], {'max_length': '255', 'db_index': 'True'}), + 'node': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'navigation_set'", 'to': "orm['philo.Node']"}) + }, + 'shipherd.navigationitem': { + 'Meta': {'object_name': 'NavigationItem'}, + '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'}), + 'navigation': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'roots'", 'null': 'True', 'to': "orm['shipherd.Navigation']"}), + 'order': ('django.db.models.fields.PositiveSmallIntegerField', [], {'default': '0'}), + 'parent': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'children'", 'null': 'True', 'to': "orm['shipherd.NavigationItem']"}), + 'reversing_parameters': ('philo.models.fields.JSONField', [], {'blank': 'True'}), + 'rght': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}), + 'target_node': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'shipherd_navigationitem_related'", 'null': 'True', 'to': "orm['philo.Node']"}), + 'text': ('django.db.models.fields.CharField', [], {'max_length': '50'}), + 'tree_id': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}), + 'url_or_subpath': ('django.db.models.fields.CharField', [], {'max_length': '200', 'blank': 'True'}) + } + } + + complete_apps = ['shipherd'] diff --git a/philo/contrib/shipherd/models.py b/philo/contrib/shipherd/models.py index a520fbe..429faaa 100644 --- a/philo/contrib/shipherd/models.py +++ b/philo/contrib/shipherd/models.py @@ -7,7 +7,7 @@ from django.core.validators import RegexValidator, MinValueValidator from django.db import models from django.forms.models import model_to_dict -from philo.models.base import TreeEntity, TreeManager, Entity +from philo.models.base import TreeEntity, TreeEntityManager, Entity from philo.models.nodes import Node, TargetURLModel @@ -224,7 +224,7 @@ class Navigation(Entity): unique_together = ('node', 'key') -class NavigationItemManager(TreeManager): +class NavigationItemManager(TreeEntityManager): use_for_related = True def get_query_set(self): @@ -249,8 +249,9 @@ class NavigationItem(TreeEntity, TargetURLModel): self._initial_data = model_to_dict(self) self._is_cached = False - def __unicode__(self): - return self.get_path(field='text', pathsep=u' › ') + def get_path(self, root=None, pathsep=u' › ', field='text'): + return super(NavigationItem, self).get_path(root, pathsep, field) + path = property(get_path) def clean(self): super(NavigationItem, self).clean() diff --git a/philo/migrations/0016_auto__add_unique_node_slug_parent__add_unique_template_slug_parent.py b/philo/migrations/0016_auto__add_unique_node_slug_parent__add_unique_template_slug_parent.py new file mode 100644 index 0000000..7a79fec --- /dev/null +++ b/philo/migrations/0016_auto__add_unique_node_slug_parent__add_unique_template_slug_parent.py @@ -0,0 +1,144 @@ +# 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): + + # Adding unique constraint on 'Node', fields ['slug', 'parent'] + db.create_unique('philo_node', ['slug', 'parent_id']) + + # Adding unique constraint on 'Template', fields ['slug', 'parent'] + db.create_unique('philo_template', ['slug', 'parent_id']) + + + def backwards(self, orm): + + # Removing unique constraint on 'Template', fields ['slug', 'parent'] + db.delete_unique('philo_template', ['slug', 'parent_id']) + + # Removing unique constraint on 'Node', fields ['slug', 'parent'] + db.delete_unique('philo_node', ['slug', 'parent_id']) + + + models = { + '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'}) + }, + 'philo.attribute': { + 'Meta': {'unique_together': "(('key', 'entity_content_type', 'entity_object_id'), ('value_content_type', 'value_object_id'))", 'object_name': 'Attribute'}, + 'entity_content_type': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'attribute_entity_set'", 'to': "orm['contenttypes.ContentType']"}), + 'entity_object_id': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'key': ('django.db.models.fields.CharField', [], {'max_length': '255', 'db_index': 'True'}), + 'value_content_type': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'attribute_value_set'", 'null': 'True', 'to': "orm['contenttypes.ContentType']"}), + 'value_object_id': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True', 'null': 'True', 'blank': 'True'}) + }, + 'philo.collection': { + 'Meta': {'object_name': 'Collection'}, + 'description': ('django.db.models.fields.TextField', [], {'null': 'True', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '255'}) + }, + 'philo.collectionmember': { + 'Meta': {'object_name': 'CollectionMember'}, + 'collection': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'members'", 'to': "orm['philo.Collection']"}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'index': ('django.db.models.fields.PositiveIntegerField', [], {'null': 'True', 'blank': 'True'}), + 'member_content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}), + 'member_object_id': ('django.db.models.fields.PositiveIntegerField', [], {}) + }, + 'philo.contentlet': { + 'Meta': {'object_name': 'Contentlet'}, + 'content': ('philo.models.fields.TemplateField', [], {}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '255', 'db_index': 'True'}), + 'page': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'contentlets'", 'to': "orm['philo.Page']"}) + }, + 'philo.contentreference': { + 'Meta': {'object_name': 'ContentReference'}, + 'content_id': ('django.db.models.fields.PositiveIntegerField', [], {'null': 'True', 'blank': 'True'}), + 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']"}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '255', 'db_index': 'True'}), + 'page': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'contentreferences'", 'to': "orm['philo.Page']"}) + }, + 'philo.file': { + 'Meta': {'object_name': 'File'}, + 'file': ('django.db.models.fields.files.FileField', [], {'max_length': '100'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'mimetype': ('django.db.models.fields.CharField', [], {'max_length': '255'}) + }, + 'philo.foreignkeyvalue': { + 'Meta': {'object_name': 'ForeignKeyValue'}, + 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']", 'null': 'True', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'object_id': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True', 'null': 'True', 'blank': 'True'}) + }, + 'philo.jsonvalue': { + 'Meta': {'object_name': 'JSONValue'}, + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'value': ('philo.models.fields.JSONField', [], {'default': "'null'", 'db_index': 'True'}) + }, + 'philo.manytomanyvalue': { + 'Meta': {'object_name': 'ManyToManyValue'}, + 'content_type': ('django.db.models.fields.related.ForeignKey', [], {'to': "orm['contenttypes.ContentType']", 'null': 'True', 'blank': 'True'}), + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'values': ('django.db.models.fields.related.ManyToManyField', [], {'symmetrical': 'False', 'to': "orm['philo.ForeignKeyValue']", 'null': 'True', 'blank': 'True'}) + }, + 'philo.node': { + 'Meta': {'unique_together': "(('parent', 'slug'),)", 'object_name': 'Node'}, + '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'}), + 'parent': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'children'", 'null': 'True', 'to': "orm['philo.Node']"}), + 'rght': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}), + 'slug': ('django.db.models.fields.SlugField', [], {'max_length': '255', 'db_index': 'True'}), + 'tree_id': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}), + 'view_content_type': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'node_view_set'", 'null': 'True', 'to': "orm['contenttypes.ContentType']"}), + 'view_object_id': ('django.db.models.fields.PositiveIntegerField', [], {'null': 'True', 'blank': 'True'}) + }, + 'philo.page': { + 'Meta': {'object_name': 'Page'}, + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'template': ('django.db.models.fields.related.ForeignKey', [], {'related_name': "'pages'", 'to': "orm['philo.Template']"}), + 'title': ('django.db.models.fields.CharField', [], {'max_length': '255'}) + }, + 'philo.redirect': { + 'Meta': {'object_name': 'Redirect'}, + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'reversing_parameters': ('philo.models.fields.JSONField', [], {'blank': 'True'}), + 'status_code': ('django.db.models.fields.IntegerField', [], {'default': '302'}), + 'target_node': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'philo_redirect_related'", 'null': 'True', 'to': "orm['philo.Node']"}), + 'url_or_subpath': ('django.db.models.fields.CharField', [], {'max_length': '200', 'blank': 'True'}) + }, + 'philo.tag': { + 'Meta': {'ordering': "('name',)", 'object_name': 'Tag'}, + 'id': ('django.db.models.fields.AutoField', [], {'primary_key': 'True'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '255'}), + 'slug': ('django.db.models.fields.SlugField', [], {'unique': 'True', 'max_length': '255', 'db_index': 'True'}) + }, + 'philo.template': { + 'Meta': {'unique_together': "(('parent', 'slug'),)", 'object_name': 'Template'}, + 'code': ('philo.models.fields.TemplateField', [], {}), + 'documentation': ('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'}), + 'mimetype': ('django.db.models.fields.CharField', [], {'default': "'text/html'", 'max_length': '255'}), + 'name': ('django.db.models.fields.CharField', [], {'max_length': '255'}), + 'parent': ('django.db.models.fields.related.ForeignKey', [], {'blank': 'True', 'related_name': "'children'", 'null': 'True', 'to': "orm['philo.Template']"}), + 'rght': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}), + 'slug': ('django.db.models.fields.SlugField', [], {'max_length': '255', 'db_index': 'True'}), + 'tree_id': ('django.db.models.fields.PositiveIntegerField', [], {'db_index': 'True'}) + } + } + + complete_apps = ['philo'] diff --git a/philo/models/base.py b/philo/models/base.py index b41a00e..0218261 100644 --- a/philo/models/base.py +++ b/philo/models/base.py @@ -35,18 +35,6 @@ class Tag(models.Model): ordering = ('name',) -class Titled(models.Model): - # Use of this model is deprecated. - title = models.CharField(max_length=255) - slug = models.SlugField(max_length=255) - - def __unicode__(self): - return self.title - - class Meta: - abstract = True - - #: An instance of :class:`.ContentTypeRegistryLimiter` which is used to track the content types which can be related to by :class:`ForeignKeyValue`\ s and :class:`ManyToManyValue`\ s. value_content_type_limiter = ContentTypeRegistryLimiter() @@ -337,10 +325,18 @@ class Entity(models.Model): abstract = True -class TreeManager(models.Manager): +class TreeEntityBase(MPTTModelBase, EntityBase): + def __new__(meta, name, bases, attrs): + attrs['_mptt_meta'] = MPTTOptions(attrs.pop('MPTTMeta', None)) + cls = EntityBase.__new__(meta, name, bases, attrs) + + return meta.register(cls) + + +class TreeEntityManager(models.Manager): use_for_related_fields = True - def get_with_path(self, path, root=None, absolute_result=True, pathsep='/', field='slug'): + def get_with_path(self, path, root=None, absolute_result=True, pathsep='/', field='pk'): """ If ``absolute_result`` is ``True``, returns the object at ``path`` (starting at ``root``) or raises an :class:`~django.core.exceptions.ObjectDoesNotExist` exception. Otherwise, returns a tuple containing the deepest object found along ``path`` (or ``root`` if no deeper object is found) and the remainder of the path after that object as a string (or None if there is no remaining path). @@ -450,10 +446,12 @@ class TreeManager(models.Manager): return find_obj(segments, len(segments)/2 or len(segments)) -class TreeModel(MPTTModel): - objects = TreeManager() +class TreeEntity(Entity, MPTTModel): + """An abstract subclass of Entity which represents a tree relationship.""" + + __metaclass__ = TreeEntityBase + objects = TreeEntityManager() parent = models.ForeignKey('self', related_name='children', null=True, blank=True) - slug = models.SlugField(max_length=255) def get_path(self, root=None, pathsep='/', field='slug'): """ @@ -468,7 +466,7 @@ class TreeModel(MPTTModel): return '' if root is None and self.is_root_node(): - return self.slug + return getattr(self, field, '?') if root is not None and not self.is_descendant_of(root): raise AncestorDoesNotExist(root) @@ -481,27 +479,6 @@ class TreeModel(MPTTModel): return pathsep.join([getattr(parent, field, '?') for parent in qs]) path = property(get_path) - def __unicode__(self): - return self.path - - class Meta: - unique_together = (('parent', 'slug'),) - abstract = True - - -class TreeEntityBase(MPTTModelBase, EntityBase): - def __new__(meta, name, bases, attrs): - attrs['_mptt_meta'] = MPTTOptions(attrs.pop('MPTTMeta', None)) - cls = EntityBase.__new__(meta, name, bases, attrs) - - return meta.register(cls) - - -class TreeEntity(Entity, TreeModel): - """An abstract subclass of Entity which represents a tree relationship.""" - - __metaclass__ = TreeEntityBase - def get_attribute_mapper(self, mapper=None): """ Returns a :class:`.TreeAttributeMapper` or :class:`.AttributeMapper` which can be used to retrieve related :class:`Attribute`\ s' values directly. If an :class:`Attribute` with a given key is not related to the :class:`Entity`, then the mapper will check the parent's attributes. @@ -525,5 +502,26 @@ class TreeEntity(Entity, TreeModel): return super(TreeEntity, self).get_attribute_mapper(mapper) attributes = property(get_attribute_mapper) + def __unicode__(self): + return self.path + + class Meta: + abstract = True + + +class SlugTreeEntityManager(TreeEntityManager): + def get_with_path(self, path, root=None, absolute_result=True, pathsep='/', field='slug'): + return super(SlugTreeEntityManager, self).get_with_path(path, root, absolute_result, pathsep, field) + + +class SlugTreeEntity(TreeEntity): + objects = SlugTreeEntityManager() + slug = models.SlugField(max_length=255) + + def get_path(self, root=None, pathsep='/', field='slug'): + return super(SlugTreeEntity, self).get_path(root, pathsep, field) + path = property(get_path) + class Meta: + unique_together = ('parent', 'slug') abstract = True \ No newline at end of file diff --git a/philo/models/nodes.py b/philo/models/nodes.py index 0e041f2..5de7338 100644 --- a/philo/models/nodes.py +++ b/philo/models/nodes.py @@ -11,7 +11,7 @@ from django.http import HttpResponse, HttpResponseServerError, HttpResponseRedir from django.utils.encoding import smart_str from philo.exceptions import MIDDLEWARE_NOT_CONFIGURED, ViewCanNotProvideSubpath, ViewDoesNotProvideSubpaths -from philo.models.base import TreeEntity, Entity, register_value_model +from philo.models.base import SlugTreeEntity, Entity, register_value_model from philo.models.fields import JSONField from philo.utils import ContentTypeSubclassLimiter from philo.utils.entities import LazyPassthroughAttributeMapper @@ -24,7 +24,7 @@ __all__ = ('Node', 'View', 'MultiView', 'Redirect', 'File') _view_content_type_limiter = ContentTypeSubclassLimiter(None) -class Node(TreeEntity): +class Node(SlugTreeEntity): """ :class:`Node`\ s are the basic building blocks of a website using Philo. They define the URL hierarchy and connect each URL to a :class:`View` subclass instance which is used to generate an HttpResponse. @@ -104,7 +104,7 @@ class Node(TreeEntity): return '%s%s%s%s' % (domain, root_url, path, subpath) - class Meta: + class Meta(SlugTreeEntity.Meta): app_label = 'philo' diff --git a/philo/models/pages.py b/philo/models/pages.py index 0ce047e..ea3bb64 100644 --- a/philo/models/pages.py +++ b/philo/models/pages.py @@ -16,7 +16,7 @@ from django.template import TemplateDoesNotExist, Context, RequestContext, Templ from django.template.loader_tags import BlockNode, ExtendsNode, BlockContext from django.utils.datastructures import SortedDict -from philo.models.base import TreeModel, register_value_model +from philo.models.base import SlugTreeEntity, register_value_model from philo.models.fields import TemplateField from philo.models.nodes import View from philo.signals import page_about_to_render_to_string, page_finished_rendering_to_string @@ -100,7 +100,7 @@ def build_extension_tree(nodelist): return nodelists -class Template(TreeModel): +class Template(SlugTreeEntity): """Represents a database-driven django template.""" #: The name of the template. Used for organization and debugging. name = models.CharField(max_length=255) @@ -148,7 +148,7 @@ class Template(TreeModel): """Returns the value of the :attr:`name` field.""" return self.name - class Meta: + class Meta(SlugTreeEntity.Meta): app_label = 'philo' -- 2.20.1