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,19 +13,21 @@ 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(
'Lead', filters={'email_id': self.customer_email}, ignore_permissions=True)
if lead_list: if lead_list:
return lead_list[0].name return lead_list[0].name
return None 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(
'Appointment', filters={'scheduled_time': self.scheduled_time})
settings = frappe.get_doc('Appointment Booking Settings') settings = frappe.get_doc('Appointment Booking Settings')
if(number_of_appointments_in_same_slot >= settings.number_of_agents): if(number_of_appointments_in_same_slot >= settings.number_of_agents):
frappe.throw('Time slot is not available') frappe.throw('Time slot is not available')
@ -42,18 +44,20 @@ class Appointment(Document):
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(
['Please click the following link to confirm your appointment:', verify_url])
frappe.sendmail(recipients=[self.customer_email], frappe.sendmail(recipients=[self.customer_email],
message=message, message=message,
subject=_('Appointment Confirmation')) subject=_('Appointment Confirmation'))
frappe.msgprint('Please check your email to confirm the appointment') 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))
@ -62,17 +66,18 @@ class Appointment(Document):
# 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):
def set_verified(self, email):
if not email == self.customer_email: if not email == self.customer_email:
frappe.throw('Email verification failed.') frappe.throw('Email verification failed.')
# Create new lead # Create new lead
@ -90,11 +95,11 @@ class Appointment(Document):
if self.lead: if self.lead:
return return
lead = frappe.get_doc({ lead = frappe.get_doc({
'doctype':'Lead', 'doctype': 'Lead',
'lead_name':self.customer_name, 'lead_name': self.customer_name,
'email_id':self.customer_email, 'email_id': self.customer_email,
'notes':self.customer_details, 'notes': self.customer_details,
'phone':self.customer_phone_number, 'phone': self.customer_phone_number,
}) })
lead.insert(ignore_permissions=True) lead.insert(ignore_permissions=True)
# Link lead # Link lead
@ -103,14 +108,15 @@ class Appointment(Document):
def auto_assign(self): def auto_assign(self):
if self._assign: if self._assign:
return return
available_agents = _get_agents_sorted_by_asc_workload(self.scheduled_time.date()) available_agents = _get_agents_sorted_by_asc_workload(
self.scheduled_time.date())
for agent in available_agents: for agent in available_agents:
if(_check_agent_availability(agent, self.scheduled_time)): if(_check_agent_availability(agent, self.scheduled_time)):
agent = agent[0] agent = agent[0]
add_assignemnt({ add_assignemnt({
'doctype':self.doctype, 'doctype': self.doctype,
'name':self.name, 'name': self.name,
'assign_to':agent 'assign_to': agent
}) })
break break
@ -123,18 +129,19 @@ class Appointment(Document):
'starts_on': self.scheduled_time, 'starts_on': self.scheduled_time,
'status': 'Open', 'status': 'Open',
'type': 'Public', 'type': 'Public',
'send_reminder': frappe.db.get_single_value('Appointment Booking Settings','email_reminders'), 'send_reminder': frappe.db.get_single_value('Appointment Booking Settings', 'email_reminders'),
'event_participants': [dict(reference_doctype = 'Lead', reference_docname = self.lead)] 'event_participants': [dict(reference_doctype='Lead', reference_docname=self.lead)]
}) })
employee = _get_employee_from_user(self._assign) employee = _get_employee_from_user(self._assign)
if employee: if employee:
appointment_event.append('event_participants', dict( appointment_event.append('event_participants', dict(
reference_doctype = 'Employee', reference_doctype='Employee',
reference_docname = employee.name)) reference_docname=employee.name))
appointment_event.insert(ignore_permissions=True) appointment_event.insert(ignore_permissions=True)
self.calendar_event = appointment_event.name self.calendar_event = appointment_event.name
self.save(ignore_permissions=True) 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()
@ -151,6 +158,7 @@ def _get_agents_sorted_by_asc_workload(date):
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
@ -158,15 +166,20 @@ def _get_agent_list_as_strings():
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}) 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: for appointment in appointemnts_at_scheduled_time:
if appointment._assign == agent_email: if appointment._assign == agent_email:
return False return False
return True 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(
{'doctype': 'Employee', 'user_id': user})
if employee_docname: if employee_docname:
return frappe.get_doc('Employee', employee_docname[0][0]) # frappe.db.exists returns a tuple of a tuple # frappe.db.exists returns a tuple of a tuple
return frappe.get_doc('Employee', employee_docname[0][0])
return None 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)