Skip to content

Commit 69ca261

Browse files
authored
Fix reports (#483)
* Version -> 1.6.0 * Remove check_fix_default_person * Remove duplicate test * Check if removing test fixes unit tests * Add test again * Try setting date format explicitly * Revert "Try setting date format explicitly" This reverts commit 8dfabbf. * Try removing test * Try renaming tree in test * Try removing check * Restore test * Fix date format
1 parent 5fb71e9 commit 69ca261

File tree

8 files changed

+77
-52
lines changed

8 files changed

+77
-52
lines changed

gramps_webapi/_version.py

Lines changed: 1 addition & 1 deletion
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.5.2"
21+
__version__ = "1.6.0"

gramps_webapi/api/report.py

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
from gramps.gen.const import GRAMPS_LOCALE as glocale
3333
from gramps.gen.db.base import DbReadBase
3434
from gramps.gen.display.name import displayer as name_displayer
35+
from gramps.gen.errors import HandleError
3536
from gramps.gen.filters import reload_custom_filters
3637
from gramps.gen.plug import BasePluginManager
3738
from gramps.gen.plug.docgen import PaperStyle
@@ -76,26 +77,33 @@ def get_report_profile(
7677
"""Extract and return report attributes and options."""
7778
module = plugin_manager.load_plugin(report_data)
7879
option_class = getattr(module, report_data.optionclass)
79-
report = ModifiedCommandLineReport(
80-
db_handle,
81-
report_data.name,
82-
report_data.category,
83-
option_class,
84-
{},
85-
include_options_help=include_options_help,
86-
)
80+
try:
81+
report = ModifiedCommandLineReport(
82+
db_handle,
83+
report_data.name,
84+
report_data.category,
85+
option_class,
86+
{},
87+
include_options_help=include_options_help,
88+
)
89+
options_dict = report.options_dict
90+
except HandleError:
91+
# HandleError can happen in particular on an empty database
92+
# when there is no person yet
93+
report = None
94+
options_dict = {}
8795
result = {
8896
"authors": report_data.authors,
8997
"authors_email": report_data.authors_email,
9098
"category": report_data.category,
9199
"description": report_data.description,
92100
"id": report_data.id,
93101
"name": report_data.name,
94-
"options_dict": report.options_dict,
102+
"options_dict": options_dict,
95103
"report_modes": report_data.report_modes,
96104
"version": report_data.version,
97105
}
98-
if include_options_help:
106+
if include_options_help and report is not None:
99107
options_help = report.options_help
100108
if REPORT_FILTERS:
101109
for report_type in REPORT_FILTERS:

gramps_webapi/api/resources/exporters.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@
4646
)
4747
from . import ProtectedResource
4848
from .emit import GrampsJSONEncoder
49-
from .util import check_fix_default_person
5049

5150

5251
class ExportersResource(ProtectedResource, GrampsJSONEncoder):
@@ -119,8 +118,6 @@ def post(self, args: Dict, extension: str) -> Response:
119118
exporters = get_exporters(extension)
120119
if not exporters:
121120
abort(404)
122-
if has_permissions({PERM_EDIT_OBJ}):
123-
check_fix_default_person(get_db_handle(readonly=False))
124121
tree = get_tree_from_jwt()
125122
# remove JWT from args
126123
options = {k: v for k, v in args.items() if k != "jwt"}
@@ -180,8 +177,6 @@ def get(self, args: Dict, extension: str) -> Response:
180177
exporters = get_exporters(extension)
181178
if not exporters:
182179
abort(404)
183-
if has_permissions({PERM_EDIT_OBJ}):
184-
check_fix_default_person(get_db_handle(readonly=False))
185180
options = prepare_options(db_handle, args)
186181
file_name, file_type = run_export(db_handle, extension, options)
187182
export_path = current_app.config.get("EXPORT_DIR")

gramps_webapi/api/resources/reports.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
from ..util import get_buffer_for_file, get_db_handle, get_tree_from_jwt, use_args
3838
from . import ProtectedResource
3939
from .emit import GrampsJSONEncoder
40-
from .util import check_fix_default_person
4140

4241

4342
class ReportsResource(ProtectedResource, GrampsJSONEncoder):
@@ -46,8 +45,6 @@ class ReportsResource(ProtectedResource, GrampsJSONEncoder):
4645
@use_args({"include_help": fields.Boolean(load_default=False)}, location="query")
4746
def get(self, args: Dict) -> Response:
4847
"""Get all available report attributes."""
49-
if has_permissions({PERM_EDIT_OBJ}):
50-
check_fix_default_person(get_db_handle(readonly=False))
5148
reports = get_reports(
5249
get_db_handle(), include_options_help=args["include_help"]
5350
)
@@ -60,8 +57,6 @@ class ReportResource(ProtectedResource, GrampsJSONEncoder):
6057
@use_args({"include_help": fields.Boolean(load_default=True)}, location="query")
6158
def get(self, args: Dict, report_id: str) -> Response:
6259
"""Get specific report attributes."""
63-
if has_permissions({PERM_EDIT_OBJ}):
64-
check_fix_default_person(get_db_handle(readonly=False))
6560
reports = get_reports(
6661
get_db_handle(),
6762
report_id=report_id,
@@ -96,9 +91,6 @@ def get(self, args: Dict, report_id: str) -> Response:
9691
if "of" in report_options:
9792
abort(422)
9893

99-
if has_permissions({PERM_EDIT_OBJ}):
100-
check_fix_default_person(get_db_handle(readonly=False))
101-
10294
file_name, file_type = run_report(
10395
db_handle=get_db_handle(),
10496
report_id=report_id,
@@ -130,8 +122,6 @@ def post(self, args: Dict, report_id: str) -> Response:
130122
abort(400)
131123
if "of" in report_options:
132124
abort(422)
133-
if has_permissions({PERM_EDIT_OBJ}):
134-
check_fix_default_person(get_db_handle(readonly=False))
135125
tree = get_tree_from_jwt()
136126
task = run_task(
137127
generate_report,

gramps_webapi/api/resources/timeline.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@
2121
from typing import Dict, List, Optional, Set, Tuple, Union
2222

2323
from flask import abort
24+
from gramps.gen.config import config
2425
from gramps.gen.const import GRAMPS_LOCALE as glocale
2526
from gramps.gen.db.base import DbReadBase
2627
from gramps.gen.display.place import PlaceDisplay
2728
from gramps.gen.errors import HandleError
28-
from gramps.gen.lib import Date, Event, EventRef, EventType, Person, Span
29+
from gramps.gen.lib import Date, Event, EventType, Person, Span
2930
from gramps.gen.relationship import get_relationship_calculator
3031
from gramps.gen.utils.alive import probably_alive_range
3132
from gramps.gen.utils.db import (
@@ -473,7 +474,8 @@ def profile(self, page=0, pagesize=20):
473474
if not age:
474475
age = person_age
475476
person["age"] = person_age
476-
477+
date_format = config.get("preferences.date-format")
478+
self.locale.date_displayer.set_format(date_format)
477479
profile = {
478480
"date": self.locale.date_displayer.display(event.date),
479481
"description": event.description,

gramps_webapi/api/resources/util.py

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,24 +1226,3 @@ def dry_run_import(
12261226
"notes": db_handle.get_number_of_notes(),
12271227
"tags": db_handle.get_number_of_tags(),
12281228
}
1229-
1230-
1231-
def check_fix_default_person(db_handle: Union[DbReadBase, DbWriteBase]) -> None:
1232-
"""If the db is writable, check if the default person still exists.
1233-
1234-
If it doesn't exist, set the default person to None.
1235-
"""
1236-
if not isinstance(db_handle, DbWriteBase):
1237-
# not writable
1238-
return None
1239-
handle = db_handle.get_default_handle()
1240-
if not handle:
1241-
# default person is already empty
1242-
return None
1243-
if db_handle.has_person_handle(handle):
1244-
# default person exists
1245-
return None
1246-
# OK, we have a problem - default person is not empty but does not exist
1247-
# - set to empty
1248-
db_handle.set_default_person_handle(None)
1249-
return None

gramps_webapi/data/apispec.yaml

Lines changed: 1 addition & 1 deletion
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.5.2" # make sure to match this version with the one in _version.py
11+
version: "1.6.0" # 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"

tests/test_endpoints/test_reports.py

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,23 @@
1919

2020
"""Tests for the /api/reports endpoints using example_gramps."""
2121

22+
import os
2223
import unittest
2324
from mimetypes import types_map
25+
from unittest.mock import patch
2426

25-
from gramps_webapi.const import REPORT_DEFAULTS
27+
from gramps.cli.clidbman import CLIDbManager
28+
from gramps.gen.dbstate import DbState
2629

27-
from . import BASE_URL, get_test_client
30+
from gramps_webapi.app import create_app
31+
from gramps_webapi.auth import add_user, user_db
32+
from gramps_webapi.const import (
33+
ENV_CONFIG_FILE,
34+
REPORT_DEFAULTS,
35+
TEST_EMPTY_GRAMPS_AUTH_CONFIG,
36+
)
37+
38+
from . import BASE_URL, TEST_USERS, get_test_client
2839
from .checks import (
2940
check_conforms_to_schema,
3041
check_invalid_semantics,
@@ -257,3 +268,43 @@ def test_post_reports_report_id_file_one_of_each(self):
257268
if rv.status_code != 200:
258269
bad_reports.append(report["id"])
259270
self.assertEqual(bad_reports, [])
271+
272+
273+
class TestReportsEmptyDatabase(unittest.TestCase):
274+
"""Test cases for the /api/reports/ endpoint with an empty database."""
275+
276+
@classmethod
277+
def setUpClass(cls):
278+
"""Test class setup."""
279+
cls.name = "empty2"
280+
cls.dbman = CLIDbManager(DbState())
281+
cls.dbpath, _name = cls.dbman.create_new_db_cli(cls.name, dbid="sqlite")
282+
cls.dbman.create_new_db_cli(cls.name, dbid="sqlite")
283+
with patch.dict(
284+
"os.environ",
285+
{
286+
ENV_CONFIG_FILE: TEST_EMPTY_GRAMPS_AUTH_CONFIG,
287+
"TREE": cls.name,
288+
},
289+
):
290+
cls.test_app = create_app()
291+
cls.test_app.config["TESTING"] = True
292+
cls.client = cls.test_app.test_client()
293+
cls.tree = os.path.basename(cls.dbpath)
294+
with cls.test_app.app_context():
295+
user_db.create_all()
296+
for role in TEST_USERS:
297+
add_user(
298+
name=TEST_USERS[role]["name"],
299+
password=TEST_USERS[role]["password"],
300+
role=role,
301+
)
302+
303+
@classmethod
304+
def tearDownClass(cls):
305+
cls.dbman.remove_database(cls.name)
306+
307+
def test_reports_empty_db(self):
308+
"""Test that importers are loaded also for a fresh db."""
309+
rv = check_success(self, TEST_URL)
310+
assert len(rv) > 0

0 commit comments

Comments
 (0)