Skip to content

Commit be6dd06

Browse files
committed
Rename ActionBase to actionutils more accurately reflect its purpose.
Remove the class ActionBase
1 parent ff7a62d commit be6dd06

File tree

3 files changed

+99
-106
lines changed

3 files changed

+99
-106
lines changed

Form/UK1841.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
# Gramplet modules
3737
#
3838
#------------------------------------------------------------------------
39-
from actionbase import ActionBase, represents_int
39+
import actionutils
4040

4141
#------------------------------------------------------------------------
4242
#
@@ -69,7 +69,7 @@ class PrimaryNameCitation:
6969
def get_actions(dbstate, citation, form_event):
7070
db = dbstate.db
7171
actions = []
72-
for (person, attr) in ActionBase.get_form_person_attr(db, form_event.get_handle(), 'Name'):
72+
for (person, attr) in actionutils.get_form_person_attr(db, form_event.get_handle(), 'Name'):
7373
actions.append((name_displayer.display(person), attr.get_value(),
7474
lambda dbstate, uistate, track, citation_handle = citation.handle, person_handle = person.handle: PrimaryNameCitation.command(dbstate, uistate, track, citation_handle, person_handle)))
7575
return (_("Add Primary Name citation"), actions)
@@ -87,7 +87,7 @@ class AlternateName:
8787
def get_actions(dbstate, citation, form_event):
8888
db = dbstate.db
8989
actions = []
90-
for (person, attr) in ActionBase.get_form_person_attr(db, form_event.get_handle(), 'Name'):
90+
for (person, attr) in actionutils.get_form_person_attr(db, form_event.get_handle(), 'Name'):
9191
alternate = Name()
9292
alternate.set_first_name(attr.get_value())
9393
alternate.add_citation(citation.handle)
@@ -110,9 +110,9 @@ def get_actions(dbstate, citation, form_event):
110110
actions = []
111111
# if there is no date on the form, no actions can be performed
112112
if form_event.get_date_object():
113-
for (person, attr) in ActionBase.get_form_person_attr(db, form_event.get_handle(), 'Age'):
113+
for (person, attr) in actionutils.get_form_person_attr(db, form_event.get_handle(), 'Age'):
114114
age_string = attr.get_value()
115-
if age_string and represents_int(age_string):
115+
if age_string and actionutils.represents_int(age_string):
116116
age = int(age_string)
117117
if age:
118118
birth_date = form_event.get_date_object() - age
@@ -130,19 +130,19 @@ def get_actions(dbstate, citation, form_event):
130130
birth_date.set_quality(Date.QUAL_CALCULATED)
131131

132132
actions.append((name_displayer.display(person), date_displayer.display(birth_date),
133-
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)))
133+
lambda dbstate, uistate, track, citation_handle = citation.handle, person_handle = person.handle, birth_date_ = birth_date: actionutils.add_event_to_person(dbstate, uistate, track, person_handle, EventType.BIRTH, birth_date_, None, citation_handle, EventRoleType.PRIMARY)))
134134
return (_("Add Birth event"), actions)
135135

136136
class OccupationEvent:
137137
@staticmethod
138138
def get_actions(dbstate, citation, form_event):
139139
db = dbstate.db
140140
actions = []
141-
for (person, attr) in ActionBase.get_form_person_attr(db, form_event.get_handle(), 'Occupation'):
141+
for (person, attr) in actionutils.get_form_person_attr(db, form_event.get_handle(), 'Occupation'):
142142
occupation = attr.get_value()
143143
if (occupation) :
144144
actions.append((name_displayer.display(person), occupation,
145-
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)))
145+
lambda dbstate, uistate, track, citation_handle = citation.handle, person_handle = person.handle, occupation_ = occupation: actionutils.add_event_to_person(dbstate, uistate, track, person_handle, EventType.OCCUPATION, form_event.get_date_object(), occupation_, citation_handle, EventRoleType.PRIMARY)))
146146
return (_("Add Occupation event"), actions)
147147

148148
class ResidenceEvent:

Form/actionbase.py

Lines changed: 0 additions & 98 deletions
This file was deleted.

Form/actionutils.py

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
#
2+
# Gramps - a GTK+/GNOME based genealogy program
3+
#
4+
# Copyright (C) 2019 Steve Youngs
5+
#
6+
# This program is free software; you can redistribute it and/or modify
7+
# it under the terms of the GNU General Public License as published by
8+
# the Free Software Foundation; either version 2 of the License, or
9+
# (at your option) any later version.
10+
#
11+
# This program is distributed in the hope that it will be useful,
12+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
# GNU General Public License for more details.
15+
#
16+
# You should have received a copy of the GNU General Public License
17+
# along with this program; if not, write to the Free Software
18+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19+
#
20+
21+
#------------------------------------------------------------------------
22+
#
23+
# Gramps modules
24+
#
25+
#------------------------------------------------------------------------
26+
from gramps.gen.db import DbTxn
27+
from gramps.gen.display.name import displayer as name_displayer
28+
from gramps.gen.lib import (Event, EventType, EventRef, EventRoleType,
29+
Person)
30+
31+
#------------------------------------------------------------------------
32+
#
33+
# Internationalisation
34+
#
35+
#------------------------------------------------------------------------
36+
from gramps.gen.const import GRAMPS_LOCALE as glocale
37+
try:
38+
_trans = glocale.get_addon_translator(__file__)
39+
except ValueError:
40+
_trans = glocale.translation
41+
42+
_ = _trans.gettext
43+
44+
def add_event_to_person(dbstate, uistate, track, person_handle, event_type, event_date_object, event_description, citation_handle, event_role_type):
45+
db = dbstate.db
46+
"""
47+
Add a new event to the specified person.
48+
"""
49+
event = Event()
50+
event.set_type(event_type)
51+
event.set_date_object(event_date_object)
52+
event.add_citation(citation_handle)
53+
event.set_description(event_description)
54+
55+
# add to the database
56+
with DbTxn(_("Add Event (%s)") % event.get_gramps_id(), db) as trans:
57+
db.add_event(event, trans)
58+
# Add new event reference to the Person record
59+
event_ref = EventRef()
60+
event_ref.ref = event.get_handle()
61+
event_ref.set_role(event_role_type)
62+
person = db.get_person_from_handle(person_handle)
63+
person.add_event_ref(event_ref)
64+
with DbTxn(_("Add Event (%s)") % name_displayer.display(person), db) as trans:
65+
db.commit_person(person, trans)
66+
67+
def get_form_person_attr(db, form_event_handle, attr_type):
68+
"""
69+
Find all persons referencing the form_event and which have an attribute of type attr_type
70+
returns a list of matching (person, attribute) tuples
71+
"""
72+
result = []
73+
for item in db.find_backlink_handles(form_event_handle, include_classes=['Person']):
74+
handle = item[1]
75+
person = db.get_person_from_handle(handle)
76+
for event_ref in person.get_event_ref_list():
77+
if event_ref.ref == form_event_handle:
78+
for attr in event_ref.get_attribute_list():
79+
if (attr.get_type() == attr_type):
80+
result.append((person, attr))
81+
return result
82+
83+
def represents_int(s):
84+
"""
85+
return True iff s is convertable to an int, False otherwise
86+
"""
87+
try:
88+
int(s)
89+
return True
90+
except ValueError:
91+
return False

0 commit comments

Comments
 (0)