-
Notifications
You must be signed in to change notification settings - Fork 0
/
forms.py
75 lines (64 loc) · 2.89 KB
/
forms.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
from wtforms import Form, BooleanField, TextField, RadioField, validators
from database import db_session
import models
QUESTION_PREFIX = "q_"
class BaseSurveyForm(Form):
username = TextField('Username', [validators.Length(min=4, max=25,message="must be 4-25 characters")])
email = TextField('Email Address', [validators.Length(min=6),validators.Email(message="this doesn't seem like an email")])
password = TextField('Password', [validators.Length(min=6, max=35,message="must be 6-35 characters")])
accept_rules = BooleanField('I accept the site rules', [validators.Required(message="must accept site rules to proceed")])
def generate_survey_instance_form(survey_name,request_form=None):
survey_questions = get_survey_questions(survey_name) #list of model question objects
class SurveyInstanceForm(BaseSurveyForm):
pass
question_ids = []
for question in survey_questions:
question_id = QUESTION_PREFIX+str(question.id)
question_ids.append(question_id)
setattr(SurveyInstanceForm,question_id, generate_field_for_question(question))
setattr(SurveyInstanceForm,"question_ids",question_ids)
if request_form is None:
form = SurveyInstanceForm()
else:
form = SurveyInstanceForm(request_form)
return form
#used to load responses back into database
def submit_user_responses(user_id,form):
user = db_session.query(models.User).filter_by(flask_session_id=user_id).one()
u_id = user.id
for question_id in form.question_ids:
q_id = int(question_id[len(QUESTION_PREFIX):])
data = form[question_id].data
print "here's the data:",data,len(data)
if len(data) > 0:
question = db_session.query(models.SurveyQuestion).filter_by(id=q_id).one() #get the question answered
response_id = None
if question.input_type == "select":
if data == 'None': # no data
continue
else:
response_id = int(data)
#assume no comments for now
user_response = models.UserResponse(data,None,u_id,q_id,response_id)
db_session.add(user_response)
print user_response
db_session.commit()
def get_survey_questions(survey_name):
survey = db_session.query(models.Survey).filter_by(name=survey_name).one()
print "survey id:",survey.id
survey_id = survey.id # better be a number
questions = db_session.query(models.SurveyQuestion).filter_by(survey_id=survey_id).order_by(models.SurveyQuestion.id).all()
return questions
def generate_field_for_question(question):
if question.input_type == "select": # get selections
if question.parent_question is not None:
print "parent question:",question.parent_question
responses = db_session.query(models.SurveyQuestion).filter_by(id=question.parent_question).one().responses
else:
responses = question.responses
selections = [0] * len(responses)
for i,response in enumerate(responses):
selections[i] = (response.id,response.value)
return RadioField(question.question_text,choices=selections)
else:
return TextField(question.question_text)