Skip to content

Commit

Permalink
fix: ppi admi layout
Browse files Browse the repository at this point in the history
  • Loading branch information
moogoo78 committed Jul 9, 2024
1 parent e63b2c8 commit 2ffe349
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 46 deletions.
16 changes: 8 additions & 8 deletions app/blueprints/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'] = {
Expand Down
5 changes: 5 additions & 0 deletions app/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions app/models/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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}
Expand All @@ -650,6 +654,7 @@ def get_editable_fields(field_types=['date', 'int', 'str', 'decimal']):
'verbatim_identification',
'date_text',
'verbatim_date',
'note',
]
decimal_fields = [
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
};

});
Expand Down Expand Up @@ -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}));

Expand Down Expand Up @@ -181,39 +209,47 @@

let data = {
units: [{}],
identifications: [{
sequence: 0,
},]
identifications: [],
};

if (identificationCounter > 0) {
for (let i=0;i<identificationCounter;i++) {
// TODO custom sequence
let taxon = document.getElementById(`identifications-${i+1}-taxon-id-display`);
let identifier = document.getElementById(`identifications-${i+1}-identifier-id-display`);
let idObj = {
sequence: i+1,
};
if (taxon && taxon.dataset.value) {
idObj.taxon_id = taxon.dataset.value
}
if (identifier && identifier.dataset.value) {
idObj.identifier_id = identifier.dataset.value
}
data.identifications.push(idObj);
console.log(identificationCounter);
for (let i=0;i<=identificationCounter;i++) {
// TODO custom sequence
let idObj = {
sequence: i+1,
};
let taxon = null;
let identifier = null;
if (i === 0) {
taxon = document.getElementById(`taxon-id-display`);
identifier = document.getElementById(`identifier-id-display`);
} else {
taxon = document.getElementById(`identifications-${i}-taxon-id-display`);
identifier = document.getElementById(`identifications-${i}-identifier-id-display`);
}
if (taxon && taxon.dataset.value) {
idObj.taxon_id = taxon.dataset.value
}
if (identifier && identifier.dataset.value) {
idObj.identifier_id = identifier.dataset.value
}
data.identifications.push(idObj);
}

let formData = new FormData(formElem);
for (const pair of formData.entries()) {
//data[pair[0]] = pair[1];
//console.log(pair);

/*
if (pair[0].indexOf('identifications-')>=0){
let stacks = pair[0].split('-');
if (stacks[1]) {
let idx = parseInt(stacks[1]);
data.identifications[idx][stacks[2]] = pair[1];
}
}
*/
}

for (let i=0;i<comboboxDisplays.length;i++) {
Expand All @@ -222,15 +258,17 @@
if (comboboxDisplays[i].dataset.value) {
data[`${name}_id`] = comboboxDisplays[i].dataset.value
}
} else if (name === 'identifier' || name === 'taxon'){
}
/*
else if (name === 'identifier' || name === 'taxon'){
if (comboboxDisplays[i].dataset.value) {
data.identifications[0][`${name}_id`] = comboboxDisplays[i].dataset.value
}
}
}*/
}

//console.log(data);
['verbatim_collector', 'collect_date', 'collect_date_text', 'field_number', 'verbatim_locality'].forEach( x => {
['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) || '';
});

Expand All @@ -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);
Expand Down Expand Up @@ -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`);
Expand All @@ -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`);
Expand All @@ -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;
}
});
Expand All @@ -348,9 +404,9 @@
}
});

console.log(result);
//console.log(result);
if (result.identifications.length > 1) {
for (let i=0; i<result.identifications.length-1;i++) {
for (let i=1; i<result.identifications.length;i++) {
identificationCounter++;
const clone = identificationTemplate.content.cloneNode(true);
let seqAuto = clone.getElementById('tpl-sequence-auto');
Expand All @@ -364,9 +420,10 @@
['sequence', 'verbatim_identification', 'date', 'date_text', 'verbatim_date', 'note'].forEach(x => {
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);
}
}
}
Expand All @@ -376,8 +433,8 @@
for (let i=0;i<comboboxDisplays.length;i++) {
let name = comboboxDisplays[i].dataset.name;
let icon = document.getElementById(`${name}-id-icon`);
icon.setAttribute('uk-icon', 'icon: clear');
icon.onclick = handleClear(name);
icon.setAttribute('uk-icon', 'icon: close');
icon.onclick = (e) => { handleClear(name);}
}
}
init();
Expand Down Expand Up @@ -459,7 +516,7 @@
<label class="uk-form-label" for="{{ name }}-id">{{ label }}</label>
<div class="uk-form-controls">
{% if type == "input" %}
<input class="uk-input" id="{{ name }}-id" type="text" placeholder="{{ placeholder }}" name="{{ name }}">
<input class="uk-input" id="{{ name }}-id" type="text" placeholder="{{ placeholder }}" name="{{ name }}" value="{{ value }}">
{% elif type == "input-date" %}
<input class="uk-input" id="{{ name }}-id" type="date" name="{{ name }}">
{% elif type == "textarea" %}
Expand Down Expand Up @@ -494,15 +551,20 @@ <h3 class="uk-heading-bullet">採集資訊</h3>
{{ widget('verbatim_collector', '[逐字]採集者', '', '1-4@s') }}
{{ widget('field_number', '採集號', '', '1-4@s') }}
<div class="uk-width-1-4@s"></div>
{{ 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') }}
<div class="uk-width-1-4@s"></div>
{{ 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') #}
<div class="uk-width-1-1">
<h3 class="uk-heading-bullet">鑑定 <button class="uk-button uk-button-small" type="button" id="identification-add-button">+</button></h3>
</div>
Expand All @@ -512,7 +574,7 @@ <h3 class="uk-heading-bullet">鑑定 <button class="uk-button uk-button-small" t
{{ widget('taxon', '學名', '', '1-2@s', 'combobox', 'fetch:/api/v1/taxa') }}
{{ widget('verbatim_identification', '[逐字]學名', '', '1-2@s', 'input') }}
{{ widget('identifier', '鑑定者', '', '1-4@s', 'combobox') }}
{{ widget('date', '鑑定日期', '', '1-4@s', 'input-date') }}
{{ widget('date', '鑑定日期', '', '1-4@s', 'input', placeholder='2024-07-09') }}
{{ widget('date_text', '鑑定日期(部份)', '', '1-4@s', 'input') }}
{{ widget('verbatim_date', '[逐字]鑑定日期', '', '1-4@s', 'input') }}
{{ widget('note', '備註', '', '1-2@s', 'textarea') }}
Expand Down Expand Up @@ -547,7 +609,7 @@ <h3 class="uk-heading-bullet">標本</h3>
{{ 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') }}
Expand Down

0 comments on commit 2ffe349

Please sign in to comment.