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/utils.py | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) (limited to 'ishtar_common/utils.py') 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 -- cgit v1.2.3