Skip to content

Commit db2b469

Browse files
committed
Allow each action to control its own db transaction.
In future this will allow objects created by the action to be, optionally, edited by the user before committing to the db.
1 parent 9302e88 commit db2b469

File tree

3 files changed

+50
-16
lines changed

3 files changed

+50
-16
lines changed

Form/UK1841.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,22 @@
1818
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
1919
#
2020

21+
#------------------------------------------------------------------------
22+
#
23+
# Gramps modules
24+
#
25+
#------------------------------------------------------------------------
2126
from gramps.gen.datehandler import displayer as date_displayer
27+
from gramps.gen.db import DbTxn
2228
from gramps.gen.display.name import displayer as name_displayer
2329
from gramps.gen.lib import (Date, Event, EventType, EventRef, EventRoleType,
2430
Person)
2531

32+
#------------------------------------------------------------------------
33+
#
34+
# Gramplet modules
35+
#
36+
#------------------------------------------------------------------------
2637
from actionbase import ActionBase, represents_int
2738

2839
#------------------------------------------------------------------------
@@ -54,12 +65,14 @@ def populate_model(self, db, citation, form_event, model):
5465
for attr in event_ref.get_attribute_list():
5566
if (attr.get_type() == "Name"): # Form specific _attribute name
5667
model.append(parent, (name_displayer.display(person), attr.get_value(),
57-
lambda db, trans, citation_handle = citation.handle, person_handle = person.handle: PrimaryNameCitation.command(db, trans, citation_handle, person_handle)))
68+
lambda dbstate, uistate, track, citation_handle = citation.handle, person_handle = person.handle: PrimaryNameCitation.command(dbstate, uistate, track, citation_handle, person_handle)))
5869

59-
def command(db, trans, citation_handle, person_handle):
70+
def command(dbstate, uistate, track, citation_handle, person_handle):
71+
db = dbstate.db
6072
person = db.get_person_from_handle(person_handle)
6173
person.get_primary_name().add_citation(citation_handle)
62-
db.commit_person(person, trans)
74+
with DbTxn(_("Add Person (%s)") % name_displayer.display(person), db) as trans:
75+
db.commit_person(person, trans)
6376

6477
class BirthEvent(ActionBase):
6578
def __init__(self):
@@ -97,7 +110,7 @@ def populate_model(self, db, citation, form_event, model):
97110
birth_date.set_quality(Date.QUAL_CALCULATED)
98111

99112
model.append(parent, (name_displayer.display(person), date_displayer.display(birth_date),
100-
lambda db, trans, citation_handle = citation.handle, person_handle = person.handle, birth_date_ = birth_date: ActionBase.add_event_to_person(db, trans, person_handle, EventType.BIRTH, birth_date_, None, citation_handle, EventRoleType.PRIMARY)))
113+
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)))
101114

102115
class OccupationEvent(ActionBase):
103116
def __init__(self):
@@ -117,4 +130,4 @@ def populate_model(self, db, citation, form_event, model):
117130
occupation = attr.get_value()
118131
if (occupation) :
119132
model.append(parent, (name_displayer.display(person), occupation,
120-
lambda db, trans, citation_handle = citation.handle, person_handle = person.handle, occupation_ = occupation: ActionBase.add_event_to_person(db, trans, person_handle, EventType.OCCUPATION, form_event.get_date_object(), occupation_, citation_handle, EventRoleType.PRIMARY)))
133+
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: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,38 @@
2222
ActionBase definitions.
2323
"""
2424

25+
#------------------------------------------------------------------------
26+
#
27+
# Gramps modules
28+
#
29+
#------------------------------------------------------------------------
30+
from gramps.gen.db import DbTxn
2531
from gramps.gen.display.name import displayer as name_displayer
2632
from gramps.gen.lib import (Event, EventType, EventRef, EventRoleType,
2733
Person)
2834

35+
#------------------------------------------------------------------------
36+
#
37+
# Internationalisation
38+
#
39+
#------------------------------------------------------------------------
40+
from gramps.gen.const import GRAMPS_LOCALE as glocale
41+
try:
42+
_trans = glocale.get_addon_translator(__file__)
43+
except ValueError:
44+
_trans = glocale.translation
45+
46+
_ = _trans.gettext
47+
2948
class ActionBase():
3049
"""
3150
A class to read form definitions from an XML file.
3251
"""
3352
def __init__(self):
3453
pass
3554

36-
def add_event_to_person(db, trans, person_handle, event_type, event_date_object, event_description, citation_handle, event_role_type):
55+
def add_event_to_person(dbstate, uistate, track, person_handle, event_type, event_date_object, event_description, citation_handle, event_role_type):
56+
db = dbstate.db
3757
"""
3858
Add a new event to the specified person.
3959
"""
@@ -44,14 +64,16 @@ def add_event_to_person(db, trans, person_handle, event_type, event_date_object,
4464
event.set_description(event_description)
4565

4666
# add to the database
47-
db.add_event(event, trans)
67+
with DbTxn(_("Add Event (%s)") % event.get_gramps_id(), db) as trans:
68+
db.add_event(event, trans)
4869
# Add new event reference to the Person record
4970
event_ref = EventRef()
5071
event_ref.ref = event.get_handle()
5172
event_ref.set_role(event_role_type)
5273
person = db.get_person_from_handle(person_handle)
5374
person.add_event_ref(event_ref)
54-
db.commit_person(person, trans)
75+
with DbTxn(_("Add Event (%s)") % name_displayer.display(person), db) as trans:
76+
db.commit_person(person, trans)
5577

5678
def represents_int(s):
5779
"""

Form/formactions.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,14 +173,13 @@ def run(self):
173173
self._config.save()
174174

175175
# run the selected actions
176-
with DbTxn("FormActions", self.db) as trans:
177-
(model, pathlist) = self.tree.get_selection().get_selected_rows()
178-
for path in pathlist :
179-
tree_iter = model.get_iter(path)
180-
181-
command = model.get_value(tree_iter, 2)
182-
if command:
183-
(command)(self.db, trans)
176+
(model, pathlist) = self.tree.get_selection().get_selected_rows()
177+
for path in pathlist :
178+
tree_iter = model.get_iter(path)
179+
180+
command = model.get_value(tree_iter, 2)
181+
if command:
182+
(command)(self.dbstate, self.uistate, self.track)
184183

185184
self.top.destroy()
186185

0 commit comments

Comments
 (0)