Added get_branch support to TreeManager.
[philo.git] / models / base.py
1 from django import forms
2 from django.db import models
3 from django.contrib.contenttypes.models import ContentType
4 from django.contrib.contenttypes import generic
5 from django.utils import simplejson as json
6 from django.core.exceptions import ObjectDoesNotExist
7 from philo.exceptions import AncestorDoesNotExist
8 from philo.models.fields import JSONField
9 from philo.utils import ContentTypeRegistryLimiter, ContentTypeSubclassLimiter
10 from philo.signals import entity_class_prepared
11 from philo.validators import json_validator
12 from UserDict import DictMixin
13
14
15 class Tag(models.Model):
16         name = models.CharField(max_length=255)
17         slug = models.SlugField(max_length=255, unique=True)
18         
19         def __unicode__(self):
20                 return self.name
21         
22         class Meta:
23                 app_label = 'philo'
24
25
26 class Titled(models.Model):
27         title = models.CharField(max_length=255)
28         slug = models.SlugField(max_length=255)
29         
30         def __unicode__(self):
31                 return self.title
32         
33         class Meta:
34                 abstract = True
35
36
37 value_content_type_limiter = ContentTypeRegistryLimiter()
38
39
40 def register_value_model(model):
41         value_content_type_limiter.register_class(model)
42
43
44 def unregister_value_model(model):
45         value_content_type_limiter.unregister_class(model)
46
47
48 class AttributeValue(models.Model):
49         attribute_set = generic.GenericRelation('Attribute', content_type_field='value_content_type', object_id_field='value_object_id')
50         
51         @property
52         def attribute(self):
53                 return self.attribute_set.all()[0]
54         
55         def apply_data(self, data):
56                 raise NotImplementedError
57         
58         def value_formfield(self, **kwargs):
59                 raise NotImplementedError
60         
61         def __unicode__(self):
62                 return unicode(self.value)
63         
64         class Meta:
65                 abstract = True
66
67
68 attribute_value_limiter = ContentTypeSubclassLimiter(AttributeValue)
69
70
71 class JSONValue(AttributeValue):
72         value = JSONField() #verbose_name='Value (JSON)', help_text='This value must be valid JSON.')
73         
74         def __unicode__(self):
75                 return self.value_json
76         
77         def value_formfield(self, **kwargs):
78                 kwargs['initial'] = self.value_json
79                 return self._meta.get_field('value').formfield(**kwargs)
80         
81         def apply_data(self, cleaned_data):
82                 self.value = cleaned_data.get('value', None)
83         
84         class Meta:
85                 app_label = 'philo'
86
87
88 class ForeignKeyValue(AttributeValue):
89         content_type = models.ForeignKey(ContentType, limit_choices_to=value_content_type_limiter, verbose_name='Value type', null=True, blank=True)
90         object_id = models.PositiveIntegerField(verbose_name='Value ID', null=True, blank=True)
91         value = generic.GenericForeignKey()
92         
93         def value_formfield(self, form_class=forms.ModelChoiceField, **kwargs):
94                 if self.content_type is None:
95                         return None
96                 kwargs.update({'initial': self.object_id, 'required': False})
97                 return form_class(self.content_type.model_class()._default_manager.all(), **kwargs)
98         
99         def apply_data(self, cleaned_data):
100                 if 'value' in cleaned_data and cleaned_data['value'] is not None:
101                         self.value = cleaned_data['value']
102                 else:
103                         self.content_type = cleaned_data.get('content_type', None)
104                         # If there is no value set in the cleaned data, clear the stored value.
105                         self.object_id = None
106         
107         class Meta:
108                 app_label = 'philo'
109
110
111 class ManyToManyValue(AttributeValue):
112         content_type = models.ForeignKey(ContentType, limit_choices_to=value_content_type_limiter, verbose_name='Value type', null=True, blank=True)
113         values = models.ManyToManyField(ForeignKeyValue, blank=True, null=True)
114         
115         def get_object_id_list(self):
116                 if not self.values.count():
117                         return []
118                 else:
119                         return self.values.values_list('object_id', flat=True)
120         
121         def get_value(self):
122                 if self.content_type is None:
123                         return None
124                 
125                 return self.content_type.model_class()._default_manager.filter(id__in=self.get_object_id_list())
126         
127         def set_value(self, value):
128                 # Value is probably a queryset - but allow any iterable.
129                 
130                 # These lines shouldn't be necessary; however, if value is an EmptyQuerySet,
131                 # the code (specifically the object_id__in query) won't work without them. Unclear why...
132                 if not value:
133                         value = []
134                 
135                 # Before we can fiddle with the many-to-many to foreignkeyvalues, we need
136                 # a pk.
137                 if self.pk is None:
138                         self.save()
139                 
140                 if isinstance(value, models.query.QuerySet):
141                         value = value.values_list('id', flat=True)
142                 
143                 self.values.filter(~models.Q(object_id__in=value)).delete()
144                 current = self.get_object_id_list()
145                 
146                 for v in value:
147                         if v in current:
148                                 continue
149                         self.values.create(content_type=self.content_type, object_id=v)
150         
151         value = property(get_value, set_value)
152         
153         def value_formfield(self, form_class=forms.ModelMultipleChoiceField, **kwargs):
154                 if self.content_type is None:
155                         return None
156                 kwargs.update({'initial': self.get_object_id_list(), 'required': False})
157                 return form_class(self.content_type.model_class()._default_manager.all(), **kwargs)
158         
159         def apply_data(self, cleaned_data):
160                 if 'value' in cleaned_data and cleaned_data['value'] is not None:
161                         self.value = cleaned_data['value']
162                 else:
163                         self.content_type = cleaned_data.get('content_type', None)
164                         # If there is no value set in the cleaned data, clear the stored value.
165                         self.value = []
166         
167         class Meta:
168                 app_label = 'philo'
169
170
171 class Attribute(models.Model):
172         entity_content_type = models.ForeignKey(ContentType, related_name='attribute_entity_set', verbose_name='Entity type')
173         entity_object_id = models.PositiveIntegerField(verbose_name='Entity ID')
174         entity = generic.GenericForeignKey('entity_content_type', 'entity_object_id')
175         
176         value_content_type = models.ForeignKey(ContentType, related_name='attribute_value_set', limit_choices_to=attribute_value_limiter, verbose_name='Value type', null=True, blank=True)
177         value_object_id = models.PositiveIntegerField(verbose_name='Value ID', null=True, blank=True)
178         value = generic.GenericForeignKey('value_content_type', 'value_object_id')
179         
180         key = models.CharField(max_length=255)
181         
182         def __unicode__(self):
183                 return u'"%s": %s' % (self.key, self.value)
184         
185         class Meta:
186                 app_label = 'philo'
187                 unique_together = (('key', 'entity_content_type', 'entity_object_id'), ('value_content_type', 'value_object_id'))
188
189
190 class QuerySetMapper(object, DictMixin):
191         def __init__(self, queryset, passthrough=None):
192                 self.queryset = queryset
193                 self.passthrough = passthrough
194         
195         def __getitem__(self, key):
196                 try:
197                         value = self.queryset.get(key__exact=key).value
198                 except ObjectDoesNotExist:
199                         if self.passthrough is not None:
200                                 return self.passthrough.__getitem__(key)
201                         raise KeyError
202                 else:
203                         if value is not None:
204                                 return value.value
205                         return value
206         
207         def keys(self):
208                 keys = set(self.queryset.values_list('key', flat=True).distinct())
209                 if self.passthrough is not None:
210                         keys |= set(self.passthrough.keys())
211                 return list(keys)
212
213
214 class EntityOptions(object):
215         def __init__(self, options):
216                 if options is not None:
217                         for key, value in options.__dict__.items():
218                                 setattr(self, key, value)
219                 if not hasattr(self, 'proxy_fields'):
220                         self.proxy_fields = []
221         
222         def add_proxy_field(self, proxy_field):
223                 self.proxy_fields.append(proxy_field)
224
225
226 class EntityBase(models.base.ModelBase):
227         def __new__(cls, name, bases, attrs):
228                 new = super(EntityBase, cls).__new__(cls, name, bases, attrs)
229                 entity_options = attrs.pop('EntityMeta', None)
230                 setattr(new, '_entity_meta', EntityOptions(entity_options))
231                 entity_class_prepared.send(sender=new)
232                 return new
233
234
235 class Entity(models.Model):
236         __metaclass__ = EntityBase
237         
238         attribute_set = generic.GenericRelation(Attribute, content_type_field='entity_content_type', object_id_field='entity_object_id')
239         
240         @property
241         def attributes(self):
242                 return QuerySetMapper(self.attribute_set.all())
243         
244         @property
245         def _added_attribute_registry(self):
246                 if not hasattr(self, '_real_added_attribute_registry'):
247                         self._real_added_attribute_registry = {}
248                 return self._real_added_attribute_registry
249         
250         @property
251         def _removed_attribute_registry(self):
252                 if not hasattr(self, '_real_removed_attribute_registry'):
253                         self._real_removed_attribute_registry = []
254                 return self._real_removed_attribute_registry
255         
256         def save(self, *args, **kwargs):
257                 super(Entity, self).save(*args, **kwargs)
258                 
259                 for key in self._removed_attribute_registry:
260                         self.attribute_set.filter(key__exact=key).delete()
261                 del self._removed_attribute_registry[:]
262                 
263                 for field, value in self._added_attribute_registry.items():
264                         try:
265                                 attribute = self.attribute_set.get(key__exact=field.key)
266                         except Attribute.DoesNotExist:
267                                 attribute = Attribute()
268                                 attribute.entity = self
269                                 attribute.key = field.key
270                         
271                         field.set_attribute_value(attribute, value)
272                         attribute.save()
273                 self._added_attribute_registry.clear()
274         
275         class Meta:
276                 abstract = True
277
278
279 class TreeManager(models.Manager):
280         use_for_related_fields = True
281         
282         def roots(self):
283                 return self.filter(parent__isnull=True)
284         
285         def get_branch_pks(self, root, depth=5, inclusive=True):
286                 branch_pks = []
287                 parent_pks = [root.pk]
288                 
289                 if inclusive:
290                         branch_pks.append(root.pk)
291                 
292                 for i in xrange(depth):
293                         child_pks = list(self.filter(parent__pk__in=parent_pks).exclude(pk__in=branch_pks).values_list('pk', flat=True))
294                         if not child_pks:
295                                 break
296                         
297                         branch_pks += child_pks
298                         parent_pks = child_pks
299                 
300                 return branch_pks
301         
302         def get_branch(self, root, depth=5, inclusive=True):
303                 return self.filter(pk__in=self.get_branch_pks(root, depth, inclusive))
304         
305         def get_with_path(self, path, root=None, absolute_result=True, pathsep='/', field='slug'):
306                 """
307                 Returns the object with the path, unless absolute_result is set to False, in which
308                 case it returns a tuple containing the deepest object found along the path, and the
309                 remainder of the path after that object as a string (or None if there is no remaining
310                 path). Raises a DoesNotExist exception if no object is found with the given path.
311                 """
312                 segments = path.split(pathsep)
313                 
314                 # Check for a trailing pathsep so we can restore it later.
315                 trailing_pathsep = False
316                 if segments[-1] == '':
317                         trailing_pathsep = True
318                 
319                 # Clean out blank segments. Handles multiple consecutive pathseps.
320                 while True:
321                         try:
322                                 segments.remove('')
323                         except ValueError:
324                                 break
325                 
326                 # Special-case a lack of segments. No queries necessary.
327                 if not segments:
328                         if root is not None:
329                                 return root, None
330                         else:
331                                 raise self.model.DoesNotExist('%s matching query does not exist.' % self.model._meta.object_name)
332                 
333                 def make_query_kwargs(segments):
334                         kwargs = {}
335                         prefix = ""
336                         revsegs = list(segments)
337                         revsegs.reverse()
338                         
339                         for segment in revsegs:
340                                 kwargs["%s%s__exact" % (prefix, field)] = segment
341                                 prefix += "parent__"
342                         
343                         kwargs[prefix[:-2]] = root
344                         return kwargs
345                 
346                 def build_path(segments):
347                         path = pathsep.join(segments)
348                         if trailing_pathsep and segments and segments[-1] != '':
349                                 path += pathsep
350                         return path
351                 
352                 def find_obj(segments, depth, deepest_found):
353                         try:
354                                 obj = self.get(**make_query_kwargs(segments[:depth]))
355                         except self.model.DoesNotExist:
356                                 if absolute_result:
357                                         raise
358                                 
359                                 depth = (deepest_found + depth)/2
360                                 if deepest_found == depth:
361                                         # This should happen if nothing is found with any part of the given path.
362                                         raise
363                                 
364                                 # Try finding one with half the path since the deepest find.
365                                 return find_obj(segments, depth, deepest_found)
366                         else:
367                                 # Yay! Found one! Could there be a deeper one?
368                                 if absolute_result:
369                                         return obj
370                                 
371                                 deepest_found = depth
372                                 depth = (len(segments) + depth)/2
373                                 
374                                 if deepest_found == depth:
375                                         return obj, build_path(segments[deepest_found:]) or None
376                                 
377                                 try:
378                                         return find_obj(segments, depth, deepest_found)
379                                 except self.model.DoesNotExist:
380                                         # Then the deepest one was already found.
381                                         return obj, build_path(segments[deepest_found:])
382                 
383                 return find_obj(segments, len(segments), 0)
384
385
386 class TreeModel(models.Model):
387         objects = TreeManager()
388         parent = models.ForeignKey('self', related_name='children', null=True, blank=True)
389         slug = models.SlugField(max_length=255)
390         
391         def has_ancestor(self, ancestor, inclusive=False):
392                 if inclusive:
393                         parent = self
394                 else:
395                         parent = self.parent
396                 
397                 parents = []
398                 
399                 while parent:
400                         if parent == ancestor:
401                                 return True
402                         # If we've found this parent before, the path is recursive and ancestor wasn't on it.
403                         if parent in parents:
404                                 return False
405                         parents.append(parent)
406                         parent = parent.parent
407                 # If ancestor is None, catch it here.
408                 if parent == ancestor:
409                         return True
410                 return False
411         
412         def get_path(self, root=None, pathsep='/', field='slug'):
413                 parent = self.parent
414                 parents = [self]
415                 
416                 def compile_path(parents):
417                         return pathsep.join([getattr(parent, field, '?') for parent in parents])
418                 
419                 while parent and parent != root:
420                         if parent in parents:
421                                 if root is not None:
422                                         raise AncestorDoesNotExist(root)
423                                 parents.append(parent)
424                                 return u"\u2026%s%s" % (pathsep, compile_path(parents[::-1]))
425                         parents.append(parent)
426                         parent = parent.parent
427                 
428                 if root is not None and parent is None:
429                         raise AncestorDoesNotExist(root)
430                 
431                 return compile_path(parents[::-1])
432         path = property(get_path)
433         
434         def __unicode__(self):
435                 return self.path
436         
437         class Meta:
438                 unique_together = (('parent', 'slug'),)
439                 abstract = True
440
441
442 class TreeEntity(Entity, TreeModel):
443         @property
444         def attributes(self):
445                 if self.parent:
446                         return QuerySetMapper(self.attribute_set.all(), passthrough=self.parent.attributes)
447                 return super(TreeEntity, self).attributes
448         
449         class Meta:
450                 abstract = True