Skip to content

Commit

Permalink
Merge abb2cc6 into 43b0ec8
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad-bm committed Sep 11, 2019
2 parents 43b0ec8 + abb2cc6 commit aafa4c6
Show file tree
Hide file tree
Showing 68 changed files with 1,886 additions and 763 deletions.
2 changes: 1 addition & 1 deletion invenio_app_ils/circulation/stats/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def fetch_most_loaned_documents(from_date, to_date, bucket_size):
extensions=loan_extensions
)

# Enchance the document serializer
# Enhance the document serializer
doc_search = DocumentSearch()
doc_search = doc_search.with_preference_param().params(version=True)
doc_search = doc_search.search_by_pid(*document_pids)
Expand Down
54 changes: 38 additions & 16 deletions invenio_app_ils/circulation/stats/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,29 @@

from datetime import datetime

from flask import Blueprint, request
from flask import Blueprint, current_app, request
from invenio_pidstore import current_pidstore
from invenio_records_rest.utils import obj_or_import_string
from invenio_rest import ContentNegotiatedMethodView

from invenio_app_ils.circulation.stats.api import fetch_most_loaned_documents
from invenio_app_ils.circulation.views import need_permissions
from invenio_app_ils.errors import InvalidParameterError
from invenio_app_ils.pidstore.pids import DOCUMENT_PID_FETCHER, \
DOCUMENT_PID_TYPE

from invenio_app_ils.pidstore.pids import ( # isort:skip
DOCUMENT_PID_FETCHER,
DOCUMENT_PID_TYPE,
)


def create_most_loaned_documents_view(blueprint, app):
"""Add url rule for most loaned documents."""
endpoints = app.config.get("RECORDS_REST_ENDPOINTS", [])
document_endpoint = endpoints.get(DOCUMENT_PID_TYPE, {})

default_media_type = document_endpoint.get("default_media_type", "")
search_serializers_aliases = document_endpoint.get(
"search_serializers_aliases", ""
)
search_serializers = document_endpoint.get("search_serializers")
serializers = {
mime: obj_or_import_string(func)
Expand All @@ -40,9 +46,11 @@ def create_most_loaned_documents_view(blueprint, app):
"/circulation/stats/most-loaned",
view_func=view_class.as_view(
view_class.view_name,
serializers=serializers
serializers=serializers,
serializers_query_aliases=search_serializers_aliases,
default_media_type=default_media_type,
),
methods=["GET"]
methods=["GET"],
)


Expand All @@ -59,16 +67,32 @@ class MostLoanedDocumentsResource(ContentNegotiatedMethodView):
"""Statistics view for the documents with the most loans."""

view_name = "most-loaned"
bucket_size = 30
default_bucket_size = 30

def __init__(self, *args, **kwargs):
"""Constructor."""
super(MostLoanedDocumentsResource, self).__init__(*args, **kwargs)
endpoints = current_app.config.get("RECORDS_REST_ENDPOINTS", [])
document_endpoint = endpoints.get(DOCUMENT_PID_TYPE, {})
self.max_result_window = document_endpoint.get(
"max_result_window", 10000
)

def _validate_bucket_size(self):
"""Validate bucket size parameter."""
size_param = request.args.get("size", self.bucket_size)
size_param = request.args.get("size", self.default_bucket_size)
try:
return int(size_param)
value = int(size_param)
except ValueError:
msg = "Parameter 'size' is invalid: {}".format(size_param)
msg = "Parameter `size` is not a number: {}".format(size_param)
raise InvalidParameterError(description=msg)

if value >= self.max_result_window:
msg = "Parameter `size` should be lower than {}".format(
self.max_result_window
)
raise InvalidParameterError(description=msg)
return value

def _validate_start_date_range(self):
"""Validate start date range parameters."""
Expand All @@ -86,9 +110,9 @@ def validate_date(param, date):
to_date_obj = None

if from_date:
from_date_obj = validate_date('from_date', from_date)
from_date_obj = validate_date("from_date", from_date)
if to_date:
to_date_obj = validate_date('to_date', to_date)
to_date_obj = validate_date("to_date", to_date)

if from_date_obj and to_date_obj and to_date_obj < from_date_obj:
msg = "Parameter 'to_date' cannot be before 'from_date'."
Expand All @@ -110,11 +134,9 @@ def get(self, *args, **kwargs):
size = self._validate_bucket_size()
from_date, to_date = self._validate_start_date_range()
most_loaned_documents = fetch_most_loaned_documents(
from_date,
to_date,
size
from_date, to_date, size
)
return self.make_response(
pid_fetcher=current_pidstore.fetchers[DOCUMENT_PID_FETCHER],
search_result=most_loaned_documents
search_result=most_loaned_documents,
)
26 changes: 17 additions & 9 deletions invenio_app_ils/circulation/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,21 @@ def decorate(*args, **kwargs):
current_app.config["ILS_VIEWS_PERMISSIONS_FACTORY"](action)
)
return f(*args, **kwargs)

return decorate

return decorator_builder


def create_circulation_blueprint(app):
"""Add circulation views to the blueprint."""
blueprint = Blueprint(
"invenio_app_ils_circulation",
__name__,
url_prefix="",
"invenio_app_ils_circulation", __name__, url_prefix=""
)

endpoints = app.config.get("RECORDS_REST_ENDPOINTS", [])
options = endpoints.get('loanid', {})
options = endpoints.get("loanid", {})
default_media_type = options.get("default_media_type", "")
rec_serializers = options.get("record_serializers", {})
serializers = {
mime: obj_or_import_string(func)
Expand All @@ -56,6 +57,7 @@ def create_circulation_blueprint(app):
loan_request = LoanRequestResource.as_view(
LoanRequestResource.view_name,
serializers=serializers,
default_media_type=default_media_type,
ctx=dict(links_factory=loan_links_factory),
)

Expand All @@ -66,6 +68,7 @@ def create_circulation_blueprint(app):
loan_create = LoanCreateResource.as_view(
LoanCreateResource.view_name,
serializers=serializers,
default_media_type=default_media_type,
ctx=dict(links_factory=loan_links_factory),
)

Expand All @@ -81,7 +84,9 @@ class IlsCirculationResource(ContentNegotiatedMethodView):

def __init__(self, serializers, ctx, *args, **kwargs):
"""Constructor."""
super(IlsCirculationResource, self).__init__(serializers, *args, **kwargs)
super(IlsCirculationResource, self).__init__(
serializers, *args, **kwargs
)
for key, value in ctx.items():
setattr(self, key, value)

Expand All @@ -91,7 +96,7 @@ class LoanRequestResource(IlsCirculationResource):

view_name = "loan_request"

@need_permissions('circulation-loan-request')
@need_permissions("circulation-loan-request")
def post(self, **kwargs):
"""Loan request post method."""
pid, loan = request_loan(request.get_json())
Expand All @@ -106,12 +111,15 @@ class LoanCreateResource(IlsCirculationResource):

view_name = "loan_create"

@need_permissions('circulation-loan-create')
@need_permissions("circulation-loan-create")
def post(self, **kwargs):
"""Loan create post method."""
params = request.get_json()
should_force_checkout = params.pop("force_checkout")\
if "force_checkout" in params else False
should_force_checkout = (
params.pop("force_checkout")
if "force_checkout" in params
else False
)
pid, loan = create_loan(params, should_force_checkout)

return self.make_response(
Expand Down

0 comments on commit aafa4c6

Please sign in to comment.