Skip to content

Commit

Permalink
actions: fix conversion language
Browse files Browse the repository at this point in the history
* Fixes missing conversion of i18n language.
* Fixes date format.
  • Loading branch information
lauren-d committed Jul 19, 2021
1 parent 7533d55 commit b20b17c
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 7 deletions.
7 changes: 5 additions & 2 deletions invenio_sip2/actions/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
from ..models import SelfcheckSummary
from ..proxies import current_logger
from ..proxies import current_sip2 as acs_system
from ..utils import get_circulation_status, get_language_code, \
get_security_marker_type
from ..utils import ensure_i18n_language, get_circulation_status, \
get_language_code, get_security_marker_type


class SelfCheckLogin(Action):
Expand All @@ -56,6 +56,9 @@ def execute(self, message, **kwargs):
.debug(f'[SelfCheckLogin]: handler response: '
f'{selfcheck_user}')
if selfcheck_user:
language = selfcheck_user.get('library_language',
acs_system.sip2_language)
selfcheck_user['library_language'] = ensure_i18n_language(language)
client.update(selfcheck_user)

response_message = self.prepare_message_response(
Expand Down
11 changes: 8 additions & 3 deletions invenio_sip2/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@
RenewAll, RequestResend, SelfCheckLogin
from .models import SelfcheckSecurityMarkerType
from .permissions import default_permission_factory
from .utils import convert_bool_to_char, convert_to_char, get_media_type
from .utils import convert_bool_to_char, convert_to_char, get_media_type, \
parse_circulation_date

# I18N
# ====
Expand Down Expand Up @@ -214,7 +215,10 @@
"""Support patron status updating by the selfcheck."""

SIP2_DATE_FORMAT = '%Y%m%d %H%M%S'
"""SIP2 date format."""
"""SIP2 date format for transaction."""

SIP2_CIRCULATION_DATE_FORMAT = '%Y%m%d %H%M%S'
"""SIP2 date format for circulation."""

SIP2_DEFAULT_SECURITY_MARKER = SelfcheckSecurityMarkerType.OTHER
"""SIP2 default security marker type."""
Expand Down Expand Up @@ -1063,7 +1067,8 @@
screen_messages=dict(field_id='AF', multiple=True,
label=_('screen message')),
print_line=dict(field_id='AG', multiple=True, label=_('print line')),
due_date=dict(field_id='AH', label=_('due date')),
due_date=dict(field_id='AH', label=_('due date'),
callback=parse_circulation_date),
title_id=dict(field_id='AJ', label=_('title identifier')),
blocked_card_msg=dict(field_id='AL', label=_('blocked card msg')),
library_name=dict(field_id='AM', label=_('library name')),
Expand Down
3 changes: 1 addition & 2 deletions invenio_sip2/records/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
from invenio_sip2 import datastore

from ..errors import ServerAlreadyRunning
from ..proxies import current_sip2 as acs


class Sip2RecordMetadata(dict):
Expand Down Expand Up @@ -251,7 +250,7 @@ def library_name(self):
@property
def library_language(self):
"""Shortcut for library language."""
return self.get('library_language', acs.sip2_language)
return self.get('library_language')

def get_current_patron_session(self):
"""Shortcut to patron session."""
Expand Down
20 changes: 20 additions & 0 deletions invenio_sip2/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@

from __future__ import absolute_import, print_function

import pytz
from dateutil import parser
from flask import current_app
from pycountry import languages

from .models import SelfcheckCirculationStatus, SelfcheckLanguage, \
SelfcheckMediaType, SelfcheckSecurityMarkerType
Expand All @@ -42,6 +45,15 @@ def decode_char_to_bool(value='N'):
return value == 'Y'


def parse_circulation_date(date):
"""Converts a date of string format to a formatted date utc aware."""
date_format = current_app.config.get('SIP2_CIRCULATION_DATE_FORMAT')
parsed_date = parser.parse(date)
if parsed_date.tzinfo:
return parsed_date.strftime(date_format)
return pytz.utc.localize(parsed_date).strftime(date_format)


def get_language_code(language):
"""Get mapped selfcheck language.
Expand All @@ -54,6 +66,14 @@ def get_language_code(language):
return SelfcheckLanguage.UNKNOWN.value


def ensure_i18n_language(language):
"""Ensure that the given language is an i18n language."""
if len(language) > 2:
return languages.lookup(language).alpha_2
else:
return language


def get_security_marker_type(marker_type=None):
"""Get mapped security marker type."""
try:
Expand Down
7 changes: 7 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@

"""Pytest utils."""

from datetime import datetime, timezone

from flask import current_app

from invenio_sip2.models import PatronStatus, SelfcheckCheckin, \
SelfcheckCheckout, SelfcheckCirculationStatus, SelfcheckFeeType, \
SelfcheckHold, SelfcheckItemInformation, SelfcheckMediaType, \
Expand Down Expand Up @@ -125,6 +129,9 @@ def remote_checkout_handler(user_id, item_id, patron_id,
security_marker=SelfcheckSecurityMarkerType.OTHER
)
response['media_type'] = SelfcheckMediaType.OTHER
response['due_date'] = datetime.now(timezone.utc).strftime(
current_app.config['SIP2_CIRCULATION_DATE_FORMAT']
)
response['hold_queue_length'] = 0
response['owner'] = 'owner'
response['permanent_location'] = 'permanent_location'
Expand Down

0 comments on commit b20b17c

Please sign in to comment.