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 <Roberto.Rosario@mayan-edms.com>
This commit is contained in:
Roberto Rosario
2018-12-07 03:34:02 -04:00
parent 8039dfa30a
commit 28f387cf6c
3 changed files with 21 additions and 5 deletions

View File

@@ -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

View File

@@ -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'}

View File

@@ -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()