Skip to content

Commit 0188c19

Browse files
authored
Fixes for the case where the db default person is gone (#401)
* Fixes for the case where the db default person is gone * Remove missing import * Version -> 1.1.8
1 parent b17e0eb commit 0188c19

File tree

6 files changed

+42
-3
lines changed

6 files changed

+42
-3
lines changed

gramps_webapi/_version.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@
1818
#
1919

2020
# make sure to match this version with the one in apispec.yaml
21-
__version__ = "1.1.7"
21+
__version__ = "1.1.8"

gramps_webapi/api/resources/exporters.py

+3
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
)
4747
from . import ProtectedResource
4848
from .emit import GrampsJSONEncoder
49+
from .util import check_fix_default_person
4950

5051

5152
class ExportersResource(ProtectedResource, GrampsJSONEncoder):
@@ -118,6 +119,8 @@ def post(self, args: Dict, extension: str) -> Response:
118119
exporters = get_exporters(extension)
119120
if not exporters:
120121
abort(404)
122+
if has_permissions({PERM_EDIT_OBJ}):
123+
check_fix_default_person(get_db_handle(readonly=False))
121124
tree = get_tree_from_jwt()
122125
# remove JWT from args
123126
options = {k: v for k, v in args.items() if k != "jwt"}

gramps_webapi/api/resources/reports.py

+11-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
from flask import Response, abort, current_app, jsonify, send_file
3030
from webargs import fields, validate
3131

32-
from ...auth.const import PERM_VIEW_PRIVATE
32+
from ...auth.const import PERM_EDIT_OBJ, PERM_VIEW_PRIVATE
3333
from ...const import MIME_TYPES
3434
from ..auth import has_permissions
3535
from ..report import check_report_id_exists, get_reports, run_report
@@ -42,6 +42,7 @@
4242
)
4343
from . import ProtectedResource
4444
from .emit import GrampsJSONEncoder
45+
from .util import check_fix_default_person
4546

4647

4748
class ReportsResource(ProtectedResource, GrampsJSONEncoder):
@@ -50,6 +51,8 @@ class ReportsResource(ProtectedResource, GrampsJSONEncoder):
5051
@use_args({}, location="query")
5152
def get(self, args: Dict) -> Response:
5253
"""Get all available report attributes."""
54+
if has_permissions({PERM_EDIT_OBJ}):
55+
check_fix_default_person(get_db_handle(readonly=False))
5356
reports = get_reports(get_db_handle())
5457
return self.response(200, reports)
5558

@@ -60,6 +63,8 @@ class ReportResource(ProtectedResource, GrampsJSONEncoder):
6063
@use_args({}, location="query")
6164
def get(self, args: Dict, report_id: str) -> Response:
6265
"""Get specific report attributes."""
66+
if has_permissions({PERM_EDIT_OBJ}):
67+
check_fix_default_person(get_db_handle(readonly=False))
6368
reports = get_reports(get_db_handle(), report_id=report_id)
6469
if not reports:
6570
abort(404)
@@ -90,6 +95,9 @@ def get(self, args: Dict, report_id: str) -> Response:
9095
if "of" in report_options:
9196
abort(422)
9297

98+
if has_permissions({PERM_EDIT_OBJ}):
99+
check_fix_default_person(get_db_handle(readonly=False))
100+
93101
file_name, file_type = run_report(
94102
db_handle=get_db_handle(),
95103
report_id=report_id,
@@ -121,6 +129,8 @@ def post(self, args: Dict, report_id: str) -> Response:
121129
abort(400)
122130
if "of" in report_options:
123131
abort(422)
132+
if has_permissions({PERM_EDIT_OBJ}):
133+
check_fix_default_person(get_db_handle(readonly=False))
124134
tree = get_tree_from_jwt()
125135
task = run_task(
126136
generate_report,

gramps_webapi/api/resources/transactions.py

+5
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@ def post(self, args) -> Response:
9090
new_obj = from_json(json.dumps(new_data))
9191
if trans_type == "delete":
9292
self.handle_delete(trans, class_name, handle)
93+
if (
94+
class_name == "Person"
95+
and handle == db_handle.get_default_handle()
96+
):
97+
db_handle.set_default_person_handle(None)
9398
elif trans_type == "add":
9499
self.handle_add(trans, class_name, new_obj)
95100
elif trans_type == "update":

gramps_webapi/api/resources/util.py

+21
Original file line numberDiff line numberDiff line change
@@ -1303,3 +1303,24 @@ def run_import_media_archive(
13031303
"uploaded": len(to_upload) - num_failures,
13041304
"failures": num_failures,
13051305
}
1306+
1307+
1308+
def check_fix_default_person(db_handle: Union[DbReadBase, DbWriteBase]) -> None:
1309+
"""If the db is writable, check if the default person still exists.
1310+
1311+
If it doesn't exist, set the default person to None.
1312+
"""
1313+
if not isinstance(db_handle, DbWriteBase):
1314+
# not writable
1315+
return None
1316+
handle = db_handle.get_default_handle()
1317+
if not handle:
1318+
# default person is already empty
1319+
return None
1320+
if db_handle.has_person_handle(handle):
1321+
# default person exists
1322+
return None
1323+
# OK, we have a problem - default person is not empty but does not exist
1324+
# - set to empty
1325+
db_handle.set_default_person_handle(None)
1326+
return None

gramps_webapi/data/apispec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ info:
88
99
1010
* More about Gramps and the numerous features it provides for genealogists can be found at https://gramps-project.org
11-
version: "1.1.7" # make sure to match this version with the one in _version.py
11+
version: "1.1.8" # make sure to match this version with the one in _version.py
1212
license:
1313
name: "GNU Affero General Public License v3.0"
1414
url: "http://www.gnu.org/licenses/agpl-3.0.html"

0 commit comments

Comments
 (0)