Exemplo n.º 1
0
class BatchChannel(BaseModel):
    ACCESS_CHANNELS = [(name, name)
                       for name in InterviewerAccess.access_channels()]
    batch = models.ForeignKey(Batch, related_name='baccess_channels')
    channel = models.CharField(max_length=100, choices=ACCESS_CHANNELS)

    class Meta:
        app_label = 'survey'
        unique_together = ('batch', 'channel')
Exemplo n.º 2
0
class QuestionSetChannel(CloneableMixin, BaseModel):
    ACCESS_CHANNELS = [(name, name)
                       for name in InterviewerAccess.access_channels()]
    qset = models.ForeignKey(QuestionSet, related_name='access_channels')
    channel = models.CharField(max_length=100, choices=ACCESS_CHANNELS)

    class Meta:
        app_label = 'survey'
        unique_together = ('qset', 'channel')
Exemplo n.º 3
0
class AnswerAccessDefinition(BaseModel):
    ACCESS_CHANNELS = [(name, name) for name in InterviewerAccess.access_channels()]
    ANSWER_TYPES = [(name, name) for name in Answer.answer_types()]
    answer_type = models.CharField(max_length=100, choices=ANSWER_TYPES)
    channel = models.CharField(max_length=100, choices=ACCESS_CHANNELS)

    class Meta:
        app_label = 'survey'
        unique_together = [('answer_type', 'channel'), ]

    @classmethod
    def is_valid(cls, channel, answer_type):
        try:
            return cls.objects.get(answer_type=answer_type, channel=channel) is not None
        except cls.DoesNotExist:
            return False

    @classmethod
    def access_channels(cls, answer_type):
        return set(AnswerAccessDefinition.objects.filter(answer_type=answer_type).values_list('channel', flat=True))

    @classmethod
    def answer_types(cls, channel):
        return set(AnswerAccessDefinition.objects.filter(channel=channel).values_list('answer_type', flat=True))
Exemplo n.º 4
0
class AnswerAccessDefinition(BaseModel):
    ACCESS_CHANNELS = [(name, name)
                       for name in InterviewerAccess.access_channels()]
    ANSWER_TYPES = [(name, name) for name in Answer.answer_types()]
    answer_type = models.CharField(max_length=100, choices=ANSWER_TYPES)
    channel = models.CharField(max_length=100, choices=ACCESS_CHANNELS)

    class Meta:
        app_label = 'survey'
        unique_together = [('answer_type', 'channel'), ]

    @classmethod
    def is_valid(cls, channel, answer_type):
        try:
            return cls.objects.get(
                answer_type=answer_type,
                channel=channel) is not None
        except cls.DoesNotExist:
            return False

    @classmethod
    def access_channels(cls, answer_type):
        return set(AnswerAccessDefinition.objects.filter(answer_type=answer_type).values_list('channel', flat=True))

    @classmethod
    def answer_types(cls, channel):
        """Returns the answer type compatible with this channel"""
        return set(
            AnswerAccessDefinition.objects.filter(
                channel=channel).values_list(
                'answer_type', flat=True))

    @classmethod
    def reload_answer_categories(cls):
        from survey.models import USSDAccess, ODKAccess, WebAccess
        cls.objects.get_or_create(channel=USSDAccess.choice_name(),
                                                     answer_type=AutoResponse.choice_name())
        cls.objects.get_or_create(channel=USSDAccess.choice_name(),
                                                     answer_type=NumericalAnswer.choice_name())
        cls.objects.get_or_create(channel=USSDAccess.choice_name(),
                                                     answer_type=TextAnswer.choice_name())
        cls.objects.get_or_create(channel=USSDAccess.choice_name(),
                                                     answer_type=MultiChoiceAnswer.choice_name())

        # ODK definition
        cls.objects.get_or_create(channel=ODKAccess.choice_name(),
                                                     answer_type=AutoResponse.choice_name())
        cls.objects.get_or_create(channel=ODKAccess.choice_name(),
                                                     answer_type=NumericalAnswer.choice_name())
        cls.objects.get_or_create(channel=ODKAccess.choice_name(),
                                                     answer_type=TextAnswer.choice_name())
        cls.objects.get_or_create(channel=ODKAccess.choice_name(),
                                                     answer_type=MultiChoiceAnswer.choice_name())
        cls.objects.get_or_create(channel=ODKAccess.choice_name(),
                                                     answer_type=MultiSelectAnswer.choice_name())
        cls.objects.get_or_create(channel=ODKAccess.choice_name(),
                                                     answer_type=ImageAnswer.choice_name())
        cls.objects.get_or_create(channel=ODKAccess.choice_name(),
                                                     answer_type=GeopointAnswer.choice_name())
        cls.objects.get_or_create(channel=ODKAccess.choice_name(),
                                                     answer_type=DateAnswer.choice_name())
        cls.objects.get_or_create(channel=ODKAccess.choice_name(),
                                                     answer_type=AudioAnswer.choice_name())
        cls.objects.get_or_create(channel=ODKAccess.choice_name(),
                                                     answer_type=VideoAnswer.choice_name())

        # web form definition
        cls.objects.get_or_create(channel=WebAccess.choice_name(),
                                                     answer_type=NumericalAnswer.choice_name())
        cls.objects.get_or_create(channel=WebAccess.choice_name(),
                                                     answer_type=TextAnswer.choice_name())
        cls.objects.get_or_create(channel=WebAccess.choice_name(),
                                                     answer_type=MultiChoiceAnswer.choice_name())
        cls.objects.get_or_create(channel=WebAccess.choice_name(),
                                                     answer_type=MultiSelectAnswer.choice_name())
        cls.objects.get_or_create(channel=WebAccess.choice_name(),
                                                     answer_type=ImageAnswer.choice_name())
        cls.objects.get_or_create(channel=WebAccess.choice_name(),
                                                     answer_type=GeopointAnswer.choice_name())
        cls.objects.get_or_create(channel=WebAccess.choice_name(),
                                                     answer_type=DateAnswer.choice_name())
        cls.objects.get_or_create(channel=WebAccess.choice_name(),
                                                     answer_type=AudioAnswer.choice_name())
        cls.objects.get_or_create(channel=WebAccess.choice_name(),
                                                     answer_type=VideoAnswer.choice_name())
Exemplo n.º 5
0
class Household(BaseModel):
    MALE = True
    FEMALE = False
    REGISTRATION_CHANNELS = [(name, name)
                             for name in InterviewerAccess.access_channels()]
    house_number = models.PositiveIntegerField(verbose_name="Household Number")
    listing = models.ForeignKey(HouseholdListing, related_name='households')
    physical_address = models.CharField(max_length=200,
                                        null=True,
                                        blank=True,
                                        verbose_name="Structure Address")
    last_registrar = models.ForeignKey('Interviewer',
                                       related_name='registered_households',
                                       verbose_name='Interviewer')
    registration_channel = models.CharField(max_length=100,
                                            choices=REGISTRATION_CHANNELS)
    head_desc = models.CharField(max_length=200)
    head_sex = models.BooleanField(default=FEMALE)

    class Meta:
        app_label = 'survey'
        unique_together = [
            ('house_number', 'listing'),
        ]

    def __unicode__(self):
        return 'HH-%s' % self.house_number

    @classmethod
    def next_new_house(cls, listing):
        max = cls.objects.filter(listing=listing).aggregate(
            Max('house_number'))
        if not max: max = {'house_number': 0}
        return max['house_number'] + 1

    # def clean(self):
    #     super(Household, self).clean()
    #     if self.house_number > self.ea.total_households:
    #          raise ValidationError('Household number has exceeded total households in the Enumeration area')

    def get_head(self):
        try:
            return HouseholdHead.objects.filter(household=self)[0]
        except IndexError:
            return None

    @property
    def head_name(self):
        head = self.get_head()
        if head:
            return '%s %s' % (head.first_name or '', head.surname or '')

    @property
    def members(self):
        return HouseholdMember.objects.filter(household=self)

    def _get_related_location_name(self, key, location_hierarchy):
        location_object = location_hierarchy.get(key, None)
        location_name = ""
        if location_object:
            location_name = location_object.name
        return location_name

    def get_related_location(self):
        location_hierarchy = self.location_hierarchy()
        related_location = SortedDict()
        return related_location

    @classmethod
    def set_related_locations(cls, households):
        for household in households:
            household.related_locations = household.get_related_location()
        return households

    @classmethod
    def all_households_in(cls, location, survey, ea=None):
        all_households = Household.objects.filter(
            listing__survey_houselistings__survey=survey,
            listing__ea__locations__in=location.get_leafnodes(
                include_self=True)).distinct()
        if ea:
            return all_households.filter(listing__ea=ea)
        return all_households

    def has_completed(self, survey):
        completion_recs = HouseMemberSurveyCompletion.objects.filter(
            householdmember__household=self, survey=survey).distinct()
        return completion_recs.count() >= self.members.count()

    def has_completed_batch(self, batch):
        completion_recs = HouseholdMemberBatchCompletion.objects.filter(
            householdmember__household=self, batch=batch).distinct()
        return completion_recs.count() >= self.members.count()

    def survey_completed(self, survey, registrar):
        return HouseSurveyCompletion.objects.create(household=self,
                                                    survey=survey,
                                                    interviewer=registrar)

    def batch_completed(self, batch, registrar):
        return HouseholdBatchCompletion.objects.create(household=self,
                                                       batch=batch,
                                                       interviewer=registrar)

    def date_interviewed_for(self, batch):
        hmcs = HouseholdMemberBatchCompletion.objects.filter(
            householdmember__household=self, batch=batch)
        if hmcs.exists():
            return hmcs[0].created

    def total_members_interviewed(self, batch):
        return HouseholdMemberBatchCompletion.objects.\
            filter(householdmember__household=self, batch=batch).values('householdmember').distinct().count()