-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
81 lines (71 loc) · 2.84 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
from google.appengine.ext import ndb
from google.appengine.ext.ndb import polymodel
from google.appengine.ext.blobstore import BlobInfo
from google.appengine.api import images
import math
import json
import time
import datetime
class NDBJSONEncoder(json.JSONEncoder):
def default(self,obj):
if isinstance(obj,ndb.Key):
return obj.get().to_dict()
elif isinstance(obj,datetime.date):
return obj.strftime('%Y-%m-%d')
return super(NDBJSONEncoder,self).default(obj)
class EdTechModel(ndb.Model):
def to_dict(self):
result = super(EdTechModel,self).to_dict()
result['id'] = self.key.id()
return result
class LocationModel(EdTechModel):
Name = ndb.StringProperty()
Latitude = ndb.FloatProperty()
Longitude = ndb.FloatProperty()
def __init__(self, **kwds):
if 'Latitude' in kwds:
if isinstance(kwds['Latitude'],basestring):
kwds['Latitude'] = float(kwds['Latitude'])
if 'Longitude' in kwds:
if isinstance(kwds['Longitude'],basestring):
kwds['Longitude'] = float(kwds['Longitude'])
super(LocationModel, self).__init__(**kwds)
class ResponseModel(EdTechModel):
ResponeDate = ndb.DateProperty(default=datetime.datetime.now())
Response = ndb.StringProperty()
class QuestionModel(EdTechModel):
DateAsked = ndb.DateProperty(default=datetime.datetime.now())
Question = ndb.StringProperty()
class AverageGradeModel(EdTechModel):
Date = ndb.DateProperty()
Grade = ndb.FloatProperty()
def __init__(self, **kwds):
if 'Date' in kwds:
if not isinstance(kwds['Date'],datetime.date):
kwds['Date']=datetime.datetime.strptime(kwds['Date'],"%Y-%m-%d")
super(AverageGradeModel,self).__init__(**kwds)
class PledgeModel(EdTechModel):
HoursPledged = ndb.IntegerProperty()
Student = ndb.KeyProperty(kind='StudentModel')
def __init__(self, **kwds):
if 'Student' in kwds:
if not isinstance(kwds['Student'],ndb.Key):
kwds['Student'] = ndb.Key( 'StudentModel', int(kwds['Student']) )
super(PledgeModel, self).__init__(**kwds)
class TutorModel(EdTechModel):
Name = ndb.StringProperty()
def to_dict(self):
result = super(TutorModel,self).to_dict()
result['Pledges'] = []
pledges = PledgeModel.query(ancestor=self.key).fetch()
for pledge in pledges:
result['Pledges'].append(pledge.to_dict())
return result
class StudentModel(EdTechModel):
Name = ndb.StringProperty()
SchoolName = ndb.StringProperty()
Subject = ndb.StringProperty()
SchoolLocation = ndb.StructuredProperty(LocationModel, repeated=False)
AverageGrades = ndb.StructuredProperty(AverageGradeModel, repeated=True)
Description = ndb.StringProperty()
Image = ndb.BlobKeyProperty()