Skip to content

Commit c395868

Browse files
committed
Introduce gramps_object_from_dict
1 parent cb8dc30 commit c395868

File tree

5 files changed

+61
-8
lines changed

5 files changed

+61
-8
lines changed

gramps_webapi/api/resources/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@
4545
from ..search import SearchIndexer, get_search_indexer, get_semantic_search_indexer
4646
from ..util import (
4747
check_quota_people,
48-
from_json_legacy,
4948
get_db_handle,
5049
get_locale_for_language,
5150
get_tree_from_jwt_or_fail,
51+
gramps_object_from_dict,
5252
update_usage_people,
5353
use_args,
5454
)
@@ -163,7 +163,7 @@ def _parse_object(self) -> GrampsObject:
163163
abort_with_message(400, f"Error while processing object: {exc}")
164164
if not validate_object_dict(obj_dict):
165165
abort_with_message(400, "Schema validation failed")
166-
return from_json_legacy(json.dumps(obj_dict))
166+
return gramps_object_from_dict(obj_dict)
167167

168168
def has_handle(self, handle: str) -> bool:
169169
"""Check if the handle exists in the database."""

gramps_webapi/api/resources/objects.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@
4141
from ..util import (
4242
abort_with_message,
4343
check_quota_people,
44-
from_json_legacy,
4544
get_db_handle,
4645
get_tree_from_jwt,
46+
gramps_object_from_dict,
4747
update_usage_people,
4848
use_args,
4949
)
@@ -71,7 +71,7 @@ def _parse_objects(self) -> Sequence[GrampsObject]:
7171
abort_with_message(400, "Error processing objects")
7272
if not validate_object_dict(obj_dict):
7373
abort_with_message(400, "Validation error while processing objects")
74-
obj = from_json_legacy(json.dumps(obj_dict))
74+
obj = gramps_object_from_dict(obj_dict)
7575
objects.append(obj)
7676
return objects
7777

gramps_webapi/api/tasks.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
from gramps.gen.db import DbTxn
3333
from gramps.gen.db.base import DbReadBase
3434
from gramps.gen.errors import HandleError
35-
from gramps.gen.lib.json_utils import data_to_object, object_to_dict
35+
from gramps.gen.lib.json_utils import object_to_dict
3636
from gramps.gen.merge.diff import diff_items
3737

3838
from gramps_webapi.api.search.indexer import SearchIndexer, SemanticSearchIndexer
@@ -57,9 +57,9 @@
5757
abort_with_message,
5858
check_quota_people,
5959
close_db,
60-
complete_gramps_object_dict,
6160
get_config,
6261
get_db_outside_request,
62+
gramps_object_from_dict,
6363
send_email,
6464
update_usage_people,
6565
upgrade_gramps_database,
@@ -493,7 +493,7 @@ def process_transactions(
493493
abort_with_message(409, "Object has changed")
494494
new_data = item["new"]
495495
if new_data:
496-
new_obj = data_to_object(complete_gramps_object_dict(new_data))
496+
new_obj = gramps_object_from_dict(new_data)
497497
if trans_type == "delete":
498498
handle_delete(trans, class_name, handle)
499499
if (

gramps_webapi/api/util.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
from gramps.gen.db.exceptions import DbUpgradeRequiredError
6767
from gramps.gen.dbstate import DbState
6868
from gramps.gen.errors import HandleError
69-
from gramps.gen.lib.json_utils import object_to_dict
69+
from gramps.gen.lib.json_utils import data_to_object, object_to_dict
7070
from gramps.gen.proxy import PrivateProxyDb
7171
from gramps.gen.proxy.private import sanitize_media
7272
from gramps.gen.proxy.proxybase import ProxyDbBase
@@ -826,3 +826,11 @@ def complete_gramps_object_dict(data: dict[str, Any]):
826826
if key not in data:
827827
data[key] = value
828828
return data
829+
830+
831+
def gramps_object_from_dict(data: dict[str, Any]):
832+
"""Instantiate a Gramps object from a dictionary.
833+
834+
The dictionary can be incomplete, i.e. not contain all properties of
835+
the object class."""
836+
return data_to_object(complete_gramps_object_dict(data))

tests/test_endpoints/test_transactions.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,3 +398,48 @@ def test_missing_gramps_id(self):
398398
"/api/transactions/?background=1", json=trans, headers=headers
399399
)
400400
self.assertEqual(rv.status_code, 400)
401+
402+
def test_gramps60_issue(self):
403+
"""Test for an issue that occurred after upgrading to Gramps 6.0"""
404+
obj_dict = {
405+
"handle": "fe171ce847937f3212613fb24c1",
406+
"change": 1746095141,
407+
"private": False,
408+
"tag_list": [],
409+
"gramps_id": "E4403",
410+
"citation_list": [],
411+
"note_list": [],
412+
"media_list": [],
413+
"attribute_list": [],
414+
"date": {
415+
"format": None,
416+
"calendar": 0,
417+
"modifier": 0,
418+
"quality": 0,
419+
"dateval": [2, 2, 1991, False],
420+
"text": "1991-02-02",
421+
"sortval": 2448290,
422+
"newyear": 0,
423+
"_class": "Date",
424+
},
425+
"place": "",
426+
"_class": "Event",
427+
"type": {"_class": "EventType", "value": 11, "string": ""},
428+
"description": "Testereignis",
429+
}
430+
trans = [
431+
{
432+
"type": "add",
433+
"_class": "Event",
434+
"handle": obj_dict["handle"],
435+
"old": None,
436+
"new": obj_dict,
437+
}
438+
]
439+
headers = get_headers(self.client, "editor", "123")
440+
# add
441+
rv = self.client.post("/api/transactions/", json=trans, headers=headers)
442+
assert rv.status_code == 200
443+
rv = self.client.get(f"/api/events/{obj_dict['handle']}", headers=headers)
444+
assert rv.status_code == 200
445+
assert rv.json["type"] == "Adopted"

0 commit comments

Comments
 (0)