diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-09-18 18:02:00 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2019-09-18 18:02:00 +0200 |
commit | 203f1930527e84006f290f79dd86cc9ce6e3d01c (patch) | |
tree | c1f0770fe17721c97719a15dfcad5e6195f3f412 | |
parent | 3219dff5bb5abc8a8af00e66ca12612addee83bb (diff) | |
download | Ishtar-203f1930527e84006f290f79dd86cc9ce6e3d01c.tar.bz2 Ishtar-203f1930527e84006f290f79dd86cc9ce6e3d01c.zip |
Display full hierarchy on selection for material types and object types
-rw-r--r-- | archaeological_finds/forms.py | 10 | ||||
-rw-r--r-- | ishtar_common/models.py | 80 |
2 files changed, 58 insertions, 32 deletions
diff --git a/archaeological_finds/forms.py b/archaeological_finds/forms.py index aa29b57bf..4d2b399fd 100644 --- a/archaeological_finds/forms.py +++ b/archaeological_finds/forms.py @@ -240,9 +240,11 @@ class BaseFindForm(CustomForm, ManageOldType): initial=get_now, label=_(u"Check date"), widget=DatePicker) TYPES = [ - FieldType('material_type', models.MaterialType, is_multiple=True), + FieldType('material_type', models.MaterialType, is_multiple=True, + extra_args={"full_hierarchy": True}), FieldType('material_type_quality', models.MaterialTypeQualityType), - FieldType('object_type', models.ObjectType, is_multiple=True), + FieldType('object_type', models.ObjectType, is_multiple=True, + extra_args={"full_hierarchy": True}), FieldType('object_type_quality', models.ObjectTypeQualityType), FieldType('communicabilitie', models.CommunicabilityType, is_multiple=True), @@ -388,8 +390,8 @@ class FindForm(BaseFindForm): srs.srid) except forms.ValidationError as e: raise forms.ValidationError( - str(_(u"Coordinates are not relevant for the spatial " - u"reference system used: {}.")).format(e)) + str(_("Coordinates are not relevant for the spatial " + "reference system used: {}.")).format(e)) return self.cleaned_data diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 0b89e6416..c0ac5004f 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -683,7 +683,11 @@ class GeneralType(Cached, models.Model): return "_".join(items) @classmethod - def get_help(cls, dct={}, exclude=[], force=False): + def get_help(cls, dct=None, exclude=None, force=False, full_hierarchy=None): + if not dct: + dct = {} + if not exclude: + exclude = [] keys = ['__get_help'] keys += ["{}".format(ex) for ex in exclude] keys += ['{}-{}'.format(str(k), dct[k]) for k in dct] @@ -752,7 +756,8 @@ class GeneralType(Cached, models.Model): @classmethod def get_types(cls, dct=None, instances=False, exclude=None, - empty_first=True, default=None, initial=None, force=False): + empty_first=True, default=None, initial=None, force=False, + full_hierarchy=False): if not dct: dct = {} if not exclude: @@ -761,7 +766,8 @@ class GeneralType(Cached, models.Model): if not instances and empty_first and not default: types = [('', '--')] types += cls._pre_get_types(dct, instances, exclude, - default, force) + default, force, + get_full_hierarchy=full_hierarchy) if not initial: return types new_vals = cls._get_initial_types(initial, [idx for idx, lbl in types]) @@ -770,7 +776,7 @@ class GeneralType(Cached, models.Model): @classmethod def _pre_get_types(cls, dct=None, instances=False, exclude=None, - default=None, force=False): + default=None, force=False, get_full_hierarchy=False): if not dct: dct = {} if not exclude: @@ -790,10 +796,10 @@ class GeneralType(Cached, models.Model): if not cache_key: return cls._get_parent_types( base_dct, instances, exclude=exclude, - default=default) + default=default, get_full_hierarchy=get_full_hierarchy) vals = [v for v in cls._get_parent_types( base_dct, instances, exclude=exclude, - default=default)] + default=default, get_full_hierarchy=get_full_hierarchy)] cache.set(cache_key, vals, settings.CACHE_TIMEOUT) return vals @@ -873,7 +879,7 @@ class GeneralType(Cached, models.Model): @classmethod def _get_childs(cls, item, child_list, prefix=0, instances=False, - is_last=False, last_of=None): + is_last=False, last_of=None, get_full_hierarchy=False): if not last_of: last_of = [] @@ -884,32 +890,39 @@ class GeneralType(Cached, models.Model): lst = [] total = len(current_child_lst) + full_hierarchy_initial = get_full_hierarchy for idx, child in enumerate(current_child_lst): mylast_of = last_of[:] + p = '' if instances: child.rank = prefix lst.append(child) else: - p = '' - cprefix = prefix - while cprefix: - cprefix -= 1 - if not cprefix: - if (idx + 1) == total: - p += cls.PREFIX_LAST - else: - p += cls.PREFIX_MEDIUM - elif is_last: - if mylast_of: - clast = mylast_of.pop(0) - if clast: - p += cls.PREFIX_EMPTY + if full_hierarchy_initial: + if isinstance(full_hierarchy_initial, str): + p = full_hierarchy_initial + " > " + else: + p = "" + else: + cprefix = prefix + while cprefix: + cprefix -= 1 + if not cprefix: + if (idx + 1) == total: + p += cls.PREFIX_LAST + else: + p += cls.PREFIX_MEDIUM + elif is_last: + if mylast_of: + clast = mylast_of.pop(0) + if clast: + p += cls.PREFIX_EMPTY + else: + p += cls.PREFIX else: - p += cls.PREFIX + p += cls.PREFIX_EMPTY else: - p += cls.PREFIX_EMPTY - else: - p += cls.PREFIX + p += cls.PREFIX lst.append(( child[0], SafeText(p + str(_(child[1]))) )) @@ -919,15 +932,23 @@ class GeneralType(Cached, models.Model): child_id = child.id else: child_id = child[0] + if get_full_hierarchy: + if p: + if not p.endswith(" > "): + p += " > " + get_full_hierarchy = p + child[1] + else: + get_full_hierarchy = child[1] for sub_child in cls._get_childs( child_id, child_list, prefix, instances, - is_last=((idx + 1) == total), last_of=clast_of): + is_last=((idx + 1) == total), last_of=clast_of, + get_full_hierarchy=get_full_hierarchy): lst.append(sub_child) return lst @classmethod def _get_parent_types(cls, dct=None, instances=False, exclude=None, - default=None): + default=None, get_full_hierarchy=False): if not dct: dct = {} if not exclude: @@ -944,8 +965,11 @@ class GeneralType(Cached, models.Model): else: item_id = item[0] yield item + if get_full_hierarchy: + get_full_hierarchy = item[1] for child in cls._get_childs( - item_id, child_list, instances=instances): + item_id, child_list, instances=instances, + get_full_hierarchy=get_full_hierarchy): yield child def save(self, *args, **kwargs): |