Skip to content

Commit

Permalink
Add calendar for lunch schedules
Browse files Browse the repository at this point in the history
  • Loading branch information
filips123 committed Sep 1, 2021
1 parent c9df9e8 commit 213fe30
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 10 deletions.
22 changes: 16 additions & 6 deletions API/gimvicurnik/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from .database import Class, Classroom, Document, Entity, LunchMenu, LunchSchedule, Session, SnackMenu, Teacher
from .errors import ConfigError, ConfigParseError, ConfigReadError, ConfigValidationError
from .utils.flask import DateConverter, ListConverter
from .utils.ical import create_calendar
from .utils.ical import create_schedule_calendar, create_school_calendar
from .utils.url import tokenize_url


Expand Down Expand Up @@ -485,7 +485,7 @@ def _substitutions_get_rss():
def _schedules_get_atom():
return create_feed(
filter=Document.type == "lunch-schedule",
name="Razporedi kosil",
name="Razporedi delitve kosila",
type="schedules",
format="atom",
display_date=True,
Expand All @@ -496,7 +496,7 @@ def _schedules_get_atom():
def _schedules_get_rss():
return create_feed(
filter=Document.type == "lunch-schedule",
name="Razporedi kosil",
name="Razporedi delitve kosila",
type="schedules",
format="rss",
display_date=True,
Expand Down Expand Up @@ -527,7 +527,7 @@ def _menu_get_rss():

@self.app.route("/calendar/combined/<list:classes>")
def _get_calendar_for_classes(classes):
return create_calendar(
return create_school_calendar(
Class.get_substitutions(self.session, None, classes),
Class.get_lessons(self.session, classes),
self.config["hourtimes"],
Expand All @@ -536,7 +536,7 @@ def _get_calendar_for_classes(classes):

@self.app.route("/calendar/timetable/<list:classes>")
def _get_calendar_timetable_for_classes(classes):
return create_calendar(
return create_school_calendar(
Class.get_substitutions(self.session, None, classes),
Class.get_lessons(self.session, classes),
self.config["hourtimes"],
Expand All @@ -546,14 +546,24 @@ def _get_calendar_timetable_for_classes(classes):

@self.app.route("/calendar/substitutions/<list:classes>")
def _get_calendar_substitutions_for_classes(classes):
return create_calendar(
return create_school_calendar(
Class.get_substitutions(self.session, None, classes),
Class.get_lessons(self.session, classes),
self.config["hourtimes"],
f"Nadomeščanja - {', '.join(classes)} - Gimnazija Vič",
timetable=False,
)

@self.app.route("/calendar/schedules/<list:classes>")
def _get_calendar_schedules_for_classes(classes):
return create_schedule_calendar(
self.session.query(LunchSchedule)
.join(Class)
.filter(Class.name.in_(classes))
.order_by(LunchSchedule.time, LunchSchedule.class_),
f"Razporedi delitve kosila - {', '.join(classes)} - Gimnazija Vič",
)


def create_app():
"""Application factory that accepts a configuration file from environment variable."""
Expand Down
51 changes: 48 additions & 3 deletions API/gimvicurnik/utils/ical.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def datecount(start_date, i):


@with_span(op="generate")
def create_calendar(details, timetables, hours, name, timetable=True, substitutions=True):
def create_school_calendar(details, timetables, hours, name, timetable=True, substitutions=True):
logger = logging.getLogger(__name__)

calendar = Calendar()
Expand Down Expand Up @@ -93,7 +93,6 @@ def create_calendar(details, timetables, hours, name, timetable=True, substituti
logger.info("Preparing iCalendar event", extra={"type": "substitution", "source": subject})

event = Event()

event.add("dtstamp", datetime.now())
event.add("CATEGORIES", vText("SUBSTITUTION"))
event.add("COLOR", vText("darkred"))
Expand Down Expand Up @@ -132,5 +131,51 @@ def create_calendar(details, timetables, hours, name, timetable=True, substituti

response = make_response(calendar.to_ical().decode("utf-8").replace("\\", ""))
response.headers["Content-Disposition"] = "attachment; filename=calendar.ics"
response.headers["Content-Type"] = "text/calendar"
response.headers["Content-Type"] = "text/calendar; charset=utf-8"
return response


@with_span(op="generate")
def create_schedule_calendar(query, name):
logger = logging.getLogger(__name__)

calendar = Calendar()
calendar.add("prodid", "gimvicurnik")
calendar.add("version", "2.0")
calendar.add("X-WR-TIMEZONE", "Europe/Ljubljana")
calendar.add("X-WR-CALNAME", name)
calendar.add("X-WR-CALDESC", name)
calendar.add("NAME", name)
calendar.add("X-PUBLISHED-TTL", vDuration(timedelta(hours=12)))
calendar.add("REFRESH-INTERVAL", vDuration(timedelta(hours=12)))

for model in query:
with start_span(op="event") as span:
span.set_tag("event.type", "lunch-schedule")
span.set_tag("event.date", model.date)
span.set_tag("event.time", model.time)
span.set_data("event.source", model)

logger.info("Preparing iCalendar event", extra={"type": "lunch-schedule", "source": model})

event = Event()
event.add("dtstamp", datetime.now())
event.add("CATEGORIES", vText("LUNCH"))
event.add("COLOR", vText("darkblue"))
event.add(
"UID",
sha256((str(model.date) + str(model.time) + str(model.class_.name) + str(model.location)).encode()).hexdigest(),
)

event.add("summary", "Kosilo")
event.add("description", model.notes or "")
event.add("location", vText(model.location))
event.add("dtstart", datetime.combine(model.date, model.time))
event.add("dtend", datetime.combine(model.date, model.time) + timedelta(minutes=15))

calendar.add_component(event)

response = make_response(calendar.to_ical().decode("utf-8").replace("\\", ""))
response.headers["Content-Disposition"] = "attachment; filename=calendar.ics"
response.headers["Content-Type"] = "text/calendar; charset=utf-8"
return response
3 changes: 2 additions & 1 deletion website/src/views/Subscribe.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@
<url-display label="Okrožnice" :value="`${vueAppApi}/feeds/circulars.atom`"></url-display>
<url-display label="Nadomeščanja" :value="`${vueAppApi}/feeds/substitutions.atom`"></url-display>
<url-display label="Jedilniki" :value="`${vueAppApi}/feeds/menus.atom`"></url-display>
<url-display label="Razporedi kosil" :value="`${vueAppApi}/feeds/schedules.atom`"></url-display>
<url-display label="Razporedi delitve kosila" :value="`${vueAppApi}/feeds/schedules.atom`"></url-display>
</div>

<div class="pt-6" ref="calendarLinks">
<h2 class="text-h5 pb-4">Koledar</h2>
<url-display label="Urnik & Nadomeščanja" :value="`${vueAppApi}/calendar/combined/${selectedEntity}`"></url-display>
<url-display label="Urnik" :value="`${vueAppApi}/calendar/timetable/${selectedEntity}`"></url-display>
<url-display label="Nadomeščanja" :value="`${vueAppApi}/calendar/substitutions/${selectedEntity}`"></url-display>
<url-display label="Razporedi delitve kosila" :value="`${vueAppApi}/calendar/schedules/${selectedEntity}`"></url-display>
</div>
</div>
</template>
Expand Down

0 comments on commit 213fe30

Please sign in to comment.