Skip to content

Commit

Permalink
Use CachePeopleFamiliesProxy in dna and relations endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidMStraub committed Jan 2, 2025
1 parent 438e7d8 commit 6327cac
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
12 changes: 10 additions & 2 deletions gramps_webapi/api/resources/dna.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#
# Copyright (C) 2020 Nick Hall
# Copyright (C) 2020-2023 Gary Griffin
# Copyright (C) 2023 David Straub
# Copyright (C) 2023-2025 David Straub
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
Expand Down Expand Up @@ -32,6 +32,8 @@
from gramps.gen.utils.grampslocale import GrampsLocale
from webargs import fields, validate

from gramps_webapi.api.people_families_cache import CachePeopleFamiliesProxy

from ...types import Handle
from ..util import get_db_handle, get_locale_for_language, use_args
from .util import get_person_profile_for_handle
Expand All @@ -57,12 +59,18 @@ class PersonDnaMatchesResource(ProtectedResource):
)
def get(self, args: Dict, handle: str):
"""Get the DNA match data."""
db_handle = get_db_handle()
db_handle = CachePeopleFamiliesProxy(get_db_handle())

try:
person = db_handle.get_person_from_handle(handle)
except HandleError:
abort(404)

db_handle.cache_people()
db_handle.cache_families()

locale = get_locale_for_language(args["locale"], default=True)

matches = []
for association in person.get_person_ref_list():
if association.get_relation() == "DNA":
Expand Down
51 changes: 32 additions & 19 deletions gramps_webapi/api/resources/relations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Gramps Web API - A RESTful API for the Gramps genealogy program
#
# Copyright (C) 2020 Christopher Horn
# Copyright (C) 2025 David Straub
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
Expand All @@ -21,16 +22,18 @@

from typing import Dict

from flask import Response, abort
from flask import Response
from gramps.gen.errors import HandleError
from gramps.gen.relationship import get_relationship_calculator
from webargs import fields, validate

from gramps_webapi.api.people_families_cache import CachePeopleFamiliesProxy

from ...types import Handle
from ..util import use_args
from ..util import get_db_handle, get_locale_for_language
from ..util import get_db_handle, get_locale_for_language, use_args, abort_with_message
from . import ProtectedResource
from .emit import GrampsJSONEncoder
from .util import get_one_relationship, get_person_by_handle
from .util import get_one_relationship


class RelationResource(ProtectedResource, GrampsJSONEncoder):
Expand All @@ -47,14 +50,18 @@ class RelationResource(ProtectedResource, GrampsJSONEncoder):
)
def get(self, args: Dict, handle1: Handle, handle2: Handle) -> Response:
"""Get the most direct relationship between two people."""
db_handle = get_db_handle()
person1 = get_person_by_handle(db_handle, handle1)
if person1 == {}:
abort(404)
db_handle = CachePeopleFamiliesProxy(get_db_handle())
try:
person1 = db_handle.get_person_from_handle(handle1)
except HandleError:
abort_with_message(404, f"Person {handle1} not found")
try:
person2 = db_handle.get_person_from_handle(handle2)
except HandleError:
abort_with_message(404, f"Person {handle2} not found")

person2 = get_person_by_handle(db_handle, handle2)
if person2 == {}:
abort(404)
db_handle.cache_people()
db_handle.cache_families()

locale = get_locale_for_language(args["locale"], default=True)
data = get_one_relationship(
Expand Down Expand Up @@ -88,14 +95,20 @@ class RelationsResource(ProtectedResource, GrampsJSONEncoder):
)
def get(self, args: Dict, handle1: Handle, handle2: Handle) -> Response:
"""Get all possible relationships between two people."""
db_handle = get_db_handle()
person1 = get_person_by_handle(db_handle, handle1)
if person1 == {}:
abort(404)

person2 = get_person_by_handle(db_handle, handle2)
if person2 == {}:
abort(404)
db_handle = CachePeopleFamiliesProxy(get_db_handle())

try:
person1 = db_handle.get_person_from_handle(handle1)
except HandleError:
abort_with_message(404, f"Person {handle1} not found")

try:
person2 = db_handle.get_person_from_handle(handle2)
except HandleError:
abort_with_message(404, f"Person {handle2} not found")

db_handle.cache_people()
db_handle.cache_families()

locale = get_locale_for_language(args["locale"], default=True)
calc = get_relationship_calculator(reinit=True, clocale=locale)
Expand Down

0 comments on commit 6327cac

Please sign in to comment.