Extract year and group validation to function
This commit is contained in:
parent
0e89a89714
commit
2bfbb4c7ed
1 changed files with 24 additions and 13 deletions
37
app.py
37
app.py
|
@ -27,19 +27,8 @@ def hello():
|
||||||
year = request.args.get('year', '')
|
year = request.args.get('year', '')
|
||||||
group = request.args.get('group', '')
|
group = request.args.get('group', '')
|
||||||
|
|
||||||
try:
|
year = validate(year, ALLOWED_YEARS, YEAR)
|
||||||
int(year)
|
group = validate(group, ALLOWED_GROUPS, GROUP)
|
||||||
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)
|
url += "&promo={year}&groupe={group}".format(year=year, group=group)
|
||||||
cal_str = urllib.request.urlopen(url).read()
|
cal_str = urllib.request.urlopen(url).read()
|
||||||
|
@ -93,5 +82,27 @@ def should_add(event):
|
||||||
course_code not in excluded_courses \
|
course_code not in excluded_courses \
|
||||||
and course_type not in excluded_types
|
and course_type not in excluded_types
|
||||||
|
|
||||||
|
|
||||||
|
def validate(item, validation_list, default):
|
||||||
|
"""
|
||||||
|
Validates an item from user input against a list of possible values.
|
||||||
|
|
||||||
|
:param item: The item to validate.
|
||||||
|
:type item: object
|
||||||
|
:param validation_list: The list of possible values.
|
||||||
|
:type item: list(object)
|
||||||
|
:param default: The default value to fallback on if item is not in list.
|
||||||
|
:type default: object
|
||||||
|
:return: The item if it is valid (in the list), or default
|
||||||
|
:rtype: object
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
int(item)
|
||||||
|
if item not in validation_list:
|
||||||
|
raise ValueError(item.__name__ + ' not allowed')
|
||||||
|
except ValueError:
|
||||||
|
return default
|
||||||
|
return item
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
app.run('0.0.0.0', PORT)
|
app.run('0.0.0.0', PORT)
|
||||||
|
|
Loading…
Reference in a new issue