Skip to content

Commit

Permalink
ill: add json resolvers to BorrowingRequest
Browse files Browse the repository at this point in the history
* adds json resolvers to BorrowingRequest for Document and Patron
* refactors ChangedBy marshmallow loader schema to be re-usable
* changes Acq Order and ILL Brw Req to use ChangeBy schema
* fixes Document json resolver fields for other records
  • Loading branch information
ntarocco committed Mar 31, 2020
1 parent 1ad1b47 commit 6da7d71
Show file tree
Hide file tree
Showing 39 changed files with 918 additions and 380 deletions.
3 changes: 1 addition & 2 deletions invenio_app_ils/acquisition/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from invenio_app_ils.errors import RecordHasReferencesError
from invenio_app_ils.fetchers import pid_fetcher
from invenio_app_ils.minters import pid_minter
from invenio_app_ils.records.api import IlsRecord, preserve_fields
from invenio_app_ils.records.api import IlsRecord

VENDOR_PID_TYPE = "acqvid"
VENDOR_PID_MINTER = "acqvid"
Expand Down Expand Up @@ -85,7 +85,6 @@ def create(cls, data, id_=None, **kwargs):
cls.build_resolver_fields(data)
return super().create(data, id_=id_, **kwargs)

@preserve_fields(fields=["created_by_pid"])
def update(self, *args, **kwargs):
"""Update record."""
super().update(*args, **kwargs)
Expand Down
14 changes: 8 additions & 6 deletions invenio_app_ils/acquisition/loaders/jsonschemas/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@

"""Order schema for marshmallow loader."""

from flask_login import current_user
from invenio_circulation.records.loaders.schemas.json import DateString
from invenio_records_rest.schemas import RecordMetadataSchemaJSONV1
from marshmallow import EXCLUDE, Schema, fields, pre_load, validate

from invenio_app_ils.acquisition.api import Order
from invenio_app_ils.records.loaders.schemas.changed_by import ChangedBySchema, \
set_changed_by


class PriceSchema(Schema):
Expand Down Expand Up @@ -77,7 +78,7 @@ class Meta:
unknown = EXCLUDE

cancel_reason = fields.Str()
created_by_pid = fields.Str()
created_by = fields.Nested(ChangedBySchema)
received_date = DateString()
expected_delivery_date = DateString()
funds = fields.List(fields.Str())
Expand All @@ -89,9 +90,10 @@ class Meta:
payment = fields.Nested(PaymentSchema)
status = fields.Str(required=True, validate=validate.OneOf(Order.STATUSES))
vendor_pid = fields.Str(required=True) # TODO: validate
updated_by = fields.Nested(ChangedBySchema)

@pre_load
def add_created_by(self, data, **kwargs):
"""Automatically add the `created_by_pid`."""
data["created_by_pid"] = str(current_user.id)
return data
def set_changed_by(self, data, **kwargs):
"""Automatically set `created_by` and `updated_by`."""
record = self.context.get("record")
return set_changed_by(data, record)
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,28 @@
"date_detection": false,
"numeric_detection": false,
"properties": {
"$schema": {
"type": "keyword"
},
"_created": {
"type": "date"
},
"_updated": {
"type": "date"
},
"$schema": {
"type": "keyword"
},
"cancel_reason": {
"type": "text"
},
"created_by_pid": {
"type": "keyword"
"created_by": {
"properties": {
"type": {
"type": "keyword"
},
"value": {
"type": "keyword"
}
},
"type": "object"
},
"expected_delivery_date": {
"type": "date"
Expand Down Expand Up @@ -64,6 +72,23 @@
"copies_received": {
"type": "integer"
},
"document": {
"properties": {
"edition": {
"type": "text"
},
"pid": {
"type": "keyword"
},
"publication_year": {
"type": "integer"
},
"title": {
"type": "text"
}
},
"type": "object"
},
"document_pid": {
"type": "keyword"
},
Expand All @@ -82,6 +107,20 @@
"notes": {
"type": "text"
},
"patron": {
"properties": {
"email": {
"type": "text"
},
"id": {
"type": "keyword"
},
"name": {
"type": "text"
}
},
"type": "object"
},
"patron_pid": {
"type": "keyword"
},
Expand Down Expand Up @@ -167,6 +206,17 @@
"status": {
"type": "keyword"
},
"updated_by": {
"properties": {
"type": {
"type": "keyword"
},
"value": {
"type": "keyword"
}
},
"type": "object"
},
"vendor": {
"properties": {
"address": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"type": "text"
},
"email": {
"type": "keyword"
"type": "text"
},
"name": {
"type": "keyword"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,28 @@
"date_detection": false,
"numeric_detection": false,
"properties": {
"$schema": {
"type": "keyword"
},
"_created": {
"type": "date"
},
"_updated": {
"type": "date"
},
"$schema": {
"type": "keyword"
},
"cancel_reason": {
"type": "text"
},
"created_by_pid": {
"type": "keyword"
"created_by": {
"properties": {
"type": {
"type": "keyword"
},
"value": {
"type": "keyword"
}
},
"type": "object"
},
"expected_delivery_date": {
"type": "date"
Expand Down Expand Up @@ -63,6 +71,23 @@
"copies_received": {
"type": "integer"
},
"document": {
"properties": {
"edition": {
"type": "text"
},
"pid": {
"type": "keyword"
},
"publication_year": {
"type": "integer"
},
"title": {
"type": "text"
}
},
"type": "object"
},
"document_pid": {
"type": "keyword"
},
Expand All @@ -81,6 +106,20 @@
"notes": {
"type": "text"
},
"patron": {
"properties": {
"email": {
"type": "text"
},
"id": {
"type": "keyword"
},
"name": {
"type": "text"
}
},
"type": "object"
},
"patron_pid": {
"type": "keyword"
},
Expand Down Expand Up @@ -166,6 +205,17 @@
"status": {
"type": "keyword"
},
"updated_by": {
"properties": {
"type": {
"type": "keyword"
},
"value": {
"type": "keyword"
}
},
"type": "object"
},
"vendor": {
"properties": {
"address": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"type": "text"
},
"email": {
"type": "keyword"
"type": "text"
},
"name": {
"type": "keyword"
Expand Down
42 changes: 36 additions & 6 deletions invenio_app_ils/acquisition/schemas/acq_orders/order-v1.0.0.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,12 @@
"title": "Price per unit for the order line"
}
},
"required": ["document_pid", "recipient", "medium", "copies_ordered"],
"required": [
"document_pid",
"recipient",
"medium",
"copies_ordered"
],
"type": "object"
},
"payment": {
Expand Down Expand Up @@ -95,7 +100,9 @@
"type": "string"
}
},
"required": ["mode"],
"required": [
"mode"
],
"type": "object"
},
"price": {
Expand All @@ -110,7 +117,10 @@
"type": "number"
}
},
"required": ["value", "currency"],
"required": [
"value",
"currency"
],
"type": "object"
}
},
Expand All @@ -123,9 +133,17 @@
"title": "Reason for cancelling the request",
"type": "string"
},
"created_by_pid": {
"title": "PID of the user who created the order",
"type": "string"
"created_by": {
"additionalProperties": false,
"properties": {
"type": {
"type": "string"
},
"value": {
"type": "string"
}
},
"type": "object"
},
"expected_delivery_date": {
"format": "date",
Expand Down Expand Up @@ -185,6 +203,18 @@
"title": "Status of Acquisition order",
"type": "string"
},
"updated_by": {
"properties": {
"type": {
"type": "string"
},
"value": {
"type": "string"
}
},
"title": "Last update entity information",
"type": "object"
},
"vendor_pid": {
"title": "The persistent identifier of its Vendor",
"type": "string"
Expand Down
15 changes: 15 additions & 0 deletions invenio_app_ils/acquisition/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@ def search_by_document_pid(self, document_pid=None):
)
return search

def search_by_patron_pid(self, patron_pid=None):
"""Search by patron pid."""
search = self

if patron_pid:
search = search.filter(
"term",
order_lines__patron_pid=patron_pid
)
else:
raise MissingRequiredParameterError(
description="patron_pid is required"
)
return search

def search_by_vendor_pid(self, vendor_pid=None):
"""Search by vendor pid."""
search = self
Expand Down
11 changes: 4 additions & 7 deletions invenio_app_ils/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@
BorrowingRequest, Library
from .pidstore.pids import EITEM_PID_TYPE, INTERNAL_LOCATION_PID_TYPE, \
ITEM_PID_TYPE, LOCATION_PID_TYPE, SERIES_PID_TYPE
from .records.api import EItem, InternalLocation, Item, Location, Patron, \
Series
from .records.api import EItem, InternalLocation, Item, Location, Series
from .records_relations.api import RecordRelationsParentChild, \
RecordRelationsSiblings
from .relations.api import Relation
Expand Down Expand Up @@ -1195,18 +1194,16 @@ def patrons():
@with_appcontext
def index():
"""Index patrons."""
from flask import current_app
from invenio_app_ils.pidstore.pids import PATRON_PID_TYPE
from invenio_app_ils.proxies import current_app_ils

patrons = User.query.all()
indexer = PatronIndexer()

click.secho("Now indexing {0} patrons".format(len(patrons)), fg="green")

rest_config = current_app.config["RECORDS_REST_ENDPOINTS"]
patron_cls = rest_config[PATRON_PID_TYPE]["record_class"] or Patron
Patron = current_app_ils.patron_cls
for pat in patrons:
patron = patron_cls(pat.id)
patron = Patron(pat.id)
indexer.index(patron)


Expand Down
Loading

0 comments on commit 6da7d71

Please sign in to comment.