HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: //home/arjun/projects/buyercall_forms/buyercall/buyercall/blueprints/mobile/utils.py
import logging as log
from sqlalchemy import and_
# from werkzeug.useragents import UserAgent

from buyercall.blueprints.contacts.models import Contact
from buyercall.blueprints.mobile.models import Endpoint, Domain


def get_sip_endpoint(username="", password=""):
    """Check for sip_domain and sip_endpoint."""
    sip_endpoint = Endpoint.query.filter(
        and_(Endpoint.sip_username == username,
             Endpoint.sip_password == password)
    ).first()
    if sip_endpoint:
        sip_domain = Domain.query.filter(Domain.id == sip_endpoint.domain_id).first()
    else:
        sip_domain = None

    return sip_endpoint, sip_domain


def is_iphone(request):
    """Check the mobile platform."""
    # user_agent = UserAgent(request.headers.get('User-Agent'))
    user_agent = request.headers.get('User-Agent')
    if user_agent.platform in ['ipad', 'iphone']:
        return True
    return False


def validate_phonenumber(phonenumber):
    """Validate phonenumber , check it the value is 10 digits.
    :return Phonenumber or False"""

    phonenumber = phonenumber.replace("(", "").replace(")", "").replace("-", "").replace(" ", "")
    if len(phonenumber) == 10:
        return f"+1{phonenumber}"
    return False


def send_agent_push_notification(contact):
    pass

    """
    Send Push Notifications to all the device registered for an agent.
    :param contact: Contact Object
    :return: None

    try:
        if contact and not isinstance(contact, Contact):
            contact = Contact.query.filter(id=contact.id).first()
        from buyercall.blueprints.partnership.models import PartnershipAccount, Partnership
        partnership_account = PartnershipAccount.query.filter(
            PartnershipAccount.id == contact.partnership_account_id
        ).first()
        partnership = Partnership.query.filter(
            Partnership.id == partnership_account.partnership_id
        ).first()
        if partnership and partnership.partner_type == 'general':
            log.info(f"sending agent push notification")
            push_message = f'A new contact was assigned: {contact.message_name}'
            from buyercall.blueprints.mobile.tasks import push_notification
            if contact:
                end_points = Endpoint.query.filter(
                    Endpoint.agent_id == contact.agent_id
                )
                for sip_endpoint in end_points:
                    push_notification(
                        sip_username=sip_endpoint.sip_username,
                        push_type='NotifyGenericTextMessage',
                        message=push_message
                    )
    except Exception as e:
        log.error(f"Exception : {e}")
    """