Skip to content

Commit

Permalink
Refactored form manager
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderWatzinger committed Dec 17, 2024
1 parent 75df833 commit 55fe007
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 89 deletions.
26 changes: 12 additions & 14 deletions openatlas/forms/base_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,6 @@ def process_form(self) -> None:


class PlaceBaseManager(BaseManager):

def insert_entity(self) -> None:
super().insert_entity()
self.entity.link(
Expand All @@ -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()
Expand Down Expand Up @@ -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)}
Expand Down
136 changes: 63 additions & 73 deletions openatlas/forms/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand All @@ -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': []}
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -574,7 +567,6 @@ def process_form(self) -> None:


class SourceManager(SourceBaseManager):

def add_description(self) -> None:
text = ''
linked_entities = []
Expand Down Expand Up @@ -609,7 +601,6 @@ def process_form(self) -> None:


class SourceTranslationManager(SourceBaseManager):

def add_description(self) -> None:
text = ''
linked_entities = []
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 0 additions & 2 deletions openatlas/templates/annotate_text.html

This file was deleted.

0 comments on commit 55fe007

Please sign in to comment.