Skip to content

Commit

Permalink
Make it possible to download ics events
Browse files Browse the repository at this point in the history
  • Loading branch information
autoantwort committed Jun 13, 2024
1 parent 610d28b commit 124e3ec
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 1 deletion.
21 changes: 21 additions & 0 deletions main/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from datetime import timedelta

from django.contrib.auth.models import User
from django.db import models
from django.utils import formats
from ics import Event as IcsEvent, Organizer, DisplayAlarm, Geo


class Weekday(models.IntegerChoices):
Expand Down Expand Up @@ -83,5 +86,23 @@ class Event(models.Model):
end_date = models.DateTimeField(null=True, blank=True)
poster = models.FileField(upload_to="events/", null=True, blank=True)

def to_ics_event(self) -> IcsEvent:
ics_event = IcsEvent()
ics_event.name = self.name
ics_event.begin = self.start_date
ics_event.end = self.end_date if self.end_date else self.start_date + timedelta(hours=4)
ics_event.description = self.description
ics_event.location = f"{self.bar.street}, {self.bar.zip_code} {self.bar.city}"
if self.bar.latitude and self.bar.longitude:
ics_event.geo = Geo(self.bar.latitude, self.bar.longitude)
ics_event.status = "CONFIRMED"
ics_event.uid = f"event-{self.id}@studibars-ac.de"
ics_event.organizer = Organizer(common_name=self.bar.name, email="[email protected]")

# Add an alarm/reminder
alarm = DisplayAlarm(trigger=timedelta(days=-1))
ics_event.alarms.append(alarm)
return ics_event

def __str__(self):
return self.name
4 changes: 3 additions & 1 deletion main/templates/main/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,9 @@ <h5 class="text-nowrap text-black">{{ bar.name }}</h5>
<div class="card">
<div class="card-header d-flex justify-content-between">
<span class="fw-bold">{{ event.bar }}</span>
<span>{{ event.start_date|date:"d.m." }} ab {{ event.start_date|date:"H:i" }}</span>
<span>{{ event.start_date|date:"d.m." }} ab {{ event.start_date|date:"H:i" }}
<a class="align-middle" href="{% url 'download_event_ics' event_id=event.id %}"><i class="bi bi-calendar-plus align-top"></i></a>
</span>
</div>
{% if event.poster %}
{% generateimage 'studibars:poster1x' source=event.poster as img1 %}
Expand Down
1 change: 1 addition & 0 deletions main/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@

urlpatterns = [
path('main', views.main_view, name='main'),
path('ics/event/<int:event_id>/', views.download_event_ics, name='download_event_ics'),
]
14 changes: 14 additions & 0 deletions main/views.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import datetime
from collections import defaultdict

from django.http import HttpResponse
from django.shortcuts import render
from ics import Calendar
from rest_framework import viewsets, permissions

from main.models import Bar, Weekday, Event
Expand Down Expand Up @@ -73,6 +76,17 @@ def main_view(request):
})


def download_event_ics(request, event_id):
event = Event.objects.get(id=event_id)

c = Calendar()
c.events.add(event.to_ics_event())

response = HttpResponse(c.serialize(), content_type='text/calendar')
response['Content-Disposition'] = f'attachment; filename={event.name}.ics'
return response


# Allows all CRUP Operations
class BarViewSet(viewsets.ModelViewSet):
queryset = Bar.objects.all()
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Pillow # For ImageField
djangorestframework
drf-spectacular
django-filter
ics # für ics calendar
django-imagekit # um im template thumbnails zu generieren https://github.com/matthewwithanm/django-imagekit
# See https://github.com/zelenij/django-bootstrap-v5/issues/33
git+https://github.com/math-a3k/django-bootstrap-v5.git@9d9f01988f0554028c0fd276939fc1336f0fa997#egg=django-bootstrap-v5
Expand Down

0 comments on commit 124e3ec

Please sign in to comment.