def survey(course_key): try: course = ndb.Key(urlsafe=course_key).get() except db.BadKeyError: course = None if course is None: return abort(404) if request.method == 'POST': survey = StudentSurvey(parent=course.key, participant=current_user.key) survey.put() answers = [] questions = ndb.get_multi( [ndb.Key(urlsafe=question) for question in request.form.keys()]) for question, answer in zip(questions, request.form.values()): if question is None: continue if question.question_type == 'closed': answers.append( Answer(question=question.key, int_value=int(answer), parent=survey.key)) else: answers.append( Answer(question=question.key, string_value=answer, parent=survey.key)) ndb.put_multi(answers) deferred.defer(_send_to_keen, survey.key, course.key, [answer.key for answer in answers]) return redirect(url_for('home')) questions = Question.get_active().fetch() numbering = 0 dimension_check = '' for question in questions: if dimension_check != question.dimension: numbering += 1 dimension_check = question.dimension question.dimension = str(numbering) + question.dimension return render_template('survey.haml', questions=questions, course=course)
def analysis(class_key): try: class_ = ndb.Key(urlsafe=class_key).get() except db.BadKeyError: class_ = None if class_ is None: return abort(404) course = class_.course.get() lecturer = class_.lecturer.get() surveys = StudentSurvey.query(ancestor=class_.key) return render_template('analysis.haml', surveys=surveys, course=course, lecturer=lecturer, class_key=class_key, questions=Question.get_active())
def query(): final_filter = [] """Custom queries using keen.io as a backend. The frontent form must pass values in using the names `property_name` - The name of the property you wish to query `property_value` - The value of the property being entered `operator` - 'eq', 'gt', 'lt' """ if current_user.user_type != 'admin': return 403 if request.method == 'POST': # Run the query # Used to display the query filters on the view. filter_view = [request.form.to_dict()] number_of_filters = len(request.form) / 3 for filter_number in range(0, number_of_filters): temp = {} temp['operator'] = request.form["operator" + str(filter_number)] temp['property_value'] = request.form["property_value" + str(filter_number)] temp['property_name'] = request.form["property_name" + str(filter_number)] final_filter.append(temp) response = keen.extraction('answers', filters=final_filter) if len(response) == 0: response = 'No results found' final_filter = json.dumps(final_filter) # response = json.dumps(response, indent=2, sort_keys=True) return render_template('custom_analysis.haml', filters=final_filter, questions=Question.get_active(), filter_view=filter_view, number_of_filters=number_of_filters, response=response) return 'Why'