File: //home/arjun/projects/buyercall_forms/buyercall/buyercall/blueprints/api/doc/endpoints/leads.py
from datetime import datetime
import logging
from flask import jsonify, request
from flask_restx import Resource
from sqlalchemy import and_
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.restplus import api
from buyercall.blueprints.leads.models import Lead, LeadNotes
from buyercall.extensions import db
log = logging.getLogger(__name__)
ns = api.namespace('calls', description='Operations related to calls.')
@ns.route('/')
@api.doc(responses={200: 'OK',
400: 'Error performing operation.',
401: 'Unauthorized request.'})
class CallsCollection(Resource):
@requires_auth
def get(self):
"""
Retrieves the call details of a partnership account.
Use this method to view the call details of a partnership account.
"""
if rest_is_partnership_account and rest_partnership_account:
call_query = (db.session.query(Lead)
.filter(Lead.partnership_account_id == rest_partnership_account.id)
).all()
calls = list()
if call_query:
for lead in call_query:
call = {
'id': lead.id,
'phonenumber': lead.phonenumber,
'status': lead.status,
'firstname': lead.firstname,
'lastname': lead.lastname,
'email': lead.email,
'duration': lead.duration,
'starttime': lead.starttime,
'endtime': lead.endtime,
'call_type': lead.call_type,
'response_time_seconds': lead.response_time_seconds
}
calls.append(call)
return jsonify(
calls=calls
)
else:
return jsonify(
calls=calls
)
elif rest_is_partnership and rest_partnership:
partnership_accounts = rest_partnership.get_partnership_accounts()
pa_list = list()
if partnership_accounts:
for pa in partnership_accounts:
call_query = (db.session.query(Lead)
.filter(Lead.partnership_account_id == pa.id)).all()
calls = list()
if call_query:
for lead in call_query:
add_call = {
'id': lead.id,
'phonenumber': lead.phonenumber,
'status': lead.status,
'firstname': lead.firstname,
'lastname': lead.lastname,
'email': lead.email,
'duration': lead.duration,
'starttime': lead.starttime,
'endtime': lead.endtime,
'call_type': lead.call_type,
'response_time_seconds': lead.response_time_seconds
}
calls.append(add_call)
add_pa = {
'name': pa.name,
'calls': calls
}
pa_list.append(add_pa)
return jsonify(
accounts=pa_list
)
else:
return pa_list
else:
return api.abort(401)
@ns.route('/notes/<int:cid>')
@api.doc(responses={200: 'OK',
400: 'Error performing operation.',
401: 'Unauthorized request.',
404: 'Call not found.'},
params={'cid': 'The call ID.'})
class CallNotesCollection(Resource):
@requires_auth
def get(self, cid):
"""
Retrieves the call notes of a partnership account for a lead.
Use this method to view the call notes of a partnership account for a lead.
* Specify the lead ID in the request URL path.
"""
if rest_is_partnership_account and rest_partnership_account:
lead_query = db.session.query(Lead)\
.filter(and_(Lead.partnership_account_id == rest_partnership_account.id, Lead.id == cid))\
.first()
notes = list()
if lead_query:
lead_note_query = (db.session.query(LeadNotes)
.filter(LeadNotes.lead_id == lead_query.id))
for lead_note in lead_note_query:
user_firstname = ''
user_lastname = ''
if lead_note.user:
user_firstname = lead_note.user.firstname
user_lastname = lead_note.user.lastname
note = {
'lead_id': lead_note.lead_id,
'created_on': lead_note.created_on,
'updated_on': lead_note.updated_on,
'firstname': user_firstname,
'lastname': user_lastname,
'text': lead_note.text
}
notes.append(note)
return jsonify(
call_notes=notes
)
else:
api.abort(404)
else:
api.abort(401)
@api.response(204, 'Call note successfully added.')
@api.expect(serializers.call_note_row, validate=True)
@requires_auth
def post(self, cid):
"""
Adds a call note to a call for a partnership account.
Use this method to add a call note to a call for a partnership account.
"""
if rest_is_partnership_account and rest_partnership_account:
result = request.json
if result:
lead = db.session.query(Lead)\
.filter(and_(Lead.id == cid, Lead.partnership_account_id == rest_partnership_account.id))\
.first()
if not lead:
api.abort(code=404)
else:
is_active = True
params = {
'lead_id': cid,
'is_enabled': is_active,
'created_on': datetime.now(),
'updated_on': datetime.now(),
'text': result['text'],
'user_id': result['user_id']
}
final_result = LeadNotes.create(params)
if final_result:
return final_result, 204
else:
api.abort(code=400, message="Error creating call note.")
else:
api.abort(400)
else:
api.abort(401)