-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
data: add dump function for documents
* Computes `_text` field for provisionActivity and series to documents, to display unstructured statement from the structured data. Co-Authored-by: Peter Weber <[email protected]>
- Loading branch information
Showing
5 changed files
with
121 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# RERO ILS | ||
# Copyright (C) 2019 RERO | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as published by | ||
# the Free Software Foundation, version 3 of the License. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
"""Blueprint used for loading templates.""" | ||
|
||
from __future__ import absolute_import, print_function | ||
|
||
from .dojson.contrib.marc21tojson.model import remove_trailing_punctuation | ||
|
||
|
||
def localized_data_name(data, language): | ||
"""Get localized name.""" | ||
return data.get( | ||
'name_{language}'.format(language=language), | ||
data.get('name', '') | ||
) | ||
|
||
|
||
def clean_text(data): | ||
"""Delete all _text from data.""" | ||
if isinstance(data, list): | ||
new_val = [] | ||
for val in data: | ||
new_val.append(clean_text(val)) | ||
data = new_val | ||
elif isinstance(data, dict): | ||
if '_text' in data: | ||
del data['_text'] | ||
new_data = {} | ||
for key, val in data.items(): | ||
new_data[key] = clean_text(val) | ||
data = new_data | ||
return data | ||
|
||
|
||
def publication_statement_text(provision_activity): | ||
"""Create publication statement from place, agent and date values.""" | ||
punctuation = { | ||
'bf:Place': ' ; ', | ||
'bf:Agent': ', ' | ||
} | ||
|
||
statement_with_language = {'default': ''} | ||
statement_type = None | ||
|
||
for statement in provision_activity['statement']: | ||
labels = statement['label'] | ||
|
||
for label in labels: | ||
language = label.get('language', 'default') | ||
|
||
if not statement_with_language.get(language): | ||
statement_with_language[language] = '' | ||
|
||
if statement_with_language[language]: | ||
if statement_type == statement['type']: | ||
statement_with_language[language] += punctuation[ | ||
statement_type | ||
] | ||
else: | ||
if statement['type'] == 'bf:Place': | ||
statement_with_language[language] += ' ; ' | ||
else: | ||
statement_with_language[language] += ' : ' | ||
|
||
statement_with_language[language] += label['value'] | ||
statement_type = statement['type'] | ||
|
||
# date field: remove ';' and append | ||
for key, value in statement_with_language.items(): | ||
value = remove_trailing_punctuation(value) | ||
if provision_activity.get('date'): | ||
value += ', ' + provision_activity.get('date') | ||
statement_with_language[key] = value | ||
return statement_with_language | ||
|
||
|
||
def series_format_text(serie): | ||
"""Format series for template.""" | ||
output = [] | ||
if serie.get('name'): | ||
output.append(serie.get('name')) | ||
if serie.get('number'): | ||
output.append(', ' + serie.get('number')) | ||
return ''.join(str(x) for x in output) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters