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/api/doc/endpoints/groups.py
import logging

from flask import jsonify
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.restplus import api
from buyercall.blueprints.agents.models import Agent
from buyercall.extensions import db


log = logging.getLogger(__name__)
ns = api.namespace('groups', description='Operations related to agents groups.')


@ns.route('/')
@api.doc(responses={200: 'OK',
                    400: 'Error performing operation.',
                    401: 'Unauthorized request.'})
class ApiGroupCollection(Resource):
    @requires_auth
    def get(self):
        """
        Retrieves the agent groups of a partnership account.

        Use this method to view the details of a partnership account's agent groups.

        """
        if rest_is_partnership_account and rest_partnership_account:
            group_list = list()
            groups = db.session.query(Agent) \
                .filter(and_(Agent.partnership_account_id == rest_partnership_account.id, Agent.is_group == True)) \
                .distinct() \
                .all()

            if groups:
                for group in groups:
                    group_agent_list = list()
                    if group.id and group.id > 0:
                        if group.agents:
                            for agent in group.agents:
                                add_agent = {
                                    'firstname': agent.firstname,
                                    'lastname': agent.lastname,
                                    'email': agent.email,
                                    'phonenumber': agent.phonenumber,
                                    'mobile': agent.mobile,
                                    'department': agent.department,
                                    'description': agent.description,
                                    'extension': agent.extension,
                                    'title': agent.title,
                                    'available': agent.available_now
                                }
                                group_agent_list.append(add_agent)

                    add_group = {
                        'name': group.firstname,
                        'description': group.description,
                        'agents': group_agent_list
                    }
                    group_list.append(add_group)

            return jsonify(
                groups=group_list
            )
        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:
                    group_list = list()
                    groups = db.session.query(Agent) \
                        .filter(and_(Agent.partnership_account_id == pa.id, Agent.is_group == True)) \
                        .distinct() \
                        .all()

                    if groups:
                        for group in groups:
                            group_agent_list = list()

                            if group.agents:
                                for agent in group.agents:
                                    add_agent = {
                                        'firstname': agent.firstname,
                                        'lastname': agent.lastname,
                                        'email': agent.email,
                                        'phonenumber': agent.phonenumber,
                                        'mobile': agent.mobile,
                                        'department': agent.department,
                                        'description': agent.description,
                                        'extension': agent.extension,
                                        'title': agent.title,
                                        'available': agent.available_now
                                    }
                                    group_agent_list.append(add_agent)

                            add_group = {
                                'name': group.firstname,
                                'description': group.description,
                                'agents': group_agent_list
                            }
                            group_list.append(add_group)

                    add_pa = {
                        'name': pa.name,
                        'groups': group_list
                    }
                    pa_list.append(add_pa)
            return jsonify(
                accounts=pa_list
            )
        else:
            api.abort(401)