Skip to content

Commit 6f1f25a

Browse files
New forms: update
1 parent 7aeb81e commit 6f1f25a

File tree

4 files changed

+54
-59
lines changed

4 files changed

+54
-59
lines changed

openatlas/forms/entity_form.py

Lines changed: 53 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class Form(FlaskForm):
3232
add_buttons(Form, entity)
3333
form: Any = Form(obj=entity)
3434
if request.method == 'GET' and entity.id:
35-
populate_update(form, entity)
35+
populate_update(entity, form)
3636
# elif request.method == 'GET' and entity.id:
3737
# populate_insert(form, entity, origin, date)
3838
return form
@@ -187,13 +187,28 @@ def process_form_data(entity: Entity, form: Any) -> Entity:
187187
value = getattr(form, attr).data
188188
data[attr] = value.strip() if isinstance(value, str) else value
189189
if entity.id:
190-
update_entity(entity, form, data)
190+
delete_links(entity)
191+
entity.update(data)
191192
else:
192193
entity = insert_entity(form, data)
194+
process_types(entity, form)
193195
process_relations(entity, form)
194196
return entity
195197

196198

199+
def process_types(entity: Entity, form: Any) -> None:
200+
for type_ in [g.types[id_] for id_ in entity.class_.hierarchies]:
201+
if data := convert(getattr(form, str(type_.id)).data):
202+
# if entity.class_.name in \ # Todo: check still needed?
203+
# ['actor_function', 'actor_relation', 'involvement']:
204+
# continue
205+
if type_.class_.name == 'administrative_unit':
206+
pass
207+
# manager.data['administrative_units'] += value
208+
else: # if entity.class_.view != 'type': # Todo: check needed?
209+
entity.link('P2', [g.types[id_] for id_ in data])
210+
211+
197212
def process_relations(entity: Entity, form: Any) -> None:
198213
for name, relation in entity.class_.relations.items():
199214
if relation['mode'] == 'tab':
@@ -217,25 +232,40 @@ def insert_entity(form: Any, data: dict[str, Any]) -> Entity:
217232
return entity
218233

219234

220-
def update_entity(entity: Entity, form: Any, data: dict[str, Any]) -> None:
221-
delete_links: dict[str, Any] = {'property': [], 'property_inverse': []}
222-
for i in entity.class_.relations.values():
223-
if i['mode'] != 'tab':
224-
if i['inverse']:
225-
delete_links['property_inverse'].append(i['property'])
235+
def delete_links(entity: Entity) -> None:
236+
links: dict[str, Any] = {'property': [], 'property_inverse': []}
237+
if entity.class_.hierarchies:
238+
links['property'].append('P2') # Todo: what about place types?
239+
for item in entity.class_.relations.values():
240+
if item['mode'] != 'tab':
241+
if item['inverse']:
242+
links['property_inverse'].append(item['property'])
226243
else:
227-
delete_links['property'].append(i['property'])
228-
if delete_links['property_inverse']:
229-
entity.delete_links(delete_links['property_inverse'], True)
230-
if delete_links['property']:
231-
entity.delete_links(delete_links['property'])
232-
if entity.class_.name in ['place', 'type']:
233-
new_super = g.types[entity.root[0]]
234-
if data1 := getattr(form, new_super.name).data:
235-
new_super = g.types[int(convert(data1)[0])]
236-
entity.link('has super', new_super.id)
237-
entity.update(data)
238-
239-
240-
def populate_update(form: Any, entity: Entity) -> None:
241-
form.opened.data = time.time()
244+
links['property'].append(item['property'])
245+
if links['property_inverse']:
246+
entity.delete_links(links['property_inverse'], True)
247+
if links['property']:
248+
entity.delete_links(links['property'])
249+
250+
251+
def populate_update(entity: Entity, form: Any) -> None:
252+
form.opened.data = time.time() # Todo: what if POST because of not valid?
253+
# Todo: deal with link types
254+
# types: dict[Any, Any] = manager.link_.types \
255+
# if manager.link_ else manager.entity.types
256+
# Todo: deal with place types
257+
# if manager.entity and manager.entity.class_.name == 'place':
258+
# if location := \
259+
# manager.entity.get_linked_entity_safe('P53', types=True):
260+
# types |= location.types # Admin. units and historical places
261+
type_data: dict[int, list[int]] = {}
262+
for type_, value in entity.types.items():
263+
root = g.types[type_.root[0]] if type_.root else type
264+
if root.id not in type_data:
265+
type_data[root.id] = []
266+
type_data[root.id].append(type_.id)
267+
if root.category == 'value':
268+
getattr(form, str(type_.id)).data = value
269+
for root_id, types_ in type_data.items():
270+
if hasattr(form, str(root_id)):
271+
getattr(form, str(root_id)).data = types_

openatlas/forms/manager_base.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
from openatlas.forms.add_fields import add_date_fields, add_reference_systems
1313
from openatlas.forms.field import TableField, TreeField
1414
from openatlas.forms.populate import (
15-
populate_dates, populate_reference_systems, populate_types)
15+
populate_dates, populate_reference_systems)
1616
from openatlas.forms.process import (
1717
process_dates, process_origin, process_standard_fields)
1818
from openatlas.forms.util import check_if_entity_has_time, convert
@@ -105,7 +105,6 @@ def populate_insert(self) -> None:
105105
def populate_update(self) -> None:
106106
if self.entity and not self.copy:
107107
self.form.entity_id.data = self.entity.id
108-
populate_types(self)
109108
populate_reference_systems(self)
110109
if 'date' in self.fields:
111110
populate_dates(self)

openatlas/forms/populate.py

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,6 @@
55
from openatlas.display.util2 import format_date_part
66

77

8-
def populate_types(manager: Any) -> None:
9-
types: dict[Any, Any] = manager.link_.types \
10-
if manager.link_ else manager.entity.types
11-
if manager.entity and manager.entity.class_.name == 'place':
12-
if location := \
13-
manager.entity.get_linked_entity_safe('P53', types=True):
14-
types |= location.types # Admin. units and historical places
15-
type_data: dict[int, list[int]] = {}
16-
for type_, value in types.items():
17-
root = g.types[type_.root[0]] if type_.root else type
18-
if root.id not in type_data:
19-
type_data[root.id] = []
20-
type_data[root.id].append(type_.id)
21-
if root.category == 'value':
22-
getattr(manager.form, str(type_.id)).data = value
23-
for root_id, types_ in type_data.items():
24-
if hasattr(manager.form, str(root_id)):
25-
getattr(manager.form, str(root_id)).data = types_
26-
27-
288
def populate_reference_systems(manager: Any) -> None:
299
if not manager.entity:
3010
return # It's a link update which have no reference systems

openatlas/forms/process.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,6 @@ def process_standard_fields(manager: Any) -> None:
6666
manager.data['attributes']['name'] = name
6767
elif key == 'alias':
6868
manager.data['aliases'] = value
69-
elif field_type in ['TreeField', 'TreeMultiField']:
70-
71-
if manager.class_.name in \
72-
['actor_function', 'actor_relation', 'involvement']:
73-
continue
74-
if g.types[int(getattr(manager.form, key).id)].class_.name \
75-
== 'administrative_unit':
76-
if 'administrative_units' not in manager.data:
77-
manager.data['administrative_units'] = []
78-
manager.data['administrative_units'] += value
79-
elif not manager.entity or manager.entity.class_.view != 'type':
80-
manager.data['links']['delete'].add('P2')
81-
manager.add_link('P2', [g.types[id_] for id_ in value])
82-
8369
elif field_type == 'ValueTypeField':
8470
if value is not None: # Allow the number zero
8571
manager.add_link('P2', g.types[int(key)], value)

0 commit comments

Comments
 (0)