コード例 #1
0
    def start_instance(instance):
        instance_id = instance.id
        print("Starting instance " + str(instance_id))

        survey = SurveyService.get_survey(instance.survey_id)
        timeout_minutes = survey.timeout
        timeout_timestamp = datetime.now(tz=pytz.utc) + timedelta(
            minutes=timeout_minutes)

        StateService.create_state(instance_id, 1, Status.CREATED_START,
                                  timeout_timestamp, 0)

        participants = ParticipantService.get_participants_in_enrollment(
            survey.enrollment_id)
        plugin_ids = set()

        for participant in participants:
            plugin_ids.add(participant.plugin_id)

        for plugin_id in plugin_ids:
            PluginService.poke(plugin_id, survey.id)
コード例 #2
0
    def post(self, instance_id):
        auth_response = authenticate(self, [Permissions.WRITE_RESPONSE])
        if auth_response["valid"]:
            data = json_decode(self.request.body)
            if 'response' in data:
                response = data['response']
                contact = data['contact']
            else:
                self.set_status(400)
                self.write('{"status":"error","message":"Missing response parameter"}')
                return
            
            instance = InstanceService.get_instance(instance_id)
            if instance is None:
                self.set_status(410)
                self.write('{"status":"error","message":"No response was expected for this survey"}')
                self.finish()
                return

            instance_id = instance.id  # Ensure that id is of right type
            state = StateService.get_next_state_in_instance(instance, Status.AWAITING_USER_RESPONSE)
            if state is not None and state.status is not Status.NO_RESPONSE_REQUIRED:
                survey = SurveyService.get_survey(instance.survey_id)
                if str(survey.owner_id) == auth_response["owner_id"]:
                    question_number = state.question_number
                    question_service = QuestionService()
                    question = question_service.get(survey.protocol_id, question_number)
                    now = datetime.now(tz=pytz.utc)
                    now_csv_entry = str(datetime.now(tz=pytz.utc))
                    loaddata = LoadData()
                    participant_id = loaddata.sendparticpantid(contact)
                    loaddata_response = LoadData1()
                    #loaddata_response.concatresponse(participant_id,question_number,response, now_csv_entry[:10],now_csv_entry[11:16])
                    if question is not None:

                        if question.final:
                            state.status = Status.TERMINATED_COMPLETE.value
                            StateService.update_state(state)

                            self.set_status(200)
                            self.write('{"status":"success","response_accepted":"False","reason":"Survey has finished"}')
                            self.flush()
                        elif state.timeout.replace(tzinfo=pytz.utc) < now:
                            state.status = Status.TERMINATED_TIMEOUT.value
                            state.save()
                            self.set_status(200)
                            self.write(
                                '{"status":"success","response_accepted":"False","reason":"Survey has timed out"}')
                        else:
                            state.status = Status.PROCESSING_USER_RESPONSE.value
                            StateService.update_state(state)
                            loaddata_response.concatresponse(participant_id,question_number,response, now_csv_entry[:10])
                            new_questions = question.process(response)
                            if new_questions == 'INV_RESP':
                                state.status = Status.AWAITING_USER_RESPONSE.value
                                StateService.update_state(state)

                                self.set_status(200)
                                self.write('{"status":"success","response_accepted":"False","reason":"Invalid Response","pass_along_message":"'
                                           + question.invalid_message + '"}')
                                self.flush()
                            else:
                                response_service = ResponseService()
                                variable_name = question.variable_name
                                survey_id = instance.survey_id
                                response_service.insert_response(survey_id, instance_id, variable_name, response)

                                if new_questions is not None:
                                    for new_question in new_questions:
                                        #print("Is this the final question", isFinal)
                                        #if not new_question.final:
                                        if not question.final:
                                            status = Status.CREATED_MID
                                        else:
                                            status = Status.NO_RESPONSE_REQUIRED

                                        StateService.create_state(instance_id, new_question[0][1], status,
                                                                  state.timeout, new_question[1])

                                state.status = Status.TERMINATED_COMPLETE.value
                                StateService.update_state(state)

                                new_state = StateService.get_next_state_in_instance(instance, Status.CREATED_MID)

                                new_state.status = Status.AWAITING_USER_RESPONSE.value
                                StateService.update_state(new_state)

                                self.set_status(200)
                                self.write('{"status":"success","response_accepted":"True"}')
                                self.flush()

                    else:
                        self.set_status(410, "No response was expected for this survey")
                        self.write('{"status":"error","message":"No response was expected for this survey"}')
                        self.finish()
                else:
                    self.set_status(403)
                    self.write('{"status":"error","message":"Owner does not have authorization to modify survey"}')
                    self.flush()
            else:
                self.set_status(410)
                self.write('{"status":"error","message":"No response was expected for this survey"}')
                self.finish()