Skip to content

Commit

Permalink
messages: Add message cli
Browse files Browse the repository at this point in the history
* Adds messages cli.
* Adds `message_footer` info to footer.

Co-Authored-by: Peter Weber <[email protected]>
  • Loading branch information
rerowep committed Jan 15, 2025
1 parent dba2d83 commit de1505f
Show file tree
Hide file tree
Showing 10 changed files with 494 additions and 316 deletions.
2 changes: 1 addition & 1 deletion rero_ils/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from jinja2 import TemplateNotFound
from markupsafe import Markup

from .modules.message import Message
from .modules.messages import Message
from .modules.utils import extracted_data_from_ref


Expand Down
94 changes: 94 additions & 0 deletions rero_ils/modules/cli/messages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# -*- coding: utf-8 -*-
#
# RERO ILS
# Copyright (C) 2019-2024 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/>.

"""Click command-line interface for item record management."""

import click
from flask.cli import with_appcontext

from rero_ils.modules.messages import Message


@click.group()
def messages():
"""Messages commands."""


def abort_if_false(ctx, param, value):
"""Abort command is value is False."""
if not value:
ctx.abort()


@messages.command()
@with_appcontext
def info():
"""Messages info."""
click.secho(f"{'KEY':<20}{'TYPE':<15}MESSAGE", fg="green")
for key, data in Message.get_all_messages().items():
click.secho(f'{key:<20}{data["type"]:<15}"{data["message"]}"', fg="green")


@messages.command()
@click.argument("key")
@with_appcontext
def get(key):
"""Get messages for key."""
if message := Message.get(key):
click.secho(f'{key:<20}{message["type"]:<15}"{message["message"]}"', fg="green")
else:
click.secho(f"{key:<20}KEY NOT FOUND!", fg="red")
raise click.BadParameter


@messages.command("set")
@click.argument("key")
@click.argument("type")
@click.argument("message")
@click.option("-t", "--timeout", "timeout", default=0)
@with_appcontext
def set_message(key, type, message, timeout):
"""Set messages for name."""
msg = f'{key:<20}{type:<15}"{message}"'
if Message.set(key=key, type=type, value=message, timeout=timeout):
fg = "green"
msg = f"OK: {msg}"
else:
fg = "red"
msg = f"ERROR: {msg}"
click.secho(msg, fg=fg)
if fg == "red":
raise click.BadParameter


@messages.command()
@click.argument("key")
@click.option(
"--yes-i-know",
is_flag=True,
callback=abort_if_false,
expose_value=False,
prompt="Do you really want to delete the message?",
)
@with_appcontext
def delete(key):
"""Delete message for name."""
if Message.delete(key=key):
click.secho(f"{key:<20}DELETED", fg="yellow")
else:
click.secho(f"{key:<20}KEY NOT FOUND!", fg="red")
raise click.BadParameter
2 changes: 2 additions & 0 deletions rero_ils/modules/cli/reroils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

from rero_ils.modules.acquisition.cli import acquisition
from rero_ils.modules.apiharvester.cli import apiharvester
from rero_ils.modules.cli.messages import messages
from rero_ils.modules.ebooks.cli import oaiharvester
from rero_ils.modules.entities.remote_entities.cli import entity
from rero_ils.modules.migrations.cli import migrations
Expand All @@ -48,6 +49,7 @@ def reroils():
reroils.add_command(fixtures)
reroils.add_command(index)
reroils.add_command(migrations)
reroils.add_command(messages)
reroils.add_command(monitoring)
reroils.add_command(notifications)
reroils.add_command(oaiharvester)
Expand Down
30 changes: 28 additions & 2 deletions rero_ils/modules/message.py → rero_ils/modules/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@

"""Message interface."""

from flask import current_app
from flask_caching.backends import RedisCache
from invenio_cache.proxies import current_cache
from markupsafe import Markup
from redis import Redis


class Message:
Expand All @@ -27,7 +30,7 @@ class Message:
prefix = "message_"

@classmethod
def set(cls, key, type, value):
def set(cls, key, type, value, timeout=0):
"""Set value.
:param key: the cache key.
Expand All @@ -36,7 +39,7 @@ def set(cls, key, type, value):
:return: True if the insertion went well.
"""
data = {"type": type or "primary", "message": Markup(value)}
return current_cache.set(f"{cls.prefix}{key}", data)
return current_cache.set(f"{cls.prefix}{key}", data, timeout=0)

@classmethod
def get(cls, key):
Expand All @@ -55,3 +58,26 @@ def delete(cls, key):
:return: True if the removal went well.
"""
return current_cache.delete(f"{cls.prefix}{key}")

@classmethod
def get_all_messages(cls):
"""Get All Messages."""
messages = {}
if isinstance(current_cache.cache, RedisCache):
# current_cache for REDIS has no function to get all values. Get them directly from REDIS.
if url := current_app.config.get("CACHE_REDIS_URL"):
redis = Redis.from_url(url)
redis_keys = [
redis_key.decode("utf-8").replace(f"cache::{cls.prefix}", "")
for redis_key in redis.scan_iter(f"cache::{cls.prefix}*")
]
for key in redis_keys:
messages[key] = cls.get(key)
else:
# needed for tests
messages = {
key.replace(f"{cls.prefix}", ""): current_cache.get(key)
for key in current_cache.cache._cache
}

return messages
Original file line number Diff line number Diff line change
Expand Up @@ -130,3 +130,4 @@ <h5>Altri cataloghi</h5>
</ul>
{% endif %}
</section>

4 changes: 4 additions & 0 deletions rero_ils/theme/templates/rero_ils/footer.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
{% if config.RERO_ILS_PRIVACY_POLICY_URL %} |
<a href="{{ config.RERO_ILS_PRIVACY_POLICY_URL }}" target="_blank">{{ _('Privacy policy') }}</a>
{% endif %}
{% set footer_message = footer_message() %}
{% if footer_message %} |
<a>{{ footer_message }}</a>
{% endif %}
</p>
</div>
</footer>
8 changes: 8 additions & 0 deletions rero_ils/theme/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from invenio_jsonschemas.errors import JSONSchemaNotFound
from invenio_jsonschemas.proxies import current_refresolver_store

from rero_ils.modules.messages import Message
from rero_ils.modules.organisations.api import Organisation
from rero_ils.modules.utils import cached
from rero_ils.permissions import can_access_professional_view
Expand Down Expand Up @@ -184,6 +185,13 @@ def view_organisation_name(viewcode):
return current_app.config.get("RERO_ILS_SEARCH_GLOBAL_NAME", "")


@blueprint.add_app_template_global
def footer_message():
"""Get footer message."""
if message := Message.get("footer"):
return message["message"]


def prepare_jsonschema(schema):
"""Json schema prep."""
schema = copy.deepcopy(schema)
Expand Down
3 changes: 3 additions & 0 deletions scripts/setup
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,9 @@ info_msg "Initialize wiki search"
eval ${PREFIX} invenio flask_wiki init-index
eval ${PREFIX} invenio flask_wiki index

info_msg "Set footer date"
eval ${PREFIX} invenio reroils messages set footer info `date +'%d-%m-%Y'`

date
success_msg "Perfect ${PROGRAM}! See you soon…"
exit 0
Loading

0 comments on commit de1505f

Please sign in to comment.