summaryrefslogtreecommitdiff
path: root/ishtar_common/utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'ishtar_common/utils.py')
-rw-r--r--ishtar_common/utils.py37
1 files changed, 36 insertions, 1 deletions
diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py
index e69ef0290..9b84804e8 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 <etienne.loks_AT_peacefrogsDOTnet>
-
# 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
@@ -2848,6 +2849,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 = "&ndash;"
+ desc = f"""<div class="row">
+ <div class="col-3 date">{published}</div>
+ <div class="col-9"><a href="{entry['link']}" target="_blank">{entry['title']}</a></div>
+</div>
+"""
+ 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