Display of ContainerFormSets functional; display of their values is not; saving of...
[philo.git] / models / base.py
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.exceptions import AncestorDoesNotExist
7 from philo.utils import ContentTypeRegistryLimiter
8 from philo.signals import entity_class_prepared
9 from UserDict import DictMixin
10
11
12 class Tag(models.Model):
13         name = models.CharField(max_length=250)
14         slug = models.SlugField(unique=True)
15         
16         def __unicode__(self):
17                 return self.name
18         
19         class Meta:
20                 app_label = 'philo'
21
22
23 class Titled(models.Model):
24         title = models.CharField(max_length=255)
25         slug = models.SlugField(max_length=255)
26         
27         def __unicode__(self):
28                 return self.title
29         
30         class Meta:
31                 abstract = True
32
33
34 class Attribute(models.Model):
35         entity_content_type = models.ForeignKey(ContentType, verbose_name='Entity type')
36         entity_object_id = models.PositiveIntegerField(verbose_name='Entity ID')
37         entity = generic.GenericForeignKey('entity_content_type', 'entity_object_id')
38         key = models.CharField(max_length=255)
39         json_value = models.TextField(verbose_name='Value (JSON)', help_text='This value must be valid JSON.')
40         
41         def get_value(self):
42                 return json.loads(self.json_value)
43         
44         def set_value(self, value):
45                 self.json_value = json.dumps(value)
46         
47         def delete_value(self):
48                 self.json_value = json.dumps(None)
49         
50         value = property(get_value, set_value, delete_value)
51         
52         def __unicode__(self):
53                 return u'"%s": %s' % (self.key, self.value)
54         
55         class Meta:
56                 app_label = 'philo'
57                 unique_together = ('key', 'entity_content_type', 'entity_object_id')
58
59
60 value_content_type_limiter = ContentTypeRegistryLimiter()
61
62
63 def register_value_model(model):
64         value_content_type_limiter.register_class(model)
65
66
67 def unregister_value_model(model):
68         value_content_type_limiter.unregister_class(model)
69
70
71
72 class Relationship(models.Model):
73         entity_content_type = models.ForeignKey(ContentType, related_name='relationship_entity_set', verbose_name='Entity type')
74         entity_object_id = models.PositiveIntegerField(verbose_name='Entity ID')
75         entity = generic.GenericForeignKey('entity_content_type', 'entity_object_id')
76         key = models.CharField(max_length=255)
77         value_content_type = models.ForeignKey(ContentType, related_name='relationship_value_set', limit_choices_to=value_content_type_limiter, verbose_name='Value type', null=True, blank=True)
78         value_object_id = models.PositiveIntegerField(verbose_name='Value ID', null=True, blank=True)
79         value = generic.GenericForeignKey('value_content_type', 'value_object_id')
80         
81         def __unicode__(self):
82                 return u'"%s": %s' % (self.key, self.value)
83         
84         class Meta:
85                 app_label = 'philo'
86                 unique_together = ('key', 'entity_content_type', 'entity_object_id')
87
88
89 class QuerySetMapper(object, DictMixin):
90         def __init__(self, queryset, passthrough=None):
91                 self.queryset = queryset
92                 self.passthrough = passthrough
93         
94         def __getitem__(self, key):
95                 try:
96                         return self.queryset.get(key__exact=key).value
97                 except ObjectDoesNotExist:
98                         if self.passthrough is not None:
99                                 return self.passthrough.__getitem__(key)
100                         raise KeyError
101         
102         def keys(self):
103                 keys = set(self.queryset.values_list('key', flat=True).distinct())
104                 if self.passthrough is not None:
105                         keys |= set(self.passthrough.keys())
106                 return list(keys)
107
108
109 class EntityOptions(object):
110         def __init__(self, options):
111                 if options is not None:
112                         for key, value in options.__dict__.items():
113                                 setattr(self, key, value)
114                 if not hasattr(self, 'proxy_fields'):
115                         self.proxy_fields = []
116         
117         def add_proxy_field(self, proxy_field):
118                 self.proxy_fields.append(proxy_field)
119
120
121 class EntityBase(models.base.ModelBase):
122         def __new__(cls, name, bases, attrs):
123                 new = super(EntityBase, cls).__new__(cls, name, bases, attrs)
124                 entity_options = attrs.pop('EntityMeta', None)
125                 setattr(new, '_entity_meta', EntityOptions(entity_options))
126                 entity_class_prepared.send(sender=new)
127                 return new
128
129
130 class Entity(models.Model):
131         __metaclass__ = EntityBase
132         
133         attribute_set = generic.GenericRelation(Attribute, content_type_field='entity_content_type', object_id_field='entity_object_id')
134         relationship_set = generic.GenericRelation(Relationship, content_type_field='entity_content_type', object_id_field='entity_object_id')
135         
136         @property
137         def attributes(self):
138                 return QuerySetMapper(self.attribute_set)
139         
140         @property
141         def relationships(self):
142                 return QuerySetMapper(self.relationship_set)
143         
144         @property
145         def _added_attribute_registry(self):
146                 if not hasattr(self, '_real_added_attribute_registry'):
147                         self._real_added_attribute_registry = {}
148                 return self._real_added_attribute_registry
149         
150         @property
151         def _removed_attribute_registry(self):
152                 if not hasattr(self, '_real_removed_attribute_registry'):
153                         self._real_removed_attribute_registry = []
154                 return self._real_removed_attribute_registry
155         
156         @property
157         def _added_relationship_registry(self):
158                 if not hasattr(self, '_real_added_relationship_registry'):
159                         self._real_added_relationship_registry = {}
160                 return self._real_added_relationship_registry
161         
162         @property
163         def _removed_relationship_registry(self):
164                 if not hasattr(self, '_real_removed_relationship_registry'):
165                         self._real_removed_relationship_registry = []
166                 return self._real_removed_relationship_registry
167         
168         def save(self, *args, **kwargs):
169                 super(Entity, self).save(*args, **kwargs)
170                 
171                 for key in self._removed_attribute_registry:
172                         self.attribute_set.filter(key__exact=key).delete()
173                 del self._removed_attribute_registry[:]
174                 
175                 for key, value in self._added_attribute_registry.items():
176                         try:
177                                 attribute = self.attribute_set.get(key__exact=key)
178                         except Attribute.DoesNotExist:
179                                 attribute = Attribute()
180                                 attribute.entity = self
181                                 attribute.key = key
182                         attribute.value = value
183                         attribute.save()
184                 self._added_attribute_registry.clear()
185                 
186                 for key in self._removed_relationship_registry:
187                         self.relationship_set.filter(key__exact=key).delete()
188                 del self._removed_relationship_registry[:]
189                 
190                 for key, value in self._added_relationship_registry.items():
191                         try:
192                                 relationship = self.relationship_set.get(key__exact=key)
193                         except Relationship.DoesNotExist:
194                                 relationship = Relationship()
195                                 relationship.entity = self
196                                 relationship.key = key
197                         relationship.value = value
198                         relationship.save()
199                 self._added_relationship_registry.clear()
200         
201         class Meta:
202                 abstract = True
203
204
205 class TreeManager(models.Manager):
206         use_for_related_fields = True
207         
208         def roots(self):
209                 return self.filter(parent__isnull=True)
210         
211         def get_with_path(self, path, root=None, absolute_result=True, pathsep='/'):
212                 """
213                 Returns the object with the path, or None if there is no object with that path,
214                 unless absolute_result is set to False, in which case it returns a tuple containing
215                 the deepest object found along the path, and the remainder of the path after that
216                 object as a string (or None in the case that there is no remaining path).
217                 """
218                 slugs = path.split(pathsep)
219                 obj = root
220                 remaining_slugs = list(slugs)
221                 remainder = None
222                 for slug in slugs:
223                         remaining_slugs.remove(slug)
224                         if slug: # ignore blank slugs, handles for multiple consecutive pathseps
225                                 try:
226                                         obj = self.get(slug__exact=slug, parent__exact=obj)
227                                 except self.model.DoesNotExist:
228                                         if absolute_result:
229                                                 obj = None
230                                         remaining_slugs.insert(0, slug)
231                                         remainder = pathsep.join(remaining_slugs)
232                                         break
233                 if obj:
234                         if absolute_result:
235                                 return obj
236                         else:
237                                 return (obj, remainder)
238                 raise self.model.DoesNotExist('%s matching query does not exist.' % self.model._meta.object_name)
239
240
241 class TreeModel(models.Model):
242         objects = TreeManager()
243         parent = models.ForeignKey('self', related_name='children', null=True, blank=True)
244         slug = models.SlugField(max_length=255)
245         
246         def has_ancestor(self, ancestor):
247                 parent = self
248                 while parent:
249                         if parent == ancestor:
250                                 return True
251                         parent = parent.parent
252                 return False
253         
254         def get_path(self, root=None, pathsep='/', field='slug'):
255                 if root is not None:
256                         if not self.has_ancestor(root):
257                                 raise AncestorDoesNotExist(root)
258                         path = ''
259                         parent = self
260                         while parent and parent != root:
261                                 path = getattr(parent, field, '?') + pathsep + path
262                                 parent = parent.parent
263                         return path
264                 else:
265                         path = getattr(self, field, '?')
266                         parent = self.parent
267                         while parent and parent != root:
268                                 path = getattr(parent, field, '?') + pathsep + path
269                                 parent = parent.parent
270                         return path
271         path = property(get_path)
272         
273         def __unicode__(self):
274                 return self.path
275         
276         class Meta:
277                 unique_together = (('parent', 'slug'),)
278                 abstract = True
279
280
281 class TreeEntity(Entity, TreeModel):
282         @property
283         def attributes(self):
284                 if self.parent:
285                         return QuerySetMapper(self.attribute_set, passthrough=self.parent.attributes)
286                 return super(TreeEntity, self).attributes
287         
288         @property
289         def relationships(self):
290                 if self.parent:
291                         return QuerySetMapper(self.relationship_set, passthrough=self.parent.relationships)
292                 return super(TreeEntity, self).relationships
293         
294         class Meta:
295                 abstract = True