From 4661c088d284ec390ad3ce45c21f37a8945e00e5 Mon Sep 17 00:00:00 2001 From: Alexander Watzinger Date: Tue, 17 Dec 2024 16:00:48 +0100 Subject: [PATCH] Refactored form manager --- openatlas/forms/base_manager.py | 26 +++--- openatlas/forms/manager.py | 136 +++++++++++++++----------------- 2 files changed, 75 insertions(+), 87 deletions(-) diff --git a/openatlas/forms/base_manager.py b/openatlas/forms/base_manager.py index 4c202559a..b5239fed9 100644 --- a/openatlas/forms/base_manager.py +++ b/openatlas/forms/base_manager.py @@ -302,7 +302,6 @@ def process_form(self) -> None: class PlaceBaseManager(BaseManager): - def insert_entity(self) -> None: super().insert_entity() self.entity.link( @@ -313,15 +312,15 @@ def insert_entity(self) -> None: def get_place_info_for_insert(self) -> None: super().get_place_info_for_insert() - if self.origin: - structure = self.origin.get_structure_for_insert() - self.place_info['structure'] = structure - self.place_info['gis_data'] = Gis.get_all([self.origin], structure) - if current_user.settings['module_map_overlay'] \ - and self.origin.class_.view == 'place': - self.place_info['overlay'] = Overlay.get_by_object(self.origin) - else: + if not self.origin: self.place_info['gis_data'] = Gis.get_all() + return + structure = self.origin.get_structure_for_insert() + self.place_info['structure'] = structure + self.place_info['gis_data'] = Gis.get_all([self.origin], structure) + if current_user.settings['module_map_overlay'] \ + and self.origin.class_.view == 'place': + self.place_info['overlay'] = Overlay.get_by_object(self.origin) def get_place_info_for_update(self) -> None: super().get_place_info_for_update() @@ -382,11 +381,10 @@ def additional_fields(self) -> dict[str, Any]: event_preceding = self.entity.get_linked_entity('P134') filter_ids = [self.entity.id] + [ e.id for e in self.entity.get_linked_entities_recursive('P9') + - self.entity.get_linked_entities_recursive('P134', inverse=True) - ] - if self.class_.name != 'move': - if place_ := self.entity.get_linked_entity('P7'): - place = place_.get_linked_entity_safe('P53', True) + self.entity.get_linked_entities_recursive('P134', True)] + if self.class_.name != 'move' \ + and (place_ := self.entity.get_linked_entity('P7')): + place = place_.get_linked_entity_safe('P53', True) self.table_items = { 'event_view': Entity.get_by_view('event', True, self.aliases), 'place': Entity.get_by_class('place', True, self.aliases)} diff --git a/openatlas/forms/manager.py b/openatlas/forms/manager.py index a54aa2616..f83b5879f 100644 --- a/openatlas/forms/manager.py +++ b/openatlas/forms/manager.py @@ -162,16 +162,15 @@ def additional_fields(self) -> dict[str, Any]: and self.origin.class_.view in ['artifact', 'place'] else None else: selection = self.entity.get_linked_entity('P46', inverse=True) - fields = super().additional_fields() - fields['super'] = TableField( - Entity.get_by_class( - g.view_class_mapping['place'] + ['artifact'], - types=True, - aliases=self.aliases), - selection, - filter_ids, - add_dynamic=['place']) - return fields + return super().additional_fields() | { + 'super': TableField( + Entity.get_by_class( + g.view_class_mapping['place'] + ['artifact'], + types=True, + aliases=self.aliases), + selection, + filter_ids, + add_dynamic=['place'])} def process_form(self) -> None: super().process_form() @@ -191,11 +190,9 @@ def additional_fields(self) -> dict[str, Any]: selection = [self.origin] else: selection = self.entity.get_linked_entities('P94', sort=True) - fields = super().additional_fields() - fields['document'] = TableMultiField( - Entity.get_by_class('file'), - selection) - return fields + return super().additional_fields() | { + 'document': + TableMultiField(Entity.get_by_class('file'), selection)} def process_form(self) -> None: super().process_form() @@ -229,26 +226,26 @@ class FeatureManager(PlaceBaseManager): def add_buttons(self) -> None: super().add_buttons() - if not self.entity: - setattr( - self.form_class, - 'insert_continue_sub', - SubmitField( - _('insert and add') + ' ' + _('stratigraphic unit'))) + if self.entity: + return + setattr( + self.form_class, + 'insert_continue_sub', + SubmitField(_('insert and add') + ' ' + _('stratigraphic unit'))) def additional_fields(self) -> dict[str, Any]: - fields = super().additional_fields() if self.insert: selection = self.origin if self.origin \ and self.origin.class_.name == 'place' else None else: selection = self.entity.get_linked_entity('P46', inverse=True) - fields['super'] = TableField( - Entity.get_by_class('place', True, self.aliases), - selection, - validators=[InputRequired()], - add_dynamic=['place']) - return fields + return super().additional_fields() | { + 'super': + TableField( + Entity.get_by_class('place', True, self.aliases), + selection, + validators=[InputRequired()], + add_dynamic=['place'])} def process_form(self) -> None: super().process_form() @@ -298,16 +295,15 @@ def additional_fields(self) -> dict[str, Any]: and self.origin.class_.view in ['artifact', 'place'] else None else: selection = self.entity.get_linked_entity('P46', inverse=True) - fields = super().additional_fields() - fields['super'] = TableField( - Entity.get_by_class( - g.view_class_mapping['place'] + ['human remains'], - types=True, - aliases=self.aliases), - selection, - filter_ids, - add_dynamic=['place']) - return fields + return super().additional_fields() | { + 'super': TableField( + Entity.get_by_class( + g.view_class_mapping['place'] + ['human remains'], + types=True, + aliases=self.aliases), + selection, + filter_ids, + add_dynamic=['place'])} def process_form(self) -> None: super().process_form() @@ -400,14 +396,13 @@ def additional_fields(self) -> dict[str, Any]: artifacts.append(item) elif item.cidoc_class.code == 'E18': places.append(item) - fields = super().additional_fields() - fields['artifact'] = TableMultiField( - Entity.get_by_class('artifact', True), - artifacts) - fields['modified_place'] = TableMultiField( - self.table_items['place'], - places) - return fields + return super().additional_fields() | { + 'artifact': TableMultiField( + Entity.get_by_class('artifact', True), + artifacts), + 'modified_place': TableMultiField( + self.table_items['place'], + places)} def process_form(self) -> None: super().process_form() @@ -425,7 +420,6 @@ class MoveManager(EventBaseManager): _('place from') def additional_fields(self) -> dict[str, Any]: - fields = super().additional_fields() place_from = None place_to = None data: dict[str, list[Any]] = {'artifact': [], 'person': []} @@ -443,21 +437,21 @@ def additional_fields(self) -> dict[str, Any]: data['artifact'] = [self.origin] elif self.origin.class_.view == 'place': place_from = self.origin - fields['place_from'] = TableField( - self.table_items['place'], - place_from, - add_dynamic=['place']) - fields['place_to'] = TableField( - self.table_items['place'], - place_to, - add_dynamic=['place']) - fields['moved_artifact'] = TableMultiField( - Entity.get_by_class('artifact', True), - data['artifact']) - fields['moved_person'] = TableMultiField( - Entity.get_by_class('person', aliases=self.aliases), - data['person']) - return fields + return super().additional_fields() | { + 'place_from': TableField( + self.table_items['place'], + place_from, + add_dynamic=['place']), + 'place_to': TableField( + self.table_items['place'], + place_to, + add_dynamic=['place']), + 'moved_artifact': TableMultiField( + Entity.get_by_class('artifact', True), + data['artifact']), + 'moved_person': TableMultiField( + Entity.get_by_class('person', aliases=self.aliases), + data['person'])} def process_form(self) -> None: super().process_form() @@ -503,14 +497,13 @@ def populate_insert(self) -> None: class ProductionManager(EventBaseManager): def additional_fields(self) -> dict[str, Any]: - fields = super().additional_fields() selection = None if not self.insert and self.entity: selection = self.entity.get_linked_entities('P108', sort=True) - fields['artifact'] = TableMultiField( - Entity.get_by_class('artifact', True), - selection) - return fields + return super().additional_fields() | { + 'artifact': TableMultiField( + Entity.get_by_class('artifact', True), + selection)} def process_form(self) -> None: super().process_form() @@ -574,7 +567,6 @@ def process_form(self) -> None: class SourceManager(SourceBaseManager): - def add_description(self) -> None: text = '' linked_entities = [] @@ -609,7 +601,6 @@ def process_form(self) -> None: class SourceTranslationManager(SourceBaseManager): - def add_description(self) -> None: text = '' linked_entities = [] @@ -652,17 +643,16 @@ def add_buttons(self) -> None: SubmitField(_('insert and add') + ' ' + _('human remains'))) def additional_fields(self) -> dict[str, Any]: - fields = super().additional_fields() selection = None if not self.insert and self.entity: selection = self.entity.get_linked_entity_safe('P46', inverse=True) elif self.origin and self.origin.class_.name == 'feature': selection = self.origin - fields['super'] = TableField( + return super().additional_fields() | { + 'super': TableField( Entity.get_by_class('feature', True), selection, - validators=[InputRequired()]) - return fields + validators=[InputRequired()])} def process_form(self) -> None: super().process_form()