Example #1
0
    def expand_recipients(self):
        """
        Can be used as a generator to iterate over all individual contacts who
        are the recipients of this ScheduleInstance.
        """
        if self.recipient is None:
            return
        elif self.recipient_is_an_individual_contact:
            yield self.recipient
        elif isinstance(self.recipient, CommCareCaseGroup):
            case_group = self.recipient
            for case in case_group.get_cases():
                yield case
        elif isinstance(self.recipient, Group):
            group = self.recipient
            for user in group.get_users(is_active=True, only_commcare=False):
                yield user
        elif isinstance(self.recipient, SQLLocation):
            location = self.recipient
            if self.memoized_schedule.include_descendant_locations:
                location_ids = location.get_descendants(
                    include_self=True).filter(
                        is_archived=False).location_ids()
            else:
                location_ids = [location.location_id]

            user_ids = set()
            for location_id in location_ids:
                for user in get_all_users_by_location(self.domain,
                                                      location_id):
                    if user.is_active and user.get_id not in user_ids:
                        user_ids.add(user.get_id)
                        yield user
        else:
            raise UnknownRecipientType(self.recipient.__class__.__name__)
Example #2
0
 def recipient(self):
     if self.recipient_type == 'CommCareCase':
         return CaseAccessors(self.domain).get_case(self.recipient_id)
     elif self.recipient_type == 'CommCareUser':
         return CommCareUser.get(self.recipient_id)
     elif self.recipient_type == 'WebUser':
         return WebUser.get(self.recipient_id)
     elif self.recipient_type == 'CommCareCaseGroup':
         return CommCareCaseGroup.get(self.recipient_id)
     elif self.recipient_type == 'Group':
         return Group.get(self.recipient_id)
     elif self.recipient_type == 'Location':
         return SQLLocation.by_location_id(self.recipient_id)
     else:
         raise UnknownRecipientType(self.recipient_type)
Example #3
0
 def recipient(self):
     if self.recipient_type == self.RECIPIENT_TYPE_CASE:
         return CaseAccessors(self.domain).get_case(self.recipient_id)
     elif self.recipient_type == self.RECIPIENT_TYPE_MOBILE_WORKER:
         return CommCareUser.get(self.recipient_id)
     elif self.recipient_type == self.RECIPIENT_TYPE_WEB_USER:
         return WebUser.get(self.recipient_id)
     elif self.recipient_type == self.RECIPIENT_TYPE_CASE_GROUP:
         return CommCareCaseGroup.get(self.recipient_id)
     elif self.recipient_type == self.RECIPIENT_TYPE_USER_GROUP:
         return Group.get(self.recipient_id)
     elif self.recipient_type == self.RECIPIENT_TYPE_LOCATION:
         return SQLLocation.by_location_id(self.recipient_id)
     else:
         raise UnknownRecipientType(self.recipient_type)
Example #4
0
    def _expand_recipient(self, recipient):
        if recipient is None:
            return
        elif self.recipient_is_an_individual_contact(recipient):
            yield recipient
        elif isinstance(recipient, CommCareCaseGroup):
            case_group = recipient
            for case in case_group.get_cases():
                yield case
        elif isinstance(recipient, Group):
            group = recipient
            for user in group.get_users(is_active=True, only_commcare=False):
                yield user
        elif isinstance(recipient, SQLLocation):
            location = recipient
            if (self.recipient_type == self.RECIPIENT_TYPE_LOCATION
                    and self.memoized_schedule.include_descendant_locations):
                # Only include descendant locations when the recipient_type
                # is RECIPIENT_TYPE_LOCATION. This is because we only do this
                # for locations the user selected in the UI, and not for
                # locations that happen to get here because they are a case
                # owner, for example.
                qs = location.get_descendants(include_self=True).filter(
                    is_archived=False)

                # We also only apply the location_type_filter when the recipient_type
                # is RECIPIENT_TYPE_LOCATION for the same reason.
                if self.memoized_schedule.location_type_filter:
                    qs = qs.filter(location_type_id__in=self.memoized_schedule.
                                   location_type_filter)

                location_ids = qs.location_ids()
            else:
                location_ids = [location.location_id]

            user_ids = set()
            for location_id in location_ids:
                for user in get_all_users_by_location(self.domain,
                                                      location_id):
                    if user.is_active and user.get_id not in user_ids:
                        user_ids.add(user.get_id)
                        yield user
        else:
            raise UnknownRecipientType(recipient.__class__.__name__)
Example #5
0
            if group.domain != self.domain:
                return None

            return group
        elif self.recipient_type == self.RECIPIENT_TYPE_LOCATION:
            location = SQLLocation.by_location_id(self.recipient_id)

            if location is None:
                return None

            if location.domain != self.domain:
                return None

            return location
        else:
            raise UnknownRecipientType(self.recipient_type)

    @staticmethod
    def recipient_is_an_individual_contact(recipient):
        return (
            isinstance(recipient, (CommCareUser, WebUser)) or
            is_commcarecase(recipient)
        )

    @property
    @memoized
    def domain_timezone(self):
        try:
            return get_timezone_for_domain(self.domain)
        except ValidationError:
            return pytz.UTC