File: //home/arjun/projects/buyercall/buyercall/blueprints/api/doc/endpoints/blockednumbers.py
import logging
from flask import jsonify, request
from flask_restx import Resource
from buyercall.lib.util_rest import (rest_partnership_account, rest_partnership,
requires_auth, rest_is_partnership_account, rest_is_partnership)
from buyercall.blueprints.api.doc import serializers
from buyercall.blueprints.api.restx import api
from buyercall.blueprints.block_numbers.models import Block
from buyercall.extensions import db
from sqlalchemy import and_
log = logging.getLogger(__name__)
ns = api.namespace('blockednumbers', description='Operations related to blocked numbers.')
@ns.route('/')
@api.doc(responses={200: 'OK',
400: 'Error performing operation.',
401: 'Unauthorized request.'})
class BlockedNumbersCollection(Resource):
@requires_auth
def get(self):
"""
Retrieves the blocked numbers of a partnership account.
Use this method to view the blocked number details of a partnership account.
"""
if rest_is_partnership_account and rest_partnership_account:
numbers = list()
blocked_number_query = (db.session.query(Block)
.filter(Block.partnership_accounts_id == rest_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
)
else:
return jsonify(
blocked_numbers=numbers
)
elif rest_is_partnership and rest_partnership:
partnership_accounts = rest_partnership.get_partnership_accounts()
pa_list = list()
for pa in partnership_accounts:
numbers = list()
blocked_numbers = (db.session.query(Block)
.filter(Block.partnership_accounts_id == pa.id)).all()
for bn in blocked_numbers:
add_pa_bn = {
'id': bn.id,
'phonenumber': bn.phonenumber,
'status': bn.status,
'is_global': bn.global_exclusion
}
numbers.append(add_pa_bn)
add_pa = {
'name': pa.name,
'blocked_numbers': numbers
}
pa_list.append(add_pa)
return jsonify(
accounts=pa_list
)
else:
return api.abort(401)
@api.response(204, 'Blocked phone number successfully added.')
@api.expect(serializers.blocked_number_row, validate=True)
@requires_auth
def post(self):
"""
Adds a partnership account blocked number.
Use this method to add a blocked number to a partnership account.
"""
if rest_is_partnership_account and rest_partnership_account:
result = request.json
if result:
blocked_number = db.session.query(Block)\
.filter(and_(Block.phonenumber == result['phonenumber'],
Block.partnership_accounts_id == rest_partnership_account.id))\
.first()
if blocked_number:
api.abort(code=400, message="Blocked number already exists.")
else:
is_global_exclusion = False
global_exclusion = result['global_exclusion']
if global_exclusion and (global_exclusion is 'True' or global_exclusion is 'true'):
is_global_exclusion = True
params = {
'phonenumber': result['phonenumber'],
'partnership_accounts_id': rest_partnership_account.id,
'global_exclusion': is_global_exclusion,
'status': result['status']
}
final_result = Block.create(params)
if final_result:
return final_result, 204
else:
api.abort(code=400, message="Error creating blocked number.")
else:
api.abort(400)
else:
api.abort(401)
@ns.route('/<int:bpnid>')
@api.doc(responses={200: 'OK',
400: 'Error performing operation.',
401: 'Unauthorized request.'}, params={'bpnid': 'The blocked phone number ID.'})
class BlockedNumber(Resource):
@api.response(204, 'Blocked number successfully updated.')
@api.expect(serializers.blocked_number_row, validate=True)
@requires_auth
def put(self, bpnid):
"""
Update a partnership account blocked number.
Use this method to update a partnership account's blocked numbers.
"""
if rest_is_partnership_account and rest_partnership_account:
result = request.json
if result:
is_global_exclusion = False
global_exclusion = result['global_exclusion']
if global_exclusion and (global_exclusion is 'True' or global_exclusion is 'true'):
is_global_exclusion = True
final_result = Block.update(bpnid,
rest_partnership_account.id,
result['status'],
result['phonenumber'],
is_global_exclusion)
if final_result:
return final_result, 204
else:
api.abort(code=400, message="Error updating blocked number.")
else:
api.abort(400)
else:
api.abort(401)
@api.response(204, 'Blocked number successfully deleted.')
@requires_auth
def delete(self, bpnid):
"""
Delete a partnership account blocked number.
Use this method to delete a partnership account blocked number.
"""
if rest_is_partnership_account and rest_partnership_account:
number = db.session.query(Block)\
.filter(and_(Block.id == bpnid, Block.partnership_accounts_id == rest_partnership_account.id))\
.first()
if number:
final_result = number.delete()
return final_result
else:
api.abort(code=400, message="Error deactivating blocked number.")
else:
return api.abort(401)