def normalize_urn(self, value): if self.request.user.get_org().is_anon: raise InvalidQueryError("URN lookups not allowed for anonymous organizations") try: return URN.identity(URN.normalize(value)) except ValueError: raise InvalidQueryError("Invalid URN: %s" % value)
def get_object(self, value): # try to normalize as URN but don't blow up if it's a UUID try: as_urn = URN.identity(URN.normalize(value)) except ValueError: as_urn = value return self.get_queryset().filter(Q(uuid=value) | Q(urns__identity=as_urn)).first()
def get_object(self, value): # try to normalize as URN but don't blow up if it's a UUID try: as_urn = URN.identity(URN.normalize(value)) except ValueError: as_urn = value contact_ids_with_urn = list(ContactURN.objects.filter(identity=as_urn).values_list("contact_id", flat=True)) return self.get_queryset().filter(Q(uuid=value) | Q(id__in=contact_ids_with_urn)).first()
def get_object(self, value): # try to normalize as URN but don't blow up if it's a UUID try: as_urn = URN.identity(URN.normalize(value)) except ValueError: as_urn = value contact_ids_with_urn = list(ContactURN.objects.filter(identity=as_urn).values_list('contact_id', flat=True)) return self.get_queryset().filter(Q(uuid=value) | Q(id__in=contact_ids_with_urn)).first()
def validate(self, data): if data.get('urns') is not None and data.get('phone') is not None: raise serializers.ValidationError( "Cannot provide both urns and phone parameters together") if data.get('group_uuids') is not None and data.get( 'groups') is not None: raise serializers.ValidationError( "Parameter groups is deprecated and can't be used together with group_uuids" ) if self.org.is_anon and self.instance and self.parsed_urns is not None: raise serializers.ValidationError( "Cannot update contact URNs on anonymous organizations") if self.parsed_urns is not None: # look up these URNs, keeping track of the contacts that are connected to them urn_contacts = set() country = self.org.get_country_code() for parsed_urn in self.parsed_urns: normalized_urn = URN.identity( URN.normalize(parsed_urn, country)) urn = ContactURN.objects.filter( org=self.org, identity__exact=normalized_urn).first() if urn and urn.contact: urn_contacts.add(urn.contact) if len(urn_contacts) > 1: raise serializers.ValidationError( _("URNs are used by multiple contacts")) contact_by_urns = urn_contacts.pop( ) if len(urn_contacts) > 0 else None if self.instance and contact_by_urns and contact_by_urns != self.instance: # pragma: no cover raise serializers.ValidationError( _("URNs are used by other contacts")) else: contact_by_urns = None contact = self.instance or contact_by_urns # if contact is blocked, they can't be added to groups if contact and contact.is_blocked and self.group_objs: raise serializers.ValidationError( "Cannot add blocked contact to groups") return data
def validate(self, data): if data.get("urns") is not None and data.get("phone") is not None: raise serializers.ValidationError("Cannot provide both urns and phone parameters together") if data.get("group_uuids") is not None and data.get("groups") is not None: raise serializers.ValidationError( "Parameter groups is deprecated and can't be used together with group_uuids" ) if self.org.is_anon and self.instance and self.parsed_urns is not None: raise serializers.ValidationError("Cannot update contact URNs on anonymous organizations") if self.parsed_urns is not None: # look up these URNs, keeping track of the contacts that are connected to them urn_contacts = set() country = self.org.get_country_code() for parsed_urn in self.parsed_urns: normalized_urn = URN.identity(URN.normalize(parsed_urn, country)) urn = ContactURN.objects.filter(org=self.org, identity__exact=normalized_urn).first() if urn and urn.contact: urn_contacts.add(urn.contact) if len(urn_contacts) > 1: raise serializers.ValidationError(_("URNs are used by multiple contacts")) contact_by_urns = urn_contacts.pop() if len(urn_contacts) > 0 else None if self.instance and contact_by_urns and contact_by_urns != self.instance: # pragma: no cover raise serializers.ValidationError(_("URNs are used by other contacts")) else: contact_by_urns = None contact = self.instance or contact_by_urns # if contact is blocked, they can't be added to groups if contact and contact.is_blocked and self.group_objs: raise serializers.ValidationError("Cannot add blocked contact to groups") return data