From 2ffe34998dabc45ffbee38449cdda76e0312513b Mon Sep 17 00:00:00 2001 From: MooGoo Date: Tue, 9 Jul 2024 16:22:30 +0800 Subject: [PATCH] fix: ppi admi layout --- app/blueprints/admin.py | 16 +- app/helpers.py | 5 + app/models/collection.py | 5 + ...-form-view1.html => record-form-view.html} | 138 +++++++++++++----- 4 files changed, 118 insertions(+), 46 deletions(-) rename app/templates/sites/ppi/admin/{record-form-view1.html => record-form-view.html} (81%) diff --git a/app/blueprints/admin.py b/app/blueprints/admin.py index 7928aca..77e006a 100644 --- a/app/blueprints/admin.py +++ b/app/blueprints/admin.py @@ -543,14 +543,14 @@ def get_all_options(collection): # TODO if collection.id == 1: ac_list = AreaClass.query.filter(AreaClass.collection_id==collection.id, AreaClass.id > 4) - elif collection.id == 5: - ac_list = AreaClass.query.filter(AreaClass.id > 7, AreaClass.id < 11) - - for ac in ac_list.all(): - data['named_areas'][ac.name] = { - 'label': ac.label, - 'options': [x.to_dict() for x in ac.named_area] - } + #elif collection.id == 5: + # ac_list = AreaClass.query.filter(AreaClass.id > 7, AreaClass.id < 11) + + #for ac in ac_list.all(): + # data['named_areas'][ac.name] = { + # 'label': ac.label, + # 'options': [x.to_dict() for x in ac.named_area] + # } ac = session.get(AreaClass, 7) data['named_areas']['country'] = { diff --git a/app/helpers.py b/app/helpers.py index 61f51e5..43b22d6 100644 --- a/app/helpers.py +++ b/app/helpers.py @@ -67,6 +67,7 @@ def save_record(record, payload, collection, uid): updated_keys = [] for name, selected in value.items(): + print(name, selected, flush=True) if selected: new_val = int(selected['value']) updated_keys.append(name) @@ -143,12 +144,16 @@ def save_record(record, payload, collection, uid): session.commit() i2 = dict(i) + #print(i2, flush=True) if x := i2.get('identifier_id'): i2['identifier_id'] = x + iden.update({'identifier_id': x}) if x := i2.get('taxon_id'): i2['taxon_id'] = x + iden.update({'taxon_id': x}) iden_modify = make_editable_values(iden, i) + #print(iden_modify, flush=True) if len(iden_modify): iden.update(iden_modify) iden_changes = inspect_model(iden) diff --git a/app/models/collection.py b/app/models/collection.py index e6a0082..22958a8 100644 --- a/app/models/collection.py +++ b/app/models/collection.py @@ -441,6 +441,7 @@ def get_editable_fields(field_types=['date', 'int', 'str', 'float']): str_fields = [ 'field_number', 'collect_date_text', + 'verbatim_collect_date', 'verbatim_collector', 'companion_text', 'companion_text_en', @@ -630,6 +631,9 @@ def to_dict(self): 'date_text': self.date_text or '', 'verification_level': self.verification_level or '', 'sequence': self.sequence if self.sequence != None else '', + 'verbatim_date': self.verbatim_date or '', + 'verbatim_identification': self.verbatim_identification or '', + 'note': self.note or '', } if self.taxon: data['taxon'] = self.taxon.to_dict() #{'id': self.taxon_id, 'text': self.taxon.display_name} @@ -650,6 +654,7 @@ def get_editable_fields(field_types=['date', 'int', 'str', 'decimal']): 'verbatim_identification', 'date_text', 'verbatim_date', + 'note', ] decimal_fields = [ ] diff --git a/app/templates/sites/ppi/admin/record-form-view1.html b/app/templates/sites/ppi/admin/record-form-view.html similarity index 81% rename from app/templates/sites/ppi/admin/record-form-view1.html rename to app/templates/sites/ppi/admin/record-form-view.html index 17b2bb0..1615a96 100644 --- a/app/templates/sites/ppi/admin/record-form-view1.html +++ b/app/templates/sites/ppi/admin/record-form-view.html @@ -88,7 +88,9 @@ displayInput.onclick = (e) => { e.preventDefault(); e.stopPropagation(); - toggleBox(`identifications-${identificationCounter}-${x}`); + let idx = e.target.dataset.index; + console.log(identificationCounter, idx); + toggleBox(`identifications-${idx}-${x}`); }; }); @@ -142,7 +144,33 @@ attrs[k] = v; } if ('fetch' in attrs) { - let url = `${attrs.fetch}?filter={"q":"${value}"}`; + let filtr = { + q: value, + }; + if ('filter' in attrs) { + let x = attrs.filter.split('='); + filtr[x[0]] = x[1]; + } + + // append parent filter + if (name === 'ADM1') { + let parent = document.getElementById('COUNTRY-id-display'); + if (parent.dataset.value) { + filtr['parent_id'] = parent.dataset.value; + } + } else if (name === 'ADM2') { + let parent = document.getElementById('ADM1-id-display'); + if (parent.dataset.value) { + filtr['parent_id'] = parent.dataset.value; + } + } else if (name === 'ADM3') { + let parent = document.getElementById('ADM2-id-display'); + if (parent.dataset.value) { + filtr['parent_id'] = parent.dataset.value; + } + } + + let url = `${attrs.fetch}?filter=${JSON.stringify(filtr)}`; let results = await fetchData(url); options = results.data.map( x => ({value: x.id, text: x.display_name})); @@ -181,32 +209,39 @@ let data = { units: [{}], - identifications: [{ - sequence: 0, - },] + identifications: [], }; - if (identificationCounter > 0) { - for (let i=0;i=0){ let stacks = pair[0].split('-'); if (stacks[1]) { @@ -214,6 +249,7 @@ data.identifications[idx][stacks[2]] = pair[1]; } } + */ } for (let i=0;i { + ['verbatim_collector', 'collect_date', 'collect_date_text', 'field_number','verbatim_collect_date', 'verbatim_locality', 'verbatim_longitude', 'verbatim_latitude', 'altitude', 'altitude2'].forEach( x => { data[x] = formData.get(x) || ''; }); @@ -241,6 +279,15 @@ data.units[0][x] = formData.get(x); }); + data['named_areas__via'] = 'C'; + data['named_areas'] = {}; + ['COUNTRY', 'ADM1', 'ADM2', 'ADM3'].forEach( x => { + let elem = document.getElementById(`${x}-id-display`); + let v = elem.dataset.value; + if (v) { + data['named_areas'][x] = {value: v, text: x}; + } + }); let url = (RECORD_ID) ? `/admin/api/collections/${COLLECTION_ID}/records/${RECORD_ID}` : `/admin/api/collections/${COLLECTION_ID}/records`; //console.log(data); @@ -305,12 +352,13 @@ let result = await fetchData(`/admin/api/collections/${COLLECTION_ID}/records/${RECORD_ID}`, options); console.log(result); // input - ['verbatim_collector','collect_date_text', 'collect_date', 'field_number'].forEach( x => { + ['verbatim_collector','collect_date_text', 'verbatim_collect_date', 'collect_date', 'field_number', 'verbatim_locality', 'verbatim_latitude', 'verbatim_longitude', 'altitude', 'altitude2'].forEach( x => { let fooInput = document.getElementById(`${x}-id`); if (x in result){ fooInput.value = result[x]; } }); + //textarea ['verbatim_locality',].forEach( x => { let fooInput = document.getElementById(`${x}-id`); @@ -326,6 +374,13 @@ fooInput.dataset.value = result[x].id; } }); + ['COUNTRY', 'ADM1', 'ADM2', 'ADM3'].forEach( x => { + let fooInput = document.getElementById(`${x}-id-display`); + if (x in result.named_areas) { + fooInput.value = result.named_areas[x].display_name; + fooInput.dataset.value = result.named_areas[x].id; + } + }); // id_ ['date', 'date_text', 'verbatim_date', 'note', 'verbatim_identification'].forEach( x => { let fooInput = document.getElementById(`${x}-id`); @@ -337,6 +392,7 @@ let fooInput = document.getElementById(`${x}-id-display`); if (x in result.identifications[0]) { fooInput.value = result.identifications[0][x].display_name; + fooInput.setAttribute('value', result.identifications[0][x].display_name); fooInput.dataset.value = result.identifications[0][x].id; } }); @@ -348,9 +404,9 @@ } }); - console.log(result); + //console.log(result); if (result.identifications.length > 1) { - for (let i=0; i { let elem = clone.getElementById(`tpl-${x}-id`); elem.id = `identifications-${identificationCounter}-${x}-id`; - elem.name =`identifications-${identificationCounter}-${x}`; - //elem.value = result.identifications[identificationCounter][x]; + elem.name =`identifications-${identificationCounter}-${x}`; + elem.value = result.identifications[identificationCounter][x]; }); + identificationMore.appendChild(clone); } } } @@ -376,8 +433,8 @@ for (let i=0;i { handleClear(name);} } } init(); @@ -459,7 +516,7 @@
{% if type == "input" %} - + {% elif type == "input-date" %} {% elif type == "textarea" %} @@ -494,15 +551,20 @@

採集資訊

{{ widget('verbatim_collector', '[逐字]採集者', '', '1-4@s') }} {{ widget('field_number', '採集號', '', '1-4@s') }}
- {{ widget('collect_date', '採集日期', '', '1-4@s', 'input-date') }} + {{ widget('collect_date', '採集日期', '', '1-4@s', 'input', placeholder='1988-05-15') }} {{ widget('collect_date_text', '採集日期(部份)', '', '1-4@s', placeholder='2015-03') }} {{ widget('verbatim_collect_date', '[逐字]採集日期', '', '1-4@s') }}
- {{ widget('country', '國家', '', '1-4@s', 'combobox', 'fetch:/api/v1/named_area') }} - {{ widget('country', '1級行政區', '', '1-4@s', 'combobox', 'fetch:/api/v1/named_area') }} - {{ widget('country', '2級行政區', '', '1-4@s', 'combobox', 'fetch:/api/v1/named_area') }} - {{ widget('country', '3級行政區', '', '1-4@s', 'combobox', 'fetch:/api/v1/named_area') }} + {{ widget('verbatim_longitude', '[逐字]經度', '', '1-4@s') }} + {{ widget('verbatim_latitude', '[逐字]緯度', '', '1-4@s') }} + {{ widget('altitude', '海拔', '', '1-4@s') }} + {{ widget('altitude2', '海拔2', '', '1-4@s') }} + {{ widget('COUNTRY', '國家', '', '1-4@s', 'combobox', 'fetch:/api/v1/named-areas;filter:area_class_id=7') }} + {{ widget('ADM1', '1級行政區', '', '1-4@s', 'combobox', 'fetch:/api/v1/named-areas;filter:area_class_id=8') }} + {{ widget('ADM2', '2級行政區', '', '1-4@s', 'combobox', 'fetch:/api/v1/named-areas;filter:area_class_id=9') }} + {{ widget('ADM3', '3級行政區', '', '1-4@s', 'combobox', 'fetch:/api/v1/named-areas;filter:area_class_id=10') }} {{ widget('verbatim_locality', '採集地點(文字)', '', '1-2@s', 'textarea') }} + {# widget('field_note', '', '', '1-2@s', 'textarea') #}

鑑定

@@ -512,7 +574,7 @@

鑑定

{{ widget('tpl-taxon', '學名', '', '1-2@s', 'combobox', 'fetch:/api/v1/taxa') }} {{ widget('tpl-verbatim_identification', '[逐字]學名', '', '1-2@s', 'input') }} {{ widget('tpl-identifier', '鑑定者', '', '1-4@s', 'combobox') }} - {{ widget('tpl-date', '鑑定日期', '', '1-4@s', 'input-date') }} + {{ widget('tpl-date', '鑑定日期', '', '1-4@s', 'input', placeholder='2024-07-09') }} {{ widget('tpl-date_text', '鑑定日期(部份)', '', '1-4@s', 'input') }} {{ widget('tpl-verbatim_date', '[逐字]鑑定日期', '', '1-4@s', 'input') }} {{ widget('tpl-note', '備註', '', '1-2@s', 'textarea') }}