diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2024-10-10 08:01:08 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2024-10-16 13:51:43 +0200 |
commit | f94dc19857cec3cc9d8443228454b7e264fffa0e (patch) | |
tree | 8199a6bafd87318b94165bbb1570c8be83da0a4b | |
parent | 2283d0056155265bb2fbf9cc8c9f10e71355d1ee (diff) | |
download | Ishtar-f94dc19857cec3cc9d8443228454b7e264fffa0e.tar.bz2 Ishtar-f94dc19857cec3cc9d8443228454b7e264fffa0e.zip |
✨ display forum entries: activate/deactivate by user profile - profile form: edit news/forum display
-rw-r--r-- | ishtar_common/forms_common.py | 11 | ||||
-rw-r--r-- | ishtar_common/migrations/0253_display_news.py | 24 | ||||
-rw-r--r-- | ishtar_common/models.py | 1 | ||||
-rw-r--r-- | ishtar_common/templates/index.html | 14 | ||||
-rw-r--r-- | ishtar_common/templates/ishtar/blocks/news_feed.html | 5 | ||||
-rw-r--r-- | ishtar_common/templates/ishtar/forms/profile.html | 8 | ||||
-rw-r--r-- | ishtar_common/urls.py | 1 | ||||
-rw-r--r-- | ishtar_common/utils.py | 14 | ||||
-rw-r--r-- | ishtar_common/views.py | 11 |
9 files changed, 79 insertions, 10 deletions
diff --git a/ishtar_common/forms_common.py b/ishtar_common/forms_common.py index 495499425..b1e4e7ef4 100644 --- a/ishtar_common/forms_common.py +++ b/ishtar_common/forms_common.py @@ -1631,6 +1631,12 @@ class ProfilePersonForm(forms.Form): Edit the current profile """ + display_forum_entries = forms.BooleanField( + label=_("Display forum entries"), required=False + ) + display_news = forms.BooleanField( + label=_("Display news"), required=False + ) current_profile = forms.ChoiceField(label=_("Current profile"), choices=[]) name = forms.CharField(label=_("Name"), required=False) profile_type = forms.ChoiceField( @@ -1664,6 +1670,8 @@ class ProfilePersonForm(forms.Form): initial["name"] = current_profile.name or current_profile.profile_type initial["profile_type"] = current_profile.profile_type.pk initial["auto_pin"] = current_profile.auto_pin + initial["display_forum_entries"] = self.user.ishtaruser.display_forum_entries + initial["display_news"] = self.user.ishtaruser.display_news # initial["display_pin_menu"] = current_profile.display_pin_menu kwargs["initial"] = initial super(ProfilePersonForm, self).__init__(*args, **kwargs) @@ -1786,6 +1794,9 @@ class ProfilePersonForm(forms.Form): for src in q.all(): if self.cleaned_data.get(f"external_source_{src.pk}", False): profile.external_sources.add(src) + self.user.ishtaruser.display_forum_entries = self.cleaned_data["display_forum_entries"] + self.user.ishtaruser.display_news = self.cleaned_data["display_news"] + self.user.ishtaruser.save() clean_session_cache(session) if "EXTERNAL_SOURCES" in session: session.pop("EXTERNAL_SOURCES") diff --git a/ishtar_common/migrations/0253_display_news.py b/ishtar_common/migrations/0253_display_news.py new file mode 100644 index 000000000..46b026d00 --- /dev/null +++ b/ishtar_common/migrations/0253_display_news.py @@ -0,0 +1,24 @@ +# Generated by Django 2.2.24 on 2024-08-28 11:45 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('ishtar_common', '0252_export_formats_def_head_len'), + ] + + operations = [ + migrations.AddField( + model_name='ishtaruser', + name='display_forum_entries', + field=models.BooleanField(default=True, verbose_name='Display forum entries'), + ), + migrations.AlterField( + model_name='ishtarsiteprofile', + name='no_context_button', + field=models.ForeignKey(blank=True, help_text='If provided a button is displayed on find add page to create a "No context" find', null=True, on_delete=django.db.models.deletion.SET_NULL, to='archaeological_context_records.ContextRecord', verbose_name='Context record for no context button'), + ), + ] diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 688197c73..c8b4bfee3 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -3741,6 +3741,7 @@ class IshtarUser(FullSearch): latest_news_version = models.CharField(_("Latest news version"), default="", blank=True, max_length=20) display_news = models.BooleanField(_("Display news"), default=True) + display_forum_entries = models.BooleanField(_("Display forum entries"), default=True) class Meta: verbose_name = _("Ishtar user") diff --git a/ishtar_common/templates/index.html b/ishtar_common/templates/index.html index eaaf7dd54..47c51223e 100644 --- a/ishtar_common/templates/index.html +++ b/ishtar_common/templates/index.html @@ -2,12 +2,6 @@ {% load i18n %} {% block content %}<div id="welcome" class="container"> <h2>{{welcome_title}}</h2> - {% if news_feed %} - <div id="news-feed" class="card p-3 mb-4"> - <h3><i class="fa fa-newspaper-o" aria-hidden="true"></i> {% trans "Latest news" %}</h3> - {{ news_feed|safe }} - </div> - {% endif %} <div class="row"> {% if display_random_image %} {{random_image}} @@ -19,6 +13,9 @@ </div> </div> </div> + {% if news_feed %} + <div id="news-feed" class="col col-12 mt-4"></div> + {% endif %} {% if display_statistics %} <div class="col col-lg-12 lead mt-4"> {% for label, number in statistics %} @@ -28,3 +25,8 @@ {% endif %} </div> </div>{% endblock %} +{% block end_js %} +$(document).ready(function() { + dynamic_load("/news-feed/", "#news-feed"); +}); +{% endblock %} diff --git a/ishtar_common/templates/ishtar/blocks/news_feed.html b/ishtar_common/templates/ishtar/blocks/news_feed.html new file mode 100644 index 000000000..63524303a --- /dev/null +++ b/ishtar_common/templates/ishtar/blocks/news_feed.html @@ -0,0 +1,5 @@ +{% load i18n %} +{% if news_feed %}<div class="container card p-3"> + <h3><i class="fa fa-newspaper-o" aria-hidden="true"></i> {% trans "Latest forum entries" %}</h3> + {{ news_feed|safe }} +</div>{% endif %} diff --git a/ishtar_common/templates/ishtar/forms/profile.html b/ishtar_common/templates/ishtar/forms/profile.html index 795ab6ab1..f4e3f5b41 100644 --- a/ishtar_common/templates/ishtar/forms/profile.html +++ b/ishtar_common/templates/ishtar/forms/profile.html @@ -14,7 +14,6 @@ $(document).ready(function(){ {% block content %} <div class="container"> -<h2>{{page_name}}</h2> <div class='form'> {% if form.non_field_errors %} @@ -30,9 +29,14 @@ $(document).ready(function(){ </div>{% endif %} {% endfor %} + <h2>{% trans "Edit user preferences" %}</h2> + <h3>{% trans "Global preferences" %}</h3> <div class="form-row"> {% for field in form.visible_fields %} - {% if forloop.counter0 == 0 %} + {% if forloop.counter0 == 2 %} + </div> + <h3>{% trans "Current profile" %}</h3> + <div class="form-row"> <div class="form-group col-12"><div class="col-lg-6"> {{field|bs_field|safe}} </div></div> diff --git a/ishtar_common/urls.py b/ishtar_common/urls.py index babdece35..a205e3b01 100644 --- a/ishtar_common/urls.py +++ b/ishtar_common/urls.py @@ -45,6 +45,7 @@ urlpatterns = [ url(r"^js/settings.js", views.settings_js, name="settings-js"), # General url(r"shortcut_menu/", views.shortcut_menu, name="shortcut-menu"), + url(r"news-feed/", views.display_news_feed, name="news-feed"), url( r"display/(?P<item_type>\w+)/(?P<pk>\d+)/", views.DisplayItemView.as_view(), diff --git a/ishtar_common/utils.py b/ishtar_common/utils.py index bf64339e4..b62ee1ddf 100644 --- a/ishtar_common/utils.py +++ b/ishtar_common/utils.py @@ -2833,12 +2833,22 @@ def get_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) + # "" could be a temporary unavailability of the forum re-test 5 minutes later + timeout = settings.CACHE_TIMEOUT if news_feed != "" else 60 * 5 + cache.set(cache_key, news_feed, timeout=timeout) return news_feed def update_news_feed(): - feed = feedparser.parse(settings.ISHTAR_FEED_URL) + try: + response = requests.get(settings.ISHTAR_FEED_URL, timeout=5) + except requests.ReadTimeout: + logger.warning(f"Timeout when reading RSS {settings.ISHTAR_FEED_URL}") + return "" + + # Put it to memory stream object universal feedparser + content = io.BytesIO(response.content) + feed = feedparser.parse(content) news_feed = [] if "entries" in feed: for entry in feed["entries"][:5]: diff --git a/ishtar_common/views.py b/ishtar_common/views.py index 557fb1412..780a2253b 100644 --- a/ishtar_common/views.py +++ b/ishtar_common/views.py @@ -242,6 +242,17 @@ def index(request): return render(request, "index.html", dct) +def display_news_feed(request): + """ + Display news feed + """ + news_feed = "" + if hasattr(request.user, "ishtaruser") and \ + request.user.ishtaruser and request.user.ishtaruser.display_forum_entries: + news_feed = get_news_feed() + return render(request, "ishtar/blocks/news_feed.html", {"news_feed": news_feed}) + + class LoginView(auth_view.LoginView): form_class = forms.AuthenticationForm |