summaryrefslogtreecommitdiff
path: root/ishtar_common/context_processors.py
diff options
context:
space:
mode:
Diffstat (limited to 'ishtar_common/context_processors.py')
-rw-r--r--ishtar_common/context_processors.py70
1 files changed, 66 insertions, 4 deletions
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."))
+ "<form>."))
msg = msg.replace(
- str(_("form")),
+ "<form>",
f'<a href="{reverse("password_change")}">'
- f'<i class="fa fa-external-link" aria-hidden="true"></i> '
- f'{_("form")}</a>'
+ f'{_("form")} '
+ f'<i class="fa fa-external-link" aria-hidden="true"></i>'
+ '</a>'
)
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 <old-version> to <new-version>. "
+ "Check the <changelog>."))
+ else:
+ msg = str(_("Ishtar have been updated to version <new-version>. Check the "
+ "<changelog>."))
+ msg = msg.replace(
+ "<changelog>",
+ f'<a href="{reverse("changelog")}">'
+ f'{_("changelog")} '
+ f'<i class="fa fa-external-link" aria-hidden="true"></i>'
+ f'</a>'
+ ).replace("<old-version>", user_version).replace("<new-version>", current_version)
+ dct["MESSAGES"].append((msg, "info"))
+
# external sources
if (
request.user.ishtaruser.current_profile