Added flow for verifying emails

This commit is contained in:
Pranav Nachanekar 2019-09-20 10:08:48 +05:30
parent 5324234bd0
commit df1a5a9633
4 changed files with 96 additions and 38 deletions

View File

@ -13,28 +13,56 @@ from frappe.desk.form.assign_to import add as add_assignemnt
class Appointment(Document): class Appointment(Document):
def validate(self): email=''
def find_lead_by_email(self,email):
lead_list = frappe.get_list('Lead', filters = {'email_id':email}, ignore_permissions = True)
if lead_list:
return lead_list[0].name
self.email = email
return None
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')
# Link lead
def before_insert(self): self.lead = self.find_lead_by_email(self.lead)
self.lead = _find_lead_by_email(self.lead).name
def after_insert(self): def after_insert(self):
appointment_event = frappe.get_doc({ # Auto assign
'doctype': 'Event', self.auto_assign()
'subject': ' '.join(['Appointment with', self.customer_name]), # Check if lead was found
'starts_on': self.scheduled_time, if(self.lead):
'status': 'Open', # Create Calendar event
'type': 'Private', self.create_calendar_event()
'send_reminder': frappe.db.get_single_value('Appointment Booking Settings','email_reminders'), else:
'event_participants': [dict(reference_doctype = "Lead", reference_docname = self.lead)] # Send email to confirm
# frappe.sendmail(recipients=[self.email],message='https:/',subject="")
frappe.msgprint("Please check your email to confirm the appointment")
def set_verified(self,email):
# Create new lead
self.create_lead(email)
# Create calender event
self.create_calendar_event()
self.save( ignore_permissions=True )
frappe.db.commit()
def create_lead(self,email):
lead = frappe.get_doc({
'doctype':'Lead',
'lead_name':self.customer_name,
'email_id':email,
'notes':self.customer_details,
'phone':self.customer_phone_number,
}) })
appointment_event.insert(ignore_permissions=True) print(lead.insert( ignore_permissions=True ))
self.calendar_event = appointment_event.name # Link lead
self.lead = lead.name
def auto_assign(self):
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)):
@ -44,14 +72,26 @@ class Appointment(Document):
'name':self.name, 'name':self.name,
'assign_to':agent 'assign_to':agent
}) })
employee = _get_employee_from_user(agent) break
if employee:
calendar_event = frappe.get_doc('Event', self.calendar_event) def create_calendar_event(self):
calendar_event.append('event_participants', dict( appointment_event = frappe.get_doc({
reference_doctype= 'Employee', 'doctype': 'Event',
reference_docname= employee.name)) 'subject': ' '.join(['Appointment with', self.customer_name]),
calendar_event.save() 'starts_on': self.scheduled_time,
break '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='*')
@ -70,13 +110,6 @@ def _get_agents_sorted_by_asc_workload(date):
return sorted_agent_list return sorted_agent_list
def _find_lead_by_email(email):
lead_list = frappe.get_list('Lead', filters={'email_id':email}, ignore_permissions=True)
if lead_list:
return lead_list[0]
frappe.throw('Email ID not associated with any Lead. Please make sure to use the email address you got this mail on')
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
@ -97,4 +130,4 @@ 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 return frappe.get_doc('Employee', employee_docname[0][0]) # frappe.db.exists returns a tuple of a tuple
return None return None

View File

@ -111,18 +111,15 @@ 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:
@ -130,17 +127,14 @@ 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)
def _convert_to_ist(datetime_object, timezone): def _convert_to_ist(datetime_object, timezone):
offset = datetime.timedelta(minutes=timezone) offset = datetime.timedelta(minutes=timezone)
datetime_object = datetime_object + offset datetime_object = datetime_object + offset
@ -148,7 +142,6 @@ def _convert_to_ist(datetime_object, timezone):
datetime_object = datetime_object - offset datetime_object = datetime_object - offset
return datetime_object return datetime_object
def _convert_to_tz(datetime_object, timezone): def _convert_to_tz(datetime_object, timezone):
offset = datetime.timedelta(minutes=timezone) offset = datetime.timedelta(minutes=timezone)
datetime_object = datetime_object - offset datetime_object = datetime_object - offset

View File

@ -0,0 +1,18 @@
{% extends "templates/web.html" %}
{% block title %}
{{ _("Verify Email") }}
{% endblock%}
{% block page_content %}
{% if success==True %}
<div class="alert alert-success">
Your email has been verified and your appointment has been scheduled
</div>
{% else %}
<div class="alert alert-danger">
Verification failed please check the link
</div>
{% endif %}
{% endblock%}

View File

@ -0,0 +1,14 @@
import frappe
@frappe.whitelist(allow_guest=True)
def get_context(context):
email = frappe.form_dict['email']
appointment_name = frappe.form_dict['appointment']
if email and appointment_name:
appointment = frappe.get_doc('Appointment',appointment_name)
appointment.set_verified(email)
context.success = True
return context
else:
print('Something not found')
context.success = False
return context