def get(self, survey_id, action = 'repr'): args = self.get_args() try: s_id = HashId.decode(survey_id) svey = Survey.objects(id = s_id).first() if svey is None: raise TypeError if svey.hidden: raise APIException("This Survey has been deleted", 404) except TypeError: raise APIException("Invalid Survey ID", 404) if action == 'json': if args['editing'] == 'true': return svey.struct try: return svey.render_json except TypeError as e: raise APIException(str(e), 400) elif action == 'deepjson': return svey.render_deepjson elif action == 'repr': return svey.repr raise APIException("Must specify a valid option", 400)
def delete(self, survey_id, action = 'self'): if current_user.is_authenticated(): try: s_id = HashId.decode(survey_id) svey = Survey.objects(id = s_id).first() if svey is None: raise TypeError if svey.hidden: raise APIException("This Survey has already been deleted", 404) except TypeError: raise APIException("Invalid Survey ID", 404) if action == 'self': svey.hidden = True svey.save() ret = { 'id': str(svey), 'field': action, 'saved': True, } return ret, 200 elif action == 'img': args = self.post_args() dat = args['swag'] try: im_id = HashId.decode(dat) img = AttachmentImage.objects(id = im_id).first() if img is None: raise TypeError if img.hidden: raise APIException("This Image has already been deleted", 404) img.hidden = True svey.attachments.remove(img) img.save() svey.save() ret = { 'id': str(svey), 'img_id': str(img), 'field': action, 'saved': True, } return ret, 200 except (TypeError, ValueError): raise APIException("Invalid Image ID", 404) else: raise APIException("Login Required", 401)
def get(self): svey = Survey.objects(created_by = current_user.id) survey_list = [_.repr_sm for _ in svey if not _.hidden] ret = { "data": survey_list, "survey_count": len(survey_list), } return ret, 200
def post(self): args = self.post_args() svey = Survey() usr = User.objects(id = current_user.id).first() svey.metadata['name'] = args['s_name'] # struct_dict = starter_template # opt = [] # for option in args['s_tags']: # opt.append({ # 'checked': False, # 'label': option # }) # struct_dict['fields'][0]['field_options']['options'] = opt # svey.struct = struct_dict svey.created_by.append(usr) svey.save() return svey.repr, 200
def get_analysis_page(survey_id): try: s_id = HashId.decode(survey_id) svey = Survey.objects(id = s_id).first() if svey is None: raise TypeError except TypeError: raise ViewException("Invalid Survey ID", 404) return render_template('srvy.analysis.html', title = "Analytics", survey = svey.repr)
def get_analysis_page(survey_id): try: s_id = HashId.decode(survey_id) svey = Survey.objects(id = s_id).first() if svey is None: raise TypeError except TypeError: raise ViewException("Invalid Survey ID", 404) return render_template('survaiderdashboard/index.analysis.html', title = "Analytics", survey = svey.repr)
def dashboard_home(): try: survey = Survey.objects(created_by=current_user()).first() if survey is None: raise ValueError url = "/survey/s:{0}/analysis".format(str(survey)) if not survey._cls == 'Survey.SurveyUnit': url += "?parent=true" return redirect(url) except ValueError: return render_template("onboarding.html", title="Onboarding")
def get(self, survey_id): """ GET /api/survey/<survey_id>/response Returns whether any valid response object for the <survey_id> exists in the Collection. It is up to the Client Code to implement saving of the responses so that the survey can be continued even after it is terminated pre-maturely. For security, the responses are NOT returned. Args: new (bool): Send a True GET parameter in URL to end the currently running response. Example: GET /api/survey/<survey_id>/response?new=true """ try: s_id = HashId.decode(survey_id) svey = Survey.objects(id = s_id).first() if svey is None: raise TypeError if svey.hidden: raise APIException("This Survey has been deleted", 404) except TypeError: raise APIException("Invalid Survey ID", 404) args = self.get_args() is_running = ResponseSession.is_running(s_id) ret = { 'response_session_running': is_running, 'will_accept_response': svey.active, 'will_end_session': False, 'is_expired': svey.expires <= datetime.datetime.now(), 'is_paused': svey.paused, 'is_active': svey.active, 'expires': str(svey.expires), } if is_running: "End The Existing Survey." if args['new']: ResponseSession.finish_running(s_id) ret['will_accept_response'] = False ret['will_end_session'] = True return ret, 201
def get_index(survey_id): try: s_id = HashId.decode(survey_id) svey = Survey.objects(id = s_id).first() if svey is None: raise TypeError if svey.hidden: raise ViewException("This Survey has been deleted.", 404) except TypeError: raise ViewException("Invalid Survey ID", 404) return render_template('srvy.index.html', title = "Editing Survaider", survey = svey)
def get_simple_survey(survey_id): try: s_id = HashId.decode(survey_id) svey = Survey.objects(id = s_id).first() if svey is None: raise TypeError if not svey.active: raise ViewException("This Survey is not accepting Responses at this moment", 403) except TypeError: raise ViewException("Invalid Survey ID", 404) return app.send_static_file('simplesurvey/index.simplesurvey.html')
def get_gamified_survey(survey_id): try: s_id = HashId.decode(survey_id) svey = Survey.objects(id = s_id).first() if svey is None: raise TypeError if not svey.active: raise ViewException("This Survey is not accepting Responses at this moment", 403) if not svey.gamified_enabled: raise ViewException("This Survey cannot be rendered as a Game.", 403) except TypeError: raise ViewException("Invalid Survey ID", 404) return app.send_static_file('gamified/index.html')
def get(self, survey_id, response_id): try: s_id = HashId.decode(survey_id) svey = Survey.objects(id = s_id).first() if svey is None: raise TypeError("Invalid Survey ID") if svey.hidden: raise APIException("This Survey has been deleted", 404) r_id = HashId.decode(response_id) res = Response.objects(id = r_id, parent_survey = s_id).first() if res is None: raise TypeError("Invalid Response ID") except TypeError as e: ret = str(e) if len(str(e)) > 0 else "Invalid Hash ID" raise APIException(ret, 404) return json.loads(res.to_json()), 201
def get(self, survey_id, action = 'flat'): try: s_id = HashId.decode(survey_id) svey = Survey.objects(id = s_id).first() if svey is None: raise TypeError if svey.hidden: raise APIException("This Survey has been deleted", 404) except TypeError: raise APIException("Invalid Survey ID", 404) responses = ResponseAggregation(svey) if action == 'flat': return responses.flat(), 201 elif action == 'nested': return responses.nested(), 201 raise APIException("Must specify a valid option", 400)
def migrate_db(): "Against Commit: c46d853c2949cc78ba86c0a8a618cf3ba0a3e44b" create_app() from survaider.survey.model import Survey from survaider.minions.attachment import Image c2 = 0 for svey in Survey.objects(): #: Take the Old Uploaded images and move them to the Images DB old_img = svey.metadata.get('img_uploads', []) u = svey.created_by[0] c1 = 0 for img in old_img: i = Image() i.owner = u i.filename = img i.save() svey.attachments.append(i) c1 += 1 if 'img_uploads' in svey.metadata: del svey.metadata['img_uploads'] svey.save() c2 += 1 print("Updated {0} entries in Survey {1}".format(c1, c2))
def post(self): args = self.post_args() svey = Survey() usr = User.objects(id = current_user.id).first() svey.metadata['name'] = args['s_name'] struct_dict = starter_template opt = [] for option in args['s_tags']: opt.append({ 'checked': False, 'label': option }) struct_dict['fields'][0]['field_options']['options'] = opt svey.struct = struct_dict svey.created_by.append(usr) svey.save() return svey.repr, 200
def post(self, survey_id): """ POST /api/survey/<survey_id>/response Saves the Question Responses. The responses are saved atomically. Aggregate saving of the responses is NOT implemented yet, since the game requires atomic response storage. Appends to the existing response document if it exists, otherwise creats a new document. Args: q_id (str): Question ID, as generated in the survey structure. q_res (str): Response. """ try: s_id = HashId.decode(survey_id) svey = Survey.objects(id = s_id).first() if svey is None: raise TypeError if svey.hidden: raise APIException("This Survey has been deleted", 404) except TypeError: raise APIException("Invalid Survey ID", 404) if not svey.active: raise APIException("This Survey is Inactive or is not accepting responses anymore.", 403) resp = None ret = { "existing_response_session": False, "new_response_session": False, "will_add_id": None, } if ResponseSession.is_running(svey.id): "Uses existing Response Session." ret['existing_response_session'] = True resp_id = ResponseSession.get_running_id(s_id) resp = Response.objects(id = resp_id).first() else: "Creates a New Response Session." ret['new_response_session'] = True resp = Response(parent_survey = svey) resp.metadata['started'] = datetime.datetime.now() resp.save() ResponseSession.start(s_id, resp.id) args = self.post_args() try: resp.add(args['q_id'], args['q_res']) ret['will_add_id'] = args['q_id'] except TypeError as te: raise APIException(str(te), 400) return ret, 200