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