Skip to content

Commit 9a7563b

Browse files
authored
Refactor extend to accept keyword list, fix emit bug (Closes Issue #33) (#50)
* Refactor extend to accept keyword list, fix emit bug * Modify extend to require at least one key
1 parent 3051821 commit 9a7563b

File tree

8 files changed

+310
-100
lines changed

8 files changed

+310
-100
lines changed

gramps_webapi/api/resources/base.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def gramps_class_name(self):
2828
def object_extend(self, obj: GrampsObject, args: Dict) -> GrampsObject:
2929
"""Extend the base object attributes as needed."""
3030
if "extend" in args:
31-
obj.extended = get_extended_attributes(self.db_handle, obj)
31+
obj.extended = get_extended_attributes(self.db_handle, obj, args)
3232
return obj
3333

3434
@property
@@ -60,7 +60,7 @@ class GrampsObjectResource(GrampsObjectResourceHelper, Resource):
6060
"keys": fields.DelimitedList(fields.Str(), missing=[]),
6161
"skipkeys": fields.DelimitedList(fields.Str(), missing=[]),
6262
"profile": fields.Str(validate=validate.Length(equal=0)),
63-
"extend": fields.Str(validate=validate.Length(equal=0)),
63+
"extend": fields.DelimitedList(fields.Str(validate=validate.Length(min=1))),
6464
},
6565
location="query",
6666
)
@@ -79,12 +79,11 @@ class GrampsObjectsResource(GrampsObjectResourceHelper, Resource):
7979
@use_args(
8080
{
8181
"gramps_id": fields.Str(validate=validate.Length(min=1)),
82-
"handle": fields.Str(validate=validate.Length(min=1)),
8382
"strip": fields.Str(validate=validate.Length(equal=0)),
8483
"keys": fields.DelimitedList(fields.Str(), missing=[]),
8584
"skipkeys": fields.DelimitedList(fields.Str(), missing=[]),
8685
"profile": fields.Str(validate=validate.Length(equal=0)),
87-
"extend": fields.Str(validate=validate.Length(equal=0)),
86+
"extend": fields.DelimitedList(fields.Str(validate=validate.Length(min=1))),
8887
"filter": fields.Str(validate=validate.Length(min=1)),
8988
"rules": fields.Str(validate=validate.Length(min=1)),
9089
},

gramps_webapi/api/resources/citation.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ def object_extend(self, obj: Citation, args: Dict) -> Citation:
2121
"""Extend citation attributes as needed."""
2222
if "extend" in args:
2323
db_handle = self.db_handle
24-
obj.extended = get_extended_attributes(db_handle, obj)
25-
obj.extended["source"] = get_source_by_handle(db_handle, obj.source_handle)
24+
obj.extended = get_extended_attributes(db_handle, obj, args)
25+
if "all" in args["extend"] or "source" in args["extend"]:
26+
obj.extended["source"] = get_source_by_handle(
27+
db_handle, obj.source_handle, args
28+
)
2629
return obj
2730

2831

gramps_webapi/api/resources/emit.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,26 @@ def __init__(self):
2727
]
2828

2929
def response(
30-
self, status: int = 200, payload: Any = {}, args: Optional[Dict] = None
30+
self,
31+
status: int = 200,
32+
payload: Optional[Any] = None,
33+
args: Optional[Dict] = None,
3134
) -> Response:
3235
"""Prepare response."""
36+
payload = payload or {}
3337
args = args or {}
3438
if "strip" in args:
3539
self.strip_empty_keys = True
3640
else:
3741
self.strip_empty_keys = False
3842
if "keys" in args:
3943
self.filter_only_keys = args["keys"]
44+
else:
45+
self.filter_only_keys = []
4046
if "skipkeys" in args:
4147
self.filter_skip_keys = args["skipkeys"]
48+
else:
49+
self.filter_skip_keys = []
4250

4351
return Response(
4452
response=self.encode(payload),
@@ -68,6 +76,11 @@ def is_null(value: Any) -> bool:
6876
):
6977
apply_filter = True
7078
for key, value in obj.__class__.__dict__.items():
79+
if apply_filter:
80+
if self.filter_only_keys and key not in self.filter_only_keys:
81+
continue
82+
if self.filter_skip_keys and key in self.filter_skip_keys:
83+
continue
7184
if isinstance(value, property):
7285
data[key] = getattr(obj, key)
7386
for key, value in obj.__dict__.items():

gramps_webapi/api/resources/event.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ def object_extend(self, obj: Event, args: Dict) -> Event:
2727
if "profile" in args:
2828
obj.profile = get_event_profile_for_object(db_handle, obj)
2929
if "extend" in args:
30-
obj.extended = get_extended_attributes(db_handle, obj)
31-
obj.extended["place"] = get_place_by_handle(db_handle, obj.place)
30+
obj.extended = get_extended_attributes(db_handle, obj, args)
31+
if "all" in args["extend"] or "place" in args["extend"]:
32+
obj.extended["place"] = get_place_by_handle(db_handle, obj.place)
3233
return obj
3334

3435

gramps_webapi/api/resources/family.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,15 @@ def object_extend(self, obj: Family, args: Dict) -> Family:
2929
db_handle, obj, with_events=True
3030
)
3131
if "extend" in args:
32-
obj.extended = get_extended_attributes(db_handle, obj)
33-
obj.extended["father"] = get_person_by_handle(db_handle, obj.father_handle)
34-
obj.extended["mother"] = get_person_by_handle(db_handle, obj.mother_handle)
32+
obj.extended = get_extended_attributes(db_handle, obj, args)
33+
if "all" in args["extend"] or "father" in args["extend"]:
34+
obj.extended["father"] = get_person_by_handle(
35+
db_handle, obj.father_handle
36+
)
37+
if "all" in args["extend"] or "mother" in args["extend"]:
38+
obj.extended["mother"] = get_person_by_handle(
39+
db_handle, obj.mother_handle
40+
)
3541
return obj
3642

3743

gramps_webapi/api/resources/person.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,22 +29,21 @@ def object_extend(self, obj: Person, args: Dict) -> Person:
2929
db_handle, obj, with_family=True, with_events=True
3030
)
3131
if "extend" in args:
32-
obj.extended = get_extended_attributes(db_handle, obj)
33-
obj.extended.update(
34-
{
35-
"families": [
36-
get_family_by_handle(db_handle, handle)
37-
for handle in obj.family_list
38-
],
39-
"parent_families": [
40-
get_family_by_handle(db_handle, handle)
41-
for handle in obj.parent_family_list
42-
],
43-
"primary_parent_family": get_family_by_handle(
44-
db_handle, obj.get_main_parents_family_handle()
45-
),
46-
}
47-
)
32+
obj.extended = get_extended_attributes(db_handle, obj, args)
33+
if "all" in args["extend"] or "families" in args["extend"]:
34+
obj.extended["families"] = [
35+
get_family_by_handle(db_handle, handle)
36+
for handle in obj.family_list
37+
]
38+
if "all" in args["extend"] or "parent_families" in args["extend"]:
39+
obj.extended["parent_families"] = [
40+
get_family_by_handle(db_handle, handle)
41+
for handle in obj.parent_family_list
42+
]
43+
if "all" in args["extend"] or "primary_parent_family" in args["extend"]:
44+
obj.extended["primary_parent_family"] = get_family_by_handle(
45+
db_handle, obj.get_main_parents_family_handle()
46+
)
4847
return obj
4948

5049

gramps_webapi/api/resources/util.py

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,31 @@ def get_place_by_handle(db_handle: DbReadBase, handle: Handle) -> Optional[Place
4141

4242

4343
def get_family_by_handle(
44-
db_handle: DbReadBase, handle: Handle, extended: bool = False
44+
db_handle: DbReadBase, handle: Handle, args: Optional[Dict] = None
4545
) -> Optional[Family]:
46-
"""Get a family and all extended attributes."""
46+
"""Get a family and optional extended attributes."""
4747
try:
4848
obj = db_handle.get_family_from_handle(handle)
4949
except HandleError:
5050
return None
51-
if extended:
52-
obj.extended = get_extended_attributes(db_handle, obj)
53-
obj.extended["father"] = get_person_by_handle(db_handle, obj.father_handle)
54-
obj.extended["mother"] = get_person_by_handle(db_handle, obj.mother_handle)
51+
args = args or {}
52+
if "extend" in args:
53+
obj.extended = get_extended_attributes(db_handle, obj, args)
54+
if "all" in args["extend"] or "father" in args["extend"]:
55+
obj.extended["father"] = get_person_by_handle(db_handle, obj.father_handle)
56+
if "all" in args["extend"] or "mother" in args["extend"]:
57+
obj.extended["mother"] = get_person_by_handle(db_handle, obj.mother_handle)
5558
return obj
5659

5760

58-
def get_source_by_handle(db_handle: DbReadBase, handle: Handle) -> Source:
59-
"""Get a source and all extended attributes."""
61+
def get_source_by_handle(
62+
db_handle: DbReadBase, handle: Handle, args: Optional[Dict] = None
63+
) -> Source:
64+
"""Get a source and optional extended attributes."""
65+
args = args or {}
6066
obj = db_handle.get_source_from_handle(handle)
61-
obj.extended = get_extended_attributes(db_handle, obj)
67+
if "extend" in args:
68+
obj.extended = get_extended_attributes(db_handle, obj, args)
6269
return obj
6370

6471

@@ -216,43 +223,55 @@ def get_family_profile_for_handle(
216223
return get_family_profile_for_object(db_handle, obj, with_events)
217224

218225

219-
def get_extended_attributes(db_handle: DbReadBase, obj: GrampsObject) -> Dict:
226+
def get_extended_attributes(
227+
db_handle: DbReadBase, obj: GrampsObject, args: Optional[Dict] = None
228+
) -> Dict:
220229
"""Get extended attributes for a GrampsObject."""
230+
args = args or {}
221231
result = {}
222-
if hasattr(obj, "child_ref_list"):
232+
do_all = False
233+
if "all" in args["extend"]:
234+
do_all = True
235+
if (do_all or "child_ref_list" in args["extend"]) and hasattr(
236+
obj, "child_ref_list"
237+
):
223238
result["children"] = [
224239
db_handle.get_person_from_handle(child_ref.ref)
225240
for child_ref in obj.child_ref_list
226241
]
227-
if hasattr(obj, "citation_list"):
242+
if (do_all or "citation_list" in args["extend"]) and hasattr(obj, "citation_list"):
228243
result["citations"] = [
229244
db_handle.get_citation_from_handle(handle) for handle in obj.citation_list
230245
]
231-
if hasattr(obj, "event_ref_list"):
246+
if (do_all or "event_ref_list" in args["extend"]) and hasattr(
247+
obj, "event_ref_list"
248+
):
232249
result["events"] = [
233250
db_handle.get_event_from_handle(event_ref.ref)
234251
for event_ref in obj.event_ref_list
235252
]
236-
if hasattr(obj, "media_list"):
253+
if (do_all or "media_list" in args["extend"]) and hasattr(obj, "media_list"):
237254
result["media"] = [
238255
db_handle.get_media_from_handle(media_ref.ref)
239256
for media_ref in obj.media_list
240257
]
241-
if hasattr(obj, "note_list"):
258+
if (do_all or "note_list" in args["extend"]) and hasattr(obj, "note_list"):
242259
result["notes"] = [
243260
db_handle.get_note_from_handle(handle) for handle in obj.note_list
244261
]
245-
if hasattr(obj, "person_ref_list"):
262+
if (do_all or "person_ref_list" in args["extend"]) and hasattr(
263+
obj, "person_ref_list"
264+
):
246265
result["people"] = [
247266
db_handle.get_person_from_handle(person_ref.ref)
248267
for person_ref in obj.person_ref_list
249268
]
250-
if hasattr(obj, "reporef_list"):
269+
if (do_all or "reporef_list" in args["extend"]) and hasattr(obj, "reporef_list"):
251270
result["repositories"] = [
252271
db_handle.get_repository_from_handle(repo_ref.ref)
253272
for repo_ref in obj.reporef_list
254273
]
255-
if hasattr(obj, "tag_list"):
274+
if (do_all or "tag_list" in args["extend"]) and hasattr(obj, "tag_list"):
256275
result["tags"] = [
257276
db_handle.get_tag_from_handle(handle) for handle in obj.tag_list
258277
]

0 commit comments

Comments
 (0)