# Path: app/callwidget/calls.py
# def make_outbound_call(record_id):
#     """
#     Twilio callback for a call to get connected to the respective respondent.
#     :param record_id: id of the respondent
#     :return: flask response (XML).
#     """
# 
#     response = twiml.Response()
# 
#     record = Respondent().query.filter_by(id=record_id).first()
#     if record is None:
#         return abort(404)
# 
#     response.say("Hello, thanks for your Participation. We will now connect you to your Member of Parliament.",
#                  voice='alice')
# 
#     if current_app.config['demo_mode']:
#         response.hangup()
#     with response.dial() as dial:
#         if current_app.config['TWILIO_TEST_NUMBER']:
#             dial.number(current_app.config['TWILIO_TEST_NUMBER'])
#         else:
#             dial.number(record.phone)
# 
#     return str(response)
# 
# def get_call_widget(request, id_campaign=1, embedded=False):
#     """
#     Render a call page due to a get request.
#     :param request: a flask request object.
#     :param id_campaign: an campaign id.
#     :param embedded: True if embed template is supposed to be rendered.
#     :return: flask response.
#     """
#     form = CallForm(request.form)
#     campaign = Campaign().query.filter_by(id=id_campaign).first()
#     if campaign is None:
#         return abort(404)
#     if embedded:
#         template = 'callform/callform_embedded.html'
#     else:
#         template = 'callform/callform.html'
#     audience = Audience().query.filter_by(id=campaign.id_audience).first()
#     random_id = current_app.random.get_random_value(campaign.id_audience)
#     record = sorted(audience.respondents, key=attrgetter('id'))[random_id]
#     return render_template(template, record=record, form=form, campaign=campaign)
# 
# def post_call_widget(request, id_campaign=1, embedded=False):
#     """
#     Render a call page due to a post request.
#     :param request: a flask request object.
#     :param id_campaign: an campaign id.
#     :param embedded: True if embed template is supposed to be rendered.
#     :return: flask response.
#     """
#     form = CallForm(request.form)
#     campaign = Campaign().query.filter_by(id=id_campaign).first()
#     if campaign is None:
#         return abort(404)
#     if embedded:
#         template = 'callform/callform_embedded.html'
#     else:
#         template = 'callform/callform.html'
#     if form.validate_on_submit():
#         record = Respondent().query.filter_by(id=form.id_mdb.data).first()
#         tel_mdb = record.phone
#         tel_caller = form.phone_number.data
#         if abuse_detected(phone_number=tel_caller):
#             flash('I’m sorry Dave, I’m afraid I can’t do that.', category='warning')
#             return render_template(template, record=record, form=form, campaign=campaign)
#         if not initiate_call(record_id=form.id_mdb.data,
#                              tel_caller=tel_caller,
#                              audience_id=campaign.id_audience,
#                              campaign_id=id_campaign):
#             flash('something went wrong', category='warning')
#             return render_template(template, record=record, form=form, campaign=campaign)
#         else:
#             flash('dispatching call from ' + tel_caller + ' to ' + tel_mdb, category='info')
#             return render_template(template, record=record, form=form, campaign=campaign)
#     else:
#         record = Respondent().query.filter_by(id=form.id_mdb.data).first()
#         return render_template(template, record=record, form=form, campaign=campaign)
# 
# Path: app/abuse/client.py
# def is_blocked(request):
#     register_ip_address(request)
#     hashed_ip = get_hashed_ip_addr(get_ip_addr(request))
#     client = BlockedClients().query.filter_by(id=hashed_ip).first()
#     if client.persistent:
#         return True
#     elif client.date == date.today() and client.calls > current_app.config['MAX_CALLS_PER_IP_PER_DAY']:
#         return True
#     else:
#         return False


from app.callwidget.calls import make_outbound_call, get_call_widget, post_call_widget
from flask import Blueprint, request, Response, redirect, abort
from flask_login import login_required
from .abuse.client import is_blocked
    from .abuse.calls import abuse_detected
    from .statistics.calls import log_call
    from .frontend.campaign import overview
    from .frontend.campaign import edit
    from .frontend.campaign import add
@main.route('/<template>/<id_campaign>', methods=['GET', 'POST'])
def campaign(id_campaign, template):
    embedded_template = False
    if template == 'embedded':
        embedded_template = True
    if request.method == 'GET':
        return get_call_widget(request, id_campaign, embedded=embedded_template)
    elif request.method == 'POST':
        if is_blocked(request):
            return abort(429)
        return post_call_widget(request, id_campaign, embedded=embedded_template)


@main.route('/outbound/<record_id>', methods=['POST'])
def outbound(record_id):
    return make_outbound_call(record_id)


@main.route('/outbound/status/<id_campaign>', methods=['POST'])
def status(id_campaign):
    abuse_detected(phone_number=request.values['To'],
                   duration=request.values['CallDuration'])
    log_call(call_duration=request.values['CallDuration'],
             id_campaign=id_campaign)
    return Response(status=200)


@login_required
@main.route('/campaigns', methods=['GET'])
def campaign_overview():

# Path: app/callwidget/calls.py
# def make_outbound_call(record_id):
#     """
#     Twilio callback for a call to get connected to the respective respondent.
#     :param record_id: id of the respondent
#     :return: flask response (XML).
#     """
# 
#     response = twiml.Response()
# 
#     record = Respondent().query.filter_by(id=record_id).first()
#     if record is None:
#         return abort(404)
# 
#     response.say("Hello, thanks for your Participation. We will now connect you to your Member of Parliament.",
#                  voice='alice')
# 
#     if current_app.config['demo_mode']:
#         response.hangup()
#     with response.dial() as dial:
#         if current_app.config['TWILIO_TEST_NUMBER']:
#             dial.number(current_app.config['TWILIO_TEST_NUMBER'])
#         else:
#             dial.number(record.phone)
# 
#     return str(response)
# 
# def get_call_widget(request, id_campaign=1, embedded=False):
#     """
#     Render a call page due to a get request.
#     :param request: a flask request object.
#     :param id_campaign: an campaign id.
#     :param embedded: True if embed template is supposed to be rendered.
#     :return: flask response.
#     """
#     form = CallForm(request.form)
#     campaign = Campaign().query.filter_by(id=id_campaign).first()
#     if campaign is None:
#         return abort(404)
#     if embedded:
#         template = 'callform/callform_embedded.html'
#     else:
#         template = 'callform/callform.html'
#     audience = Audience().query.filter_by(id=campaign.id_audience).first()
#     random_id = current_app.random.get_random_value(campaign.id_audience)
#     record = sorted(audience.respondents, key=attrgetter('id'))[random_id]
#     return render_template(template, record=record, form=form, campaign=campaign)
# 
# def post_call_widget(request, id_campaign=1, embedded=False):
#     """
#     Render a call page due to a post request.
#     :param request: a flask request object.
#     :param id_campaign: an campaign id.
#     :param embedded: True if embed template is supposed to be rendered.
#     :return: flask response.
#     """
#     form = CallForm(request.form)
#     campaign = Campaign().query.filter_by(id=id_campaign).first()
#     if campaign is None:
#         return abort(404)
#     if embedded:
#         template = 'callform/callform_embedded.html'
#     else:
#         template = 'callform/callform.html'
#     if form.validate_on_submit():
#         record = Respondent().query.filter_by(id=form.id_mdb.data).first()
#         tel_mdb = record.phone
#         tel_caller = form.phone_number.data
#         if abuse_detected(phone_number=tel_caller):
#             flash('I’m sorry Dave, I’m afraid I can’t do that.', category='warning')
#             return render_template(template, record=record, form=form, campaign=campaign)
#         if not initiate_call(record_id=form.id_mdb.data,
#                              tel_caller=tel_caller,
#                              audience_id=campaign.id_audience,
#                              campaign_id=id_campaign):
#             flash('something went wrong', category='warning')
#             return render_template(template, record=record, form=form, campaign=campaign)
#         else:
#             flash('dispatching call from ' + tel_caller + ' to ' + tel_mdb, category='info')
#             return render_template(template, record=record, form=form, campaign=campaign)
#     else:
#         record = Respondent().query.filter_by(id=form.id_mdb.data).first()
#         return render_template(template, record=record, form=form, campaign=campaign)
# 
# Path: app/abuse/client.py
# def is_blocked(request):
#     register_ip_address(request)
#     hashed_ip = get_hashed_ip_addr(get_ip_addr(request))
#     client = BlockedClients().query.filter_by(id=hashed_ip).first()
#     if client.persistent:
#         return True
#     elif client.date == date.today() and client.calls > current_app.config['MAX_CALLS_PER_IP_PER_DAY']:
#         return True
#     else:
#         return False


# Path: app/views.py
from app.callwidget.calls import make_outbound_call, get_call_widget, post_call_widget
from flask import Blueprint, request, Response, redirect, abort
from flask_login import login_required
from .abuse.client import is_blocked
    from .abuse.calls import abuse_detected
    from .statistics.calls import log_call
    from .frontend.campaign import overview
    from .frontend.campaign import edit
    from .frontend.campaign import add

@main.route('/<template>/<id_campaign>', methods=['GET', 'POST'])
def campaign(id_campaign, template):
    embedded_template = False
    if template == 'embedded':
        embedded_template = True
    if request.method == 'GET':
        return get_call_widget(request, id_campaign, embedded=embedded_template)
    elif request.method == 'POST':
        if is_blocked(request):
            return abort(429)
        return post_call_widget(request, id_campaign, embedded=embedded_template)


@main.route('/outbound/<record_id>', methods=['POST'])
def outbound(record_id):
    return make_outbound_call(record_id)


@main.route('/outbound/status/<id_campaign>', methods=['POST'])
def status(id_campaign):
    abuse_detected(phone_number=request.values['To'],
                   duration=request.values['CallDuration'])
    log_call(call_duration=request.values['CallDuration'],
             id_campaign=id_campaign)
    return Response(status=200)


@login_required
@main.route('/campaigns', methods=['GET'])
def campaign_overview():

    return overview()