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_new/buyercall/buyercall/blueprints/dashboard/views.py
from datetime import datetime
import logging
import math
from flask import (
    Blueprint,
    url_for,
    redirect,
    render_template)
from sqlalchemy import extract, or_
from sqlalchemy import and_
from flask_login import login_required, current_user

from buyercall.blueprints.leads.models import Lead
from buyercall.blueprints.phonenumbers.models import Phone
from buyercall.blueprints.sms.models import Message
from buyercall.blueprints.contacts.models import Contact
from buyercall.blueprints.form_leads.models import ExternalForm, FormLead
from buyercall.blueprints.agents.models import Agent
from buyercall.blueprints.user.decorators import role_required


dashboard = Blueprint('dashboard', __name__, template_folder='templates')

log = logging.getLogger(__name__)


# The user dashboard page
@dashboard.route('/dashboard')
@login_required
@role_required('admin', 'agent')
def user_dashboard():
    # TODO: no partner allowed as role
    partnership_account_id = current_user.partnership_account_id
    partnership_account_group_viewing = current_user.is_viewing_partnership
    subscription_plan = current_user.get_user_viewing_partnership_account_subscription_plan

    if current_user.is_admin_user_with_groups and partnership_account_group_viewing \
            and subscription_plan != 'partnershipsingle':
        partnership_account_id = current_user.get_user_viewing_partnership_account_id
    elif current_user.is_admin_user_with_groups and partnership_account_group_viewing \
            and subscription_plan == 'partnershipsingle':
        return redirect(url_for('contacts.contact_list'))
    elif current_user.role == 'admin' and current_user.is_admin_user_with_groups \
            and not current_user.is_viewing_partnership:
        return redirect(url_for('partnership.company_accounts'))
    elif current_user.role == 'partner':
        return redirect(url_for('partnership.accounts', page=1))

    filter_by_leads = and_(
        Lead.partnership_account_id == partnership_account_id, current_user.role == 'admin')

    filter_by_phone = and_(
        Phone.partnership_account_id == partnership_account_id, current_user.role == 'admin')

    filter_by_agent = and_(
        Agent.partnership_account_id == partnership_account_id, current_user.role == 'admin')

    filter_by_form = and_(
        ExternalForm.partnership_account_id == partnership_account_id, current_user.role == 'admin')

    filter_by_form_lead = and_(
        FormLead.partnership_account_id == partnership_account_id, current_user.role == 'admin')

    filter_by_messages = and_(
        Message.partnership_account_id == partnership_account_id, current_user.role == 'admin')

    filter_by_contact_leads = and_(
        Contact.partnership_account_id == partnership_account_id,  current_user.role == 'admin')

    # Get current year, month
    now = datetime.now()
    year = now.year
    month = now.month
    previous_month = month - 1

    call_duration = Lead.query.with_entities(
        Lead.duration
    ).filter(
        or_(filter_by_leads)
    ).filter(
        extract('year', Lead.created_on) == year
    ).filter(extract('month', Lead.created_on) == month).all()

    new_call_duration = [i.duration for i in call_duration]
    call_duration_list = list()
    for duration in new_call_duration:
        if not duration:
            duration_zero = float(0)
            call_duration_list.append(duration_zero)
        else:
            float_duration = math.ceil(float(duration) / 60.0)
            call_duration_list.append(float_duration)

    account_total_minutes = int(sum(call_duration_list))

    # Monthly unique call count
    monthly_unique_calls = Lead.query.filter(extract('month', Lead.created_on) == month) \
        .filter(extract('year', Lead.created_on) == year) \
        .filter(or_(filter_by_leads))\
        .distinct(Lead.phonenumber).count()

    # Total tracking phonenumbers per account
    account_total_numbers = Phone.query.\
        filter(or_(filter_by_phone))\
        .filter(and_(Phone.type == 'tracking', Phone.is_deactivated == '0')).count()

    # Total priority phonenumbers per account
    account_total_numbers_priority = Phone.query. \
        filter(or_(filter_by_phone))\
        .filter(and_(Phone.type == 'priority', Phone.is_deactivated == '0')).count()

    # Total agents per account
    account_total_agents = Agent.query\
        .filter(or_(filter_by_agent))\
        .filter(Agent.is_group == '0').count()

    # Total groups per account
    account_total_groups = Agent.query\
        .filter(or_(filter_by_agent))\
        .filter(Agent.is_group == '1').count()

    # Total Forms per account
    account_total_forms = ExternalForm.query\
        .filter(or_(filter_by_form)).count()

    # Total form leads per account
    account_total_form_leads = FormLead.query.\
        filter(extract('month', FormLead.created_on) == month).\
        filter(extract('year', FormLead.created_on) == year).\
        filter(or_(filter_by_form_lead)).count()

    # Monthly unique contact leads
    monthly_contact_leads = Contact.query.filter(extract('month', Contact.created_on) == month) \
        .filter(extract('year', Contact.created_on) == year) \
        .filter(or_(filter_by_contact_leads)).distinct(Contact.phonenumber_1).count()

    # Previous month unique contact leads
    previous_month_contact_leads = Contact.query.filter(extract('month', Contact.created_on) == previous_month) \
        .filter(extract('year', Contact.created_on) == year) \
        .filter(or_(filter_by_contact_leads)).distinct(Contact.phonenumber_1).count()

    # Monthly unique messages
    monthly_unique_messages = Message.query.filter(extract('month', Message.created_on) == month) \
        .filter(extract('year', Message.created_on) == year) \
        .filter(or_(filter_by_messages))\
        .filter(Message.direction == 'inbound')\
        .distinct(Message.from_).count()

    # Total unique messages
    total_unique_messages = Message.query.filter(or_(filter_by_messages)) \
        .filter(Message.direction == 'inbound') \
        .distinct(Message.from_).count()

    return render_template('dashboard/dashboard.jinja2',
                           account_total_minutes=account_total_minutes,
                           monthly_unique_calls=monthly_unique_calls,
                           monthly_contact_leads=monthly_contact_leads,
                           previous_month_contact_leads=previous_month_contact_leads,
                           monthly_unique_messages=monthly_unique_messages,
                           total_unique_messages=total_unique_messages,
                           account_total_numbers=account_total_numbers,
                           account_total_agents=account_total_agents,
                           account_total_groups=account_total_groups,
                           account_total_forms=account_total_forms,
                           account_total_form_leads=account_total_form_leads,
                           account_total_numbers_priority=account_total_numbers_priority)