From 71a256dc52ed3391638dcf9669cf57d75475d326 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Thu, 6 Apr 2023 18:36:47 +0200 Subject: Display of a changelog with alert display when updates are made --- ishtar_common/context_processors.py | 70 ++++++++++++++++++++++++++++++++++--- 1 file changed, 66 insertions(+), 4 deletions(-) (limited to 'ishtar_common/context_processors.py') diff --git a/ishtar_common/context_processors.py b/ishtar_common/context_processors.py index c1d1224ea..8131e0382 100644 --- a/ishtar_common/context_processors.py +++ b/ishtar_common/context_processors.py @@ -18,6 +18,8 @@ # See the file COPYING for details. import datetime +import os +import re from django.conf import settings from django.core.cache import cache @@ -33,6 +35,45 @@ from bootstrap_datepicker.widgets import DatePicker from .menus import Menu +def _get_changelog_version_from_file(): + changelog_dir = os.path.join( + settings.SHARE_BASE_PATH, + "changelog", + settings.LANGUAGE_CODE.split('-')[0] + ) + if not os.path.exists(changelog_dir): + return "no-version" + changelog_file = os.path.join(changelog_dir, f"changelog_1.md") + if not os.path.exists(changelog_file): + return "no-version" + current_version = None + with open(changelog_file, "r") as changelog: + for line in changelog.readlines(): + m = re.match(r"v(\d+)\.(\d+)\.(\d+)", line) + if not m: + continue + g = m.groups() + if len(g) != 3: + continue + current_version = ".".join(g) + break + if not current_version: + return "no-version" + return current_version + + +def get_changelog_version(): + cache_key = f"{settings.PROJECT_SLUG}-news-version" + current_version = cache.get(cache_key) + if current_version: + return current_version + + if not current_version: + current_version = _get_changelog_version_from_file() + cache.set(cache_key, current_version, settings.CACHE_LONGTIMEOUT) + return current_version + + def get_base_context(request): dct = { "URL_PATH": settings.URL_PATH, @@ -83,15 +124,36 @@ def get_base_context(request): cache.set(key, password_expired, settings.CACHE_TIMEOUT) if password_expired and not request.path.endswith("password_change/"): msg = str(_("Your password has expired. Please update it using this " - "form.")) + "
.")) msg = msg.replace( - str(_("form")), + "", f'' - f' ' - f'{_("form")}' + f'{_("form")} ' + f'' + '' ) dct["MESSAGES"].append((msg, "warning")) + # check changelog + if request.user.ishtaruser.display_news: + user_version = request.user.ishtaruser.latest_news_version + current_version = get_changelog_version() + if current_version != user_version and "changelog" not in dct["CURRENT_PATH"]: + if user_version: + msg = str(_("Ishtar have been updated from version to . " + "Check the .")) + else: + msg = str(_("Ishtar have been updated to version . Check the " + ".")) + msg = msg.replace( + "", + f'' + f'{_("changelog")} ' + f'' + f'' + ).replace("", user_version).replace("", current_version) + dct["MESSAGES"].append((msg, "info")) + # external sources if ( request.user.ishtaruser.current_profile -- cgit v1.2.3