Skip to content

Commit

Permalink
module: invenio-record-resources
Browse files Browse the repository at this point in the history
* Updates `invenio-record-resources` from version v.0.12.5 to v0.16.14.
  Versions greater then 17 are not working for `invenio 3.4`.

Co-Authored-by: Peter Weber <peter.weber@rero.ch>
  • Loading branch information
rerowep and rerowep committed Mar 29, 2022
1 parent 805bbe3 commit 557e7a8
Show file tree
Hide file tree
Showing 17 changed files with 368 additions and 289 deletions.
225 changes: 138 additions & 87 deletions poetry.lock

Large diffs are not rendered by default.

19 changes: 8 additions & 11 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ invenio-oaiserver = "^1.4.1"
invenio-pidstore = ">=1.2.1,<1.3.0"
invenio-records-rest = ">=1.8.0,<1.9.0"
invenio-records-ui= ">=1.2.0,<1.3.0"
invenio-records = "<=1.6.0"
invenio-stats = "^1.0.0-alpha.18"
# TODO: update invenio-records-resources
invenio-records-resources = "==0.12.5"
invenio-records = "<=1.7.0"
invenio-stats = "^1.0.0a18"
invenio-records-resources = "*"
invenio-previewer = ">=1.3.5"
invenio = {version = ">=3.4.0,<3.5.0", extras = ["base", "files", "postgresql", "auth", "elasticsearch7", "docs", "tests"]}

uwsgi = ">=2.0"
Expand All @@ -36,7 +36,7 @@ pycountry = "*"
flask-wiki = {git = "https://github.com/rero/flask-wiki.git"}
markdown-captions = "*"
bleach = ">3.11"
wand = ">=0.5.0,<0.6.0"
wand = ">=0.6.6,<0.7.0"
python-dotenv = "*"
flask-cors = ">3.0.8"
cryptography = ">=3.2"
Expand All @@ -49,19 +49,16 @@ python-Levenshtein = ">=0.12.0"
polib = ">=1.1.0"
idutils = ">=1.1.8"
urllib3 = ">=1.26.4"
coverage = {version = ">=6.0b1", allow-prereleases = true}
pillow = ">=9.0.0"
celery = ">=5.0.0"
jedi = "<0.18.0"
wtforms = "<3.0.0"
flask-wtf = "<1.0.0"
zipp = "*"
pycparser = "*"
invenio-previewer = ">=1.3.5"
markupsafe = "<2.1.0"
# TODO: normaly not needed with invenio-records[postgresql]
sqlalchemy-continuum = "<2.0.0"
sqlalchemy_continuum = "<2.0.0"
psycopg2-binary = "<3.0.0"
MarkupSafe = "<2.1.0"
jedi = "<0.18.0"

[tool.poetry.dev-dependencies]
Flask-Debugtoolbar = ">=0.10.1"
Expand Down
41 changes: 22 additions & 19 deletions sonar/dedicated/hepvs/projects/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,39 @@

"""HEP Valais Projects resource."""

from flask_resources import ResponseHandler
from flask_resources.serializers import JSONSerializer
from invenio_records_resources.resources import \
RecordResourceConfig as BaseRecordResourceConfig
from invenio_records_resources.resources.records.response import RecordResponse
from invenio_records_resources.resources.records.headers import etag_headers

from sonar.dedicated.hepvs.projects.serializers.csv import CSVSerializer
from sonar.resources.projects.resource import \
RecordResourceConfig as BaseRecordResourceConfig
from sonar.resources.resources.responses import StreamResponse
from sonar.resources.resources.responses import StreamResponseHandler


class RecordResourceConfig(BaseRecordResourceConfig):
"""HEP Valais Projects resource configuration."""

response_handlers = {
'application/json':
RecordResponse(JSONSerializer()),
'text/csv':
StreamResponse(CSVSerializer(csv_included_fields=[
'pid', 'name', 'approvalDate', 'projectSponsor', 'statusHep',
'mainTeam', 'innerSearcher', 'secondaryTeam', 'externalPartners',
'status', 'startDate', 'endDate', 'description', 'keywords',
'realizationFramework', 'funding_funder_type',
'funding_funder_name', 'funding_funder_number',
'funding_fundingReceived', 'actorsInvolved', 'benefits',
'impactOnFormation', 'impactOnProfessionalEnvironment',
'impactOnPublicAction', 'promoteInnovation',
'relatedToMandate_mandate', 'relatedToMandate_name',
'relatedToMandate_briefDescription', 'educationalDocument',
'searchResultsValorised'
]),
filename='projects.csv')
'application/json': ResponseHandler(
JSONSerializer(),
headers=etag_headers),
'text/csv': StreamResponseHandler(
CSVSerializer(csv_included_fields=[
'pid', 'name', 'approvalDate', 'projectSponsor', 'statusHep',
'mainTeam', 'innerSearcher', 'secondaryTeam',
'externalPartners', 'status', 'startDate', 'endDate',
'description', 'keywords', 'realizationFramework',
'funding_funder_type', 'funding_funder_name',
'funding_funder_number', 'funding_fundingReceived',
'actorsInvolved', 'benefits', 'impactOnFormation',
'impactOnProfessionalEnvironment', 'impactOnPublicAction',
'promoteInnovation', 'relatedToMandate_mandate',
'relatedToMandate_name', 'relatedToMandate_briefDescription',
'educationalDocument', 'searchResultsValorised'
]),
filename='projects.csv',
headers=etag_headers)
}
22 changes: 13 additions & 9 deletions sonar/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@
from sonar.modules.users.signals import add_full_name, user_registered_handler
from sonar.modules.utils import get_language_value, get_specific_theme, \
get_switch_aai_providers, get_view_code
from sonar.resources.projects.resource import \
RecordResource as ProjectRecordResource
from sonar.resources.projects.service import \
RecordService as ProjectRecordService
from sonar.resources.projects.resource import ProjectsRecordResource, \
ProjectsRecordResourceConfig
from sonar.resources.projects.service import ProjectsRecordService, \
ProjectsRecordServiceConfig

from . import config_sonar
from .route_converters import OrganisationCodeConverter
Expand Down Expand Up @@ -117,6 +117,8 @@ def init_views(self, app):

@app.route('/', defaults={
'view': app.config.get('SONAR_APP_DEFAULT_ORGANISATION')})


@app.route('/<org_code:view>/')
def index(view):
"""Homepage."""
Expand Down Expand Up @@ -201,8 +203,10 @@ def favicon(org):
def create_resources(self):
"""Create resources."""
# Initialize the project resource with the corresponding service.
projects_resource = ProjectRecordResource(
service=ProjectRecordService())
project_service = ProjectsRecordService(ProjectsRecordServiceConfig())
projects_resource = ProjectsRecordResource(
service=project_service,
config=ProjectsRecordResourceConfig)
self.resources['projects'] = projects_resource

def get_endpoints(self):
Expand Down Expand Up @@ -238,7 +242,7 @@ def register_blueprints(self, app):
"""Register the blueprints."""
# Register REST endpoint for projects resource.
app.register_blueprint(
self.resources['projects'].as_blueprint('projects'))
self.resources['projects'].as_blueprint())

@app.before_request
def set_accept_mimetype():
Expand All @@ -248,5 +252,5 @@ def set_accept_mimetype():
in flask_resources: https://github.com/inveniosoftware/flask-resources/blob/master/flask_resources/content_negotiation.py#L105
"""
if request.args.get('format'):
request.accept_mimetypes = MIMEAccept([(request.args['format'],
1)])
request.accept_mimetypes = MIMEAccept([
(request.args['format'],1)])
10 changes: 6 additions & 4 deletions sonar/modules/documents/receivers.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from os import makedirs
from os.path import exists, join

import click
import pytz
from flask import current_app

Expand Down Expand Up @@ -57,7 +58,8 @@ def transform_harvested_records(sender=None, records=None, **kwargs):
max_records = None

if kwargs.get('name'):
print('Harvesting records from "{set}"'.format(set=kwargs.get('name')))
click.echo(
'Harvesting records from "{set}"'.format(set=kwargs.get('name')))

harvested_records = list(records)

Expand All @@ -80,7 +82,7 @@ def transform_harvested_records(sender=None, records=None, **kwargs):
for chunk in list(chunks(records, CHUNK_SIZE)):
import_records.delay(chunk)

print('{count} records harvested in {time} seconds'.format(
click.echo('{count} records harvested in {time} seconds'.format(
count=len(records), time=time.time() - start_time))


Expand Down Expand Up @@ -175,7 +177,7 @@ def export_json(sender=None, records=None, **kwargs):

records_to_export = []

print('{count} records harvested'.format(count=len(records)))
click.echo('{count} records harvested'.format(count=len(records)))

for record in records:
loader_schema = LoaderSchemaFactory.create(kwargs['name'])
Expand All @@ -192,4 +194,4 @@ def export_json(sender=None, records=None, **kwargs):
client = HegClient()
client.upload_file(file_name, file_path)

print('{count} records exported'.format(count=len(records_to_export)))
click.echo('{count} records exported'.format(count=len(records_to_export)))
2 changes: 1 addition & 1 deletion sonar/modules/pdf_extractor/pdf_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def api_is_alive(self):
"""
try:
response, status = self.do_request('isalive', 'get')
except Exception:
except Exception as err:
return False

if status != 200:
Expand Down
54 changes: 35 additions & 19 deletions sonar/resources/projects/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,35 +17,51 @@

"""Projects resource."""

from flask_resources import ResponseHandler
from flask_resources.serializers import JSONSerializer
from invenio_records_resources.resources import \
RecordResourceConfig as BaseRecordResourceConfig
from invenio_records_resources.resources.records.response import RecordResponse
from invenio_records_resources.resources import RecordResourceConfig
from invenio_records_resources.resources.records.headers import etag_headers

from sonar.resources.projects.serializers.csv import CSVSerializer
from sonar.resources.resource import RecordResource as BaseRecordResource
from sonar.resources.resources.responses import StreamResponse
from sonar.resources.resource import RecordResource
from sonar.resources.resources.responses import StreamResponseHandler


class RecordResourceConfig(BaseRecordResourceConfig):
class ProjectsRecordResourceConfig(RecordResourceConfig):
"""Projects resource configuration."""

resource_name = 'projects'
list_route = '/projects/'
item_route = f'{list_route}/<pid_value>'
blueprint_name = 'projects'
url_prefix = "/projects"

response_handlers = {
'application/json':
RecordResponse(JSONSerializer()),
'text/csv':
StreamResponse(CSVSerializer(csv_included_fields=[
'pid', 'name', 'description', 'startDate', 'endDate'
]),
filename='projects.csv')
'application/json': ResponseHandler(
JSONSerializer(),
headers=etag_headers),
'text/csv': StreamResponseHandler(
CSVSerializer(csv_included_fields=[
'pid', 'name', 'description', 'startDate', 'endDate'
]),
filename='projects.csv',
headers=etag_headers)
}

# # Request parsing
# request_read_args = {}
# request_view_args = {"pid_value": ma.fields.Str()}
# request_search_args = SearchRequestArgsSchema
# request_headers = {"if_match": ma.fields.Int()}
# request_body_parsers = {
# "application/json": RequestBodyParser(JSONDeserializer())
# }
# default_content_type = "application/json"

# # Response handling
# response_handlers = {
# "application/json": ResponseHandler(
# JSONSerializer(), headers=etag_headers)
# }
# default_accept_mimetype = "application/json"

class RecordResource(BaseRecordResource):
"""Projects resource"."""

default_config = RecordResourceConfig
class ProjectsRecordResource(RecordResource):
"""Projects resource"."""
Loading

0 comments on commit 557e7a8

Please # to comment.