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.desk.form.assign_to import add as add_assignemnt
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):
def find_lead_by_email(self):
lead_list = frappe.get_list('Lead', filters = {'email_id':self.customer_email}, ignore_permissions = True)
if lead_list:
return lead_list[0].name
return None
def before_insert(self):
number_of_appointments_in_same_slot = frappe.db.count('Appointment', filters = {'scheduled_time':self.scheduled_time})
settings = frappe.get_doc('Appointment Booking Settings')
if(number_of_appointments_in_same_slot >= settings.number_of_agents):
frappe.throw('Time slot is not available')
# Link lead
self.lead = self.find_lead_by_email()
def find_lead_by_email(self):
lead_list = frappe.get_list(
'Lead', filters={'email_id': self.customer_email}, ignore_permissions=True)
if lead_list:
return lead_list[0].name
return None
def after_insert(self):
if(self.lead):
# Create Calendar event
self.create_calendar_event()
self.auto_assign()
else:
# Set status to unverified
self.status = 'Unverified'
# Send email to confirm
verify_url = self.get_verify_url()
message = ''.join(['Please click the following link to confirm your appointment:',verify_url])
frappe.sendmail(recipients=[self.customer_email],
message=message,
subject=_('Appointment Confirmation'))
frappe.msgprint('Please check your email to confirm the appointment')
def before_insert(self):
number_of_appointments_in_same_slot = frappe.db.count(
'Appointment', filters={'scheduled_time': self.scheduled_time})
settings = frappe.get_doc('Appointment Booking Settings')
if(number_of_appointments_in_same_slot >= settings.number_of_agents):
frappe.throw('Time slot is not available')
# Link lead
self.lead = self.find_lead_by_email()
def get_verify_url(self):
verify_route = '/book-appointment/verify'
def after_insert(self):
if(self.lead):
# Create Calendar event
self.create_calendar_event()
self.auto_assign()
else:
# Set status to unverified
self.status = 'Unverified'
# Send email to confirm
verify_url = self.get_verify_url()
message = ''.join(
['Please click the following link to confirm your appointment:', verify_url])
frappe.sendmail(recipients=[self.customer_email],
message=message,
subject=_('Appointment Confirmation'))
frappe.msgprint(
'Please check your email to confirm the appointment')
params = {
'email':self.customer_email,
'appointment':self.name
}
def get_verify_url(self):
verify_route = '/book-appointment/verify'
return get_url(verify_route + '?' + get_signed_params(params))
params = {
'email': self.customer_email,
'appointment': self.name
}
def on_change(self):
# Sync Calednar
if not self.calendar_event:
return
cal_event = frappe.get_doc('Event',self.calendar_event)
cal_event.starts_on = self.scheduled_time
cal_event.save(ignore_permissions=True)
return get_url(verify_route + '?' + get_signed_params(params))
def on_trash(self):
# Delete calendar event
cal_event = frappe.get_doc('Event',self.calendar_event)
if cal_event:
cal_event.delete()
# 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 on_change(self):
# Sync Calednar
if not self.calendar_event:
return
cal_event = frappe.get_doc('Event', self.calendar_event)
cal_event.starts_on = self.scheduled_time
cal_event.save(ignore_permissions=True)
def create_lead(self):
# Return if already linked
if self.lead:
return
lead = frappe.get_doc({
'doctype':'Lead',
'lead_name':self.customer_name,
'email_id':self.customer_email,
'notes':self.customer_details,
'phone':self.customer_phone_number,
})
lead.insert(ignore_permissions=True)
# Link lead
self.lead = lead.name
def on_trash(self):
# Delete calendar event
cal_event = frappe.get_doc('Event', self.calendar_event)
if cal_event:
cal_event.delete()
# Delete task?
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 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):
# Return if already linked
if self.lead:
return
lead = frappe.get_doc({
'doctype': 'Lead',
'lead_name': self.customer_name,
'email_id': self.customer_email,
'notes': self.customer_details,
'phone': self.customer_phone_number,
})
lead.insert(ignore_permissions=True)
# 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):
appointments = frappe.db.get_list('Appointment', fields='*')
agent_list = _get_agent_list_as_strings()
if not appointments:
return agent_list
appointment_counter = Counter(agent_list)
for appointment in appointments:
assigned_to = frappe.parse_json(appointment._assign)
if not assigned_to:
continue
if (assigned_to[0] in agent_list) and appointment.scheduled_time.date() == date:
appointment_counter[assigned_to[0]] += 1
sorted_agent_list = appointment_counter.most_common()
sorted_agent_list.reverse()
return sorted_agent_list
appointments = frappe.db.get_list('Appointment', fields='*')
agent_list = _get_agent_list_as_strings()
if not appointments:
return agent_list
appointment_counter = Counter(agent_list)
for appointment in appointments:
assigned_to = frappe.parse_json(appointment._assign)
if not assigned_to:
continue
if (assigned_to[0] in agent_list) and appointment.scheduled_time.date() == date:
appointment_counter[assigned_to[0]] += 1
sorted_agent_list = appointment_counter.most_common()
sorted_agent_list.reverse()
return sorted_agent_list
def _get_agent_list_as_strings():
agent_list_as_strings = []
agent_list = frappe.get_doc('Appointment Booking Settings').agent_list
for agent in agent_list:
agent_list_as_strings.append(agent.user)
return agent_list_as_strings
agent_list_as_strings = []
agent_list = frappe.get_doc('Appointment Booking Settings').agent_list
for agent in agent_list:
agent_list_as_strings.append(agent.user)
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):
employee_docname = frappe.db.exists({'doctype':'Employee', 'user_id':user})
if employee_docname:
return frappe.get_doc('Employee', employee_docname[0][0]) # frappe.db.exists returns a tuple of a tuple
return None
employee_docname = frappe.db.exists(
{'doctype': 'Employee', 'user_id': user})
if employee_docname:
# 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 datetime
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:
return frappe.get_doc('Lead',test_lead[0][0])
return frappe.get_doc('Lead', test_lead[0][0])
test_lead = frappe.get_doc({
'doctype':'Lead',
'lead_name':'Test Lead',
'email_id':'test@example.com'
'doctype': 'Lead',
'lead_name': 'Test Lead',
'email_id': 'test@example.com'
})
test_lead.insert(ignore_permissions=True)
return test_lead
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:
return frappe.get_doc('Appointment',test_appointment[0][0])
return frappe.get_doc('Appointment', test_appointment[0][0])
test_appointment = frappe.get_doc({
'doctype':'Appointment',
'email':'test@example.com',
'status':'Open',
'customer_name':'Test Lead',
'customer_phone_number':'666',
'customer_skype':'test',
'customer_email':'test@example.com',
'scheduled_time':datetime.datetime.now()
'doctype': 'Appointment',
'email': 'test@example.com',
'status': 'Open',
'customer_name': 'Test Lead',
'customer_phone_number': '666',
'customer_skype': 'test',
'customer_email': 'test@example.com',
'scheduled_time': datetime.datetime.now()
})
test_appointment.insert()
return test_appointment
class TestAppointment(unittest.TestCase):
test_appointment = test_lead = None
def setUp(self):
self.test_lead = create_test_lead()
self.test_appointment = create_test_appointments()
def test_calendar_event_created(self):
cal_event = frappe.get_doc('Event',self.test_appointment.calendar_event)
self.assertEqual(cal_event.starts_on ,self.test_appointment.scheduled_time)
cal_event = frappe.get_doc(
'Event', self.test_appointment.calendar_event)
self.assertEqual(cal_event.starts_on,
self.test_appointment.scheduled_time)
def test_lead_linked(self):
lead = frappe.get_doc('Lead',self.test_lead.name)
self.assertIsNotNone(lead)
lead = frappe.get_doc('Lead', self.test_lead.name)
self.assertIsNotNone(lead)

View File

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

View File

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