Skip to content

Commit cf6df2b

Browse files
tristan957FennemAI
andauthored
Add a /commitfests.ics feed (#118)
This feed is a calendar feed of all commitfests in iCalendar format (RFC 5545[0]). Calendaring software can subscribe to this feed to show commitfests. Link: https://icalendar.org/RFC-Specifications/iCalendar-RFC-5545/ [0] --------- Signed-off-by: Tristan Partin <tristan@partin.io> Co-authored-by: Jelterminator FennemAI-Neo <fennemai@jeltef.nl>
1 parent f749a37 commit cf6df2b

6 files changed

Lines changed: 98 additions & 2 deletions

File tree

‎pgcommitfest/commitfest/feeds.py‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
1+
from __future__ import annotations
2+
13
from django.contrib.syndication.views import Feed
24

5+
from datetime import timedelta
6+
from typing import TYPE_CHECKING
7+
8+
from icalendar import Calendar, Event
9+
10+
if TYPE_CHECKING:
11+
from collections.abc import Iterable
12+
13+
from .models import CommitFest
14+
315

416
class ActivityFeed(Feed):
517
title = description = "Commitfest Activity Log"
@@ -34,3 +46,31 @@ def item_link(self, item):
3446

3547
def item_pubdate(self, item):
3648
return item["date"]
49+
50+
51+
def calendar(commitfests: Iterable[CommitFest]) -> Calendar:
52+
"""
53+
Build an RFC 5545 iCalendar document with one all-day VEVENT per
54+
commitfest, covering its start and end date.
55+
"""
56+
calendar = Calendar.new(
57+
prodid="-//PostgreSQL Commitfest//commitfest.postgresql.org//EN",
58+
name="PostgreSQL Commitfests",
59+
description="Start and end dates of PostgreSQL Commitfests",
60+
method="PUBLISH",
61+
)
62+
63+
for cf in commitfests:
64+
event = Event.new(
65+
uid=f"commitfest-{cf.name}@commitfest.postgresql.org",
66+
summary=cf.title,
67+
start=cf.startdate,
68+
# DTEND is exclusive for all-day events, so add a day to make the
69+
# last day of the commitfest show up as included on the calendar.
70+
end=cf.enddate + timedelta(days=1),
71+
description=f"Status: {cf.statusstring}",
72+
url=f"https://commitfest.postgresql.org/{cf.id}/",
73+
)
74+
calendar.add_component(event)
75+
76+
return calendar
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{%extends "base.html"%}
22
{%block contents%}
3+
<p><a href="/commitfests.ics">Subscribe to Commitfest dates (ICS)</a></p>
34
<ul>
45
{%for c in commitfests%}
56
<li><a href="/{{c.id}}/">{{c}}</a> ({{c.statusstring}} - {{c.periodstring}})</li>
67
{%endfor%}
78
</ul>
89
<br/>
910
{%endblock%}
10-
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""
2+
Tests for the /commitfests.ics calendar feed.
3+
"""
4+
5+
from __future__ import annotations
6+
7+
from datetime import timedelta
8+
from typing import TYPE_CHECKING
9+
10+
import pytest
11+
from icalendar import Calendar
12+
13+
if TYPE_CHECKING:
14+
from pgcommitfest.commitfest.models import CommitFest
15+
16+
pytestmark = pytest.mark.django_db
17+
18+
19+
def test_commitfests_ics(client, commitfests: dict[str, CommitFest]):
20+
"""
21+
Every non-draft commitfest, regardless of status, shows up as an all-day
22+
VEVENT with DTEND being the day after enddate (exclusive).
23+
"""
24+
commitfests = {k: cf for k, cf in commitfests.items() if not cf.draft}
25+
26+
response = client.get("/commitfests.ics")
27+
assert response.status_code == 200
28+
assert response["Content-Type"] == "text/calendar; charset=utf-8"
29+
30+
events = Calendar.from_ical(response.content).walk("VEVENT")
31+
assert len(events) == len(commitfests)
32+
events_by_uid = {str(event.uid): event for event in events}
33+
34+
for cf in commitfests.values():
35+
event = events_by_uid[f"commitfest-{cf.name}@commitfest.postgresql.org"]
36+
assert event.summary == f"Commitfest {cf.name}"
37+
assert event.url == f"https://commitfest.postgresql.org/{cf.pk}/"
38+
assert event.description == f"Status: {cf.statusstring}"
39+
assert event.start == cf.startdate
40+
assert event.end == cf.enddate + timedelta(days=1)

‎pgcommitfest/commitfest/views.py‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from django.db.models import Count, Q
77
from django.http import (
88
Http404,
9+
HttpRequest,
910
HttpResponse,
1011
HttpResponseForbidden,
1112
HttpResponsePermanentRedirect,
@@ -27,7 +28,7 @@
2728
from pgcommitfest.userprofile.util import UserWrapper
2829

2930
from .ajax import _archivesAPI, doAttachThread, refresh_single_thread
30-
from .feeds import ActivityFeed
31+
from .feeds import ActivityFeed, calendar
3132
from .forms import (
3233
BulkEmailForm,
3334
CommitFestFilterForm,
@@ -228,6 +229,19 @@ def help(request):
228229
)
229230

230231

232+
def commitfests_ics(_: HttpRequest) -> HttpResponse:
233+
"""
234+
Calendar feed of all non-draft commitfests in iCalendar format (RFC 5545).
235+
"""
236+
commitfests = CommitFest.objects.filter(draft=False).order_by("startdate")
237+
238+
response = HttpResponse(
239+
calendar(commitfests).to_ical(), content_type="text/calendar; charset=utf-8"
240+
)
241+
response["Content-Disposition"] = 'inline; filename="commitfests.ics"'
242+
return response
243+
244+
231245
def activity(request, cfid=None, rss=None):
232246
# Number of notes to fetch
233247
if rss:

‎pgcommitfest/urls.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
re_path(r"^commitfest_history/$", views.commitfest_history_legacy_redirect),
2525
re_path(r"^me/$", views.me_legacy_redirect),
2626
re_path(r"^archive/$", views.archive),
27+
re_path(r"^commitfests\.ics$", views.commitfests_ics),
2728
re_path(r"^activity(?P<rss>\.rss)?/", views.activity),
2829
re_path(r"^(\d+)/$", views.commitfest),
2930
re_path(r"^(open|inprogress|current|draft)/(.*)$", views.redir),

‎pyproject.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ readme = "README.md"
66
license = "PostgreSQL"
77
dependencies = [
88
"django>=5.2,<6.0",
9+
"icalendar",
910
"psycopg2",
1011
"simplejson",
1112
"requests",

0 commit comments

Comments
 (0)