File: //home/arjun/projects/buyercall/buyercall/blueprints/admin/templates/admin/utils/add_user.py
from buyercall.blueprints.agents import AgentSchedule
from buyercall.blueprints.agents.forms import AgentForm
from buyercall.blueprints.agents.models import Agent
from flask_login import current_user
from flask import (
flash,
url_for,
redirect)
from buyercall.blueprints.agents.views import load_groups, getboolean
from html import parser
from flask_babel import gettext as _
import json
from buyercall.blueprints.filters import format_phone_number
from buyercall.blueprints.reports.models import ReportUserTie
from buyercall.blueprints.user.models import UserExternalApiAutoPostTie
class CreateUserUtils:
@staticmethod
def add_agent(form, user):
data = {"firstname": form.firstname.data,
"lastname": form.lastname.data,
"title": form.title.data,
"email": form.email.data,
"phonenumber": format_phone_number(form.phonenumber.data),
"mobile": form.phonenumber.data,
"description": "description",
"user_id": user.id}
agent = Agent()
sunday = AgentSchedule(0, '08:00 AM', '17:00 PM', False, '-1', '-1')
monday = AgentSchedule(1, '08:00 AM', '17:00 PM', True, '-1', '-1')
tuesday = AgentSchedule(2, '08:00 AM', '17:00 PM', True, '-1', '-1')
wednesday = AgentSchedule(3, '08:00 AM', '17:00 PM', True, '-1', '-1')
thursday = AgentSchedule(4, '08:00 AM', '17:00 PM', True, '-1', '-1')
friday = AgentSchedule(5, '08:00 AM', '17:00 PM', True, '-1', '-1')
saturday = AgentSchedule(6, '08:00 AM', '17:00 PM', False, '-1', '-1')
agent.all_hours = True
agent.schedules.append(sunday)
agent.schedules.append(monday)
agent.schedules.append(tuesday)
agent.schedules.append(wednesday)
agent.schedules.append(thursday)
agent.schedules.append(friday)
agent.schedules.append(saturday)
total_agents = Agent.query.filter(current_user.id == Agent.user_id).count()
partnership_account_id = current_user.partnership_account_id
is_admin_in_group = current_user.is_admin_user_with_groups
viewing_partnership_account = current_user.is_viewing_partnership
# Check if being viewed by super partner
if is_admin_in_group and viewing_partnership_account:
partnership_account_id = current_user.get_user_viewing_partnership_account_id
elif not viewing_partnership_account and is_admin_in_group:
return redirect(url_for('partnership.company_accounts'))
form = AgentForm(obj=agent)
if total_agents >= current_user.subscription.agent_limit if current_user.subscription else 0:
flash(_(
'You need to <strong>upgrade your account</strong> to add more agents. '
'<a href='"/subscription/update"' style='"color:#ffffff"'><strong>Change your plan</strong></a>'),
'danger')
return redirect(url_for('agents.agent_new'))
else:
agent.firstname = data.get("firstname")
agent.user_id = data.get('user_id')
agent.lastname = data.get('lastname')
agent.title = data.get('title')
agent.email = data.get('email').lower()
agent.phonenumber = data.get('phonenumber')
agent.mobile = data.get('mobile')
# agent.description = data.get('description')
agent.department = 'none' # form.department.data
# agent.timezone = form.timezone.data
# agent.all_hours = getboolean(form.allhours_str.data)
# load_groups(agent, form.groups_str.data)
params = {
'user_id': agent.user_id,
'firstname': agent.firstname,
'lastname': agent.lastname,
'title': agent.title,
'email': agent.email,
'phonenumber': agent.phonenumber,
'mobile': agent.mobile,
# 'extension': agent.extension,
'description': agent.description,
'department': agent.department,
# 'timezone': agent.timezone,
'partnership_account_id': partnership_account_id,
# 'all_hours': agent.all_hours
}
result = Agent.create(params)
if result > 0:
h = parser
schedulequery = str(form.schedules_str.data)
if len(schedulequery) > 0:
scheduleobj = json.loads(h.unescape(schedulequery))
day1obj = scheduleobj[0]
day1param = {
'day': int(day1obj["id"]),
'available_from': str(day1obj["start"]),
'available_to': str(day1obj["stop"]),
'is_active': getboolean(str(day1obj["active"])),
'partnership_account_id': int(partnership_account_id),
'agent_id': int(result)
}
day2obj = scheduleobj[1]
day2param = {
'day': int(day2obj["id"]),
'available_from': str(day2obj["start"]),
'available_to': str(day2obj["stop"]),
'is_active': getboolean(str(day2obj["active"])),
'partnership_account_id': int(partnership_account_id),
'agent_id': int(result)
}
day3obj = scheduleobj[2]
day3param = {
'day': int(day3obj["id"]),
'available_from': str(day3obj["start"]),
'available_to': str(day3obj["stop"]),
'is_active': getboolean(str(day3obj["active"])),
'partnership_account_id': int(partnership_account_id),
'agent_id': int(result)
}
day4obj = scheduleobj[3]
day4param = {
'day': int(day4obj["id"]),
'available_from': str(day4obj["start"]),
'available_to': str(day4obj["stop"]),
'is_active': getboolean(str(day4obj["active"])),
'partnership_account_id': int(partnership_account_id),
'agent_id': int(result)
}
day5obj = scheduleobj[4]
day5param = {
'day': int(day5obj["id"]),
'available_from': str(day5obj["start"]),
'available_to': str(day5obj["stop"]),
'is_active': getboolean(str(day5obj["active"])),
'partnership_account_id': int(partnership_account_id),
'agent_id': int(result)
}
day6obj = scheduleobj[5]
day6param = {
'day': int(day6obj["id"]),
'available_from': str(day6obj["start"]),
'available_to': str(day6obj["stop"]),
'is_active': getboolean(str(day6obj["active"])),
'partnership_account_id': int(partnership_account_id),
'agent_id': int(result)
}
day7obj = scheduleobj[6]
day7param = {
'day': int(day7obj["id"]),
'available_from': str(day7obj["start"]),
'available_to': str(day7obj["stop"]),
'is_active': getboolean(str(day7obj["active"])),
'partnership_account_id': int(partnership_account_id),
'agent_id': int(result)
}
day_result = AgentSchedule.create(
day1param, day2param, day3param, day4param, day5param, day6param, day7param
)
else:
day_result = 1
if day_result > 0:
# flash(_('Your agent has been created successfully.'), 'success')
# return redirect(url_for('agents.call_agents'))
return agent
# form.groups_str.data = ",".join(str(g.id) for g in agent.groups)
return None
@staticmethod
def add_reports(form, user, db):
print("form.reports_list.data", form.reports_list.data)
# re-adding reports
for _id in form.reports_list.data:
link = ReportUserTie()
link.report_id = _id
link.user_id = user.id
link.partnership_accounts_id = user.partnership_account_id
db.session.add(link)
db.session.commit()
@staticmethod
def set_service_provider_access(request, partnership_account_id, user_id):
ams_2000 = request.form.get('ams_2000')
ams_2000_result = False
if ams_2000 and ams_2000 == 'y':
ams_2000_result = True
UserExternalApiAutoPostTie.set_service_provider_access_state(user_id, partnership_account_id,
'AMS 2000', ams_2000_result)
ams_evolution = request.form.get('ams_evolution')
ams_evolution_result = False
if ams_evolution and ams_evolution == 'y':
ams_evolution_result = True
UserExternalApiAutoPostTie.set_service_provider_access_state(user_id, partnership_account_id,
'AMS Evolution', ams_evolution_result)
neo_verify = request.form.get('neo_verify')
neo_verify_result = False
if neo_verify and neo_verify == 'y':
neo_verify_result = True
UserExternalApiAutoPostTie.set_service_provider_access_state(user_id, partnership_account_id,
'NEO Verify', neo_verify_result)