Ejemplo n.º 1
0
    def get_recipient_info(self, recipient_doc_type, recipient_id, contact_cache):
        if recipient_doc_type in ['SQLLocation']:
            return self.get_orm_recipient_info(recipient_doc_type, recipient_id, contact_cache)

        if recipient_id in contact_cache:
            return contact_cache[recipient_id]

        doc = None
        if recipient_id not in [None, ""]:
            try:
                if recipient_doc_type.startswith('CommCareCaseGroup'):
                    doc = CommCareCaseGroup.get(recipient_id)
                elif recipient_doc_type.startswith('CommCareCase'):
                    doc = CommCareCase.get(recipient_id)
                elif recipient_doc_type in ('CommCareUser', 'WebUser'):
                    doc = CouchUser.get_by_user_id(recipient_id)
                elif recipient_doc_type.startswith('Group'):
                    doc = Group.get(recipient_id)
            except Exception:
                pass

        doc_info = None
        if doc:
            try:
                doc_info = get_doc_info(doc.to_json(), self.domain)
            except DomainMismatchException:
                # This can happen, for example, if a WebUser was sent an SMS
                # and then they unsubscribed from the domain. If that's the
                # case, we'll just leave doc_info as None and no contact link
                # will be displayed.
                pass

        contact_cache[recipient_id] = doc_info

        return doc_info
Ejemplo n.º 2
0
    def get_recipient_info(self, recipient_doc_type, recipient_id, contact_cache):
        if recipient_doc_type in ['SQLLocation']:
            return self.get_orm_recipient_info(recipient_doc_type, recipient_id, contact_cache)

        if recipient_id in contact_cache:
            return contact_cache[recipient_id]

        doc = None
        if recipient_id not in [None, ""]:
            try:
                if recipient_doc_type.startswith('CommCareCaseGroup'):
                    doc = CommCareCaseGroup.get(recipient_id)
                elif recipient_doc_type.startswith('CommCareCase'):
                    doc = CommCareCase.get(recipient_id)
                elif recipient_doc_type in ('CommCareUser', 'WebUser'):
                    doc = CouchUser.get_by_user_id(recipient_id)
                elif recipient_doc_type.startswith('Group'):
                    doc = Group.get(recipient_id)
            except Exception:
                pass

        if doc:
            doc_info = get_doc_info(doc.to_json(), self.domain)
        else:
            doc_info = None

        contact_cache[recipient_id] = doc_info

        return doc_info
Ejemplo n.º 3
0
    def get_recipient_info(self, recipient_doc_type, recipient_id, contact_cache):
        if recipient_doc_type in ['SQLLocation']:
            return self.get_orm_recipient_info(recipient_doc_type, recipient_id, contact_cache)

        if recipient_id in contact_cache:
            return contact_cache[recipient_id]

        doc = None
        if recipient_id not in [None, ""]:
            try:
                if recipient_doc_type.startswith('CommCareCaseGroup'):
                    doc = CommCareCaseGroup.get(recipient_id)
                elif recipient_doc_type.startswith('CommCareCase'):
                    doc = CommCareCase.get(recipient_id)
                elif recipient_doc_type in ('CommCareUser', 'WebUser'):
                    doc = CouchUser.get_by_user_id(recipient_id)
                elif recipient_doc_type.startswith('Group'):
                    doc = Group.get(recipient_id)
            except Exception:
                pass

        doc_info = None
        if doc:
            try:
                doc_info = get_doc_info(doc.to_json(), self.domain)
            except DomainMismatchException:
                # This can happen, for example, if a WebUser was sent an SMS
                # and then they unsubscribed from the domain. If that's the
                # case, we'll just leave doc_info as None and no contact link
                # will be displayed.
                pass

        contact_cache[recipient_id] = doc_info

        return doc_info
Ejemplo n.º 4
0
    def get_recipient_info(self, recipient_doc_type, recipient_id,
                           contact_cache):
        if recipient_id in contact_cache:
            return contact_cache[recipient_id]

        doc = None
        if recipient_id not in [None, ""]:
            try:
                if recipient_doc_type.startswith('CommCareCaseGroup'):
                    doc = CommCareCaseGroup.get(recipient_id)
                elif recipient_doc_type.startswith('CommCareCase'):
                    doc = CommCareCase.get(recipient_id)
                elif recipient_doc_type in ('CommCareUser', 'WebUser'):
                    doc = CouchUser.get_by_user_id(recipient_id)
                elif recipient_doc_type.startswith('Group'):
                    doc = Group.get(recipient_id)
            except Exception:
                pass

        if doc:
            doc_info = get_doc_info(doc.to_json(), self.domain)
        else:
            doc_info = None

        contact_cache[recipient_id] = doc_info

        return doc_info
Ejemplo n.º 5
0
    def rows(self):
        startdate = json_format_datetime(self.datespan.startdate_utc)
        enddate = json_format_datetime(self.datespan.enddate_utc)
        data = SMSLog.by_domain_date(self.domain, startdate, enddate)
        result = []

        direction_map = {
            INCOMING: _("Incoming"),
            OUTGOING: _("Outgoing"),
        }

        # Retrieve message log options
        message_log_options = getattr(settings, "MESSAGE_LOG_OPTIONS", {})
        abbreviated_phone_number_domains = message_log_options.get(
            "abbreviated_phone_number_domains", [])
        abbreviate_phone_number = (self.domain
                                   in abbreviated_phone_number_domains)

        contact_cache = {}

        for message in data:
            if message.direction == OUTGOING and not message.processed:
                continue
            recipient_id = message.couch_recipient
            doc = None
            if recipient_id not in [None, ""]:
                try:
                    if message.couch_recipient_doc_type == "CommCareCase":
                        doc = CommCareCase.get(recipient_id)
                    else:
                        doc = CouchUser.get_by_user_id(recipient_id)
                except Exception:
                    pass

            if doc:
                doc_info = get_doc_info(doc.to_json(), self.domain,
                                        contact_cache)
            else:
                doc_info = None

            phone_number = message.phone_number
            if abbreviate_phone_number and phone_number is not None:
                phone_number = phone_number[0:7] if phone_number[
                    0:1] == "+" else phone_number[0:6]

            timestamp = tz_utils.adjust_datetime_to_timezone(
                message.date, pytz.utc.zone, self.timezone.zone)
            result.append([
                self._fmt_timestamp(timestamp),
                self._fmt_contact_link(message, doc_info),
                self._fmt(phone_number),
                self._fmt(direction_map.get(message.direction, "-")),
                self._fmt(message.text),
            ])

        return result
Ejemplo n.º 6
0
 def deal_with_couch_doc(doc):
     domain = doc.get('domain') or doc.get('domains', [None])[0]
     if request.couch_user.is_superuser or (domain and request.couch_user.is_domain_admin(domain)):
         doc_info = get_doc_info(doc, domain_hint=domain)
     else:
         raise Http404()
     if redirect and doc_info.link:
         messages.info(request, _("We've redirected you to the %s matching your query") % doc_info.type_display)
         return HttpResponseRedirect(doc_info.link)
     else:
         return json_response(doc_info)
Ejemplo n.º 7
0
    def rows(self):
        startdate = json_format_datetime(self.datespan.startdate_utc)
        enddate = json_format_datetime(self.datespan.enddate_utc)
        data = SMSLog.by_domain_date(self.domain, startdate, enddate)
        result = []

        direction_map = {
            INCOMING: _("Incoming"),
            OUTGOING: _("Outgoing"),
        }

        # Retrieve message log options
        message_log_options = getattr(settings, "MESSAGE_LOG_OPTIONS", {})
        abbreviated_phone_number_domains = message_log_options.get("abbreviated_phone_number_domains", [])
        abbreviate_phone_number = (self.domain in abbreviated_phone_number_domains)

        contact_cache = {}

        for message in data:
            if message.direction == OUTGOING and not message.processed:
                continue
            recipient_id = message.couch_recipient
            doc = None
            if recipient_id not in [None, ""]:
                try:
                    if message.couch_recipient_doc_type == "CommCareCase":
                        doc = CommCareCase.get(recipient_id)
                    else:
                        doc = CouchUser.get_by_user_id(recipient_id)
                except Exception:
                    pass

            if doc:
                doc_info = get_doc_info(doc.to_json(), self.domain,
                    contact_cache)
            else:
                doc_info = None

            phone_number = message.phone_number
            if abbreviate_phone_number and phone_number is not None:
                phone_number = phone_number[0:7] if phone_number[0:1] == "+" else phone_number[0:6]

            timestamp = tz_utils.adjust_datetime_to_timezone(message.date, pytz.utc.zone, self.timezone.zone)
            result.append([
                self._fmt_timestamp(timestamp),
                self._fmt_contact_link(message, doc_info),
                self._fmt(phone_number),
                self._fmt(direction_map.get(message.direction,"-")),
                self._fmt(message.text),
            ])

        return result
Ejemplo n.º 8
0
 def deal_with_couch_doc(doc):
     domain = doc.get('domain') or doc.get('domains', [None])[0]
     if request.couch_user.is_superuser or (
             domain and request.couch_user.is_domain_admin(domain)):
         doc_info = get_doc_info(doc, domain_hint=domain)
     else:
         raise Http404()
     if redirect and doc_info.link:
         messages.info(
             request,
             _("We've redirected you to the %s matching your query") %
             doc_info.type_display)
         return HttpResponseRedirect(doc_info.link)
     else:
         return json_response(doc_info)
Ejemplo n.º 9
0
    def get_recipient_info(self, domain, recipient_doc_type, recipient_id, contact_cache):
        """
        We need to accept domain as an arg here for admin reports that extend this base.
        """

        if recipient_id in contact_cache:
            return contact_cache[recipient_id]

        couch_object = None
        sql_object = None

        if recipient_id:
            try:
                if recipient_doc_type.startswith('CommCareCaseGroup'):
                    couch_object = CommCareCaseGroup.get(recipient_id)
                elif recipient_doc_type.startswith('CommCareCase'):
                    obj = CaseAccessors(domain).get_case(recipient_id)
                    if isinstance(obj, CommCareCase):
                        couch_object = obj
                    elif isinstance(obj, CommCareCaseSQL):
                        sql_object = obj
                elif recipient_doc_type in ('CommCareUser', 'WebUser'):
                    couch_object = CouchUser.get_by_user_id(recipient_id)
                elif recipient_doc_type.startswith('Group'):
                    couch_object = Group.get(recipient_id)
                elif recipient_doc_type == 'SQLLocation':
                    sql_object = SQLLocation.objects.get(location_id=recipient_id)
            except (ResourceNotFound, CaseNotFound, ObjectDoesNotExist):
                pass

        doc_info = None
        if couch_object:
            try:
                doc_info = get_doc_info(couch_object.to_json(), domain)
            except DomainMismatchException:
                # This can happen, for example, if a WebUser was sent an SMS
                # and then they unsubscribed from the domain. If that's the
                # case, we'll just leave doc_info as None and no contact link
                # will be displayed.
                pass

        if sql_object:
            doc_info = get_object_info(sql_object)

        contact_cache[recipient_id] = doc_info

        return doc_info
Ejemplo n.º 10
0
def quick_find(request):
    query = request.GET.get('q')
    redirect = request.GET.get('redirect') != 'false'
    if not query:
        return HttpResponseBadRequest('GET param "q" must be provided')

    result = lookup_doc_id(query)
    if not result:
        raise Http404()

    is_member = result.domain and request.couch_user.is_member_of(result.domain, allow_mirroring=True)
    if is_member or request.couch_user.is_superuser:
        doc_info = get_doc_info(result.doc)
    else:
        raise Http404()
    if redirect and doc_info.link:
        messages.info(request, _("We've redirected you to the %s matching your query") % doc_info.type_display)
        return HttpResponseRedirect(doc_info.link)
    elif redirect and request.couch_user.is_superuser:
        return HttpResponseRedirect('{}?id={}'.format(reverse('raw_doc'), result.doc_id))
    else:
        return JsonResponse(doc_info.to_json())
Ejemplo n.º 11
0
    def get_recipient_info(self, message, contact_cache):
        recipient_id = message.couch_recipient

        if recipient_id in contact_cache:
            return contact_cache[recipient_id]

        doc = None
        if recipient_id not in [None, ""]:
            try:
                if message.couch_recipient_doc_type == "CommCareCase":
                    doc = CommCareCase.get(recipient_id)
                else:
                    doc = CouchUser.get_by_user_id(recipient_id)
            except Exception:
                pass

        if doc:
            doc_info = get_doc_info(doc.to_json(), self.domain)
        else:
            doc_info = None

        contact_cache[recipient_id] = doc_info

        return doc_info
Ejemplo n.º 12
0
    def get_recipient_info(self, message, contact_cache):
        recipient_id = message.couch_recipient

        if recipient_id in contact_cache:
            return contact_cache[recipient_id]

        doc = None
        if recipient_id not in [None, ""]:
            try:
                if message.couch_recipient_doc_type == "CommCareCase":
                    doc = CommCareCase.get(recipient_id)
                else:
                    doc = CouchUser.get_by_user_id(recipient_id)
            except Exception:
                pass

        if doc:
            doc_info = get_doc_info(doc.to_json(), self.domain)
        else:
            doc_info = None

        contact_cache[recipient_id] = doc_info

        return doc_info