def get_or_create_contact(self, urn): if ':' in urn: parsed = ContactURN.parse_urn(urn) urn = (parsed.scheme, parsed.path) else: urn = (TEL_SCHEME, urn) # assume phone number return Contact.get_or_create(self.org, self.user, name=None, urns=[urn])
def validate_urns(self, attrs, source): # if we have tel URNs, we may need a country to normalize by tel_sender = self.org.get_send_channel(TEL_SCHEME) country = tel_sender.country if tel_sender else None urns = [] for urn in attrs.get(source, []): try: parsed = ContactURN.parse_urn(urn) except ValueError, e: raise ValidationError(e.message) norm_scheme, norm_path = ContactURN.normalize_urn(parsed.scheme, parsed.path, country) if not ContactURN.validate_urn(norm_scheme, norm_path): raise ValidationError("Invalid URN: '%s'" % urn) urns.append((norm_scheme, norm_path))
def validate_urn(self, attrs, source): urns = [] if 'channel' in attrs and attrs['channel']: country = attrs['channel'].country for urn in attrs.get(source, []): parsed = ContactURN.parse_urn(urn) norm_scheme, norm_path = ContactURN.normalize_urn(parsed.scheme, parsed.path, country) if not ContactURN.validate_urn(norm_scheme, norm_path): raise ValidationError("Invalid URN: '%s'" % urn) urns.append((norm_scheme, norm_path)) else: raise ValidationError("You must specify a valid channel") attrs['urn'] = urns return attrs
def validate_urns(self, attrs, source): urns = [] # get a channel channel = self.org.get_send_channel(TEL_SCHEME) if channel: for urn in attrs.get(source, []): parsed = ContactURN.parse_urn(urn) norm_scheme, norm_path = ContactURN.normalize_urn(parsed.scheme, parsed.path, channel.country) if not ContactURN.validate_urn(norm_scheme, norm_path): raise ValidationError("Invalid URN: '%s'" % urn) urns.append((norm_scheme, norm_path)) else: raise ValidationError("You cannot start flows without at least one phone number configured") attrs['urns'] = urns return attrs
def validate_urns(self, attrs, source): urns = None request_urns = attrs.get(source, None) if request_urns is not None: urns = [] for urn in request_urns: try: parsed = ContactURN.parse_urn(urn) except ValueError: raise ValidationError("Unable to parse URN: '%s'" % urn) norm_scheme, norm_path = ContactURN.normalize_urn(parsed.scheme, parsed.path) if not ContactURN.validate_urn(norm_scheme, norm_path): raise ValidationError("Invalid URN: '%s'" % urn) urns.append((norm_scheme, norm_path)) attrs['urns'] = urns return attrs