summaryrefslogtreecommitdiff
path: root/ishtar_common
diff options
context:
space:
mode:
Diffstat (limited to 'ishtar_common')
-rw-r--r--ishtar_common/models.py89
1 files changed, 66 insertions, 23 deletions
diff --git a/ishtar_common/models.py b/ishtar_common/models.py
index 45ce9f504..0eb43e42b 100644
--- a/ishtar_common/models.py
+++ b/ishtar_common/models.py
@@ -660,7 +660,11 @@ class GeneralType(Cached, models.Model):
return vals
@classmethod
- def _get_types(cls, dct={}, instances=False, exclude=[], default=None):
+ def _get_types(cls, dct=None, instances=False, exclude=None, default=None):
+ if not dct:
+ dct = {}
+ if not exclude:
+ exclude = []
dct['available'] = True
if default:
try:
@@ -684,6 +688,34 @@ class GeneralType(Cached, models.Model):
yield (item.pk, _(unicode(item))
if item and unicode(item) else '')
+ @classmethod
+ def _get_childs_list(cls, dct=None, exclude=None, instances=False):
+ if not dct:
+ dct = {}
+ if not exclude:
+ exclude = []
+ if 'parent' in dct:
+ dct.pop('parent')
+ childs = cls.objects.filter(**dct)
+ if exclude:
+ childs = childs.exclude(txt_idx__in=exclude)
+ if hasattr(cls, 'order'):
+ childs = childs.order_by('order')
+ res = {}
+ if instances:
+ for item in childs.all():
+ parent_id = item.parent_id or 0
+ if parent_id not in res:
+ res[parent_id] = []
+ res[parent_id].append(item)
+ else:
+ for item in childs.values("id", "parent_id", "label").all():
+ parent_id = item["parent_id"] or 0
+ if parent_id not in res:
+ res[parent_id] = []
+ res[parent_id].append((item["id"], item["label"]))
+ return res
+
PREFIX = "│ "
PREFIX_EMPTY = "  "
PREFIX_MEDIUM = "├ "
@@ -691,19 +723,19 @@ class GeneralType(Cached, models.Model):
PREFIX_CODES = [u"\u2502", u"\u251C", u"\u2514"]
@classmethod
- def _get_childs(cls, item, dct, prefix=0, instances=False, exclude=[],
- is_last=False, last_of=[]):
+ def _get_childs(cls, item, child_list, prefix=0, instances=False,
+ is_last=False, last_of=None):
+ if not last_of:
+ last_of = []
+
prefix += 1
- dct['parent'] = item
- childs = cls.objects.filter(**dct)
- if exclude:
- childs = childs.exclude(txt_idx__in=exclude)
- if hasattr(cls, 'order'):
- childs = childs.order_by('order')
+ current_child_lst = []
+ if item in child_list:
+ current_child_lst = child_list[item]
+
lst = []
- child_lst = childs.all()
- total = len(child_lst)
- for idx, child in enumerate(child_lst):
+ total = len(current_child_lst)
+ for idx, child in enumerate(current_child_lst):
mylast_of = last_of[:]
if instances:
child.rank = prefix
@@ -730,16 +762,21 @@ class GeneralType(Cached, models.Model):
else:
p += cls.PREFIX
lst.append((
- child.pk, SafeUnicode(p + unicode(_(unicode(child))))
+ child[0], SafeUnicode(p + unicode(_(child[1])))
))
clast_of = last_of[:]
clast_of.append(idx + 1 == total)
+ if instances:
+ child_id = child.id
+ else:
+ child_id = child[0]
for sub_child in cls._get_childs(
- child, dct, prefix, instances, exclude=exclude,
+ child_id, child_list, prefix, instances,
is_last=((idx + 1) == total), last_of=clast_of):
lst.append(sub_child)
return lst
+
@classmethod
def _get_parent_types(cls, dct={}, instances=False, exclude=[],
default=None):
@@ -750,15 +787,21 @@ class GeneralType(Cached, models.Model):
items = items.exclude(txt_idx__in=exclude)
if hasattr(cls, 'order'):
items = items.order_by('order')
- for item in items.all():
- if instances:
- item.rank = 0
- yield item
- else:
- yield (item.pk, unicode(item))
- for child in cls._get_childs(item, dct, instances,
- exclude=exclude):
- yield child
+
+ child_list = cls._get_childs_list(dct, exclude, instances)
+
+ if 0 in child_list:
+ for item in child_list[0]:
+ if instances:
+ item.rank = 0
+ item_id = item.pk
+ yield item
+ else:
+ item_id = item[0]
+ yield item
+ for child in cls._get_childs(
+ item_id, child_list, instances=instances):
+ yield child
def save(self, *args, **kwargs):
if not self.id and not self.label: