From 28f387cf6c58111a2b58f97baa05689923bf5ad4 Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Fri, 7 Dec 2018 03:34:02 -0400 Subject: [PATCH] Metadata: Use generator to prepare lookup choices Change the use of the list/zip combinarion to generate the full list of metadata lookup choices to a generator. Signed-off-by: Roberto Rosario --- HISTORY.rst | 4 +++- mayan/apps/metadata/forms.py | 12 ++++++++---- mayan/apps/metadata/models.py | 10 ++++++++++ 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 93eac64380..380136fdf8 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -163,7 +163,9 @@ into a Mayan app. With change adds the new settings: "COMMON_AUTOADMIN_EMAIL", "AUTOADMIN_PASSWORD", and "AUTOADMIN_USERNAME". - +- Changed the use of the list/zip combinarion to generate + the full list of metadata lookup choices to a generator. + 3.1.9 (2018-11-01) ================== - Convert the furl instance to text to allow serializing it into diff --git a/mayan/apps/metadata/forms.py b/mayan/apps/metadata/forms.py index 0b3baeec8e..e59475c96e 100644 --- a/mayan/apps/metadata/forms.py +++ b/mayan/apps/metadata/forms.py @@ -62,11 +62,15 @@ class DocumentMetadataForm(forms.Form): self.fields['value'] = forms.ChoiceField( label=self.fields['value'].label ) - choices = self.metadata_type.get_lookup_values() - choices = list(zip(choices, choices)) + if not required: - choices.insert(0, ('', '------')) - self.fields['value'].choices = choices + first_choice=('', '------') + else: + first_choice=None + + self.fields['value'].choices = self.metadata_type.get_lookup_choices( + first_choice=first_choice + ) self.fields['value'].required = required self.fields['value'].widget.attrs.update( {'class': 'metadata-value'} diff --git a/mayan/apps/metadata/models.py b/mayan/apps/metadata/models.py index 3d4499b45d..8f55d39ac7 100644 --- a/mayan/apps/metadata/models.py +++ b/mayan/apps/metadata/models.py @@ -123,6 +123,16 @@ class MetadataType(models.Model): template = Template(self.default) return template.render() + def get_lookup_choices(self, first_choice=None): + template = Template(self.lookup) + context = MetadataLookup.get_as_context() + + if first_choice: + yield first_choice + + for value in MetadataType.comma_splitter(template.render(**context)): + yield (value, value) + def get_lookup_values(self): template = Template(self.lookup) context = MetadataLookup.get_as_context()