Skip to content

Commit

Permalink
templates: add language value filter
Browse files Browse the repository at this point in the history
* Adds a filter to get the value corresponding to a language in a list property.
* Moves `get_bibliographic_code_from_language` into utils functions.

Co-Authored-by: Sébastien Délèze <sebastien.deleze@rero.ch>
  • Loading branch information
Sébastien Délèze committed May 25, 2021
1 parent 3cd778e commit 75488c8
Show file tree
Hide file tree
Showing 7 changed files with 5,192 additions and 49 deletions.
20 changes: 18 additions & 2 deletions sonar/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
file_uploaded_listener
from sonar.modules.users.api import current_user_record
from sonar.modules.users.signals import add_full_name, user_registered_handler
from sonar.modules.utils import get_specific_theme, get_switch_aai_providers, \
get_view_code
from sonar.modules.utils import get_language_value, get_specific_theme, \
get_switch_aai_providers, get_view_code
from sonar.resources.projects.resource import \
RecordResource as ProjectRecordResource
from sonar.resources.projects.service import \
Expand Down Expand Up @@ -121,6 +121,22 @@ def nl2br(string):
r"""Replace \n to <br>."""
return string.replace('\n', '<br>')

@app.template_filter()
def language_value(values,
locale=None,
value_field='value',
language_field='language'):
"""Get the value of a field corresponding to the current language.
:params values: List of values with the language.
:params locale: Two digit locale to find.
:params value_field: Name of the property containing the value.
:params language_field: Name of the property containing the language.
:returns: The value corresponding to the current language.
"""
return get_language_value(values, locale, value_field,
language_field)

def create_resources(self):
"""Create resources."""
# Initialize the project resource with the corresponding service.
Expand Down
2 changes: 1 addition & 1 deletion sonar/heg/serializers/schemas/heg.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

from marshmallow import Schema, fields, post_dump, pre_dump

from sonar.modules.documents.views import get_bibliographic_code_from_language
from sonar.modules.utils import get_bibliographic_code_from_language


class HEGSchema(Schema):
Expand Down
Loading

0 comments on commit 75488c8

Please # to comment.