Skip to content

Commit

Permalink
feat(backend) add google calender link (#35)
Browse files Browse the repository at this point in the history
* Add "google_calender_link" Property to "Workshop", "Presentation"

* Add Fields serializers.py

* Fix Property Name

* feat: url encode

* migration

* fix(backend): fix quoting multiple times

---------

Co-authored-by: Alireza Zare <[email protected]>
Co-authored-by: Adibov <[email protected]>
  • Loading branch information
3 people authored Nov 28, 2023
1 parent 9aa8df5 commit 01e6897
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.2.4 on 2023-11-28 22:05

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("backend_api", "0050_presentation_has_project"),
]

operations = [
migrations.RemoveField(
model_name="workshop",
name="add_to_calendar_link",
),
]
36 changes: 34 additions & 2 deletions backend/backend_api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@
from django.utils.translation import gettext_lazy as _
from rest_framework.exceptions import ValidationError
from rest_framework import status
from django.utils.html import escape

from aaiss_backend import settings
from aaiss_backend.settings import BASE_URL
from backend_api import validators
from backend_api.email import MailerThread
from utils.random import create_random_string
from utils.renderers import new_detailed_response

from urllib.parse import quote
SMALL_MAX_LENGTH = 255
BIG_MAX_LENGTH = 65535

Expand Down Expand Up @@ -100,7 +101,6 @@ class Workshop(models.Model):
has_project = models.BooleanField(default=False, blank=False)
prerequisites = models.CharField(max_length=BIG_MAX_LENGTH, default='', blank=True)
capacity = models.PositiveSmallIntegerField(default=50)
add_to_calendar_link = models.CharField(max_length=SMALL_MAX_LENGTH, default='', blank=True)
year = models.IntegerField(blank=False, default=2020)

NOT_ASSIGNED = 'NOT_ASSIGNED'
Expand Down Expand Up @@ -142,6 +142,22 @@ def participants(self):
participants += participant.user
return participants

@property
def google_calendar_link(self):
base_url = "https://www.google.com/calendar/render?action=TEMPLATE"
workshop_title = escape(self.name)
workshop_description = escape(self.desc)
start_datetime = self.start_date.strftime("%Y%m%dT%H%M%S")
end_datetime = self.end_date.strftime("%Y%m%dT%H%M%S")

teachers_names = " ".join([teacher.name for teacher in self.teachers.all()])
event_details = f"Teachers: {teachers_names}\nDescription: {workshop_description}"
query_params = f"text={quote(workshop_title)}&dates={start_datetime}/{end_datetime}&details={quote(event_details)}"

link = f"{base_url}&{query_params}"

return link

def __str__(self):
name = ""
for teacher in self.teachers.all():
Expand Down Expand Up @@ -197,6 +213,22 @@ def participants(self):
participants += participant.user
return participants

@property
def google_calendar_link(self):
base_url = "https://www.google.com/calendar/render?action=TEMPLATE"
presentation_title = escape(self.name)
presentation_description = escape(self.desc)
start_datetime = self.start_date.strftime("%Y%m%dT%H%M%S")
end_datetime = self.end_date.strftime("%Y%m%dT%H%M%S")

presenters_names = " ".join([presenter.name for presenter in self.presenters.all()])
event_details = f"Presenters: {presenters_names}\nDescription: {presentation_description}"
query_params = f"text={quote(presentation_title)}&dates={start_datetime}/{end_datetime}&details={quote(event_details)}"

link = f"{base_url}&{query_params}"

return link

def __str__(self):
name = ""
for presenter in self.presenters.all():
Expand Down
2 changes: 2 additions & 0 deletions backend/backend_api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class AllStaffSectionSerializer(serializers.Serializer):

class WorkshopSerializer(serializers.ModelSerializer):
remaining_capacity = serializers.IntegerField()
google_calendar_link = serializers.CharField()

class Meta:
model = models.Workshop
Expand All @@ -65,6 +66,7 @@ class Meta:

class PresentationSerializer(serializers.ModelSerializer):
remaining_capacity = serializers.IntegerField()
google_calendar_link = serializers.CharField()

class Meta:
model = models.Presentation
Expand Down

0 comments on commit 01e6897

Please sign in to comment.