-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmodels.py
86 lines (68 loc) · 2.66 KB
/
models.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
from google.appengine.ext import db
class Event(object):
def __init__(self, *args, **kwargs):
for k, v in kwargs.items():
super(Event, self).__setattr__(k, v)
class Student(db.Model):
group = db.IntegerProperty()
faculty = db.IntegerProperty()
form = db.IntegerProperty()
course = db.IntegerProperty()
auto = db.BooleanProperty()
lastrun = db.DateTimeProperty(auto_now=True)
student = db.UserProperty(required=True)
calendar = db.TextProperty()
calendar_id = db.TextProperty()
@property
def id(self):
return str(self.key())
def __repr__(self):
return self.student.nickname
class PermanentLinks(db.Model):
group = db.IntegerProperty()
faculty = db.IntegerProperty()
form = db.IntegerProperty()
course = db.IntegerProperty()
@property
def id(self):
return str(self.key())
def __repr__(self):
return "Link: course=%s, form=%s" % (self.course, self.form)
def add_permalink_and_get_key(group, faculty, form, course):
exists = PermanentLinks.all().filter("group =", group)\
.filter("faculty =", faculty).filter("form =", form).filter("course = ", course).get()
if exists:
return exists.id
else:
link = PermanentLinks(group=group, faculty=faculty, form=form, course=course)
link.put()
return link.id
def _create_student(user, form):
return Student(group=form.get('group', type=int),
form=form.get('form', type=int),
auto=form.get('auto', type=bool),
faculty=form.get('faculty', type=int),
course=form.get('course', type=int),
student=user,
calendar_id=form.get('calendar'),
calendar=form.get('calendar_name')).put()
def create_or_update_student(user, request):
student = Student.all().filter("student =", user).get()
form = request.form
if not student:
return _create_student(user, form)
if form.get('group'):
student.group = form.get('group', type=int)
if form.get('form'):
student.form = form.get('form', type=int)
if form.get('faculty'):
student.faculty = form.get('faculty', type=int)
if form.get('course'):
student.course = form.get('course', type=int)
current_calendar_name = form.get('calendar_name', default=False)
current_calendar_id = form.get('calendar', default=False)
if current_calendar_name and current_calendar_id:
student.calendar_id = current_calendar_id
student.calendar = current_calendar_name
student.auto = form.get('auto', default=False, type=bool)
student.put()