diff options
author | Étienne Loks <etienne.loks@peacefrogs.net> | 2011-03-14 11:56:30 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@peacefrogs.net> | 2011-03-14 11:56:30 +0100 |
commit | 789536a8b59b72e95a3e36a1a368f1b583bc6d2b (patch) | |
tree | ca1186ecbf398837a217b80e937676cb97c79d23 | |
parent | fa052699ff3aa7f8871221a2cea244e574c501ca (diff) | |
download | Ishtar-789536a8b59b72e95a3e36a1a368f1b583bc6d2b.tar.bz2 Ishtar-789536a8b59b72e95a3e36a1a368f1b583bc6d2b.zip |
List types go over the second level (closes #270)
-rw-r--r-- | ishtar/furnitures/models.py | 58 |
1 files changed, 31 insertions, 27 deletions
diff --git a/ishtar/furnitures/models.py b/ishtar/furnitures/models.py index 8826c0854..78507f971 100644 --- a/ishtar/furnitures/models.py +++ b/ishtar/furnitures/models.py @@ -26,6 +26,7 @@ from django.core.exceptions import ObjectDoesNotExist, ValidationError from django.core.validators import validate_slug from django.utils.translation import ugettext_lazy as _, ugettext from django.db.utils import DatabaseError +from django.utils.safestring import SafeUnicode from django.db.models import Q from django.contrib.auth.models import User @@ -118,10 +119,39 @@ class GeneralType(models.Model): @classmethod def get_types(cls, dct={}): + base_dct = dct.copy() + if hasattr(cls, 'parent'): + return cls._get_parent_types(base_dct) + return cls._get_types(base_dct) + + @classmethod + def _get_types(cls, dct={}): dct['available'] = True yield ('', '--') for item in cls.objects.filter(**dct).all(): - yield (item.id, _(item.label)) + yield (item.pk, _(unicode(item))) + + @classmethod + def _get_childs(cls, item, dct, prefix=""): + prefix += "› " + dct['parent'] = item + childs = cls.objects.filter(**dct).order_by('order').all() + for child in childs: + yield (child.pk, SafeUnicode(prefix + \ + unicode(_(unicode(child))) )) + for sub_child in cls._get_childs(child, dct, prefix): + yield sub_child + + @classmethod + def _get_parent_types(cls, dct={}): + dct['available'] = True + yield ('', '--') + dct['parent'] = None + items = cls.objects.filter(**dct).order_by('order').all() + for item in items: + yield (item.pk, unicode(item)) + for child in cls._get_childs(item, dct): + yield child class BaseHistorizedItem(models.Model): history_modifier = models.ForeignKey(User, related_name='+', @@ -499,32 +529,6 @@ class Period(GeneralType) : def __unicode__(self): return self.label - @classmethod - def get_types(cls, dct={}): - dct['available'] = True - yield ('', '--') - items = cls.objects.filter(**dct).all() - if not items: - return - items = sorted(items, key=lambda x: x.parent) - idx, childs = 0, {None:[]} - while idx < len(items) and not items[idx].parent: - childs[items[idx]] = [] - idx += 1 - while idx < len(items): - if items[idx].parent in childs: - childs[items[idx].parent].append(items[idx]) - else: - childs[None].append(items[idx]) - idx += 1 - for parent in sorted(childs.keys(), - key=lambda x:hasattr(x, 'order') and x.order): - if parent: - yield (parent.id, _(parent.label)) - prefix = parent and "-- " or "" - for child in sorted(childs[parent], key=lambda x:x.order): - yield (child.id, _(prefix + child.label)) - class DatingType(GeneralType): class Meta: verbose_name = _(u"Dating type") |