Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move record files gather to own function #1626

Open
wants to merge 3 commits into
base: aaronhelton/issue1337
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 24 additions & 53 deletions dlx_rest/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from dlx_rest.config import Config
from dlx_rest.app import app, login_manager
from dlx_rest.models import RecordView, User, Basket, requires_permission, register_permission, DoesNotExist
from dlx_rest.api.utils import ClassDispatch, URL, ApiResponse, Schemas, abort, brief_bib, brief_auth, brief_speech, item_locked, has_permission
from dlx_rest.api.utils import ClassDispatch, URL, ApiResponse, Schemas, abort, brief_bib, brief_auth, brief_speech, item_locked, has_permission, get_record_files

# Init

Expand Down Expand Up @@ -696,25 +696,12 @@ def get(self, collection, record_id):
files_data = []

if collection == 'bibs':
symbols = record.get_values('191', 'a') + record.get_values('191', 'z') + record.get_values('791', 'a')
isbns = record.get_values('020', 'a')
isbns = [x.split(' ')[0] for x in isbns] # field may have extra text after the isbn
# Get files by original URI which was logged in the Archive-It system
uris = record.get_values('561', 'u')
all_files = []

for id_type, id_values in {'symbol': symbols, 'isbn': isbns, 'uri': uris}.items():
for id_value in id_values:
langs = ('AR', 'ZH', 'EN', 'FR', 'RU', 'ES', 'DE')
this_id_files = list(filter(None, [File.latest_by_identifier_language(Identifier(id_type, id_value), lang) for lang in langs]))
all_files += list(filter(lambda x: x.id not in [y.id for y in all_files], this_id_files))

files_data = [
{
'mimetype': f.mimetype,
'language': f.languages[0].lower(),
'url': URL('api_file_record', record_id=f.id).to_str()
} for f in all_files
} for f in get_record_files(record)
]

data = record.to_dict()
Expand Down Expand Up @@ -829,53 +816,37 @@ def delete(self, collection, record_id):

@ns.route('/marc/<string:collection>/records/<int:record_id>/files')
@ns.param('record_id', 'The record identifier')
@ns.param('collection', '"bibs" or "auths"')
@ns.param('collection', '"bibs"')
class RecordFilesList(Resource):

@ns.doc(description='Return the files for record with the given identifier')
def get(self, collection, record_id):
cls = ClassDispatch.by_collection(collection) or abort(404)
record = cls.from_id(record_id) or abort(404)
files_data = []

files_data = [
{
'mimetype': f.mimetype,
'language': f.languages[0].lower(),
'url': URL('api_file_record', record_id=f.id).to_str()
} for f in get_record_files(record)
]

if collection == 'bibs':
symbols = record.get_values('191', 'a') + record.get_values('191', 'z') + record.get_values('791', 'a')
isbns = record.get_values('020', 'a')
isbns = [x.split(' ')[0] for x in isbns] # field may have extra text after the isbn
# Get files by original URI which was logged in the Archive-It system
uris = record.get_values('561', 'u')
all_files = []

for id_type, id_values in {'symbol': symbols, 'isbn': isbns, 'uri': uris}.items():
for id_value in id_values:
langs = ('AR', 'ZH', 'EN', 'FR', 'RU', 'ES', 'DE')
this_id_files = list(filter(None, [File.latest_by_identifier_language(Identifier(id_type, id_value), lang) for lang in langs]))
all_files += list(filter(lambda x: x.id not in [y.id for y in all_files], this_id_files))

files_data = [
{
'mimetype': f.mimetype,
'language': f.languages[0].lower(),
'url': URL('api_file_record', record_id=f.id).to_str()
} for f in all_files
]
print(files_data)
meta = {
'name': 'api_record_files_list',
'returns': URL('api_schema', schema_name='api.filelist').to_str(),
'timestamp': datetime.now(timezone.utc)
}

meta = {
'name': 'api_record',
'returns': URL('api_schema', schema_name='api.response').to_str(),
'timestamp': datetime.now(timezone.utc)
}
links = {
'_next': None,
'_prev': None,
'_self': URL('api_record_files_list', collection=collection, record_id=record_id).to_str(),
'related': {
'record': URL('api_record', collection=collection, record_id=record_id).to_str()
}
links = {
'_next': None,
'_prev': None,
'_self': URL('api_record_files_list', collection=collection, record_id=record_id).to_str(),
'related': {
'record': URL('api_record', collection=collection, record_id=record_id).to_str()
}
}

return ApiResponse(links=links, meta=meta, data=files_data).jsonify()
return ApiResponse(links=links, meta=meta, data=files_data).jsonify()

@ns.route('/marc/<string:collection>/records/<int:record_id>/locked')
@ns.param('record_id', 'The record identifier')
Expand Down
32 changes: 30 additions & 2 deletions dlx_rest/api/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from dlx import DB
from dlx import Config as DlxConfig
from dlx_rest.config import Config
from dlx.marc import Bib, BibSet, Auth, AuthSet
from dlx.marc import Marc, Bib, BibSet, Auth, AuthSet
from dlx.file import File, Identifier
from dlx_rest.models import Basket, User
from flask import abort as flask_abort, url_for, jsonify
from flask_restx import reqparse
Expand Down Expand Up @@ -153,6 +154,18 @@ def get(schema_name):
}
elif schema_name == 'jfile':
data = DlxConfig.jfile_schema
elif schema_name == 'api.filelist':
data = {
'type': 'array',
'items': {
'type': 'object',
'properties': {
aaronhelton marked this conversation as resolved.
Show resolved Hide resolved
'mimetype': {'type': 'string', 'pattern': '^(text|application)/'},
'language': {'type': 'string', 'pattern': '^[a-z]{2}$'},
'url': {'type': 'string', 'format': 'uri'}
}
}
}
elif schema_name == 'api.null':
data = {'type': 'object', 'properties': {}, 'additionalProperties': False}
elif schema_name == 'api.count':
Expand Down Expand Up @@ -350,4 +363,19 @@ def parse_constraint(constraint):
except IndexError:
pass

return return_data
return return_data

def get_record_files(record: Marc) -> list[File]:
symbols = record.get_values('191', 'a') + record.get_values('191', 'z') + record.get_values('791', 'a')
isbns = record.get_values('020', 'a')
isbns = [x.split(' ')[0] for x in isbns] # field may have extra text after the isbn
uris = record.get_values('561', 'u') # Get files by original URI which was logged in the Archive-It system
all_files = []

for id_type, id_values in {'symbol': symbols, 'isbn': isbns, 'uri': uris}.items():
for id_value in id_values:
langs = ('AR', 'ZH', 'EN', 'FR', 'RU', 'ES', 'DE')
this_id_files = list(filter(None, [File.latest_by_identifier_language(Identifier(id_type, id_value), lang) for lang in langs]))
all_files += list(filter(lambda x: x.id not in [y.id for y in all_files], this_id_files))

return all_files
2 changes: 1 addition & 1 deletion dlx_rest/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def test_api_record_files_list(client, marc, files):
# get
res = client.get(f'{API}/marc/bibs/records/1/files')
data = check_response(res)
assert data['_meta']['returns'] == f'{API}/schemas/api.response'
assert data['_meta']['returns'] == f'{API}/schemas/api.filelist'

def test_api_record(client, marc, default_users):
from dlx_rest.api.utils import ClassDispatch
Expand Down
Loading