Skip to content

Commit

Permalink
books: updates to serials and multiparts (#251)
Browse files Browse the repository at this point in the history
* Store serials in _migration
* Store keywords in _migration
* Change `role` to `roles`
* Use `has_keyword`, `is_multipart` and `has_serial` to indicate if the
record has any keywords, multipart or serials
  • Loading branch information
equadon authored and kpsherva committed Sep 11, 2019
1 parent 94229bc commit ac076e3
Show file tree
Hide file tree
Showing 9 changed files with 180 additions and 96 deletions.
43 changes: 33 additions & 10 deletions cds_dojson/marc21/fields/books/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,18 @@
from .utils import extract_parts, is_excluded


@model.over('legacy_recid', '^001')
def recid(self, key, value):
"""Record Identifier."""
return int(value)


@model.over('agency_code', '^003')
def agency_code(self, key, value):
"""Control number identifier."""
return 'SzGeCERN'


@model.over('acquisition_source', '(^916__)|(^859__)|(^595__)')
@filter_values
def acquisition_source(self, key, value):
Expand Down Expand Up @@ -547,7 +559,7 @@ def subject_classification(self, key, value):
elif key == '084__':
sub_2 = clean_val('2', value, str)
if sub_2 and sub_2.upper() in SUBJECT_CLASSIFICATION_EXCEPTIONS:
self['keywords'] = keywords(self, key, value)
keywords(self, key, value)
raise IgnoreKey('subject_classification')
else:
_subject_classification.update({'schema': 'ICS'})
Expand All @@ -560,25 +572,28 @@ def subject_classification(self, key, value):
@filter_list_values
def keywords(self, key, value):
"""Keywords."""
_keywords = self.get('keywords', [])
_migration = self.get('_migration', {})
if 'keywords' not in _migration:
_migration['keywords'] = []
for v in force_list(value):
if key == '084__':
sub_2 = clean_val('2', value, str)
if sub_2 and sub_2 == 'PACS':
_keywords.append({
_migration['keywords'].append({
'name': clean_val('a', v, str, req=True),
'provenance': 'PACS',
})
else:
raise IgnoreKey('keywords')
elif key == '6531_':
_keywords.append({
_migration['keywords'].append({
'name': clean_val('a', value, str),
'provenance': value.get('9') or value.get('g'),
# Easier to solve here
})

return _keywords
_migration['has_keywords'] = True
self['_migration'] = _migration
raise IgnoreKey('keywords')


@model.over('conference_info', '(^111__)|(^270__)|(^711__)')
Expand Down Expand Up @@ -666,14 +681,22 @@ def imprints(self, key, value):

@model.over('book_series', '^490__')
@for_each_value
@filter_values
def book_series(self, key, value):
"""Translates book series field."""
return {
"""Match barcodes to volumes."""
val_n = clean_val('n', value, str)
val_x = clean_val('x', value, str)

_migration = self.get('_migration', {})
if 'serials' not in _migration:
_migration['serials'] = []
_migration['serials'].append({
'title': clean_val('a', value, str),
'volume': clean_val('v', value, str),
'issn': clean_val('x', value, str),
}
})
_migration['has_serial'] = True
self['_migration'] = _migration
raise IgnoreKey('book_series')


@model.over('public_notes', '^500__')
Expand Down
44 changes: 20 additions & 24 deletions cds_dojson/marc21/fields/books/book.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
from dojson.utils import filter_values, for_each_value

from cds_dojson.marc21.fields.books.errors import ManualMigrationRequired, \
UnexpectedValue
from cds_dojson.marc21.fields.books.utils import is_excluded, extract_parts
MissingRequiredField, UnexpectedValue
from cds_dojson.marc21.fields.books.utils import is_excluded, extract_parts, \
extract_volume_number
from cds_dojson.marc21.fields.utils import clean_val
from cds_dojson.marc21.models.books.book import model

Expand All @@ -35,18 +36,24 @@
@filter_values
def alternative_titles(self, key, value):
"""Alternative titles."""
if 'n' in value:
self['volume'] = volume(self, key, value)
if ('n' in value and 'p' not in value) or \
('n' not in value and 'p' in value):
raise MissingRequiredField(subfield='n or p')

if 'p' in value:
# if series detected
if self.get('volumes', None):
val_p = clean_val('p', value, str)
self['volumes_titles'].append(
{'title': val_p, 'volume': volume(self, key, value)}
) if val_p else None
else:
self['volumes_titles'] = []
return {}
_migration = self.get('_migration', {})
if 'volumes' not in _migration:
_migration['volumes'] = []

val_n = clean_val('n', value, str)
_migration['volumes'].append({
'volume': extract_volume_number(val_n, raise_exception=True),
'title': clean_val('p', value, str),
})
_migration['is_multipart'] = True
_migration['record_type'] = 'multipart'
self['_migration'] = _migration
raise IgnoreKey('alternative_titles')
else:
return {
'title': clean_val('a', value, str, req=True),
Expand All @@ -55,17 +62,6 @@ def alternative_titles(self, key, value):
}


@model.over('volume', '^246__')
@for_each_value
def volume(self, key, value):
"""Translates volumes index in series."""
_volume = self.get('volume', None)
val_n = clean_val('n', value, str, req=True)
if _volume and _volume != val_n:
raise ManualMigrationRequired(subfield='n')
return val_n


@model.over('number_of_pages', '^300__') # item
def number_of_pages(self, key, value):
"""Translates number_of_pages fields."""
Expand Down
12 changes: 11 additions & 1 deletion cds_dojson/marc21/fields/books/multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
from dojson.errors import IgnoreKey
from dojson.utils import for_each_value, filter_values, force_list

from cds_dojson.marc21.fields.books.base import book_series as base_book_series
from cds_dojson.marc21.fields.books.errors import UnexpectedValue, \
ManualMigrationRequired, MissingRequiredField
from cds_dojson.marc21.fields.books.utils import extract_parts, \
Expand Down Expand Up @@ -100,7 +101,9 @@ def migration(self, key, value):
_series_title = self.get('title', None)

# I added this in the model, I'm sure it's there
_migration = self.get('_migration', {'volumes': []})
_migration = self.get('_migration', {})
if 'volumes' not in _migration:
_migration['volumes'] = []

for v in force_list(value):
# check if it is a multipart monograph
Expand Down Expand Up @@ -158,3 +161,10 @@ def number_of_volumes(self, key, value):
if _volumes:
return _volumes[0]
raise IgnoreKey('number_of_volumes')


@model.over('book_series', '^490__')
@for_each_value
def book_series(self, key, value):
"""Match barcodes to volumes."""
base_book_series(self, key, value)
6 changes: 4 additions & 2 deletions cds_dojson/marc21/fields/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,8 +371,10 @@ def build_contributor_books(value):
'ids': _extract_json_ids(value, 'schema') or None,
'full_name': value.get('name') or clean_val('a', value, str),
'email': clean_email(value.get('email')),
'role': _get_correct_books_contributor_role(
'e', value.get('e', 'author')),
'roles': [
_get_correct_books_contributor_role(
'e', value.get('e', 'author')).lower()
],
'curated_relation': True if value_9 == '#BEARD#' else None
}

Expand Down
2 changes: 1 addition & 1 deletion cds_dojson/marc21/models/books/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,5 +164,5 @@ class BooksBase(OverdoJSONSchema):
"""Base model conversion MARC21 to JSON."""


model = BooksBase(bases=(cds_base, ),
model = BooksBase(bases=(),
entry_point_group='cds_dojson.marc21.books')
16 changes: 10 additions & 6 deletions cds_dojson/marc21/models/books/book.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class CDSBook(CDSOverdoBookBase):
'690C_:BOOKSUGGESTION OR 980__:PROCEEDINGS OR 980__:PERI OR ' \
'697C_:LEGSERLIB OR 697C_:"ENGLISH BOOK CLUB" -980__:DELETED'

__schema__ = 'records/books/book/book-v.0.0.1.json'
__schema__ = 'https://127.0.0.1:5000/schemas/documents/document-v1.0.0.json'

__ignore_keys__ = COMMON_IGNORE_FIELDS

Expand All @@ -44,24 +44,28 @@ def do(self, blob, ignore_missing=True, exception_handlers=None):
json['$schema'] = self.__class__.__schema__

if '_migration' in json:
json['_migration'].update({'record_type': 'document'})
json['_migration'].setdefault('record_type', 'document')
json['_migration'].setdefault('volumes', [])
json['_migration'].setdefault('serials', [])
json['_migration'].setdefault('has_serial', False)
json['_migration'].setdefault('has_multipart', False)
json['_migration'].setdefault('is_multipart', False)
json['_migration'].setdefault('has_keywords', False)
json['_migration'].setdefault('has_related', False)

else:
json['_migration'] = {
'record_type': 'document',
'has_serial': False,
'has_multipart': False,
'is_multipart': False,
'has_keywords': False,
'has_related': False,
'volumes': []
'volumes': [],
'serials': [],
}

return json


model = CDSBook(
bases=(books_base, cds_base, ),
bases=(books_base, ),
entry_point_group='cds_dojson.marc21.book')
2 changes: 1 addition & 1 deletion cds_dojson/marc21/models/books/standard.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def do(self, blob, ignore_missing=True, exception_handlers=None):
json['_migration'] = {
'record_type': 'document',
'has_serial': False,
'has_multipart': False,
'is_multipart': False,
'has_keywords': False,
'has_related': False,
'volumes': []
Expand Down

0 comments on commit ac076e3

Please sign in to comment.