fix:readability

This commit is contained in:
Pranav Nachanekar 2019-10-04 11:32:39 +05:30
parent 9e36a9ee04
commit 25148d0de5
4 changed files with 192 additions and 163 deletions

View File

@ -13,160 +13,173 @@ from frappe import _
from frappe.model.document import Document from frappe.model.document import Document
from frappe.desk.form.assign_to import add as add_assignemnt from frappe.desk.form.assign_to import add as add_assignemnt
from frappe.utils import get_url from frappe.utils import get_url
from frappe.utils.verified_command import verify_request,get_signed_params from frappe.utils.verified_command import verify_request, get_signed_params
class Appointment(Document): class Appointment(Document):
def find_lead_by_email(self): def find_lead_by_email(self):
lead_list = frappe.get_list('Lead', filters = {'email_id':self.customer_email}, ignore_permissions = True) lead_list = frappe.get_list(
if lead_list: 'Lead', filters={'email_id': self.customer_email}, ignore_permissions=True)
return lead_list[0].name if lead_list:
return None return lead_list[0].name
return None
def before_insert(self): def before_insert(self):
number_of_appointments_in_same_slot = frappe.db.count('Appointment', filters = {'scheduled_time':self.scheduled_time}) number_of_appointments_in_same_slot = frappe.db.count(
settings = frappe.get_doc('Appointment Booking Settings') 'Appointment', filters={'scheduled_time': self.scheduled_time})
if(number_of_appointments_in_same_slot >= settings.number_of_agents): settings = frappe.get_doc('Appointment Booking Settings')
frappe.throw('Time slot is not available') if(number_of_appointments_in_same_slot >= settings.number_of_agents):
# Link lead frappe.throw('Time slot is not available')
self.lead = self.find_lead_by_email() # Link lead
self.lead = self.find_lead_by_email()
def after_insert(self): def after_insert(self):
if(self.lead): if(self.lead):
# Create Calendar event # Create Calendar event
self.create_calendar_event() self.create_calendar_event()
self.auto_assign() self.auto_assign()
else: else:
# Set status to unverified # Set status to unverified
self.status = 'Unverified' self.status = 'Unverified'
# Send email to confirm # Send email to confirm
verify_url = self.get_verify_url() verify_url = self.get_verify_url()
message = ''.join(['Please click the following link to confirm your appointment:',verify_url]) message = ''.join(
frappe.sendmail(recipients=[self.customer_email], ['Please click the following link to confirm your appointment:', verify_url])
message=message, frappe.sendmail(recipients=[self.customer_email],
subject=_('Appointment Confirmation')) message=message,
frappe.msgprint('Please check your email to confirm the appointment') subject=_('Appointment Confirmation'))
frappe.msgprint(
'Please check your email to confirm the appointment')
def get_verify_url(self): def get_verify_url(self):
verify_route = '/book-appointment/verify' verify_route = '/book-appointment/verify'
params = { params = {
'email':self.customer_email, 'email': self.customer_email,
'appointment':self.name 'appointment': self.name
} }
return get_url(verify_route + '?' + get_signed_params(params)) return get_url(verify_route + '?' + get_signed_params(params))
def on_change(self): def on_change(self):
# Sync Calednar # Sync Calednar
if not self.calendar_event: if not self.calendar_event:
return return
cal_event = frappe.get_doc('Event',self.calendar_event) cal_event = frappe.get_doc('Event', self.calendar_event)
cal_event.starts_on = self.scheduled_time cal_event.starts_on = self.scheduled_time
cal_event.save(ignore_permissions=True) cal_event.save(ignore_permissions=True)
def on_trash(self): def on_trash(self):
# Delete calendar event # Delete calendar event
cal_event = frappe.get_doc('Event',self.calendar_event) cal_event = frappe.get_doc('Event', self.calendar_event)
if cal_event: if cal_event:
cal_event.delete() cal_event.delete()
# Delete task? # Delete task?
def set_verified(self,email):
if not email == self.customer_email:
frappe.throw('Email verification failed.')
# Create new lead
self.create_lead()
# Remove unverified status
self.status = 'Open'
# Create calender event
self.create_calendar_event()
self.auto_assign()
self.save(ignore_permissions=True)
frappe.db.commit()
def create_lead(self): def set_verified(self, email):
# Return if already linked if not email == self.customer_email:
if self.lead: frappe.throw('Email verification failed.')
return # Create new lead
lead = frappe.get_doc({ self.create_lead()
'doctype':'Lead', # Remove unverified status
'lead_name':self.customer_name, self.status = 'Open'
'email_id':self.customer_email, # Create calender event
'notes':self.customer_details, self.create_calendar_event()
'phone':self.customer_phone_number, self.auto_assign()
}) self.save(ignore_permissions=True)
lead.insert(ignore_permissions=True) frappe.db.commit()
# Link lead
self.lead = lead.name
def auto_assign(self): def create_lead(self):
if self._assign: # Return if already linked
return if self.lead:
available_agents = _get_agents_sorted_by_asc_workload(self.scheduled_time.date()) return
for agent in available_agents: lead = frappe.get_doc({
if(_check_agent_availability(agent, self.scheduled_time)): 'doctype': 'Lead',
agent = agent[0] 'lead_name': self.customer_name,
add_assignemnt({ 'email_id': self.customer_email,
'doctype':self.doctype, 'notes': self.customer_details,
'name':self.name, 'phone': self.customer_phone_number,
'assign_to':agent })
}) lead.insert(ignore_permissions=True)
break # Link lead
self.lead = lead.name
def auto_assign(self):
if self._assign:
return
available_agents = _get_agents_sorted_by_asc_workload(
self.scheduled_time.date())
for agent in available_agents:
if(_check_agent_availability(agent, self.scheduled_time)):
agent = agent[0]
add_assignemnt({
'doctype': self.doctype,
'name': self.name,
'assign_to': agent
})
break
def create_calendar_event(self):
if self.calendar_event:
return
appointment_event = frappe.get_doc({
'doctype': 'Event',
'subject': ' '.join(['Appointment with', self.customer_name]),
'starts_on': self.scheduled_time,
'status': 'Open',
'type': 'Public',
'send_reminder': frappe.db.get_single_value('Appointment Booking Settings', 'email_reminders'),
'event_participants': [dict(reference_doctype='Lead', reference_docname=self.lead)]
})
employee = _get_employee_from_user(self._assign)
if employee:
appointment_event.append('event_participants', dict(
reference_doctype='Employee',
reference_docname=employee.name))
appointment_event.insert(ignore_permissions=True)
self.calendar_event = appointment_event.name
self.save(ignore_permissions=True)
def create_calendar_event(self):
if self.calendar_event:
return
appointment_event = frappe.get_doc({
'doctype': 'Event',
'subject': ' '.join(['Appointment with', self.customer_name]),
'starts_on': self.scheduled_time,
'status': 'Open',
'type': 'Public',
'send_reminder': frappe.db.get_single_value('Appointment Booking Settings','email_reminders'),
'event_participants': [dict(reference_doctype = 'Lead', reference_docname = self.lead)]
})
employee = _get_employee_from_user(self._assign)
if employee:
appointment_event.append('event_participants', dict(
reference_doctype = 'Employee',
reference_docname = employee.name))
appointment_event.insert(ignore_permissions=True)
self.calendar_event = appointment_event.name
self.save(ignore_permissions=True)
def _get_agents_sorted_by_asc_workload(date): def _get_agents_sorted_by_asc_workload(date):
appointments = frappe.db.get_list('Appointment', fields='*') appointments = frappe.db.get_list('Appointment', fields='*')
agent_list = _get_agent_list_as_strings() agent_list = _get_agent_list_as_strings()
if not appointments: if not appointments:
return agent_list return agent_list
appointment_counter = Counter(agent_list) appointment_counter = Counter(agent_list)
for appointment in appointments: for appointment in appointments:
assigned_to = frappe.parse_json(appointment._assign) assigned_to = frappe.parse_json(appointment._assign)
if not assigned_to: if not assigned_to:
continue continue
if (assigned_to[0] in agent_list) and appointment.scheduled_time.date() == date: if (assigned_to[0] in agent_list) and appointment.scheduled_time.date() == date:
appointment_counter[assigned_to[0]] += 1 appointment_counter[assigned_to[0]] += 1
sorted_agent_list = appointment_counter.most_common() sorted_agent_list = appointment_counter.most_common()
sorted_agent_list.reverse() sorted_agent_list.reverse()
return sorted_agent_list return sorted_agent_list
def _get_agent_list_as_strings(): def _get_agent_list_as_strings():
agent_list_as_strings = [] agent_list_as_strings = []
agent_list = frappe.get_doc('Appointment Booking Settings').agent_list agent_list = frappe.get_doc('Appointment Booking Settings').agent_list
for agent in agent_list: for agent in agent_list:
agent_list_as_strings.append(agent.user) agent_list_as_strings.append(agent.user)
return agent_list_as_strings return agent_list_as_strings
def _check_agent_availability(agent_email, scheduled_time):
appointemnts_at_scheduled_time = frappe.get_list(
'Appointment', filters={'scheduled_time': scheduled_time})
for appointment in appointemnts_at_scheduled_time:
if appointment._assign == agent_email:
return False
return True
def _check_agent_availability(agent_email,scheduled_time):
appointemnts_at_scheduled_time = frappe.get_list('Appointment', filters = {'scheduled_time':scheduled_time})
for appointment in appointemnts_at_scheduled_time:
if appointment._assign == agent_email:
return False
return True
def _get_employee_from_user(user): def _get_employee_from_user(user):
employee_docname = frappe.db.exists({'doctype':'Employee', 'user_id':user}) employee_docname = frappe.db.exists(
if employee_docname: {'doctype': 'Employee', 'user_id': user})
return frappe.get_doc('Employee', employee_docname[0][0]) # frappe.db.exists returns a tuple of a tuple if employee_docname:
return None # frappe.db.exists returns a tuple of a tuple
return frappe.get_doc('Employee', employee_docname[0][0])
return None

View File

@ -7,45 +7,52 @@ import frappe
import unittest import unittest
import datetime import datetime
def create_test_lead(): def create_test_lead():
test_lead = frappe.db.exists({'doctype':'Lead','lead_name':'Test Lead'}) test_lead = frappe.db.exists({'doctype': 'Lead', 'lead_name': 'Test Lead'})
if test_lead: if test_lead:
return frappe.get_doc('Lead',test_lead[0][0]) return frappe.get_doc('Lead', test_lead[0][0])
test_lead = frappe.get_doc({ test_lead = frappe.get_doc({
'doctype':'Lead', 'doctype': 'Lead',
'lead_name':'Test Lead', 'lead_name': 'Test Lead',
'email_id':'test@example.com' 'email_id': 'test@example.com'
}) })
test_lead.insert(ignore_permissions=True) test_lead.insert(ignore_permissions=True)
return test_lead return test_lead
def create_test_appointments(): def create_test_appointments():
test_appointment = frappe.db.exists({ 'doctype':'Appointment', 'email':'test@example.com' }) test_appointment = frappe.db.exists(
{'doctype': 'Appointment', 'email': 'test@example.com'})
if test_appointment: if test_appointment:
return frappe.get_doc('Appointment',test_appointment[0][0]) return frappe.get_doc('Appointment', test_appointment[0][0])
test_appointment = frappe.get_doc({ test_appointment = frappe.get_doc({
'doctype':'Appointment', 'doctype': 'Appointment',
'email':'test@example.com', 'email': 'test@example.com',
'status':'Open', 'status': 'Open',
'customer_name':'Test Lead', 'customer_name': 'Test Lead',
'customer_phone_number':'666', 'customer_phone_number': '666',
'customer_skype':'test', 'customer_skype': 'test',
'customer_email':'test@example.com', 'customer_email': 'test@example.com',
'scheduled_time':datetime.datetime.now() 'scheduled_time': datetime.datetime.now()
}) })
test_appointment.insert() test_appointment.insert()
return test_appointment return test_appointment
class TestAppointment(unittest.TestCase): class TestAppointment(unittest.TestCase):
test_appointment = test_lead = None test_appointment = test_lead = None
def setUp(self): def setUp(self):
self.test_lead = create_test_lead() self.test_lead = create_test_lead()
self.test_appointment = create_test_appointments() self.test_appointment = create_test_appointments()
def test_calendar_event_created(self): def test_calendar_event_created(self):
cal_event = frappe.get_doc('Event',self.test_appointment.calendar_event) cal_event = frappe.get_doc(
self.assertEqual(cal_event.starts_on ,self.test_appointment.scheduled_time) 'Event', self.test_appointment.calendar_event)
self.assertEqual(cal_event.starts_on,
self.test_appointment.scheduled_time)
def test_lead_linked(self): def test_lead_linked(self):
lead = frappe.get_doc('Lead',self.test_lead.name) lead = frappe.get_doc('Lead', self.test_lead.name)
self.assertIsNotNone(lead) self.assertIsNotNone(lead)

View File

@ -34,7 +34,7 @@ function setup_timezone_selector() {
window.timezones.forEach(timezone => { window.timezones.forEach(timezone => {
let opt = document.createElement('option'); let opt = document.createElement('option');
opt.value = timezone; opt.value = timezone;
if(timezone == moment.tz.guess()){ if (timezone == moment.tz.guess()) {
opt.selected = true; opt.selected = true;
} }
opt.innerHTML = timezone; opt.innerHTML = timezone;
@ -140,7 +140,7 @@ function select_time() {
return; return;
} }
let selected_element = document.getElementsByClassName('selected'); let selected_element = document.getElementsByClassName('selected');
if (!(selected_element.length > 0)){ if (!(selected_element.length > 0)) {
this.classList.add('selected'); this.classList.add('selected');
show_next_button(); show_next_button();
return; return;
@ -191,7 +191,7 @@ function setup_details_page() {
async function submit() { async function submit() {
let form = document.querySelector('#customer-form'); let form = document.querySelector('#customer-form');
if(!form.checkValidity()){ if (!form.checkValidity()) {
form.reportValidity(); form.reportValidity();
return; return;
} }
@ -211,7 +211,6 @@ async function submit() {
} }
function get_form_data() { function get_form_data() {
contact = {}; contact = {};
contact.name = document.getElementById('customer_name').value; contact.name = document.getElementById('customer_name').value;
contact.number = document.getElementById('customer_number').value; contact.number = document.getElementById('customer_number').value;

View File

@ -9,21 +9,26 @@ WEEKDAYS = ["Monday", "Tuesday", "Wednesday",
no_cache = 1 no_cache = 1
def get_context(context): def get_context(context):
is_enabled = frappe.db.get_single_value('Appointment Booking Settings','enable_scheduling') is_enabled = frappe.db.get_single_value(
'Appointment Booking Settings', 'enable_scheduling')
if is_enabled: if is_enabled:
return context return context
else: else:
raise frappe.DoesNotExistError raise frappe.DoesNotExistError
@frappe.whitelist(allow_guest=True) @frappe.whitelist(allow_guest=True)
def get_appointment_settings(): def get_appointment_settings():
settings = frappe.get_doc('Appointment Booking Settings') settings = frappe.get_doc('Appointment Booking Settings')
return settings return settings
@frappe.whitelist(allow_guest=True) @frappe.whitelist(allow_guest=True)
def is_enabled(): def is_enabled():
enable_scheduling = frappe.db.get_single_value('Appointment Booking Settings','enable_scheduling') enable_scheduling = frappe.db.get_single_value(
'Appointment Booking Settings', 'enable_scheduling')
return enable_scheduling return enable_scheduling
@ -131,15 +136,18 @@ def filter_timeslots(date, timeslots):
filtered_timeslots.append(timeslot) filtered_timeslots.append(timeslot)
return filtered_timeslots return filtered_timeslots
def check_availabilty(timeslot, settings): def check_availabilty(timeslot, settings):
return frappe.db.count('Appointment', {'scheduled_time': timeslot}) < settings.number_of_agents return frappe.db.count('Appointment', {'scheduled_time': timeslot}) < settings.number_of_agents
def _is_holiday(date, holiday_list): def _is_holiday(date, holiday_list):
for holiday in holiday_list.holidays: for holiday in holiday_list.holidays:
if holiday.holiday_date == date: if holiday.holiday_date == date:
return True return True
return False return False
def _get_records(start_time, end_time, settings): def _get_records(start_time, end_time, settings):
records = [] records = []
for record in settings.availability_of_slots: for record in settings.availability_of_slots:
@ -147,10 +155,12 @@ def _get_records(start_time, end_time, settings):
records.append(record) records.append(record)
return records return records
def _deltatime_to_datetime(date, deltatime): def _deltatime_to_datetime(date, deltatime):
time = (datetime.datetime.min + deltatime).time() time = (datetime.datetime.min + deltatime).time()
return datetime.datetime.combine(date.date(), time) return datetime.datetime.combine(date.date(), time)
def _datetime_to_deltatime(date_time): def _datetime_to_deltatime(date_time):
midnight = datetime.datetime.combine(date_time.date(), datetime.time.min) midnight = datetime.datetime.combine(date_time.date(), datetime.time.min)
return (date_time-midnight) return (date_time-midnight)