Skip to content

Commit ffd68b8

Browse files
authored
Update birth ref index and death ref index when updating person (#252)
1 parent 8c950f1 commit ffd68b8

File tree

2 files changed

+148
-15
lines changed

2 files changed

+148
-15
lines changed

gramps_webapi/api/resources/util.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ def get_sex_profile(person: Person) -> str:
126126

127127

128128
def get_event_participants_for_handle(
129-
db_handle: DbReadBase, handle: Handle, locale: GrampsLocale = glocale,
129+
db_handle: DbReadBase,
130+
handle: Handle,
131+
locale: GrampsLocale = glocale,
130132
) -> Dict:
131133
"""Get event participants given a handle."""
132134
result = {"people": [], "families": []}
@@ -674,7 +676,9 @@ def get_soundex(
674676

675677

676678
def get_reference_profile_for_object(
677-
db_handle: DbReadBase, obj: GrampsObject, locale: GrampsLocale = glocale,
679+
db_handle: DbReadBase,
680+
obj: GrampsObject,
681+
locale: GrampsLocale = glocale,
678682
) -> Dict:
679683
"""Return reference profiles for an object."""
680684
profile = {}
@@ -735,14 +739,20 @@ def get_rating(db_handle: DbReadBase, obj: GrampsObject) -> Tuple[int, int]:
735739
return count, confidence
736740

737741

738-
def has_handle(db_handle: DbWriteBase, obj: GrampsObject,) -> bool:
742+
def has_handle(
743+
db_handle: DbWriteBase,
744+
obj: GrampsObject,
745+
) -> bool:
739746
"""Check if an object with the same class and handle exists in the DB."""
740747
obj_class = obj.__class__.__name__.lower()
741748
method = db_handle.method("has_%s_handle", obj_class)
742749
return method(obj.handle)
743750

744751

745-
def has_gramps_id(db_handle: DbWriteBase, obj: GrampsObject,) -> bool:
752+
def has_gramps_id(
753+
db_handle: DbWriteBase,
754+
obj: GrampsObject,
755+
) -> bool:
746756
"""Check if an object with the same class and handle exists in the DB."""
747757
if not hasattr(obj, "gramps_id"): # needed for tags
748758
return False
@@ -786,7 +796,11 @@ def add_object(
786796
raise ValueError("Database does not support writing.")
787797

788798

789-
def add_family_update_refs(db_handle: DbWriteBase, obj: Family, trans: DbTxn,) -> None:
799+
def add_family_update_refs(
800+
db_handle: DbWriteBase,
801+
obj: Family,
802+
trans: DbTxn,
803+
) -> None:
790804
"""Update the `family_list` and `parent_family_list` of family members.
791805
792806
Case where the family is new.
@@ -942,7 +956,9 @@ def _get_class_name(super_name, key_name) -> str:
942956

943957

944958
def update_object(
945-
db_handle: DbWriteBase, obj: GrampsObject, trans: DbTxn,
959+
db_handle: DbWriteBase,
960+
obj: GrampsObject,
961+
trans: DbTxn,
946962
):
947963
"""Commit a modified Gramps object to the database.
948964
@@ -969,13 +985,18 @@ def update_object(
969985
update_family_update_refs(
970986
db_handle=db_handle, obj_old=obj_old, obj=obj, trans=trans
971987
)
988+
elif obj_class == "person":
989+
db_handle.set_birth_death_index(obj)
972990
return commit_method(obj, trans)
973991
except AttributeError:
974992
raise ValueError("Database does not support writing.")
975993

976994

977995
def update_family_update_refs(
978-
db_handle: DbWriteBase, obj_old: Family, obj: Family, trans: DbTxn,
996+
db_handle: DbWriteBase,
997+
obj_old: Family,
998+
obj: Family,
999+
trans: DbTxn,
9791000
) -> None:
9801001
"""Update the `family_list` and `parent_family_list` of family members.
9811002

tests/test_endpoints/test_post.py

Lines changed: 120 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from unittest.mock import patch
2828

2929
from gramps.cli.clidbman import CLIDbManager
30+
from gramps.gen.const import GRAMPS_LOCALE as glocale
3031
from gramps.gen.dbstate import DbState
3132

3233
from gramps_webapi.app import create_app
@@ -38,6 +39,8 @@
3839
)
3940
from gramps_webapi.const import ENV_CONFIG_FILE, TEST_AUTH_CONFIG
4041

42+
_ = glocale.translation.gettext
43+
4144

4245
def get_headers(client, user: str, password: str) -> Dict[str, str]:
4346
"""Get the auth headers for a specific user."""
@@ -125,7 +128,12 @@ def test_objects_add_person(self):
125128
"handle": handle_person,
126129
"primary_name": {
127130
"_class": "Name",
128-
"surname_list": [{"_class": "Surname", "surname": "Doe",}],
131+
"surname_list": [
132+
{
133+
"_class": "Surname",
134+
"surname": "Doe",
135+
}
136+
],
129137
"first_name": "John",
130138
},
131139
"event_ref_list": [
@@ -141,7 +149,10 @@ def test_objects_add_person(self):
141149
birth = {
142150
"_class": "Event",
143151
"handle": handle_birth,
144-
"date": {"_class": "Date", "dateval": [2, 10, 1764, False],},
152+
"date": {
153+
"_class": "Date",
154+
"dateval": [2, 10, 1764, False],
155+
},
145156
"type": {"_class": "EventType", "string": "Birth"},
146157
}
147158
objects = [person, birth]
@@ -167,6 +178,82 @@ def test_objects_add_person(self):
167178
[2, 10, 1764, False],
168179
)
169180

181+
def test_objects_add_person_seperate(self):
182+
"""Add a person, then a birth event, check birth ref index."""
183+
handle_person = make_handle()
184+
handle_birth = make_handle()
185+
person = {
186+
"_class": "Person",
187+
"handle": handle_person,
188+
"primary_name": {
189+
"_class": "Name",
190+
"surname_list": [
191+
{
192+
"_class": "Surname",
193+
"surname": "Doe",
194+
}
195+
],
196+
"first_name": "John",
197+
},
198+
# "event_ref_list": [
199+
# {
200+
# "_class": "EventRef",
201+
# "ref": handle_birth,
202+
# "role": {"_class": "EventRoleType", "string": "Primary"},
203+
# },
204+
# ],
205+
# "birth_ref_index": 0,
206+
"gender": 1,
207+
}
208+
birth = {
209+
"_class": "Event",
210+
"handle": handle_birth,
211+
"date": {
212+
"_class": "Date",
213+
"dateval": [2, 10, 1764, False],
214+
},
215+
"type": {"_class": "EventType", "string": _("Birth")},
216+
}
217+
person_birth = {
218+
"_class": "Person",
219+
"handle": handle_person,
220+
"primary_name": {
221+
"_class": "Name",
222+
"surname_list": [
223+
{
224+
"_class": "Surname",
225+
"surname": "Doe",
226+
}
227+
],
228+
"first_name": "John",
229+
},
230+
"event_ref_list": [
231+
{
232+
"_class": "EventRef",
233+
"ref": handle_birth,
234+
"role": {"_class": "EventRoleType", "string": _("Primary")},
235+
},
236+
],
237+
"gender": 1,
238+
}
239+
headers = get_headers(self.client, "admin", "123")
240+
rv = self.client.post("/api/people/", json=person, headers=headers)
241+
self.assertEqual(rv.status_code, 201)
242+
rv = self.client.post("/api/events/", json=birth, headers=headers)
243+
self.assertEqual(rv.status_code, 201)
244+
rv = self.client.get(f"/api/people/{handle_person}", headers=headers)
245+
self.assertEqual(rv.status_code, 200)
246+
person_dict = rv.json
247+
self.assertEqual(person_dict["birth_ref_index"], -1)
248+
rv = self.client.put(
249+
f"/api/people/{handle_person}", json=person_birth, headers=headers
250+
)
251+
self.assertEqual(rv.status_code, 200)
252+
rv = self.client.get(f"/api/people/{handle_person}", headers=headers)
253+
self.assertEqual(rv.status_code, 200)
254+
person_dict = rv.json
255+
self.assertEqual(person_dict["birth_ref_index"], 0)
256+
170257
def test_objects_add_family(self):
171258
"""Add three people and then create a new family."""
172259
handle_father = make_handle()
@@ -275,7 +362,12 @@ def test_objects_errors(self):
275362
"handle": handle_person,
276363
"primary_name": {
277364
"_class": "Name",
278-
"surname_list": [{"_class": "Surname", "surname": "Doe",}],
365+
"surname_list": [
366+
{
367+
"_class": "Surname",
368+
"surname": "Doe",
369+
}
370+
],
279371
"first_name": "John",
280372
},
281373
"event_ref_list": [
@@ -291,7 +383,10 @@ def test_objects_errors(self):
291383
birth = {
292384
"_class": "Event",
293385
"handle": handle_birth,
294-
"date": {"_class": "Date", "dateval": [2, 10, 1764, False],},
386+
"date": {
387+
"_class": "Date",
388+
"dateval": [2, 10, 1764, False],
389+
},
295390
"type": {"_class": "EventType", "string": "Birth"},
296391
}
297392
# erroneously use string as date
@@ -314,7 +409,12 @@ def test_people_add_person(self):
314409
"handle": handle_person,
315410
"primary_name": {
316411
"_class": "Name",
317-
"surname_list": [{"_class": "Surname", "surname": "Doe",}],
412+
"surname_list": [
413+
{
414+
"_class": "Surname",
415+
"surname": "Doe",
416+
}
417+
],
318418
"first_name": "John",
319419
},
320420
"gender": 1,
@@ -432,7 +532,12 @@ def test_search_add_person(self):
432532
"handle": handle_person,
433533
"primary_name": {
434534
"_class": "Name",
435-
"surname_list": [{"_class": "Surname", "surname": "Doe",}],
535+
"surname_list": [
536+
{
537+
"_class": "Surname",
538+
"surname": "Doe",
539+
}
540+
],
436541
"first_name": "John",
437542
},
438543
"event_ref_list": [
@@ -448,7 +553,10 @@ def test_search_add_person(self):
448553
birth = {
449554
"_class": "Event",
450555
"handle": handle_birth,
451-
"date": {"_class": "Date", "dateval": [2, 10, 1764, False],},
556+
"date": {
557+
"_class": "Date",
558+
"dateval": [2, 10, 1764, False],
559+
},
452560
"type": {"_class": "EventType", "string": "Birth"},
453561
}
454562
objects = [person, birth]
@@ -482,7 +590,11 @@ def test_search_locked(self):
482590
with indexer.index(overwrite=False).writer() as writer:
483591
for _ in range(10):
484592
# write 10 objects while index is locked
485-
rv = self.client.post("/api/notes/", json=content, headers=headers,)
593+
rv = self.client.post(
594+
"/api/notes/",
595+
json=content,
596+
headers=headers,
597+
)
486598
self.assertEqual(rv.status_code, 201)
487599
sleep(2) # give the async writer time to flush
488600
rv = self.client.get(f"/api/search/?query={label}", headers=headers)

0 commit comments

Comments
 (0)