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.core.exceptions import ObjectDoesNotExist
6 from django.core.validators import RegexValidator
7 from django.utils import simplejson as json
8 from django.utils.encoding import smart_str
9 from philo.exceptions import AncestorDoesNotExist
10 from philo.models.fields import JSONField
11 from philo.utils import ContentTypeRegistryLimiter, ContentTypeSubclassLimiter
12 from philo.signals import entity_class_prepared
13 from philo.validators import json_validator
14 from UserDict import DictMixin
15 from mptt.models import MPTTModel, MPTTModelBase, MPTTOptions
18 class Tag(models.Model):
19 name = models.CharField(max_length=255)
20 slug = models.SlugField(max_length=255, unique=True)
22 def __unicode__(self):
30 class Titled(models.Model):
31 title = models.CharField(max_length=255)
32 slug = models.SlugField(max_length=255)
34 def __unicode__(self):
41 value_content_type_limiter = ContentTypeRegistryLimiter()
44 def register_value_model(model):
45 value_content_type_limiter.register_class(model)
48 def unregister_value_model(model):
49 value_content_type_limiter.unregister_class(model)
52 class AttributeValue(models.Model):
53 attribute_set = generic.GenericRelation('Attribute', content_type_field='value_content_type', object_id_field='value_object_id')
57 return self.attribute_set.all()[0]
59 def apply_data(self, data):
60 raise NotImplementedError
62 def value_formfield(self, **kwargs):
63 raise NotImplementedError
65 def __unicode__(self):
66 return unicode(self.value)
72 attribute_value_limiter = ContentTypeSubclassLimiter(AttributeValue)
75 class JSONValue(AttributeValue):
76 value = JSONField() #verbose_name='Value (JSON)', help_text='This value must be valid JSON.')
78 def __unicode__(self):
79 return smart_str(self.value)
81 def value_formfield(self, **kwargs):
82 kwargs['initial'] = self.value_json
83 return self._meta.get_field('value').formfield(**kwargs)
85 def apply_data(self, cleaned_data):
86 self.value = cleaned_data.get('value', None)
92 class ForeignKeyValue(AttributeValue):
93 content_type = models.ForeignKey(ContentType, limit_choices_to=value_content_type_limiter, verbose_name='Value type', null=True, blank=True)
94 object_id = models.PositiveIntegerField(verbose_name='Value ID', null=True, blank=True)
95 value = generic.GenericForeignKey()
97 def value_formfield(self, form_class=forms.ModelChoiceField, **kwargs):
98 if self.content_type is None:
100 kwargs.update({'initial': self.object_id, 'required': False})
101 return form_class(self.content_type.model_class()._default_manager.all(), **kwargs)
103 def apply_data(self, cleaned_data):
104 if 'value' in cleaned_data and cleaned_data['value'] is not None:
105 self.value = cleaned_data['value']
107 self.content_type = cleaned_data.get('content_type', None)
108 # If there is no value set in the cleaned data, clear the stored value.
109 self.object_id = None
115 class ManyToManyValue(AttributeValue):
116 content_type = models.ForeignKey(ContentType, limit_choices_to=value_content_type_limiter, verbose_name='Value type', null=True, blank=True)
117 values = models.ManyToManyField(ForeignKeyValue, blank=True, null=True)
119 def get_object_id_list(self):
120 if not self.values.count():
123 return self.values.values_list('object_id', flat=True)
126 if self.content_type is None:
129 return self.content_type.model_class()._default_manager.filter(id__in=self.get_object_id_list())
131 def set_value(self, value):
132 # Value is probably a queryset - but allow any iterable.
134 # These lines shouldn't be necessary; however, if value is an EmptyQuerySet,
135 # the code (specifically the object_id__in query) won't work without them. Unclear why...
139 # Before we can fiddle with the many-to-many to foreignkeyvalues, we need
144 if isinstance(value, models.query.QuerySet):
145 value = value.values_list('id', flat=True)
147 self.values.filter(~models.Q(object_id__in=value)).delete()
148 current = self.get_object_id_list()
153 self.values.create(content_type=self.content_type, object_id=v)
155 value = property(get_value, set_value)
157 def value_formfield(self, form_class=forms.ModelMultipleChoiceField, **kwargs):
158 if self.content_type is None:
160 kwargs.update({'initial': self.get_object_id_list(), 'required': False})
161 return form_class(self.content_type.model_class()._default_manager.all(), **kwargs)
163 def apply_data(self, cleaned_data):
164 if 'value' in cleaned_data and cleaned_data['value'] is not None:
165 self.value = cleaned_data['value']
167 self.content_type = cleaned_data.get('content_type', None)
168 # If there is no value set in the cleaned data, clear the stored value.
175 class Attribute(models.Model):
176 entity_content_type = models.ForeignKey(ContentType, related_name='attribute_entity_set', verbose_name='Entity type')
177 entity_object_id = models.PositiveIntegerField(verbose_name='Entity ID')
178 entity = generic.GenericForeignKey('entity_content_type', 'entity_object_id')
180 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)
181 value_object_id = models.PositiveIntegerField(verbose_name='Value ID', null=True, blank=True)
182 value = generic.GenericForeignKey('value_content_type', 'value_object_id')
184 key = models.CharField(max_length=255, validators=[RegexValidator("\w+")], help_text="Must contain one or more alphanumeric characters or underscores.")
186 def __unicode__(self):
187 return u'"%s": %s' % (self.key, self.value)
191 unique_together = (('key', 'entity_content_type', 'entity_object_id'), ('value_content_type', 'value_object_id'))
194 class QuerySetMapper(object, DictMixin):
195 def __init__(self, queryset, passthrough=None):
196 self.queryset = queryset
197 self.passthrough = passthrough
199 def __getitem__(self, key):
201 value = self.queryset.get(key__exact=key).value
202 except ObjectDoesNotExist:
203 if self.passthrough is not None:
204 return self.passthrough.__getitem__(key)
207 if value is not None:
212 keys = set(self.queryset.values_list('key', flat=True).distinct())
213 if self.passthrough is not None:
214 keys |= set(self.passthrough.keys())
218 class EntityOptions(object):
219 def __init__(self, options):
220 if options is not None:
221 for key, value in options.__dict__.items():
222 setattr(self, key, value)
223 if not hasattr(self, 'proxy_fields'):
224 self.proxy_fields = []
226 def add_proxy_field(self, proxy_field):
227 self.proxy_fields.append(proxy_field)
230 class EntityBase(models.base.ModelBase):
231 def __new__(cls, name, bases, attrs):
232 new = super(EntityBase, cls).__new__(cls, name, bases, attrs)
233 entity_options = attrs.pop('EntityMeta', None)
234 setattr(new, '_entity_meta', EntityOptions(entity_options))
235 entity_class_prepared.send(sender=new)
239 class Entity(models.Model):
240 __metaclass__ = EntityBase
242 attribute_set = generic.GenericRelation(Attribute, content_type_field='entity_content_type', object_id_field='entity_object_id')
245 def attributes(self):
246 return QuerySetMapper(self.attribute_set.all())
249 def _added_attribute_registry(self):
250 if not hasattr(self, '_real_added_attribute_registry'):
251 self._real_added_attribute_registry = {}
252 return self._real_added_attribute_registry
255 def _removed_attribute_registry(self):
256 if not hasattr(self, '_real_removed_attribute_registry'):
257 self._real_removed_attribute_registry = []
258 return self._real_removed_attribute_registry
260 def save(self, *args, **kwargs):
261 super(Entity, self).save(*args, **kwargs)
263 for key in self._removed_attribute_registry:
264 self.attribute_set.filter(key__exact=key).delete()
265 del self._removed_attribute_registry[:]
267 for field, value in self._added_attribute_registry.items():
269 attribute = self.attribute_set.get(key__exact=field.key)
270 except Attribute.DoesNotExist:
271 attribute = Attribute()
272 attribute.entity = self
273 attribute.key = field.key
275 field.set_attribute_value(attribute, value)
277 self._added_attribute_registry.clear()
283 class TreeManager(models.Manager):
284 use_for_related_fields = True
286 def get_with_path(self, path, root=None, absolute_result=True, pathsep='/', field='slug'):
288 Returns the object with the path, unless absolute_result is set to False, in which
289 case it returns a tuple containing the deepest object found along the path, and the
290 remainder of the path after that object as a string (or None if there is no remaining
291 path). Raises a DoesNotExist exception if no object is found with the given path.
293 If the path you're searching for is known to exist, it is always faster to use
294 absolute_result=True - unless the path depth is over ~40, in which case the high cost
295 of the absolute query makes a binary search (i.e. non-absolute) faster.
297 # Note: SQLite allows max of 64 tables in one join. That means the binary search will
298 # only work on paths with a max depth of 127 and the absolute fetch will only work
299 # to a max depth of (surprise!) 63. Although this could be handled, chances are your
300 # tree structure won't be that deep.
301 segments = path.split(pathsep)
303 # Check for a trailing pathsep so we can restore it later.
304 trailing_pathsep = False
305 if segments[-1] == '':
306 trailing_pathsep = True
308 # Clean out blank segments. Handles multiple consecutive pathseps.
315 # Special-case a lack of segments. No queries necessary.
322 raise self.model.DoesNotExist('%s matching query does not exist.' % self.model._meta.object_name)
324 def make_query_kwargs(segments, root):
327 revsegs = list(segments)
330 for segment in revsegs:
331 kwargs["%s%s__exact" % (prefix, field)] = segment
335 kwargs[prefix[:-2]] = root
339 def build_path(segments):
340 path = pathsep.join(segments)
341 if trailing_pathsep and segments and segments[-1] != '':
345 def find_obj(segments, depth, deepest_found=None):
346 if deepest_found is None:
349 deepest_level = deepest_found.get_level() + 1
351 deepest_level = deepest_found.get_level() - root.get_level()
353 obj = self.get(**make_query_kwargs(segments[deepest_level:depth], deepest_found or root))
354 except self.model.DoesNotExist:
355 if not deepest_level and depth > 1:
356 # make sure there's a root node...
359 # Try finding one with half the path since the deepest find.
360 depth = (deepest_level + depth)/2
362 if deepest_level == depth:
363 # This should happen if nothing is found with any part of the given path.
364 if root is not None and deepest_found is None:
365 return root, build_path(segments)
368 return find_obj(segments, depth, deepest_found)
372 deepest_level = obj.get_level() + 1
374 deepest_level = obj.get_level() - root.get_level()
376 # Could there be a deeper one?
377 if obj.is_leaf_node():
378 return obj, build_path(segments[deepest_level:]) or None
380 depth += (len(segments) - depth)/2 or len(segments) - depth
382 if depth > deepest_level + obj.get_descendant_count():
383 depth = deepest_level + obj.get_descendant_count()
385 if deepest_level == depth:
386 return obj, build_path(segments[deepest_level:]) or None
389 return find_obj(segments, depth, obj)
390 except self.model.DoesNotExist:
391 # Then this was the deepest.
392 return obj, build_path(segments[deepest_level:])
395 return self.get(**make_query_kwargs(segments, root))
397 # Try a modified binary search algorithm. Feed the root in so that query complexity
398 # can be reduced. It might be possible to weight the search towards the beginning
399 # of the path, since short paths are more likely, but how far forward? It would
400 # need to shift depending on len(segments) - perhaps logarithmically?
401 return find_obj(segments, len(segments)/2 or len(segments))
404 class TreeModel(MPTTModel):
405 objects = TreeManager()
406 parent = models.ForeignKey('self', related_name='children', null=True, blank=True)
407 slug = models.SlugField(max_length=255)
409 def get_path(self, root=None, pathsep='/', field='slug'):
413 if root is not None and not self.is_descendant_of(root):
414 raise AncestorDoesNotExist(root)
416 qs = self.get_ancestors()
419 qs = qs.filter(**{'%s__gt' % self._mptt_meta.level_attr: root.get_level()})
421 return pathsep.join([getattr(parent, field, '?') for parent in list(qs) + [self]])
422 path = property(get_path)
424 def __unicode__(self):
428 unique_together = (('parent', 'slug'),)
432 class TreeEntityBase(MPTTModelBase, EntityBase):
433 def __new__(meta, name, bases, attrs):
434 attrs['_mptt_meta'] = MPTTOptions(attrs.pop('MPTTMeta', None))
435 cls = EntityBase.__new__(meta, name, bases, attrs)
437 return meta.register(cls)
440 class TreeEntity(Entity, TreeModel):
441 __metaclass__ = TreeEntityBase
444 def attributes(self):
446 return QuerySetMapper(self.attribute_set.all(), passthrough=self.parent.attributes)
447 return super(TreeEntity, self).attributes