Exemple #1
0
    def __init__(self, *args, **kwargs):
        org = kwargs.pop('org')

        super(SpoofDataForm, self).__init__(*args, **kwargs)

        if org:
            contacts = Contact.get_all(org).order_by('name')
            self.fields['contacts'].queryset = contacts
            questions = Question.objects.filter(poll__in=Poll.get_all(org))
            self.fields['baseline_question'].queryset = questions
            self.fields['follow_up_question'].queryset = questions
Exemple #2
0
    def create_empty(cls, org, pollrun, run):
        """
        Creates an empty response from a run. Used to start or restart a
        contact in an existing pollrun.
        """
        contact = Contact.get_or_fetch(org, uuid=run.contact)

        # de-activate any existing responses for this contact
        pollrun.responses.filter(contact=contact).update(is_active=False)

        return Response.objects.create(
            flow_run_id=run.id, pollrun=pollrun, contact=contact,
            created_on=run.created_on, updated_on=run.created_on,
            status=Response.STATUS_EMPTY)
Exemple #3
0
    def create_empty(cls, org, issue, run):
        """
        Creates an empty response from a run. Used to start or restart a contact in an existing issue.
        """
        contact = Contact.get_or_fetch(org, uuid=run.contact)

        # de-activate any existing responses for this contact
        issue.responses.filter(contact=contact).update(is_active=False)

        return Response.objects.create(flow_run_id=run.id,
                                       issue=issue,
                                       contact=contact,
                                       created_on=run.created_on,
                                       updated_on=run.created_on,
                                       status=RESPONSE_EMPTY)
Exemple #4
0
    def from_run(cls, org, run, poll=None):
        """
        Gets or creates a response from a flow run. If response is not
        up-to-date with provided run, then it is updated. If the run doesn't
        match with an existing poll pollrun, it's assumed to be non-regional.
        """
        response = Response.objects.filter(pollrun__poll__org=org, flow_run_id=run.id)
        response = response.select_related('pollrun').first()
        run_updated_on = cls.get_run_updated_on(run)

        # if there is an up-to-date existing response for this run, return it
        if response and response.updated_on == run_updated_on:
            return response

        if not poll:
            poll = Poll.objects.active().by_org(org).get(flow_uuid=run.flow)

        contact = Contact.get_or_fetch(poll.org, uuid=run.contact)

        # categorize completeness
        if run.completed:
            status = Response.STATUS_COMPLETE
        elif run.values:
            status = Response.STATUS_PARTIAL
        else:
            status = Response.STATUS_EMPTY

        if response:
            # clear existing answers which will be replaced
            response.answers.all().delete()

            response.updated_on = run_updated_on
            response.status = status
            response.save(update_fields=('updated_on', 'status'))
        else:
            # if we don't have an existing response, then this poll started in
            # RapidPro and is non-regional
            pollrun = PollRun.objects.get_or_create_universal(
                poll=poll,
                for_date=run.created_on,
            )

            # if contact has an older response for this pollrun, retire it
            Response.objects.filter(pollrun=pollrun, contact=contact).update(is_active=False)

            response = Response.objects.create(
                flow_run_id=run.id, pollrun=pollrun, contact=contact,
                created_on=run.created_on, updated_on=run_updated_on,
                status=status)
            response.is_new = True

        # organize values by ruleset UUID
        questions = poll.questions.active()
        valuesets_by_ruleset = {valueset.node: valueset for valueset in run.values}
        valuesets_by_question = {q: valuesets_by_ruleset.get(q.ruleset_uuid, None)
                                 for q in questions}

        # convert valuesets to answers
        for question, valueset in valuesets_by_question.iteritems():
            if valueset:
                Answer.objects.create(
                    response=response,
                    question=question,
                    value=valueset.value,
                    category=valueset.category,
                    submitted_on=valueset.time,
                )

        return response
Exemple #5
0
 def create_contact(self, org, name, urn, region, group, uuid):
     user = org.administrators.first()
     return Contact.create(
         org, user, name, urn, region, group, 'FC123', 'eng', uuid)
Exemple #6
0
 def create_contact(self, org, name, urn, region, group, uuid):
     user = org.administrators.first()
     return Contact.create(org, user, name, urn, region, group, 'FC123', 'eng', uuid)
Exemple #7
0
    def create_contact(self, name, phone, region, uuid=None):
        if not uuid:
            uuid = uuid4()

        return Contact.create(name, 'tel', phone, region, uuid)
Exemple #8
0
    def from_run(cls, org, run, poll=None):
        """
        Gets or creates a response from a flow run. If response is not up-to-date with provided run, then it is
        updated. If the run doesn't match with an existing poll issue, it's assumed to be non-regional.
        """
        response = Response.objects.filter(
            issue__poll__org=org,
            flow_run_id=run.id).select_related('issue').first()
        run_updated_on = cls.get_run_updated_on(run)

        # if there is an up-to-date existing response for this run, return it
        if response and response.updated_on == run_updated_on:
            return response

        if not poll:
            poll = Poll.get_all(org).get(flow_uuid=run.flow)

        contact = Contact.get_or_fetch(poll.org, uuid=run.contact)

        # categorize completeness
        if run.completed:
            status = RESPONSE_COMPLETE
        elif run.values:
            status = RESPONSE_PARTIAL
        else:
            status = RESPONSE_EMPTY

        if response:
            # clear existing answers which will be replaced
            response.answers.all().delete()

            response.updated_on = run_updated_on
            response.status = status
            response.save(update_fields=('updated_on', 'status'))
        else:
            # if we don't have an existing response, then this poll started in RapidPro and is non-regional
            issue = Issue.get_or_create_non_regional(org,
                                                     poll,
                                                     for_date=run.created_on)

            # if contact has an older response for this issue, retire it
            Response.objects.filter(issue=issue,
                                    contact=contact).update(is_active=False)

            response = Response.objects.create(flow_run_id=run.id,
                                               issue=issue,
                                               contact=contact,
                                               created_on=run.created_on,
                                               updated_on=run_updated_on,
                                               status=status)
            response.is_new = True

        # organize values by ruleset UUID
        questions = poll.get_questions()
        valuesets_by_ruleset = {
            valueset.node: valueset
            for valueset in run.values
        }
        valuesets_by_question = {
            q: valuesets_by_ruleset.get(q.ruleset_uuid, None)
            for q in questions
        }

        # convert valuesets to answers
        for question, valueset in valuesets_by_question.iteritems():
            if valueset:
                Answer.create(response, question, valueset.value,
                              valueset.category, valueset.time)

        # clear answer caches for this contact's region
        for question in questions:
            response.issue.clear_answer_cache(question, contact.region)

        return response