diff --git a/Form/UK1841.py b/Form/UK1841.py index d8f6413e4..b36f82e38 100644 --- a/Form/UK1841.py +++ b/Form/UK1841.py @@ -18,11 +18,12 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # +from gramps.gen.datehandler import displayer as date_displayer from gramps.gen.display.name import displayer as name_displayer -from gramps.gen.lib import (Event, EventType, EventRef, EventRoleType, +from gramps.gen.lib import (Date, Event, EventType, EventRef, EventRoleType, Person) -from actionbase import ActionBase +from actionbase import ActionBase, represents_int #------------------------------------------------------------------------ # @@ -56,6 +57,44 @@ def command(db, trans, citation_handle, person_handle): person.get_primary_name().add_citation(citation_handle) db.commit_person(person, trans) +class BirthEvent(ActionBase): + def __init__(self): + ActionBase.__init__(self) + pass + + def populate_model(self, db, citation, form_event, model): + # if there is no date on the form, no actions can be performed + if form_event.get_date_object(): + parent = model.append(None, (_("Add Birth event"), None, None)) + for item in db.find_backlink_handles(form_event.get_handle(), + include_classes=['Person']): + handle = item[1] + person = db.get_person_from_handle(handle) + for event_ref in person.get_event_ref_list(): + if event_ref.ref == form_event.get_handle(): + for attr in event_ref.get_attribute_list(): + if (attr.get_type() == "Age"): # Form specific _attribute name + age_string = attr.get_value() + if age_string and represents_int(age_string): + age = int(age_string) + if age: + birth_date = form_event.get_date_object() - age + birth_date.make_vague() + # Age was rounded down to the nearest five years for those aged 15 or over + # In practice this rule was not always followed by enumerators + if age < 15: + # no adjustment required + birth_date.set_modifier(Date.MOD_ABOUT) + elif not birth_date.is_compound(): + # in theory, birth_date will never be compound since 1841 census date was 1841-06-06. Let's handle it anyway. + # create a compound range spanning the possible birth years + birth_range = (birth_date - 5).get_dmy() + (False,) + birth_date.get_dmy() + (False,) + birth_date.set(Date.QUAL_NONE, Date.MOD_RANGE, birth_date.get_calendar(), birth_range, newyear=birth_date.get_new_year()) + birth_date.set_quality(Date.QUAL_CALCULATED) + + model.append(parent, (name_displayer.display(person), date_displayer.display(birth_date), + lambda db, trans, citation_handle = citation.handle, person_handle = person.handle, birth_date_ = birth_date: ActionBase.AddEventToPerson(db, trans, person_handle, EventType.BIRTH, birth_date_, None, citation_handle, EventRoleType.PRIMARY))) + class OccupationEvent(ActionBase): def __init__(self): ActionBase.__init__(self) diff --git a/Form/actionbase.py b/Form/actionbase.py index 0f4b2acea..eb794e485 100644 --- a/Form/actionbase.py +++ b/Form/actionbase.py @@ -52,3 +52,13 @@ def AddEventToPerson(db, trans, person_handle, event_type, event_date_object, ev person = db.get_person_from_handle(person_handle) person.add_event_ref(event_ref) db.commit_person(person, trans) + +def represents_int(s): + """ + return True iff s is convertable to an int, False otherwise + """ + try: + int(s) + return True + except ValueError: + return False