-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
137 lines (108 loc) · 4.93 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
from sqlalchemy import Column, Integer, String, Boolean, Text, DateTime, ForeignKey, UniqueConstraint
from sqlalchemy.orm import relationship, backref
from database import Base
import datetime
#table storing all users by flask session id,
#and whether they did the surveys
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
#has to have a valid session id
flask_session_id = Column(String(50),unique=True, nullable=False)
def __init__(self, flask_session_id):
self.flask_session_id = flask_session_id
def __repr__(self):
return "User(%r)" % (self.flask_session_id)
#table storing all survey names
class Survey(Base):
__tablename__ = "surveys"
id = Column(Integer, primary_key=True)
name = Column(String(10),nullable=False)
def __init__(self,name):
self.name = name
def __repr__(self):
return "Survey(%r)" % (self.name)
#table storing all questions for all surveys
class SurveyQuestion(Base):
__tablename__ = "survey_questions"
id = Column(Integer, primary_key=True) # question id
question_text = Column(Text, nullable=False) #text of the question
input_type = Column(String(50),nullable=False) #checkboxes, menus, textbox, etc.
response_type = Column(String(10)) #number, string, null if checkbox
parent_question = Column(Integer) # if this is a subquestion, which question is the parent
survey_id = Column(Integer,ForeignKey("surveys.id"),nullable=False) #which survey this is
survey = relationship("Survey",backref=backref('questions'))
#questions must be unique for each survey
__table_args__ = (UniqueConstraint('survey_id', 'question_text', name='uix_1'),)
def __init__(self,question_text,input_type,response_type,parent_question,survey_id):
self.survey_id = survey_id
self.question_text = question_text
self.input_type = input_type
self.response_type = response_type
self.parent_question = parent_question
def __repr__(self):
return "SurveyQuestion(%r, %r, %r)" % (self.survey_id,self.question_text,self.input_type)
#table storing predefined answers for relevant survey questions
class SurveyResponse(Base):
__tablename__ = "survey_responses"
id = Column(Integer,primary_key=True)
value = Column(String(150),nullable=False)
question_id = Column(Integer,ForeignKey("survey_questions.id"),nullable=False)
question = relationship("SurveyQuestion",backref=backref("responses"))
#responses must be unique for each question
__table_args__ = (UniqueConstraint('question_id', 'value', name='uix_2'),)
def __init__(self,value,question_id):
self.value = value
self.question_id = question_id
def __repr__(self):
return "SurveyResponse(%r, %r)" % (self.question_id,self.value)
class UserResponse(Base):
__tablename__ = "user_responses"
value = Column(Text) #user's response text, may be empty or null
comment = Column(Text) # additional comments from user (i.e. elaborate on "Other")
user_id = Column(Integer,ForeignKey("users.id"),primary_key=True)
question_id = Column(Integer,ForeignKey("survey_questions.id"),primary_key=True)
response_id = Column(Integer,ForeignKey("survey_responses.id")) #may not have predefined survey response
timestamp = Column(DateTime)
user = relationship("User",backref=backref("responses"))
question = relationship("SurveyQuestion",backref=backref("user_responses"))
response = relationship("SurveyResponse",backref=backref("user_responses"))
def __init__(self,value,comment,user_id,question_id,response_id):
self.user_id = user_id
self.question_id = question_id
self.response_id = response_id
self.value = value
self.comment = comment
self.timestamp = datetime.datetime.now()
def __repr__(self):
return "UserResponse(%r, %r, %r, %r, %r)" % (self.user_id,self.question_id,self.response_id,self.value,self.timestamp)
# user movement history
class UserTrace(Base):
__tablename__ = "user_traces"
id = Column(Integer,primary_key=True)
tile_id = Column(Text,nullable=False)
timestamp = Column(DateTime(timezone=True),nullable=False)
query = Column(Text,nullable=False) # must know what query was run
user_id = Column(Integer,ForeignKey("users.id"),nullable=False)
dataset_id = Column(Integer,ForeignKey("data_sets.id")) # dataset used? may be null, but hopefully isn't
user = relationship("User",backref=backref("traces"))
dataset = relationship("DataSet",backref=backref("traces"))
def __init__(self,tile_id,query,user_id,dataset_id):
self.user_id = user_id
self.tile_id = tile_id
self.timestamp = datetime.datetime.now()
self.query = query
self.dataset_id = dataset_id
def __repr__():
return "UserTrace(%r, %r, %r, %r, %r, %r)" % (self.user_id,self.tile_id,self.query,self.dataset_id,self.timestamp)
# predetermined data sets
class DataSet(Base):
__tablename__ = "data_sets"
id = Column(Integer,primary_key=True)
name = Column(String(50),unique=True,nullable=False)
query = Column(Text,unique=True,nullable=False)
def __init__(self,name,query):
self.name = name
self.query = query
def __repr__():
return "DataSet(%r, %r)" % (self.name,self.query)