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 Attribute(models.Model):
11 entity_content_type = models.ForeignKey(ContentType, verbose_name='Entity type')
12 entity_object_id = models.PositiveIntegerField(verbose_name='Entity ID')
13 entity = generic.GenericForeignKey('entity_content_type', 'entity_object_id')
14 key = models.CharField(max_length=255)
15 json_value = models.TextField(verbose_name='Value (JSON)', help_text='This value must be valid JSON.')
18 return json.loads(self.json_value)
20 def set_value(self, value):
21 self.json_value = json.dumps(value)
23 def delete_value(self):
24 self.json_value = json.dumps(None)
26 value = property(get_value, set_value, delete_value)
28 def __unicode__(self):
29 return u'"%s": %s' % (self.key, self.value)
35 value_content_type_limiter = ContentTypeRegistryLimiter()
38 def register_value_model(model):
39 value_content_type_limiter.register_class(model)
42 def unregister_value_model(model):
43 value_content_type_limiter.unregister_class(model)
47 class Relationship(models.Model):
48 entity_content_type = models.ForeignKey(ContentType, related_name='relationship_entity_set', verbose_name='Entity type')
49 entity_object_id = models.PositiveIntegerField(verbose_name='Entity ID')
50 entity = generic.GenericForeignKey('entity_content_type', 'entity_object_id')
51 key = models.CharField(max_length=255)
52 value_content_type = models.ForeignKey(ContentType, related_name='relationship_value_set', limit_choices_to=value_content_type_limiter, verbose_name='Value type')
53 value_object_id = models.PositiveIntegerField(verbose_name='Value ID')
54 value = generic.GenericForeignKey('value_content_type', 'value_object_id')
56 def __unicode__(self):
57 return u'"%s": %s' % (self.key, self.value)
63 class QuerySetMapper(object, DictMixin):
64 def __init__(self, queryset, passthrough=None):
65 self.queryset = queryset
66 self.passthrough = passthrough
68 def __getitem__(self, key):
70 return self.queryset.get(key__exact=key).value
71 except ObjectDoesNotExist:
72 if self.passthrough is not None:
73 return self.passthrough.__getitem__(key)
77 keys = set(self.queryset.values_list('key', flat=True).distinct())
78 if self.passthrough is not None:
79 keys |= set(self.passthrough.keys())
83 class Entity(models.Model):
84 attribute_set = generic.GenericRelation(Attribute, content_type_field='entity_content_type', object_id_field='entity_object_id')
85 relationship_set = generic.GenericRelation(Relationship, content_type_field='entity_content_type', object_id_field='entity_object_id')
89 return QuerySetMapper(self.attribute_set)
92 def relationships(self):
93 return QuerySetMapper(self.relationship_set)
100 class TreeManager(models.Manager):
101 use_for_related_fields = True
104 return self.filter(parent__isnull=True)
106 def get_with_path(self, path, root=None, absolute_result=True, pathsep='/'):
108 Returns the object with the path, or None if there is no object with that path,
109 unless absolute_result is set to False, in which case it returns a tuple containing
110 the deepest object found along the path, and the remainder of the path after that
111 object as a string (or None in the case that there is no remaining path).
113 slugs = path.split(pathsep)
115 remaining_slugs = list(slugs)
118 remaining_slugs.remove(slug)
119 if slug: # ignore blank slugs, handles for multiple consecutive pathseps
121 obj = self.get(slug__exact=slug, parent__exact=obj)
122 except self.model.DoesNotExist:
125 remaining_slugs.insert(0, slug)
126 remainder = pathsep.join(remaining_slugs)
132 return (obj, remainder)
133 raise self.model.DoesNotExist('%s matching query does not exist.' % self.model._meta.object_name)
136 class TreeModel(models.Model):
137 objects = TreeManager()
138 parent = models.ForeignKey('self', related_name='children', null=True, blank=True)
139 slug = models.SlugField()
141 def get_path(self, pathsep='/', field='slug'):
142 path = getattr(self, field, '?')
145 path = getattr(parent, field, '?') + pathsep + path
146 parent = parent.parent
148 path = property(get_path)
150 def __unicode__(self):
154 unique_together = (('parent', 'slug'),)
159 class TreeEntity(TreeModel, Entity):
161 def attributes(self):
163 return QuerySetMapper(self.attribute_set, passthrough=self.parent.attributes)
164 return super(TreeEntity, self).attributes
167 def relationships(self):
169 return QuerySetMapper(self.relationship_set, passthrough=self.parent.relationships)
170 return super(TreeEntity, self).relationships