1 from django.db import models
2 from django.contrib.contenttypes.models import ContentType
3 from django.contrib.contenttypes import generic
4 from django.utils import simplejson as json
5 from django.core.exceptions import ObjectDoesNotExist
6 from philo.utils import ContentTypeRegistryLimiter
7 from UserDict import DictMixin
10 class Tag(models.Model):
11 name = models.CharField(max_length=250)
12 slug = models.SlugField(unique=True)
14 def __unicode__(self):
21 class Titled(models.Model):
22 title = models.CharField(max_length=255)
23 slug = models.SlugField(max_length=255)
25 def __unicode__(self):
32 class Attribute(models.Model):
33 entity_content_type = models.ForeignKey(ContentType, verbose_name='Entity type')
34 entity_object_id = models.PositiveIntegerField(verbose_name='Entity ID')
35 entity = generic.GenericForeignKey('entity_content_type', 'entity_object_id')
36 key = models.CharField(max_length=255)
37 json_value = models.TextField(verbose_name='Value (JSON)', help_text='This value must be valid JSON.')
40 return json.loads(self.json_value)
42 def set_value(self, value):
43 self.json_value = json.dumps(value)
45 def delete_value(self):
46 self.json_value = json.dumps(None)
48 value = property(get_value, set_value, delete_value)
50 def __unicode__(self):
51 return u'"%s": %s' % (self.key, self.value)
55 unique_together = ('key', 'entity_content_type', 'entity_object_id')
58 value_content_type_limiter = ContentTypeRegistryLimiter()
61 def register_value_model(model):
62 value_content_type_limiter.register_class(model)
65 def unregister_value_model(model):
66 value_content_type_limiter.unregister_class(model)
70 class Relationship(models.Model):
71 entity_content_type = models.ForeignKey(ContentType, related_name='relationship_entity_set', verbose_name='Entity type')
72 entity_object_id = models.PositiveIntegerField(verbose_name='Entity ID')
73 entity = generic.GenericForeignKey('entity_content_type', 'entity_object_id')
74 key = models.CharField(max_length=255)
75 value_content_type = models.ForeignKey(ContentType, related_name='relationship_value_set', limit_choices_to=value_content_type_limiter, verbose_name='Value type')
76 value_object_id = models.PositiveIntegerField(verbose_name='Value ID')
77 value = generic.GenericForeignKey('value_content_type', 'value_object_id')
79 def __unicode__(self):
80 return u'"%s": %s' % (self.key, self.value)
84 unique_together = ('key', 'entity_content_type', 'entity_object_id')
87 class QuerySetMapper(object, DictMixin):
88 def __init__(self, queryset, passthrough=None):
89 self.queryset = queryset
90 self.passthrough = passthrough
92 def __getitem__(self, key):
94 return self.queryset.get(key__exact=key).value
95 except ObjectDoesNotExist:
96 if self.passthrough is not None:
97 return self.passthrough.__getitem__(key)
101 keys = set(self.queryset.values_list('key', flat=True).distinct())
102 if self.passthrough is not None:
103 keys |= set(self.passthrough.keys())
107 class Entity(models.Model):
108 attribute_set = generic.GenericRelation(Attribute, content_type_field='entity_content_type', object_id_field='entity_object_id')
109 relationship_set = generic.GenericRelation(Relationship, content_type_field='entity_content_type', object_id_field='entity_object_id')
112 def attributes(self):
113 return QuerySetMapper(self.attribute_set)
116 def relationships(self):
117 return QuerySetMapper(self.relationship_set)
123 class TreeManager(models.Manager):
124 use_for_related_fields = True
127 return self.filter(parent__isnull=True)
129 def get_with_path(self, path, root=None, absolute_result=True, pathsep='/'):
131 Returns the object with the path, or None if there is no object with that path,
132 unless absolute_result is set to False, in which case it returns a tuple containing
133 the deepest object found along the path, and the remainder of the path after that
134 object as a string (or None in the case that there is no remaining path).
136 slugs = path.split(pathsep)
138 remaining_slugs = list(slugs)
141 remaining_slugs.remove(slug)
142 if slug: # ignore blank slugs, handles for multiple consecutive pathseps
144 obj = self.get(slug__exact=slug, parent__exact=obj)
145 except self.model.DoesNotExist:
148 remaining_slugs.insert(0, slug)
149 remainder = pathsep.join(remaining_slugs)
155 return (obj, remainder)
156 raise self.model.DoesNotExist('%s matching query does not exist.' % self.model._meta.object_name)
159 class TreeModel(models.Model):
160 objects = TreeManager()
161 parent = models.ForeignKey('self', related_name='children', null=True, blank=True)
162 slug = models.SlugField()
164 def has_ancestor(self, ancestor):
167 if parent == ancestor:
169 parent = parent.parent
172 def get_path(self, root=None, pathsep='/', field='slug'):
173 if root is not None and self.has_ancestor(root):
176 while parent and parent != root:
177 path = getattr(parent, field, '?') + pathsep + path
178 parent = parent.parent
181 path = getattr(self, field, '?')
183 while parent and parent != root:
184 path = getattr(parent, field, '?') + pathsep + path
185 parent = parent.parent
187 path = property(get_path)
189 def __unicode__(self):
193 unique_together = (('parent', 'slug'),)
197 class TreeEntity(TreeModel, Entity):
199 def attributes(self):
201 return QuerySetMapper(self.attribute_set, passthrough=self.parent.attributes)
202 return super(TreeEntity, self).attributes
205 def relationships(self):
207 return QuerySetMapper(self.relationship_set, passthrough=self.parent.relationships)
208 return super(TreeEntity, self).relationships