def test_language_iso639_2to1(app): """Test convert MARC language code to language.""" assert language_iso639_2to1('eng') == 'en' assert language_iso639_2to1('fre') == 'fr' assert language_iso639_2to1('ger') == 'de' assert language_iso639_2to1('ita') == 'it' # default language assert language_iso639_2to1('rus') == 'en'
def get_notification_context(cls, notifications=None): """Get the context to render the notification template.""" context = {} notifications = notifications or [] if not notifications: return context context['loans'] = [] doc_dumper = DocumentGenericDumper() item_dumper = ItemNotificationDumper() patron_dumper = PatronNotificationDumper() for notification in notifications: loan = notification.loan creation_date = format_date_filter( notification.get('creation_date'), date_format='medium', locale=language_iso639_2to1( notification.get_language_to_use())) request_expire_date = format_date_filter( loan.get('request_expire_date'), date_format='medium', locale=language_iso639_2to1( notification.get_language_to_use())) # merge doc and item metadata preserving document key item_data = notification.item.dumps(dumper=item_dumper) doc_data = notification.document.dumps(dumper=doc_dumper) doc_data = {**item_data, **doc_data} # pickup location name --> !! pickup is on notif.request_loan, not # on notif.loan request_loan = notification.request_loan pickup_location = Location.get_record_by_pid( request_loan.get('pickup_location_pid')) if not pickup_location: pickup_location = Location.get_record_by_pid( request_loan.get('transaction_location_pid')) # request_patron request_patron = Patron.get_record_by_pid( request_loan.get('patron_pid')) loan_context = { 'creation_date': creation_date, 'document': doc_data, 'pickup_name': pickup_location.get('pickup_name', pickup_location.get('name')), 'request_expire_date': request_expire_date, 'patron': request_patron.dumps(dumper=patron_dumper) } context['loans'].append(loan_context) return context
def get_notification_context(cls, notifications=None): """Get the context to render the notification template.""" context = {'loans': []} notifications = notifications or [] doc_dumper = DocumentGenericDumper() item_dumper = ItemNotificationDumper() patron_dumper = PatronNotificationDumper() for notification in notifications: loan = notification.loan creation_date = format_date_filter( notification.get('creation_date'), date_format='medium', locale=language_iso639_2to1(notification.get_language_to_use()) ) # merge doc and item metadata preserving document key item_data = notification.item.dumps(dumper=item_dumper) doc_data = notification.document.dumps(dumper=doc_dumper) doc_data = {**item_data, **doc_data} # pickup location name pickup_location = notification.pickup_location if not pickup_location: pickup_location = notification.transaction_location loan_context = { 'creation_date': creation_date, 'in_transit': loan.state in LoanState.ITEM_IN_TRANSIT, 'document': doc_data, 'pickup_name': pickup_location.get( 'pickup_name', pickup_location.get('name')), 'patron': notification.patron.dumps(dumper=patron_dumper) } context['loans'].append(loan_context) return context
def get_notification_context(cls, notifications=None): """Get the context to render the notification template.""" context = {'loans': []} notifications = notifications or [] doc_dumper = DocumentGenericDumper() item_dumper = ItemNotificationDumper() lib_dumper = LibraryCirculationNotificationDumper() for notification in notifications: trans_lib = notification.transaction_library creation_date = format_date_filter( notification.get('creation_date'), date_format='medium', locale=language_iso639_2to1( notification.get_language_to_use())) # merge doc and item metadata preserving document key item_data = notification.item.dumps(dumper=item_dumper) doc_data = notification.document.dumps(dumper=doc_dumper) doc_data = {**item_data, **doc_data} loan_context = { 'creation_date': creation_date, 'document': doc_data, 'transaction_library': trans_lib.dumps(dumper=lib_dumper) } context['loans'].append(loan_context) return context
def get_notification_context(cls, notifications=None): """Get the context to render the notification template.""" # REMINDERS notifications are always aggregated by library and by # patron. So we could use the first notification of the list to get # global information about these data. We need to loop on each # notification to get the documents information related to each loan. context = {} notifications = notifications or [] if not notifications: return context patron = notifications[0].patron library = notifications[0].library include_address = notifications[0].get_communication_channel == \ NotificationChannel.MAIL # Dump basic informations context.update({ 'include_patron_address': include_address, 'patron': patron.dumps(dumper=PatronNotificationDumper()), 'library': library.dumps(dumper=LibraryCirculationNotificationDumper()), 'loans': [] }) # Add metadata for any ``notification.loan`` of the notifications list doc_dumper = DocumentGenericDumper() item_dumper = ItemNotificationDumper() language = language_iso639_2to1(notifications[0].get_language_to_use()) for notification in notifications: end_date = notification.loan.get('end_date') counter = notification.get('context', {})\ .get('reminder_counter', 0) counter += 1 literal_counter = num2words(counter, to='ordinal', lang=language) if end_date: end_date = ciso8601.parse_datetime(end_date) end_date = end_date.strftime("%d.%m.%Y") # merge doc and item metadata preserving document key item_data = notification.item.dumps(dumper=item_dumper) doc_data = notification.document.dumps(dumper=doc_dumper) doc_data = {**item_data, **doc_data} context['loans'].append({ 'document': doc_data, 'end_date': end_date, 'reminder_counter': literal_counter }) return context