Skip to content

Commit d64da69

Browse files
committed
The way FormActions dynamically identified action classes gave no control over the order in which actions were added to the UI. Rather than make an already complex solution more complex, take a different, simpler, approach. The form action module now provides a standalone get_actions function which returns a list of all available actions the module can provide for the given citation and form event.
As an additional benefit this gives a natural point to optimise the get_actions functions in the future, should this become necessary. For example, by pre-loading all Person records and attributes prior to calling the class.get_action functions.
1 parent 48b81ff commit d64da69

File tree

2 files changed

+29
-34
lines changed

2 files changed

+29
-34
lines changed

Form/UK1841.py

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,21 @@
5151

5252
_ = _trans.gettext
5353

54-
class PrimaryNameCitation(ActionBase):
55-
def __init__(self):
56-
ActionBase.__init__(self)
57-
58-
def get_actions(self, dbstate, citation, form_event):
54+
def get_actions(dbstate, citation, form_event):
55+
"""
56+
return a list of all actions that this module can provide for the given citation and form
57+
each list entry is a string, describing the action category, and a list of actions that can be performed.
58+
"""
59+
actions = []
60+
actions.append(PrimaryNameCitation.get_actions(dbstate, citation, form_event))
61+
actions.append(AlternateName.get_actions(dbstate, citation, form_event))
62+
actions.append(BirthEvent.get_actions(dbstate, citation, form_event))
63+
actions.append(OccupationEvent.get_actions(dbstate, citation, form_event))
64+
actions.append(ResidenceEvent.get_actions(dbstate, citation, form_event))
65+
return actions
66+
67+
class PrimaryNameCitation:
68+
def get_actions(dbstate, citation, form_event):
5969
db = dbstate.db
6070
actions = []
6171
for (person, attr) in ActionBase.get_form_person_attr(db, form_event.get_handle(), 'Name'):
@@ -70,11 +80,8 @@ def command(dbstate, uistate, track, citation_handle, person_handle):
7080
with DbTxn(_("Add Person (%s)") % name_displayer.display(person), db) as trans:
7181
db.commit_person(person, trans)
7282

73-
class AlternateName(ActionBase):
74-
def __init__(self):
75-
ActionBase.__init__(self)
76-
77-
def get_actions(self, dbstate, citation, form_event):
83+
class AlternateName:
84+
def get_actions(dbstate, citation, form_event):
7885
db = dbstate.db
7986
actions = []
8087
for (person, attr) in ActionBase.get_form_person_attr(db, form_event.get_handle(), 'Name'):
@@ -92,11 +99,8 @@ def command(dbstate, uistate, track, person_handle, alternate):
9299
with DbTxn(_("Add Person (%s)") % name_displayer.display(person), db) as trans:
93100
db.commit_person(person, trans)
94101

95-
class BirthEvent(ActionBase):
96-
def __init__(self):
97-
ActionBase.__init__(self)
98-
99-
def get_actions(self, dbstate, citation, form_event):
102+
class BirthEvent:
103+
def get_actions(dbstate, citation, form_event):
100104
db = dbstate.db
101105
actions = []
102106
# if there is no date on the form, no actions can be performed
@@ -124,11 +128,8 @@ def get_actions(self, dbstate, citation, form_event):
124128
lambda dbstate, uistate, track, citation_handle = citation.handle, person_handle = person.handle, birth_date_ = birth_date: ActionBase.add_event_to_person(dbstate, uistate, track, person_handle, EventType.BIRTH, birth_date_, None, citation_handle, EventRoleType.PRIMARY)))
125129
return (_("Add Birth event"), actions)
126130

127-
class OccupationEvent(ActionBase):
128-
def __init__(self):
129-
ActionBase.__init__(self)
130-
131-
def get_actions(self, dbstate, citation, form_event):
131+
class OccupationEvent:
132+
def get_actions(dbstate, citation, form_event):
132133
db = dbstate.db
133134
actions = []
134135
for (person, attr) in ActionBase.get_form_person_attr(db, form_event.get_handle(), 'Occupation'):
@@ -138,11 +139,8 @@ def get_actions(self, dbstate, citation, form_event):
138139
lambda dbstate, uistate, track, citation_handle = citation.handle, person_handle = person.handle, occupation_ = occupation: ActionBase.add_event_to_person(dbstate, uistate, track, person_handle, EventType.OCCUPATION, form_event.get_date_object(), occupation_, citation_handle, EventRoleType.PRIMARY)))
139140
return (_("Add Occupation event"), actions)
140141

141-
class ResidenceEvent(ActionBase):
142-
def __init__(self):
143-
ActionBase.__init__(self)
144-
145-
def get_actions(self, dbstate, citation, form_event):
142+
class ResidenceEvent:
143+
def get_actions(dbstate, citation, form_event):
146144
db = dbstate.db
147145
# build a list of all the people referenced in the form. For 1841, all people have a PRIMARY event role
148146
people = []

Form/formactions.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
#------------------------------------------------------------------------
5959
from editform import find_form_event
6060
from form import (get_form_id, get_form_type)
61-
from actionbase import ActionBase
6261

6362
#------------------------------------------------------------------------
6463
#
@@ -209,16 +208,14 @@ def all_children_consistent(model, parent, col):
209208
def _populate_model(self):
210209
form_id = get_form_id(self.source)
211210
if self.actions_module:
212-
# get all classes defined in actions_module which are a subclass of ActionBase (but exclude ActionBase itself)
213-
action_classes = inspect.getmembers(self.actions_module, lambda obj: inspect.isclass(obj) and obj is not ActionBase and issubclass(obj, ActionBase))
214-
215-
for action_class in action_classes:
216-
action = (action_class[1])()
217-
(title, action_details) = action.get_actions(self.dbstate, self.citation, self.event)
218-
if action_details:
211+
# get the all actions that the actions module can provide for the form
212+
# because the module is dynamically loaded, use getattr to retrieve the actual function to call
213+
all_actions = getattr(self.actions_module, 'get_actions')(self.dbstate, self.citation, self.event)
214+
for (title, actions) in all_actions:
215+
if actions:
219216
# add the action category
220217
parent = self.model.append(None, (False, False, title, None, None))
221-
for action_detail in action_details:
218+
for action_detail in actions:
222219
# add available actions within this category
223220
self.model.append(parent, (False, False) + action_detail)
224221

0 commit comments

Comments
 (0)