Beispiel #1
0
def get_phone_numbers_old(instance, tenant):
    # Function to retrieve formatted phone numbers from an instance.
    # Requires the Addresses and Phone_numbers to be prefetched.
    country = tenant.country or None
    if instance.prefetched_addresses:
        country = instance.prefetched_addresses[0].country

    phone_numbers = {pn.type: pn for pn in instance.prefetched_phone_numbers}

    phone = phone_numbers.get('work', PhoneNumber()).number
    mobile = phone_numbers.get('mobile', PhoneNumber()).number

    extra_numbers = list(instance.prefetched_phone_numbers)

    if phone:
        # Remove all occurrences of phone from list.
        extra_numbers = filter(lambda a: a != phone_numbers.get('work'),
                               extra_numbers)

    if mobile:
        # Remove all occurrences of mobile from list.
        extra_numbers = filter(lambda a: a != phone_numbers.get('mobile'),
                               extra_numbers)

    return {
        'phone':
        _s(format_phone_number(phone, country, True) if phone else ''),
        'mobile':
        _s(format_phone_number(mobile, country, True) if mobile else ''),
        'extra_numbers': [en.number for en in extra_numbers],
    }
Beispiel #2
0
def get_phone_numbers(instance, tenant):
    # Function to retrieve formatted phone numbers from an instance.
    # Requires the Addresses and Phone_numbers to be prefetched.
    country = tenant.country or None
    if instance.prefetched_addresses:
        country = instance.prefetched_addresses[0].country

    phone_number_list = []
    for pn in instance.prefetched_phone_numbers:
        phone_number_list.append(
            _s(format_phone_number(pn.number, country, True)))

    return ' '.join(phone_number_list)
Beispiel #3
0
    def update(self, instance, validated_data):
        instance = super(PhoneNumberFormatMixin, self).update(instance, validated_data)

        phone_numbers = instance.phone_numbers.all()

        if phone_numbers:
            country = self.get_country(instance)

            if country:
                for phone in phone_numbers:
                    phone.number = format_phone_number(phone.number, country, True)
                    phone.save()

        return instance
Beispiel #4
0
    def create(self, validated_data):
        instance = super(PhoneNumberFormatMixin, self).create(validated_data)

        phone_numbers = instance.phone_numbers.all()

        if phone_numbers:
            country = self.get_country(instance)

            if country:
                for phone in phone_numbers:
                    phone.number = format_phone_number(phone.number, country,
                                                       True)
                    phone.save()

        return instance
Beispiel #5
0
    def extract(self, request, pk=None):
        """
        Attempt to extract phone numbers from the given email message.
        If an account is given it will attempt to use that account's country.
        Otherwise it will use the user's tenant's country.
        """
        email = self.get_object()
        account = request.data['account']
        country = None

        if account:
            account = Account.objects.get(pk=account)

            if account.addresses:
                address = account.addresses.first()

                if address:
                    country = address.country

        if not country:
            country = self.request.user.tenant.country or None

        phone_numbers = []

        # We can't extract phone numbers without a country.
        if country:
            for match in phonenumbers.PhoneNumberMatcher(
                    email.body_text, country):
                number_format = phonenumbers.PhoneNumberFormat.NATIONAL
                number = match.number

                number = phonenumbers.format_number(number,
                                                    number_format).replace(
                                                        ' ', '')
                number = format_phone_number(number, country, True)

                number_exists = PhoneNumber.objects.filter(
                    number=number, status=PhoneNumber.ACTIVE_STATUS).exists()

                if number not in phone_numbers and not number_exists:
                    phone_numbers.append(number)

        return Response({'phone_numbers': phone_numbers})
Beispiel #6
0
    def extract(self, request, pk=None):
        """
        Attempt to extract phone numbers from the given email message.
        If an account is given it will attempt to use that account's country.
        Otherwise it will use the user's tenant's country.
        """
        email = self.get_object()
        account = request.data['account']
        country = None

        if account:
            account = Account.objects.get(pk=account)

            if account.addresses:
                address = account.addresses.first()

                if address:
                    country = address.country

        if not country:
            country = self.request.user.tenant.country or None

        phone_numbers = []

        # We can't extract phone numbers without a country.
        if country:
            for match in phonenumbers.PhoneNumberMatcher(email.body_text, country):
                number_format = phonenumbers.PhoneNumberFormat.NATIONAL
                number = match.number

                number = phonenumbers.format_number(number, number_format).replace(' ', '')
                number = format_phone_number(number, country, True)

                number_exists = PhoneNumber.objects.filter(number=number, status=PhoneNumber.ACTIVE_STATUS).exists()

                if number not in phone_numbers and not number_exists:
                    phone_numbers.append(number)

        return Response({'phone_numbers': phone_numbers})
Beispiel #7
0
    def obj_to_doc(cls, obj):
        """
        Translate an object to an index document.
        """
        functions = obj.functions.filter(account__is_deleted=False)

        doc = {
            'addresses': [{
                'address': address.address,
                'postal_code': address.postal_code,
                'city': address.city,
                'state_province': address.state_province,
                'country': address.get_country_display() if address.country else None,
                'type': address.get_type_display(),
            } for address in obj.addresses.all()],
            'content_type': obj.content_type.id,
            'created': obj.created,
            'description': obj.description,
            'email_addresses': [{
                'id': email.id,
                'email_address': email.email_address,
                'status': email.status,
                'is_active': email.is_active,
            } for email in obj.email_addresses.all()],
            'first_name': obj.first_name,
            'full_name': obj.full_name,
            'gender': obj.get_gender_display(),
            'last_name': obj.last_name,
            'modified': obj.modified,
            'phone_numbers': [{
                'id': phone_number.id,
                'number': phone_number.number,
                'formatted_number': format_phone_number(phone_number.number),
                'type': phone_number.type,
                'status': phone_number.status,
                'status_name': phone_number.get_status_display(),
            } for phone_number in obj.phone_numbers.all()],
            'salutation': obj.get_salutation_display(),
            'social_media': [{
                'id': soc.id,
                'name': soc.get_name_display(),
                'username': soc.username,
                'profile_url': soc.profile_url
            } for soc in obj.social_media.all()],
            'tags': [{
                'id': tag.id,
                'name': tag.name,
                'object_id': tag.object_id,
            } for tag in obj.tags.all()],
            'title': obj.title,
            'active_at': [f.account_id for f in functions if f.is_active]
        }

        for function in functions:
            account = {
                'id': function.account_id,
                'name': function.account.name if function.account.name else '',
                'customer_id': function.account.customer_id,
                'function': function.title,
                'is_active': function.is_active,
                'phone_numbers': [{
                    'number': phone_number.number,
                    'formatted_number': format_phone_number(phone_number.number),
                } for phone_number in function.account.phone_numbers.all()],
                'domains': [website.full_domain for website in function.account.websites.all()],
            }

            doc.setdefault('accounts', []).append(account)

        return doc
Beispiel #8
0
    def obj_to_doc(cls, obj):
        """
        Translate an object to an index document.
        """
        doc = {
            'address_full':
            [address.full() for address in obj.addresses.all()],
            'addresses': [{
                'address':
                address.address,
                'postal_code':
                address.postal_code,
                'city':
                address.city,
                'country':
                address.get_country_display() if address.country else None,
            } for address in obj.addresses.all()],
            'assigned_to':
            obj.assigned_to.full_name if obj.assigned_to else None,
            'content_type':
            obj.content_type.id,
            'created':
            obj.created,
            'customer_id':
            obj.customer_id,
            'description':
            obj.description,
            'email_addresses': [{
                'id': email.id,
                'email_address': email.email_address,
                'status': email.status,
                'is_active': email.is_active,
            } for email in obj.email_addresses.all()],
            'modified':
            obj.modified,
            'name':
            obj.name,
            'name_words':
            obj.name,
            'phone_numbers': [{
                'id':
                phone_number.id,
                'number':
                phone_number.number,
                'formatted_number':
                format_phone_number(phone_number.number),
                'type':
                phone_number.type,
                'status':
                phone_number.status,
                'status_name':
                phone_number.get_status_display(),
            } for phone_number in obj.phone_numbers.all()],
            'status': {
                'id': obj.status.id,
                'name': obj.status.name,
            } if obj.status else None,
            'social_media': [{
                'id': soc.id,
                'name': soc.get_name_display(),
                'username': soc.username,
                'profile_url': soc.profile_url
            } for soc in obj.social_media.all()],
            'tags': [{
                'id': tag.id,
                'name': tag.name,
                'object_id': tag.object_id,
            } for tag in obj.tags.all()],
            'websites': [{
                'id': website.id,
                'website': website.website,
                'is_primary': website.is_primary,
            } for website in obj.websites.all()],

            # Fields not returned by the serializer.
            'domain': [website.full_domain for website in obj.websites.all()],
            'second_level_domain':
            [website.second_level for website in obj.websites.all()],
        }

        return doc
Beispiel #9
0
    def obj_to_doc(cls, obj):
        """
        Translate an object to an index document.
        """
        doc = {
            'address_full': [address.full() for address in obj.addresses.all()],
            'addresses': [{
                'address': address.address,
                'postal_code': address.postal_code,
                'city': address.city,
                'country': address.get_country_display() if address.country else None,
            } for address in obj.addresses.all()],
            'assigned_to': obj.assigned_to.full_name if obj.assigned_to else None,
            'content_type': obj.content_type.id,
            'created': obj.created,
            'customer_id': obj.customer_id,
            'description': obj.description,
            'email_addresses': [{
                'id': email.id,
                'email_address': email.email_address,
                'status': email.status,
                'is_active': email.is_active,
            } for email in obj.email_addresses.all()],
            'modified': obj.modified,
            'name': obj.name,
            'name_words': obj.name,
            'phone_numbers': [{
                'id': phone_number.id,
                'number': phone_number.number,
                'formatted_number': format_phone_number(phone_number.number),
                'type': phone_number.type,
                'status': phone_number.status,
                'status_name': phone_number.get_status_display(),
            } for phone_number in obj.phone_numbers.all()],
            'status': {
                'id': obj.status.id,
                'name': obj.status.name,
            } if obj.status else None,
            'social_media': [{
                'id': soc.id,
                'name': soc.get_name_display(),
                'username': soc.username,
                'profile_url': soc.profile_url
            } for soc in obj.social_media.all()],
            'tags': [{
                'id': tag.id,
                'name': tag.name,
                'object_id': tag.object_id,
            } for tag in obj.tags.all()],
            'websites': [{
                'id': website.id,
                'website': website.website,
                'is_primary': website.is_primary,
            } for website in obj.websites.all()],

            # Fields not returned by the serializer.
            'domain': [website.full_domain for website in obj.websites.all()],
            'second_level_domain': [website.second_level for website in obj.websites.all()],
        }

        return doc
Beispiel #10
0
    def obj_to_doc(cls, obj):
        """
        Translate an object to an index document.
        """
        functions = obj.functions.filter(account__is_deleted=False)

        doc = {
            'addresses': [{
                'address': address.address,
                'postal_code': address.postal_code,
                'city': address.city,
                'state_province': address.state_province,
                'country': address.get_country_display() if address.country else None,
                'type': address.get_type_display(),
            } for address in obj.addresses.all()],
            'content_type': obj.content_type.id,
            'created': obj.created,
            'description': obj.description,
            'email_addresses': [{
                'id': email.id,
                'email_address': email.email_address,
                'status': email.status,
            } for email in obj.email_addresses.all()],
            'first_name': obj.first_name,
            'full_name': obj.full_name,
            'gender': obj.get_gender_display(),
            'last_name': obj.last_name,
            'modified': obj.modified,
            'phone_numbers': [{
                'id': phone_number.id,
                'number': phone_number.number,
                'formatted_number': format_phone_number(phone_number.number),
                'type': phone_number.type,
                'status': phone_number.status,
                'status_name': phone_number.get_status_display(),
            } for phone_number in obj.phone_numbers.all()],
            'salutation': obj.get_salutation_display(),
            'social_media': [{
                'id': soc.id,
                'name': soc.get_name_display(),
                'username': soc.username,
                'profile_url': soc.profile_url
            } for soc in obj.social_media.all()],
            'tags': [{
                'id': tag.id,
                'name': tag.name,
                'object_id': tag.object_id,
            } for tag in obj.tags.all()],
            'title': obj.title,
            'active_at': [f.account_id for f in functions if f.is_active]
        }

        for function in functions:
            account = {
                'id': function.account_id,
                'name': function.account.name if function.account.name else '',
                'customer_id': function.account.customer_id,
                'function': function.title,
                'is_active': function.is_active,
                'phone_numbers': [{
                    'number': phone_number.number,
                    'formatted_number': format_phone_number(phone_number.number),
                } for phone_number in function.account.phone_numbers.all()],
            }

            doc.setdefault('accounts', []).append(account)

        return doc