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/buyercall/blueprints/api2/doc/endpoints/blockednumbers.py
import logging

from flask import jsonify, request
from flask_restx import Resource
from sqlalchemy import and_

from buyercall.lib.util_rest import rest_partnership, requires_auth, rest_is_partnership

from buyercall.blueprints.api2.doc import serializers
from buyercall.blueprints.api2.restx import api
from buyercall.blueprints.block_numbers.models import Block
from buyercall.blueprints.partnership.models import PartnershipAccount
from buyercall.extensions import db


log = logging.getLogger(__name__)
ns = api.namespace('Blocked Numbers', description='Operations related to blocked numbers.', path='/accounts')


@ns.route('/<int:paid>/blockednumbers')
@api.doc(responses={200: 'OK',
                    400: 'Error performing operation.',
                    401: 'Unauthorized request.'},
         params={'paid': 'The partner account Id'})
class BlockedNumbersCollection(Resource):
    @requires_auth
    def get(self, paid):
        """
        Retrieves the blocked numbers of a partnership account.

        <p>
        The Blocked Phone Numbers GET endpoint should be used to return all blocked phone numbers for a partner account.
        </p>
        <br />
        <p>
        You require a partner authentication token and a partner account id to make a successful request.
        A response will be returned, similar to the example below, based
        on a successful request:
        <br />
        <br />
        </p>
         <pre class="code-background" style="color: white">
           {
              "blocked_numbers": [
                {
                  "id": 2,
                  "is_global": false,
                  "phonenumber": "8083423344",
                  "status": "blocked"
                }
              ]
            }
         </pre>


        """
        if rest_is_partnership and rest_partnership is not None:
            numbers = []
            partnership_account = PartnershipAccount \
                .query \
                .filter(and_(PartnershipAccount.id == paid, PartnershipAccount.partnership_id == rest_partnership.id)) \
                .first()

            if partnership_account:
                blocked_number_query = (db.session.query(Block).filter(
                    Block.partnership_accounts_id == partnership_account.id)
                ).all()

                if blocked_number_query:
                    for bn in blocked_number_query:
                        number = {
                            'id': bn.id,
                            'phonenumber': bn.phonenumber,
                            'status': bn.status,
                            'is_global': bn.global_exclusion
                        }
                        numbers.append(number)

                    return jsonify(
                        blocked_numbers=numbers
                    )
                return jsonify(
                    blocked_numbers=numbers
                )
            return api.abort(404, message='Partnership account not found.')
        return api.abort(401)

    """
    # DISABLE ENDPOINT
    @api.response(204, 'Blocked phone number successfully added.')
    @api.expect(serializers.blocked_number_add, validate=True)
    @requires_auth
    def post(self, paid):
    """
    """
        Adds a partnership account blocked number.

        <p>
        The Blocked Phone Numbers POST endpoint should be used to create a blocked phone number for a partner account.
        Once a phone number is listed with 'blocked' as type all calls from the specified phone number will be blocked.
        </p>
        <br />
        <p>
        Please note that if a phone number is listed for global exclusion all calls
        from the phone number will be blocked
        for all accounts in the BuyerCall application.
        </p>
        <br />
        <p>
        You will require a partner authentication token and a partner account id to
        make a successful request. A response will be returned, similar to the example below, based
        on a successful request:
        <br />
        <br />
        </p>
         <pre class="code-background" style="color: white">
            {
              "blocked_number_id": 7,
              "partnership_account_id": 1
            }
         </pre>
    """
    """
        if rest_is_partnership and rest_partnership is not None:
            partnership_account = PartnershipAccount \
                .query \
                .filter(and_(PartnershipAccount.id == paid, PartnershipAccount.partnership_id == rest_partnership.id)) \
                .first()

            if partnership_account is not None:
                received = request.json

                if received is not None:
                    blocked_number = db.session.query(Block)\
                        .filter(and_(Block.phonenumber == received['phonenumber'],
                                     Block.partnership_accounts_id == partnership_account.id))\
                        .first()

                    if blocked_number is not None:
                        api.abort(code=400, message="Blocked number already exists.")
                    else:
                        result_global_exclusion = False
                        result_status = 'blocked'

                        if 'global_exclusion' in received:
                            result_global_exclusion = received['global_exclusion']

                        result_phonenumber = received['phonenumber']

                        if received['status'].lower() == 'unblocked':
                            result_status = 'unblocked'

                        params = {
                            'phonenumber': result_phonenumber,
                            'partnership_accounts_id': partnership_account.id,
                            'global_exclusion': result_global_exclusion,
                            'status': result_status
                        }
                        block_id = Block.api_create(params)

                        if block_id > 0:
                            return jsonify(
                                partnership_account_id=paid,
                                blocked_number_id=block_id
                            )
                        else:
                            api.abort(code=400, message="Error creating blocked number.")
                else:
                    api.abort(code=400, message="Error creating blocked number.")
            else:
                return api.abort(404, message='Partnership account not found.')
        else:
            api.abort(401)
    """
"""
# DISABLE ENDPOINT
@ns.route('/<int:paid>/blockednumbers/<int:bpnid>')
@api.doc(responses={200: 'OK',
                    400: 'Error performing operation.',
                    401: 'Unauthorized request.'},
         params={'paid': 'The partner account Id',
                 'bpnid': 'The blocked phone number Id'})
"""
class BlockedNumber(Resource):
    @api.response(204, 'Blocked number successfully updated.')
    @api.expect(serializers.blocked_number_update, validate=True)
    @requires_auth
    def put(self, paid, bpnid):
        """
        Update a partnership account blocked number.

        <p>
        The Blocked Phone Numbers PUT endpoint should be used to update a blocked phone number for a partner account.
        </p>
        <br />
        <p>
        You will require a partner authentication token, a partner account id as well as a blocked phone number id to
        make a successful request.
        </p>

        """
        if rest_is_partnership and rest_partnership is not None:
            partnership_account = PartnershipAccount \
                .query \
                .filter(and_(PartnershipAccount.id == paid, PartnershipAccount.partnership_id == rest_partnership.id)) \
                .first()

            if partnership_account is not None:
                received = request.json

                if received is not None:
                    result_global_exclusion = None
                    result_status = None
                    result_phonenumber = None

                    if 'global_exclusion' in received:
                        result_global_exclusion = received['global_exclusion']

                    if 'phonenumber' in received:
                        result_phonenumber = received['phonenumber']

                    if 'status' in received:
                        if received['status'].lower() == 'unblocked':
                            result_status = 'unblocked'
                        else:
                            result_status = 'blocked'

                    final_result = Block.api_update(
                        bpnid,
                        partnership_account.id,
                        result_status,
                        result_phonenumber,
                        result_global_exclusion
                    )

                    if final_result:
                        return final_result, 204
                    else:
                        api.abort(code=400, message="Error updating blocked number.")
                else:
                    api.abort(400)
            else:
                return api.abort(404, message='Partnership account not found.')
        else:
            api.abort(401)

    @api.response(204, 'Blocked number successfully deleted.')
    @requires_auth
    def delete(self, paid, bpnid):
        """
        Delete a partnership account blocked number.

        <p>
        The Blocked Phone Number DELETE endpoint should be used to delete a blocked phone number for a partner account.
        </p>
        <br />
        <p>
        You will require a partner authentication token,
        a partner account id as well as a blocked phone number id to make a successful request.
        </p>

        """
        if rest_is_partnership and rest_partnership is not None:
            partnership_account = PartnershipAccount \
                .query \
                .filter(and_(PartnershipAccount.id == paid, PartnershipAccount.partnership_id == rest_partnership.id)) \
                .first()

            if partnership_account is not None:
                number = db.session.query(Block)\
                    .filter(and_(Block.id == bpnid, Block.partnership_accounts_id == partnership_account.id))\
                    .first()

                if number is not None:
                    _final_result = number.delete()
                    return True, 204
                else:
                    api.abort(code=400, message="Error deactivating blocked number.")
            else:
                return api.abort(404, message='Partnership account not found.')
        else:
            return api.abort(401)