def validate_urn(value, strict=True): try: normalized = URN.normalize(value) if strict and not URN.validate(normalized): raise ValueError() except ValueError: raise serializers.ValidationError("Invalid URN: %s. Ensure phone numbers contain country codes." % value) return normalized
def clean_number(self): # check that our phone number looks sane country = self.data["country"] number = URN.normalize_number(self.data["number"], country) if not URN.validate(URN.from_parts(URN.TEL_SCHEME, number), country): raise forms.ValidationError( _("Please enter a valid phone number")) return number
def clean(self): # first check that our phone number looks sane country = self.cleaned_data["country"] normalized = URN.normalize_number(self.cleaned_data["number"], country) if not URN.validate(URN.from_parts(URN.TEL_SCHEME, normalized), country): raise forms.ValidationError(_("Please enter a valid phone number")) self.cleaned_data["number"] = normalized return self.cleaned_data
def to_internal_value(self, data): try: normalized = URN.normalize(data) if not URN.validate(normalized): raise ValueError() except ValueError: raise serializers.ValidationError("Invalid URN: %s. Ensure phone numbers contain country codes." % data) return normalized
def to_internal_value(self, data): try: country_code = self.context['org'].get_country_code() normalized = URN.normalize(data, country_code=country_code) if not URN.validate(normalized): raise ValueError() except ValueError: raise serializers.ValidationError("Invalid URN: %s" % data) return normalized
def validate(self, value): from temba.contacts.models import URN assert isinstance(value, list) for item in value: assert isinstance(item, dict) and "id" in item and "type" in item if item["type"] == "urn": if not URN.validate(item["id"], self.default_country): raise ValidationError(_("'%s' is not a valid URN.") % item["id"])
def to_internal_value(self, data): try: normalized = URN.normalize(data) if not URN.validate(normalized): raise ValueError() except ValueError: raise serializers.ValidationError( "Invalid URN: %s. Ensure phone numbers contain country codes." % data) return normalized
def clean(self): # first check that our phone number looks sane country = self.cleaned_data["country"] normalized = URN.normalize_number(self.cleaned_data["number"], country) if not URN.validate(URN.from_parts(URN.TEL_SCHEME, normalized), country): raise forms.ValidationError( _("Please enter a valid phone number")) self.cleaned_data["number"] = normalized try: resp = requests.post( self.cleaned_data["base_url"] + "/v1/users/login", auth=(self.cleaned_data["username"], self.cleaned_data["password"]), ) if resp.status_code != 200: raise Exception("Received non-200 response: %d", resp.status_code) self.cleaned_data["auth_token"] = resp.json( )["users"][0]["token"] except Exception: raise forms.ValidationError( _("Unable to check WhatsApp enterprise account, please check username and password" )) # check we can access their facebook templates from .type import TEMPLATE_LIST_URL if self.cleaned_data[ "facebook_template_list_domain"] != "graph.facebook.com": response = requests.get( TEMPLATE_LIST_URL % (self.cleaned_data["facebook_template_list_domain"], self.cleaned_data["facebook_business_id"]), params=dict(access_token=self. cleaned_data["facebook_access_token"]), ) if response.status_code != 200: raise forms.ValidationError( _("Unable to access Facebook templates, please check user id and access token and make sure " + "the whatsapp_business_management permission is enabled" )) return self.cleaned_data
def validate_urns(self, value): if value is not None: self.parsed_urns = [] for urn in value: try: normalized = URN.normalize(urn) scheme, path, display = URN.to_parts(normalized) # for backwards compatibility we don't validate phone numbers here if scheme != TEL_SCHEME and not URN.validate(normalized): # pragma: needs cover raise ValueError() except ValueError: raise serializers.ValidationError("Invalid URN: '%s'" % urn) self.parsed_urns.append(normalized) return value
def validate_urns(self, value): if value is not None: self.parsed_urns = [] for urn in value: try: normalized = URN.normalize(urn) scheme, path, query, display = URN.to_parts(normalized) # for backwards compatibility we don't validate phone numbers here if scheme != TEL_SCHEME and not URN.validate(normalized): # pragma: needs cover raise ValueError() except ValueError: raise serializers.ValidationError("Invalid URN: '%s'" % urn) self.parsed_urns.append(normalized) return value
def validate_urn(self, value): urns = [] if value: # if we have tel URNs, we may need a country to normalize by country = self.org.get_country_code() for urn in value: try: normalized = URN.normalize(urn, country) except ValueError as e: # pragma: needs cover raise serializers.ValidationError(six.text_type(e)) if not URN.validate(normalized, country): # pragma: needs cover raise serializers.ValidationError("Invalid URN: '%s'" % urn) urns.append(normalized) return urns
def validate_urn(self, value): urns = [] if value: # if we have tel URNs, we may need a country to normalize by country = self.org.get_country_code() for urn in value: try: normalized = URN.normalize(urn, country) except ValueError as e: # pragma: needs cover raise serializers.ValidationError(str(e)) if not URN.validate(normalized, country): # pragma: needs cover raise serializers.ValidationError("Invalid URN: '%s'" % urn) urns.append(normalized) return urns
def contact_resolve(self, org_id: int, channel_id: int, urn: str): org = Org.objects.get(id=org_id) user = get_anonymous_user() try: urn = URN.normalize(urn, org.default_country_code) if not URN.validate(urn, org.default_country_code): raise ValueError() except ValueError: raise MailroomException("contact/resolve", None, {"error": "invalid URN"}) contact_urn = ContactURN.lookup(org, urn) if contact_urn: contact = contact_urn.contact else: contact = create_contact_locally(org, user, name="", language="", urns=[urn], fields={}, group_uuids=[]) contact_urn = ContactURN.lookup(org, urn) return { "contact": { "id": contact.id, "uuid": str(contact.uuid), "name": contact.name }, "urn": { "id": contact_urn.id, "identity": contact_urn.identity }, }
def to_internal_value(self, data): if not URN.validate(data): raise ValidationError("Invalid URN: %s" % data) return URN.normalize(data)