1
0
mirror of https://github.com/ilri/dspace-statistics-api.git synced 2024-12-22 20:52:19 +01:00

dspace_statistics_api/app.py: Refactor for testability

I thought it was clever to only import these in the on_post handler
because they aren't needed elsewhere, but it turns out that this is
not a common pattern and even causes problems with testability.

First, if the imports are at the top of the file as PEP8 recommends,
then the WSGI server will import them once when it loads the app and
they remain in memory for the lifecycle of the app. If the imports
are in the on_post handler they would be re-imported on every request!

Second, this pattern of importing in a method makes it tricky to use
object patching in mocks.

See: https://www.python.org/dev/peps/pep-0008/#imports
This commit is contained in:
Alan Orth 2020-10-05 20:43:50 +03:00
parent 3a98de78e3
commit d4518d62ad
Signed by: alanorth
GPG Key ID: 0FB860CC9C45B1B9

View File

@ -1,6 +1,8 @@
import falcon
from .database import DatabaseManager
from .items import get_views
from .items import get_downloads
from .util import validate_items_post_parameters
@ -60,9 +62,6 @@ class AllItemsResource:
def on_post(self, req, resp):
"""Handles POST requests"""
from .items import get_views
from .items import get_downloads
# Build the Solr date string, ie: [* TO *]
if req.context.dateFrom and req.context.dateTo:
solr_date_string = f"[{req.context.dateFrom} TO {req.context.dateTo}]"