Allow user to specify year and/or group in url

This commit is contained in:
Gabriel Augendre 2016-09-17 12:54:44 +02:00
parent 3330a2df11
commit 5d960bd69d
No known key found for this signature in database
GPG key ID: D2B6A5B41FC438B1

32
app.py
View file

@ -1,15 +1,18 @@
from flask import Flask from flask import Flask, request
import os import os
import urllib.request import urllib.request
from icalendar.cal import Calendar, Component from icalendar.cal import Calendar, Component
app = Flask(__name__) app = Flask(__name__)
FILTERED_COURSES = os.environ.get('COURSES', '').split(',') EXCLUDED_COURSES = os.environ.get('COURSES', '').split(',')
FILTERED_TYPES = os.environ.get('TYPES', '').split(',') EXCLUDED_TYPES = os.environ.get('TYPES', '').split(',')
YEAR = os.environ.get('YEAR', '4') YEAR = os.environ.get('YEAR', '4')
GROUP = os.environ.get('GROUP', '1') GROUP = os.environ.get('GROUP', '1')
ALLOWED_YEARS = os.environ.get('ALLOWED_YEARS', '3,4,5').split(',')
ALLOWED_GROUPS = os.environ.get('ALLOWED_GROUPS', '1,2,3').split(',')
@app.route("/") @app.route("/")
def hello(): def hello():
@ -17,7 +20,24 @@ def hello():
if url is None: if url is None:
return "" return ""
url += "&promo={year}&groupe={group}".format(year=YEAR, group=GROUP) year = request.args.get('year', '')
group = request.args.get('group', '')
try:
int(year)
if year not in ALLOWED_YEARS:
raise ValueError('year not allowed')
except ValueError:
year = YEAR
try:
int(group)
if group not in ALLOWED_GROUPS:
raise ValueError('group not allowed')
except ValueError:
group = GROUP
url += "&promo={year}&groupe={group}".format(year=year, group=group)
cal_str = urllib.request.urlopen(url).read() cal_str = urllib.request.urlopen(url).read()
cal = Component.from_ical(cal_str) cal = Component.from_ical(cal_str)
@ -43,8 +63,8 @@ def should_add(event):
course_type = event['SUMMARY'].split('/')[1].split('_')[1] course_type = event['SUMMARY'].split('/')[1].split('_')[1]
return \ return \
course_code not in FILTERED_COURSES \ course_code not in EXCLUDED_COURSES \
and course_type not in FILTERED_TYPES and course_type not in EXCLUDED_TYPES
if __name__ == "__main__": if __name__ == "__main__":
port = int(os.environ.get('PORT', 5000)) port = int(os.environ.get('PORT', 5000))