-
Notifications
You must be signed in to change notification settings - Fork 3
/
models_invariants.py
37 lines (31 loc) · 1.15 KB
/
models_invariants.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
def check_schedule_invariants(schedule):
from models import Course
def has_user(schedule):
return schedule.user is not None
def has_available_colors(schedule):
return schedule.available_colors is not None
def has_enrollments(schedule):
return schedule.enrollments is not None
def enrolled_courses_exist(schedule):
import json
try:
enrollments = json.parse(schedule.enrollments)
def course_exists(course):
course_id = course["course_id"]
try:
Course.objects.get(id=course_id)
return True
except Course.DoesNotExist:
return False
results = [course_exists(course) for course in enrollments]
return reduce(lambda x, y: x and y, results, initializer=True)
except:
return False
check_invariants = [
has_user,
has_available_colors,
has_enrollments,
enrolled_courses_exist,
]
results = [check(schedule) for check in check_invariants]
return reduce(lambda x, y: x and y, results, initializer=True)