Skip to content

Commit

Permalink
organisations: favicon and platform name
Browse files Browse the repository at this point in the history
* Adds the display of the custom favicon for current organisation.
* Adds the organization's platform name to the browser tab.
* Closes #705.

Co-Authored-by: Bertrand Zuchuat <bertrand.zuchuat@rero.ch>
  • Loading branch information
Garfield-fr committed Mar 2, 2022
1 parent 648a7f4 commit 5cf7d74
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 13 deletions.
24 changes: 22 additions & 2 deletions sonar/ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import jinja2
import markdown
from flask import current_app, render_template, request
from flask import current_app, render_template, request, url_for
from flask_bootstrap import Bootstrap
from flask_security import user_registered
from flask_wiki import Wiki
Expand All @@ -30,6 +30,7 @@
from invenio_indexer.signals import before_record_index
from werkzeug.datastructures import MIMEAccept

from sonar.modules.organisations.utils import platform_name
from sonar.modules.permissions import has_admin_access, has_submitter_access, \
has_superuser_access
from sonar.modules.receivers import file_deleted_listener, \
Expand Down Expand Up @@ -114,7 +115,7 @@ def init_views(self, app):
"""Initialize the main flask views."""
app.url_map.converters['org_code'] = OrganisationCodeConverter

@app.route('/<org_code:view>')
@app.route('/<org_code:view>/')
def index(view):
"""Homepage."""
return render_template('sonar/frontpage.html')
Expand Down Expand Up @@ -171,6 +172,25 @@ def markdown_filter(content):
'tables'
])

@app.template_filter()
def organisation_platform_name(org):
"""Get organisation platform name."""
name = platform_name(org)
if not name:
return app.config.get('THEME_SITENAME')
return name

@app.template_filter()
def favicon(org):
"""Fav icon for current organisation."""
if org:
favicon = list(filter(
lambda d: d['mimetype'] == 'image/x-icon', org['_files']))
if favicon:
return url_for(
'invenio_records_ui.org_files',
pid_value=org.get('pid'), filename=favicon[0]['key'])

def create_resources(self):
"""Create resources."""
# Initialize the project resource with the corresponding service.
Expand Down
3 changes: 2 additions & 1 deletion sonar/modules/collections/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ def index(**kwargs):
records = RecordSearch().filter('term',
organisation__pid=kwargs['view']).scan()

return render_template('collections/index.html', records=list(records))
return render_template(
'collections/index.html', records=list(records), view=kwargs['view'])


def detail(pid, record, **kwargs):
Expand Down
30 changes: 30 additions & 0 deletions sonar/modules/organisations/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# -*- coding: utf-8 -*-
#
# Swiss Open Access Repository
# Copyright (C) 2022 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/>.

"""Utils functions for organisations module."""

import markdown
from bs4 import BeautifulSoup


def platform_name(organisation):
"""Get platform name."""
platform_name = organisation.get('platformName')
if platform_name:
html = markdown.markdown(platform_name)
return ''.join(
BeautifulSoup(html).findAll(text=True)).replace('\n', ' - ')
11 changes: 4 additions & 7 deletions sonar/modules/users/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@

"""Utils functions for user module."""

import markdown
from bs4 import BeautifulSoup
from flask_babelex import _
from flask_security import url_for_security
from flask_security.recoverable import generate_reset_password_token

from sonar.modules.organisations.utils import platform_name
from sonar.modules.utils import send_email


Expand All @@ -35,11 +34,9 @@ def send_welcome_email(user_record, user):
user_record = user_record.replace_refs()
code = user_record['organisation'].get('code', '')
plain_platform_name = 'SONAR'
platform_name = user_record['organisation'].get('platformName')
if platform_name:
html = markdown.markdown(platform_name)
plain_platform_name = ''.join(
BeautifulSoup(html).findAll(text=True)).replace('\n', ' - ')
pname = platform_name(user_record['organisation'])
if pname:
plain_platform_name = pname

token = generate_reset_password_token(user)
reset_link = url_for_security(
Expand Down
20 changes: 17 additions & 3 deletions sonar/theme/templates/sonar/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

{% set page = page|default('int') %}
{% set description = _('The SONAR project aims to create a scholarly archive that collects, promotes and preserves the publications of authors affiliated with Swiss public research institutions.') %}

<!DOCTYPE html>
<html {% if html_css_classes %} class="{{ html_css_classes|join(' ') }}" {% endif %}
lang="{{ current_i18n.locale.language|safe }}" dir="{{ current_i18n.locale.text_direction }}"
Expand Down Expand Up @@ -47,11 +46,25 @@
{%- endblock head_meta %}

{%- block head_title %}
{%- set title = title or _(config.THEME_SITENAME) or _('Invenio') %}
{# Platform name if defined on the organisation #}
{%- if g.organisation and config.SONAR_APP_DEFAULT_ORGANISATION != view %}
{%- if title %}
{%- set title = title ~ ' | ' ~ g.organisation|organisation_platform_name %}
{%- else %}
{%- set title = g.organisation|organisation_platform_name %}
{%- endif %}
{%- else %}
{%- set title = title or _(config.THEME_SITENAME) or _('Invenio') %}
{%- endif %}
<title>{{ title }}</title>
{%- endblock head_title %}

{%- block head_links %}
{# show favicon only for dedicated organisation #}
{%- set favicon = g.organisation|favicon if g.organisation and g.organisation['isDedicated'] %}
{%- if favicon %}
<link rel="icon" type="image/x-icon" href="{{ favicon }}">
{%- else %}
<meta name="msapplication-TileColor" content="#ffffff">
<meta name="msapplication-TileImage" content="/static/ms-icon-144x144.png">
<meta name="theme-color" content="#ffffff">
Expand All @@ -69,10 +82,11 @@
<link rel="icon" type="image/png" sizes="96x96" href="/static/favicon-96x96.png">
<link rel="icon" type="image/png" sizes="16x16" href="/static/favicon-16x16.png">
<link rel="manifest" href="/static/manifest.json">
{%- endif %}

{%- if canonical_url %}
<link rel="canonical" href="{{ canonical_url }}">
{% endif %}
{%- endif %}

{%- block head_links_langs %}
{%- if alternate_urls %}
Expand Down
28 changes: 28 additions & 0 deletions tests/unit/organisations/test_organisations_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# -*- coding: utf-8 -*-
#
# Swiss Open Access Repository
# Copyright (C) 2022 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/>.

"""Test organisations utils."""

from sonar.modules.organisations.utils import platform_name


def test_platform_name():
"""Test transformation of platform name."""
organisation = {
'platformName': '#SiteName\n##Platform'
}
assert 'SiteName - Platform' == platform_name(organisation)

0 comments on commit 5cf7d74

Please sign in to comment.