From 01e68976a2d87d8dfbb8464c889afbbe08873a9d Mon Sep 17 00:00:00 2001 From: Mohammad Hosein Aref <76614003+aref81@users.noreply.github.com> Date: Wed, 29 Nov 2023 03:25:38 +0330 Subject: [PATCH] feat(backend) add google calender link (#35) * 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 Co-authored-by: Adibov --- ...51_remove_workshop_add_to_calendar_link.py | 17 +++++++++ backend/backend_api/models.py | 36 +++++++++++++++++-- backend/backend_api/serializers.py | 2 ++ 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 backend/backend_api/migrations/0051_remove_workshop_add_to_calendar_link.py diff --git a/backend/backend_api/migrations/0051_remove_workshop_add_to_calendar_link.py b/backend/backend_api/migrations/0051_remove_workshop_add_to_calendar_link.py new file mode 100644 index 0000000..9f80247 --- /dev/null +++ b/backend/backend_api/migrations/0051_remove_workshop_add_to_calendar_link.py @@ -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", + ), + ] diff --git a/backend/backend_api/models.py b/backend/backend_api/models.py index b6c42e2..e3838f6 100644 --- a/backend/backend_api/models.py +++ b/backend/backend_api/models.py @@ -12,6 +12,7 @@ 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 @@ -19,7 +20,7 @@ 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 @@ -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' @@ -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(): @@ -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(): diff --git a/backend/backend_api/serializers.py b/backend/backend_api/serializers.py index 83732ff..e2b5a01 100644 --- a/backend/backend_api/serializers.py +++ b/backend/backend_api/serializers.py @@ -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 @@ -65,6 +66,7 @@ class Meta: class PresentationSerializer(serializers.ModelSerializer): remaining_capacity = serializers.IntegerField() + google_calendar_link = serializers.CharField() class Meta: model = models.Presentation