Skip to content

Commit e60741a

Browse files
committed
Refactor code, extracting get_form_person_attr
1 parent 7147ad4 commit e60741a

File tree

2 files changed

+45
-50
lines changed

2 files changed

+45
-50
lines changed

Form/UK1841.py

Lines changed: 29 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,9 @@ def __init__(self):
5757
def populate_model(self, dbstate, citation, form_event, model):
5858
db = dbstate.db
5959
parent = model.append(None, (_("Add Primary Name citation"), None, None))
60-
for item in db.find_backlink_handles(form_event.get_handle(),
61-
include_classes=['Person']):
62-
handle = item[1]
63-
person = db.get_person_from_handle(handle)
64-
for event_ref in person.get_event_ref_list():
65-
if event_ref.ref == form_event.get_handle():
66-
for attr in event_ref.get_attribute_list():
67-
if (attr.get_type() == "Name"): # Form specific _attribute name
68-
model.append(parent, (name_displayer.display(person), attr.get_value(),
69-
lambda dbstate, uistate, track, citation_handle = citation.handle, person_handle = person.handle: PrimaryNameCitation.command(dbstate, uistate, track, citation_handle, person_handle)))
60+
for (person, attr) in ActionBase.get_form_person_attr(db, form_event.get_handle(), 'Name'):
61+
model.append(parent, (name_displayer.display(person), attr.get_value(),
62+
lambda dbstate, uistate, track, citation_handle = citation.handle, person_handle = person.handle: PrimaryNameCitation.command(dbstate, uistate, track, citation_handle, person_handle)))
7063

7164
def command(dbstate, uistate, track, citation_handle, person_handle):
7265
db = dbstate.db
@@ -85,34 +78,27 @@ def populate_model(self, dbstate, citation, form_event, model):
8578
# if there is no date on the form, no actions can be performed
8679
if form_event.get_date_object():
8780
parent = model.append(None, (_("Add Birth event"), None, None))
88-
for item in db.find_backlink_handles(form_event.get_handle(),
89-
include_classes=['Person']):
90-
handle = item[1]
91-
person = db.get_person_from_handle(handle)
92-
for event_ref in person.get_event_ref_list():
93-
if event_ref.ref == form_event.get_handle():
94-
for attr in event_ref.get_attribute_list():
95-
if (attr.get_type() == "Age"): # Form specific _attribute name
96-
age_string = attr.get_value()
97-
if age_string and represents_int(age_string):
98-
age = int(age_string)
99-
if age:
100-
birth_date = form_event.get_date_object() - age
101-
birth_date.make_vague()
102-
# Age was rounded down to the nearest five years for those aged 15 or over
103-
# In practice this rule was not always followed by enumerators
104-
if age < 15:
105-
# no adjustment required
106-
birth_date.set_modifier(Date.MOD_ABOUT)
107-
elif not birth_date.is_compound():
108-
# in theory, birth_date will never be compound since 1841 census date was 1841-06-06. Let's handle it anyway.
109-
# create a compound range spanning the possible birth years
110-
birth_range = (birth_date - 5).get_dmy() + (False,) + birth_date.get_dmy() + (False,)
111-
birth_date.set(Date.QUAL_NONE, Date.MOD_RANGE, birth_date.get_calendar(), birth_range, newyear=birth_date.get_new_year())
112-
birth_date.set_quality(Date.QUAL_CALCULATED)
81+
for (person, attr) in ActionBase.get_form_person_attr(db, form_event.get_handle(), 'Age'):
82+
age_string = attr.get_value()
83+
if age_string and represents_int(age_string):
84+
age = int(age_string)
85+
if age:
86+
birth_date = form_event.get_date_object() - age
87+
birth_date.make_vague()
88+
# Age was rounded down to the nearest five years for those aged 15 or over
89+
# In practice this rule was not always followed by enumerators
90+
if age < 15:
91+
# no adjustment required
92+
birth_date.set_modifier(Date.MOD_ABOUT)
93+
elif not birth_date.is_compound():
94+
# in theory, birth_date will never be compound since 1841 census date was 1841-06-06. Let's handle it anyway.
95+
# create a compound range spanning the possible birth years
96+
birth_range = (birth_date - 5).get_dmy() + (False,) + birth_date.get_dmy() + (False,)
97+
birth_date.set(Date.QUAL_NONE, Date.MOD_RANGE, birth_date.get_calendar(), birth_range, newyear=birth_date.get_new_year())
98+
birth_date.set_quality(Date.QUAL_CALCULATED)
11399

114-
model.append(parent, (name_displayer.display(person), date_displayer.display(birth_date),
115-
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)))
100+
model.append(parent, (name_displayer.display(person), date_displayer.display(birth_date),
101+
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)))
116102

117103
class OccupationEvent(ActionBase):
118104
def __init__(self):
@@ -121,16 +107,9 @@ def __init__(self):
121107

122108
def populate_model(self, dbstate, citation, form_event, model):
123109
db = dbstate.db
124-
parent = model.append(None, (_("Add Occupation event"), None, None))
125-
for item in db.find_backlink_handles(form_event.get_handle(),
126-
include_classes=['Person']):
127-
handle = item[1]
128-
person = db.get_person_from_handle(handle)
129-
for event_ref in person.get_event_ref_list():
130-
if event_ref.ref == form_event.get_handle():
131-
for attr in event_ref.get_attribute_list():
132-
if (attr.get_type() == "Occupation"): # Form specific _attribute name
133-
occupation = attr.get_value()
134-
if (occupation) :
135-
model.append(parent, (name_displayer.display(person), occupation,
136-
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)))
110+
parent = model.append(None, (_('Add Occupation event'), None, None))
111+
for (person, attr) in ActionBase.get_form_person_attr(db, form_event.get_handle(), 'Occupation'):
112+
occupation = attr.get_value()
113+
if (occupation) :
114+
model.append(parent, (name_displayer.display(person), occupation,
115+
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)))

Form/actionbase.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,22 @@ def add_event_to_person(dbstate, uistate, track, person_handle, event_type, even
7575
with DbTxn(_("Add Event (%s)") % name_displayer.display(person), db) as trans:
7676
db.commit_person(person, trans)
7777

78+
def get_form_person_attr(db, form_event_handle, attr_type):
79+
"""
80+
Find all persons referencing the form_event and which have an attribute of type attr_type
81+
returns a list of matching (person, attribute) tuples
82+
"""
83+
result = []
84+
for item in db.find_backlink_handles(form_event_handle, include_classes=['Person']):
85+
handle = item[1]
86+
person = db.get_person_from_handle(handle)
87+
for event_ref in person.get_event_ref_list():
88+
if event_ref.ref == form_event_handle:
89+
for attr in event_ref.get_attribute_list():
90+
if (attr.get_type() == attr_type):
91+
result.append((person, attr))
92+
return result
93+
7894
def represents_int(s):
7995
"""
8096
return True iff s is convertable to an int, False otherwise

0 commit comments

Comments
 (0)