Skip to content

Commit

Permalink
Merge pull request #17 from p4kl0nc4t/add-calendar-name
Browse files Browse the repository at this point in the history
feat: add calendar name
  • Loading branch information
lc-at authored Feb 23, 2023
2 parents 9f2eb30 + fa2864a commit 44b11b3
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
16 changes: 12 additions & 4 deletions web/evdata_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"


def process_class_evdata(events: list) -> str:
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, calendar_name: str) -> str:
def preprocess_time(s: str):
return arrow.get(s, "YYYY-M-D HH:mm:ss", tzinfo=TIMEZONE)

calendar = Calendar()
calendar = NamedCalendar(calendar_name)

for event_data in events:
e = Event()
Expand All @@ -26,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
Expand All @@ -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:
Expand Down
8 changes: 6 additions & 2 deletions web/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 44b11b3

Please sign in to comment.