From 2283d0056155265bb2fbf9cc8c9f10e71355d1ee Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Thu, 10 Oct 2024 07:55:58 +0200 Subject: ✨ Display the latest forum entries on the user home page (#5812) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ishtar_common/templates/index.html | 6 ++++++ ishtar_common/utils.py | 37 ++++++++++++++++++++++++++++++++++++- ishtar_common/views.py | 2 ++ 3 files changed, 44 insertions(+), 1 deletion(-) (limited to 'ishtar_common') diff --git a/ishtar_common/templates/index.html b/ishtar_common/templates/index.html index 69bc52617..eaaf7dd54 100644 --- a/ishtar_common/templates/index.html +++ b/ishtar_common/templates/index.html @@ -2,6 +2,12 @@ {% load i18n %} {% block content %}

{{welcome_title}}

+ {% if news_feed %} +
+

 {% trans "Latest news" %}

+ {{ news_feed|safe }} +
+ {% endif %}
{% if display_random_image %} {{random_image}} diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py index 95a7ebbaa..bf64339e4 100644 --- a/ishtar_common/utils.py +++ b/ishtar_common/utils.py @@ -1,7 +1,6 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- # Copyright (C) 2013-2016 Étienne Loks - # 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, either version 3 of the @@ -20,6 +19,7 @@ from cairosvg import svg2png from csv import QUOTE_ALL import datetime +import feedparser from functools import wraps from itertools import chain from inspect import currentframe, getframeinfo @@ -34,6 +34,7 @@ import locale import math import numpy import os +import pytz import random import re import requests @@ -2827,6 +2828,40 @@ class EachCharacterTypeValidator: ) + str(_(".")) +def get_news_feed(): + cache_key = f"{settings.PROJECT_SLUG}-news_feed" + news_feed = cache.get(cache_key) + if news_feed is None: + news_feed = update_news_feed() + cache.set(cache_key, news_feed, timeout=settings.CACHE_TIMEOUT) + return news_feed + + +def update_news_feed(): + feed = feedparser.parse(settings.ISHTAR_FEED_URL) + news_feed = [] + if "entries" in feed: + for entry in feed["entries"][:5]: + published = entry.published + try: + locale.setlocale(locale.LC_TIME, "en_GB") + d = datetime.datetime.strptime(published, "%a, %d %b %Y %H:%M:%S %z") + d_aware = d.replace(tzinfo=pytz.timezone(settings.TIME_ZONE)) + lang_code = settings.LANGUAGE_CODE.split("-") + lang_code = lang_code[0] + "_" + lang_code[1].upper() + locale.setlocale(locale.LC_TIME, lang_code) + published = d_aware.strftime("%d %b %Y %H:%M") + except ValueError: + published = "–" + desc = f"""
+
{published}
+ +
+""" + news_feed.append(desc) + return "".join(news_feed) + + # picked from Django 3.2 to assure 128 bites salt - should be removed on upgrade class Argon2PasswordHasher(BaseArgon2PasswordHasher): salt_entropy = 128 diff --git a/ishtar_common/views.py b/ishtar_common/views.py index a6c3b373d..557fb1412 100644 --- a/ishtar_common/views.py +++ b/ishtar_common/views.py @@ -82,6 +82,7 @@ from ishtar_common.utils import ( get_field_labels_from_path, get_person_gdpr_log, get_random_item_image_link, + get_news_feed, shortify, dict_to_tuple, put_session_message, @@ -207,6 +208,7 @@ def index(request): ) ) authenticated = request.user.is_authenticated + dct["news_feed"] = get_news_feed() display_random_image = authenticated and profile.homepage_random_image_available if display_random_image: dct["display_random_image"] = True -- cgit v1.2.3