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 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