From 345978537cd721b2eac48d449ed1cfbd672e6de2 Mon Sep 17 00:00:00 2001 From: Faiz Jazadi Date: Mon, 20 Feb 2023 23:53:19 +0700 Subject: [PATCH 1/2] feat: add default calendar name Signed-off-by: Faiz Jazadi --- web/evdata_processor.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/web/evdata_processor.py b/web/evdata_processor.py index 989f601..08db627 100644 --- a/web/evdata_processor.py +++ b/web/evdata_processor.py @@ -3,17 +3,25 @@ import arrow from html2text import html2text from ics import Calendar, Event +from ics.parse import ContentLine TIMEZONE = "Asia/Jakarta" LOCALE = "id_ID" +class NamedCalendar(Calendar): + def __init__(self, calendar_name: str, *args, **kwargs): + super().__init__(*args, **kwargs) + self.extra.append(ContentLine( + name="X-WR-CALNAME", value=calendar_name)) + + def process_class_evdata(events: list) -> str: def preprocess_time(s: str): return arrow.get(s, "YYYY-M-D HH:mm:ss", tzinfo=TIMEZONE) - calendar = Calendar() + calendar = NamedCalendar("SIMASTER Classes") for event_data in events: e = Event() @@ -35,7 +43,7 @@ def preprocess_str(s: str): return None return s - calendar = Calendar() + calendar = NamedCalendar("SIMASTER Exams") for exam_type, exam_table in zip(("UTS", "UAS"), exam_tables): for row in exam_table: From fa2864aee3ec95f73d7a4acca0c9681460544b9f Mon Sep 17 00:00:00 2001 From: Faiz Jazadi Date: Thu, 23 Feb 2023 09:50:03 +0700 Subject: [PATCH 2/2] feat: add username and period to cal title Signed-off-by: Faiz Jazadi --- web/evdata_processor.py | 6 +++--- web/views.py | 8 ++++++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/web/evdata_processor.py b/web/evdata_processor.py index 08db627..0fa62a6 100644 --- a/web/evdata_processor.py +++ b/web/evdata_processor.py @@ -17,11 +17,11 @@ def __init__(self, calendar_name: str, *args, **kwargs): name="X-WR-CALNAME", value=calendar_name)) -def process_class_evdata(events: list) -> str: +def process_class_evdata(events: list, calendar_name: str) -> str: def preprocess_time(s: str): return arrow.get(s, "YYYY-M-D HH:mm:ss", tzinfo=TIMEZONE) - calendar = NamedCalendar("SIMASTER Classes") + calendar = NamedCalendar(calendar_name) for event_data in events: e = Event() @@ -34,7 +34,7 @@ def preprocess_time(s: str): return str(calendar) -def process_exam_evdata(exam_tables: list) -> str: +def process_exam_evdata(exam_tables: list, calendar_name: str) -> str: def preprocess_str(s: str): if not isinstance(s, str) and not s: return None diff --git a/web/views.py b/web/views.py index 82a4766..3c5e7d1 100644 --- a/web/views.py +++ b/web/views.py @@ -33,12 +33,16 @@ def get_icalendar(): if not ses: return {"error": "Invalid username or password"}, 401 + title_suffix = f"({username} / {period})" + if type_ == "class": evdata = get_class_evdata(ses, period) - ics_str = process_class_evdata(evdata) + ics_str = process_class_evdata( + evdata, f"SIMASTER Classes {title_suffix}") elif type_ == "exam": evdata = get_exam_evdata(ses, period) - ics_str = process_exam_evdata(evdata) + ics_str = process_exam_evdata( + evdata, f"SIMASTER Exams {title_suffix}") else: return {"error": "Invalid type"}, 401