21
21
from appointment .settings import APPOINTMENT_PAYMENT_URL
22
22
from appointment .utils .date_time import convert_24_hour_time_to_12_hour_time
23
23
from appointment .utils .db_helpers import get_absolute_url_ , get_website_name
24
+ from appointment .utils .ics_utils import generate_ics_file
24
25
25
26
logger = get_logger (__name__ )
26
27
@@ -46,14 +47,15 @@ def get_thank_you_message(ar: AppointmentRequest) -> str:
46
47
47
48
48
49
def send_thank_you_email (ar : AppointmentRequest , user , request , email : str , appointment_details = None ,
49
- account_details = None ):
50
+ account_details = None , ics_file = None ):
50
51
"""Send a thank-you email to the client for booking an appointment.
51
52
52
53
:param ar: The appointment request associated with the booking.
53
54
:param user: The user who booked the appointment.
54
55
:param email: The email address of the client.
55
56
:param appointment_details: Additional details about the appointment (default None).
56
57
:param account_details: Additional details about the account (default None).
58
+ :param ics_file: The ICS file to attach to the email (default None).
57
59
:param request: The request object.
58
60
:return: None
59
61
"""
@@ -87,7 +89,8 @@ def send_thank_you_email(ar: AppointmentRequest, user, request, email: str, appo
87
89
}
88
90
send_email (
89
91
recipient_list = [email ], subject = _ ("Thank you for booking us." ),
90
- template_url = 'email_sender/thank_you_email.html' , context = email_context
92
+ template_url = 'email_sender/thank_you_email.html' , context = email_context ,
93
+ attachments = [('appointment.ics' , ics_file , 'text/calendar' )]
91
94
)
92
95
93
96
@@ -142,20 +145,64 @@ def send_reset_link_to_staff_member(user, request, email: str, account_details=N
142
145
143
146
def notify_admin_about_appointment (appointment , client_name : str ):
144
147
"""Notify the admin and the staff member about a new appointment request."""
145
- logger .info (f"Sending admin notification for new appointment { appointment .id } " )
146
- email_context = {
148
+ logger .info (f"Sending notifications for new appointment { appointment .id } " )
149
+
150
+ staff_member = appointment .get_staff_member ()
151
+ ics_file = generate_ics_file (appointment )
152
+
153
+ # Create a set to keep track of notified email addresses
154
+ notified_emails = set ()
155
+
156
+ # Prepare the staff member notification
157
+ staff_email = staff_member .user .email
158
+ staff_name = staff_member .user .get_full_name () or staff_member .user .username
159
+ staff_context = {
160
+ 'recipient_name' : staff_name ,
147
161
'client_name' : client_name ,
148
- 'appointment' : appointment
162
+ 'appointment' : appointment ,
163
+ 'is_staff_member' : True ,
164
+ 'staff_member_name' : staff_name
149
165
}
150
166
151
- subject = _ ("New Appointment Request for " ) + client_name
152
- staff_member = appointment .get_staff_member ()
153
- notify_admin (subject = subject , template_url = 'email_sender/admin_new_appointment_email.html' , context = email_context )
154
- if staff_member .user .email not in settings .ADMINS :
155
- logger .info (
156
- f"Let's notify the staff member as well for new appointment { appointment .id } since they are not an admin." )
157
- send_email (recipient_list = [staff_member .user .email ], subject = subject ,
158
- template_url = 'email_sender/admin_new_appointment_email.html' , context = email_context )
167
+ # Notify admins
168
+ for admin_name , admin_email in settings .ADMINS :
169
+ if admin_email in notified_emails :
170
+ continue # Skip if this email has already been notified
171
+
172
+ is_staff_admin = admin_email == staff_email
173
+ email_context = staff_context if is_staff_admin else {
174
+ 'recipient_name' : admin_name ,
175
+ 'client_name' : client_name ,
176
+ 'appointment' : appointment ,
177
+ 'is_staff_member' : False ,
178
+ 'staff_member_name' : staff_name
179
+ }
180
+
181
+ subject = _ ("New Appointment Request for " ) + client_name
182
+ attachments = [('appointment.ics' , ics_file , 'text/calendar' )] if is_staff_admin else None
183
+
184
+ notify_admin (
185
+ subject = subject ,
186
+ template_url = 'email_sender/admin_new_appointment_email.html' ,
187
+ context = email_context ,
188
+ recipient_email = admin_email ,
189
+ attachments = attachments
190
+ )
191
+
192
+ notified_emails .add (admin_email )
193
+
194
+ # Notify staff member if they haven't been notified as an admin
195
+ if staff_email not in notified_emails :
196
+ logger .info (f"Notifying the staff member for new appointment { appointment .id } " )
197
+ send_email (
198
+ recipient_list = [staff_email ],
199
+ subject = _ ("New Appointment Request for " ) + client_name ,
200
+ template_url = 'email_sender/admin_new_appointment_email.html' ,
201
+ context = staff_context ,
202
+ attachments = [('appointment.ics' , ics_file , 'text/calendar' )]
203
+ )
204
+
205
+ logger .info (f"Notifications sent for appointment { appointment .id } " )
159
206
160
207
161
208
def send_verification_email (user , email : str ):
0 commit comments