Skip to content

Commit

Permalink
Merge pull request #1008 from ae-utbm/feed
Browse files Browse the repository at this point in the history
Add atom/rss news feed
  • Loading branch information
klmp200 authored Jan 20, 2025
2 parents c7ae709 + 5db9819 commit c555d5c
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 6 deletions.
5 changes: 5 additions & 0 deletions com/static/com/css/news-list.scss
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@
&:not(:first-of-type) {
margin: 2em 0 1em 0;
}

.feed {
float: right;
color: #f26522;
}
}

@media screen and (max-width: $small-devices) {
Expand Down
13 changes: 11 additions & 2 deletions com/templates/com/news_list.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
{% block additional_css %}
<link rel="stylesheet" href="{{ static('com/css/news-list.scss') }}">
<link rel="stylesheet" href="{{ static('com/components/ics-calendar.scss') }}">

{# Atom feed discovery, not really css but also goes there #}
<link rel="alternate" type="application/rss+xml" title="{% trans %}News feed{% endtrans %}" href="{{ url("com:news_feed") }}">
{% endblock %}

{% block additional_js %}
Expand All @@ -19,7 +22,10 @@
<div id="news">
<div id="left_column" class="news_column">
{% set events_dates = NewsDate.objects.filter(end_date__gte=timezone.now(), start_date__lte=timezone.now()+timedelta(days=5), news__is_moderated=True).datetimes('start_date', 'day') %}
<h3>{% trans %}Events today and the next few days{% endtrans %}</h3>
<h3>
{% trans %}Events today and the next few days{% endtrans %}
<a target="#" href="{{ url("com:news_feed") }}"><i class="fa fa-rss feed"></i></a>
</h3>
{% if user.is_authenticated and (user.is_com_admin or user.memberships.board().ongoing().exists()) %}
<a class="btn btn-blue margin-bottom" href="{{ url("com:news_new") }}">
<i class="fa fa-plus"></i>
Expand Down Expand Up @@ -73,7 +79,10 @@
</div>
{% endif %}

<h3>{% trans %}All coming events{% endtrans %}</h3>
<h3>
{% trans %}All coming events{% endtrans %}
<a target="#" href="{{ url("com:news_feed") }}"><i class="fa fa-rss feed"></i></a>
</h3>
<ics-calendar locale="{{ get_language() }}"></ics-calendar>
</div>

Expand Down
15 changes: 14 additions & 1 deletion com/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,15 @@

import pytest
from django.conf import settings
from django.contrib.sites.models import Site
from django.core.files.uploadedfile import SimpleUploadedFile
from django.test import TestCase
from django.urls import reverse
from django.utils import html
from django.utils.timezone import localtime, now
from django.utils.translation import gettext as _
from model_bakery import baker
from pytest_django.asserts import assertRedirects
from pytest_django.asserts import assertNumQueries, assertRedirects

from club.models import Club, Membership
from com.models import News, NewsDate, Poster, Sith, Weekmail, WeekmailArticle
Expand Down Expand Up @@ -319,3 +320,15 @@ def test_ics_updated(self):
self.valid_payload,
)
mocked.assert_called()


@pytest.mark.django_db
def test_feed(client):
"""Smoke test that checks that the atom feed is working"""
Site.objects.clear_cache()
with assertNumQueries(2):
# get sith domain with Site api: 1 request
# get all news and related info: 1 request
resp = client.get(reverse("com:news_feed"))
assert resp.status_code == 200
assert resp.headers["Content-Type"] == "application/rss+xml; charset=utf-8"
2 changes: 2 additions & 0 deletions com/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
NewsCreateView,
NewsDeleteView,
NewsDetailView,
NewsFeed,
NewsListView,
NewsModerateView,
NewsUpdateView,
Expand Down Expand Up @@ -73,6 +74,7 @@
name="weekmail_article_edit",
),
path("news/", NewsListView.as_view(), name="news_list"),
path("news/feed/", NewsFeed(), name="news_feed"),
path("news/admin/", NewsAdminListView.as_view(), name="news_admin_list"),
path("news/create/", NewsCreateView.as_view(), name="news_new"),
path("news/<int:news_id>/edit/", NewsUpdateView.as_view(), name="news_edit"),
Expand Down
30 changes: 30 additions & 0 deletions com/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@
from smtplib import SMTPRecipientsRefused
from typing import Any

from dateutil.relativedelta import relativedelta
from django.conf import settings
from django.contrib.auth.mixins import AccessMixin, PermissionRequiredMixin
from django.contrib.syndication.views import Feed
from django.core.exceptions import PermissionDenied, ValidationError
from django.db.models import Max
from django.forms.models import modelform_factory
Expand Down Expand Up @@ -268,6 +270,34 @@ def get_context_data(self, **kwargs):
return super().get_context_data(**kwargs) | {"date": self.object.dates.first()}


class NewsFeed(Feed):
title = _("News")
link = reverse_lazy("com:news_list")
description = _("All incoming events")

def items(self):
return (
NewsDate.objects.filter(
news__is_moderated=True,
end_date__gte=timezone.now() - (relativedelta(months=6)),
)
.select_related("news", "news__author")
.order_by("-start_date")
)

def item_title(self, item: NewsDate):
return item.news.title

def item_description(self, item: NewsDate):
return item.news.summary

def item_link(self, item: NewsDate):
return item.news.get_absolute_url()

def item_author_name(self, item: NewsDate):
return item.news.author.get_display_name()


# Weekmail


Expand Down
7 changes: 6 additions & 1 deletion core/management/commands/populate.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ def handle(self, *args, **options):
raise Exception("Never call this command in prod. Never.")

Sith.objects.create(weekmail_destinations="[email protected] [email protected]")
Site.objects.create(domain=settings.SITH_URL, name=settings.SITH_NAME)

site = Site.objects.get_current()
site.domain = settings.SITH_URL
site.name = settings.SITH_NAME
site.save()

groups = self._create_groups()
self._create_ban_groups()

Expand Down
12 changes: 10 additions & 2 deletions locale/fr/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
msgid ""
msgstr ""
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2025-01-10 14:52+0100\n"
"POT-Creation-Date: 2025-01-19 18:12+0100\n"
"PO-Revision-Date: 2016-07-18\n"
"Last-Translator: Maréchal <[email protected]\n"
"Language-Team: AE info <[email protected]>\n"
Expand Down Expand Up @@ -1447,7 +1447,7 @@ msgid "News admin"
msgstr "Administration des nouvelles"

#: com/templates/com/news_admin_list.jinja com/templates/com/news_detail.jinja
#: com/templates/com/news_list.jinja
#: com/templates/com/news_list.jinja com/views.py
msgid "News"
msgstr "Nouvelles"

Expand Down Expand Up @@ -1525,6 +1525,10 @@ msgstr "Éditer (sera soumise de nouveau à la modération)"
msgid "Edit news"
msgstr "Éditer la nouvelle"

#: com/templates/com/news_list.jinja
msgid "News feed"
msgstr "Flux d'actualités"

#: com/templates/com/news_list.jinja
msgid "Events today and the next few days"
msgstr "Événements aujourd'hui et dans les prochains jours"
Expand Down Expand Up @@ -1767,6 +1771,10 @@ msgstr "Message d'alerte"
msgid "Screens list"
msgstr "Liste d'écrans"

#: com/views.py
msgid "All incoming events"
msgstr "Tous les événements à venir"

#: com/views.py
msgid "Delete and save to regenerate"
msgstr "Supprimer et sauver pour régénérer"
Expand Down

0 comments on commit c555d5c

Please sign in to comment.