Skip to content

Commit 43162e3

Browse files
committed
Add a /commitfests.ics feed
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>
1 parent 07e2bc6 commit 43162e3

7 files changed

Lines changed: 118 additions & 3 deletions

File tree

‎pgcommitfest/commitfest/feeds.py‎

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

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

417
class ActivityFeed(Feed):
518
title = description = "Commitfest Activity Log"
@@ -34,3 +47,31 @@ def item_link(self, item):
3447

3548
def item_pubdate(self, item):
3649
return item["date"]
50+
51+
52+
def calendar(commitfests: Iterable[CommitFest]) -> Calendar:
53+
"""
54+
Build an RFC 5545 iCalendar document with one all-day VEVENT per
55+
commitfest, covering its start and end date.
56+
"""
57+
calendar = Calendar.new(
58+
prodid="-//PostgreSQL Commitfest//commitfest.postgresql.org//EN",
59+
name="PostgreSQL Commitfests",
60+
description="Start and end dates of PostgreSQL Commitfests",
61+
method="PUBLISH",
62+
)
63+
64+
for cf in commitfests:
65+
event = Event.new(
66+
uid=f"commitfest-{cf.name}@commitfest.postgresql.org",
67+
summary=cf.title,
68+
start=cf.startdate,
69+
# DTEND is exclusive for all-day events, so add a day to make the
70+
# last day of the commitfest show up as included on the calendar.
71+
end=cf.enddate + timedelta(days=1),
72+
description=f"Status: {cf.statusstring}",
73+
url=f"https://commitfest.postgresql.org/{cf.id}/",
74+
)
75+
calendar.add_component(event)
76+
77+
return calendar

‎pgcommitfest/commitfest/templates/all_commitfests.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
<li><a href="/{{c.id}}/">{{c}}</a> ({{c.statusstring}} - {{c.periodstring}})</li>
66
{%endfor%}
77
</ul>
8+
<p><a href="/commitfests.ics">Subscribe to Commitfest dates (ICS)</a></p>
89
<br/>
910
{%endblock%}
10-

‎pgcommitfest/commitfest/templates/archive.html‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
<li><a href="/{{c.id}}/">{{c}}</a> ({{c.statusstring}}{%if c.startdate%} - {{c.periodstring}}{%endif%})</li>
66
{%endfor%}
77
</ul>
8+
<p><a href="/commitfests.ics">Subscribe to Commitfest dates (ICS)</a></p>
89
{%endblock%}
9-
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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, cast
9+
10+
import pytest
11+
from icalendar import Calendar, Event
12+
13+
if TYPE_CHECKING:
14+
from pgcommitfest.commitfest.models import CommitFest
15+
16+
pytestmark = pytest.mark.django_db
17+
18+
19+
def _get_calendar(client) -> Calendar:
20+
response = client.get("/commitfests.ics")
21+
assert response.status_code == 200
22+
assert response["Content-Type"] == "text/calendar; charset=utf-8"
23+
return Calendar.from_ical(response.content)
24+
25+
26+
def _get_events(calendar: Calendar) -> list[Event]:
27+
return cast("list[Event]", calendar.walk("VEVENT"))
28+
29+
30+
def test_commitfests_ics_empty(client):
31+
"""
32+
With no commitfests, we still get a valid, empty calendar.
33+
"""
34+
calendar = _get_calendar(client)
35+
36+
assert len(_get_events(calendar)) == 0
37+
38+
39+
def test_commitfests_ics_contains_all_commitfests(client, commitfests: dict[str, CommitFest]):
40+
"""
41+
Every commitfest, regardless of status, shows up as a VEVENT with the right
42+
dates, DTEND being the day after enddate (exclusive).
43+
"""
44+
calendar = _get_calendar(client)
45+
46+
events = _get_events(calendar)
47+
assert len(events) == len(commitfests)
48+
49+
events_by_uid = {cast("str", event.uid): event for event in events}
50+
51+
for cf in commitfests.values():
52+
event = events_by_uid[f"commitfest-{cf.name}@commitfest.postgresql.org"]
53+
assert event.summary == f"Commitfest {cf.name}"
54+
assert event.url == f"https://commitfest.postgresql.org/{cf.pk}/"
55+
assert event.description == f"Status: {cf.statusstring}"
56+
assert event.start == cf.startdate
57+
assert event.end == cf.enddate + timedelta(days=1)

‎pgcommitfest/commitfest/views.py‎

Lines changed: 16 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
HttpResponseRedirect,
@@ -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
CommentForm,
@@ -195,6 +196,20 @@ def archive(request):
195196
)
196197

197198

199+
def commitfests_ics(_: HttpRequest) -> HttpResponse:
200+
"""
201+
Calendar feed of all commitfests in iCalendar format (RFC 5545).
202+
"""
203+
commitfests = CommitFest.objects.order_by("startdate")
204+
205+
response = HttpResponse(
206+
calendar(commitfests).to_ical(),
207+
content_type="text/calendar; charset=utf-8"
208+
)
209+
response["Content-Disposition"] = 'inline; filename="commitfests.ics"'
210+
return response
211+
212+
198213
def activity(request, cfid=None, rss=None):
199214
# Number of notes to fetch
200215
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),
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)