Skip to content

Commit aa74894

Browse files
committed
Added responsivity to appointment page
Refactored some of the code
1 parent 358cd03 commit aa74894

File tree

15 files changed

+583
-203
lines changed

15 files changed

+583
-203
lines changed

appointment/admin.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from django.contrib import admin
22

3-
from .models import Service, AppointmentRequest, Appointment, EmailVerificationCode
3+
from .models import Service, AppointmentRequest, Appointment, EmailVerificationCode, Config
44

55

66
@admin.register(Service)
@@ -27,3 +27,8 @@ class AppointmentAdmin(admin.ModelAdmin):
2727
@admin.register(EmailVerificationCode)
2828
class EmailVerificationCodeAdmin(admin.ModelAdmin):
2929
list_display = ('user', 'code')
30+
31+
32+
@admin.register(Config)
33+
class ConfigAdmin(admin.ModelAdmin):
34+
list_display = ('slot_duration', 'lead_time', 'finish_time', 'appointment_buffer_time', 'website_name')

appointment/email_messages.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
If you choose the deposit, you will have to pay the rest of the amount on the day of the booking.""")
77

88
thank_you_payment = ("""We're excited to have you on board! Thank you for booking us. The next step is to pay for
9-
the booking.""")
9+
the booking.""")

appointment/forms.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from django import forms
2+
23
from .models import AppointmentRequest
34

45

appointment/models.py

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,24 @@ def get_description(self):
4444
return self.description
4545

4646
def get_duration(self):
47-
return self.duration.seconds // 3600
47+
total_seconds = self.duration.total_seconds()
48+
if total_seconds >= 86400: # 1 day = 86400 seconds
49+
days = total_seconds // 86400
50+
return f"{days} day{'s' if days > 1 else ''}"
51+
elif total_seconds >= 3600: # 1 hour = 3600 seconds
52+
hours = total_seconds // 3600
53+
return f"{hours} hour{'s' if hours > 1 else ''}"
54+
elif total_seconds >= 60: # 1 minute = 60 seconds
55+
minutes = total_seconds // 60
56+
return f"{minutes} minute{'s' if minutes > 1 else ''}"
57+
else:
58+
return f"{total_seconds} second{'s' if total_seconds > 1 else ''}"
4859

4960
def get_price(self):
50-
return self.price
61+
if self.price == 0:
62+
return "Free"
63+
else:
64+
return f"{self.price} {self.currency}"
5165

5266
def get_down_payment(self):
5367
return self.down_payment
@@ -249,16 +263,25 @@ def set_appointment_paid_status(self, status: bool):
249263

250264
class Config(models.Model):
251265
slot_duration = models.PositiveIntegerField(
252-
default=30,
253-
help_text=_("Duration of each slot in minutes"),
266+
null=True,
267+
help_text=_("Minimum time for an appointment in minutes, recommended 30."),
254268
)
255269
lead_time = models.TimeField(
256-
default="09:00",
257-
help_text=_("Time when slots start"),
270+
null=True,
271+
help_text=_("Time when we start working."),
258272
)
259273
finish_time = models.TimeField(
260-
default="16:30",
261-
help_text=_("Time when we stop working"),
274+
null=True,
275+
help_text=_("Time when we stop working."),
276+
)
277+
appointment_buffer_time = models.DurationField(
278+
null=True,
279+
help_text=_("Time between now and the first available slot for the current day (doesn't affect tomorrow)."),
280+
)
281+
website_name = models.CharField(
282+
max_length=255,
283+
default="",
284+
help_text=_("Name of your website."),
262285
)
263286

264287
def clean(self):
@@ -325,4 +348,4 @@ def generate_code(cls, user):
325348
code = ''.join(random.choices(string.ascii_uppercase + string.digits, k=6))
326349
verification_code = cls(user=user, code=code)
327350
verification_code.save()
328-
return code
351+
return code

appointment/operations/__init__.py

Whitespace-only changes.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# appointment/operations/database_operations.py
2+
3+
from django.apps import apps
4+
from django.contrib.auth.hashers import make_password
5+
from django.urls import reverse
6+
7+
from appointment.models import Appointment, PaymentInfo
8+
from logger_config import logger
9+
from appointment.settings import APPOINTMENT_CLIENT_MODEL, APPOINTMENT_PAYMENT_URL
10+
from appointment.utils import Utility
11+
12+
CLIENT_MODEL = apps.get_model(APPOINTMENT_CLIENT_MODEL)
13+
14+
15+
def get_appointments_and_slots(date_, service=None):
16+
if service:
17+
appointments = Appointment.objects.filter(appointment_request__service=service,
18+
appointment_request__date=date_)
19+
else:
20+
appointments = Appointment.objects.filter(appointment_request__date=date_)
21+
available_slots = Utility.get_available_slots(date_, appointments)
22+
return appointments, available_slots
23+
24+
25+
def get_user_by_email(email):
26+
return CLIENT_MODEL.objects.filter(email=email).first()
27+
28+
29+
def create_new_user(client_data):
30+
username = client_data['email'].split('@')[0]
31+
user = CLIENT_MODEL.objects.create_user(first_name=client_data['name'], email=client_data['email'],
32+
username=username)
33+
password = f"{Utility.get_website_name()}{Utility.get_current_year()}"
34+
user.password = make_password(password=password)
35+
user.save()
36+
return user
37+
38+
39+
def create_and_save_appointment(ar, client_data, appointment_data):
40+
user = get_user_by_email(client_data['email'])
41+
appointment = Appointment.objects.create(
42+
client=user, appointment_request=ar,
43+
**appointment_data
44+
)
45+
appointment.save()
46+
logger.info(f"New appointment created: {appointment}")
47+
return appointment
48+
49+
50+
def create_payment_info_and_get_url(appointment):
51+
payment_info = PaymentInfo(appointment=appointment)
52+
payment_info.save()
53+
payment_url = reverse(
54+
APPOINTMENT_PAYMENT_URL,
55+
kwargs={'object_id': payment_info.id, 'id_request': payment_info.get_id_request()}
56+
)
57+
return payment_url
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# appointment/operations/email_operations.py
2+
3+
import datetime
4+
5+
from appointment import email_messages
6+
from appointment.email_sender import send_email
7+
from appointment.models import EmailVerificationCode
8+
from appointment.settings import APPOINTMENT_PAYMENT_URL
9+
from appointment.utils import Utility
10+
11+
12+
def get_email_message(ar):
13+
if APPOINTMENT_PAYMENT_URL is None:
14+
message = email_messages.thank_you_no_payment
15+
elif ar.accepts_down_payment():
16+
message = email_messages.thank_you_payment_plus_down
17+
else:
18+
message = email_messages.thank_you_payment
19+
return message
20+
21+
22+
def send_email_to_client(ar, first_name, email):
23+
email_context = {
24+
'first_name': first_name,
25+
'message': get_email_message(ar),
26+
'current_year': datetime.datetime.now().year,
27+
'company': Utility.get_website_name(),
28+
}
29+
send_email(
30+
recipient_list=[email], subject="Thank you for booking us.",
31+
template_url='email_sender/thank_you_email.html', context=email_context
32+
)
33+
34+
35+
def send_verification_email(user, email):
36+
code = EmailVerificationCode.generate_code(user=user)
37+
message = f"Your verification code is {code}."
38+
send_email(recipient_list=[email], subject="Email Verification", message=message)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# appointment/operations/session_operations.py
2+
3+
from django.contrib import messages
4+
from django.shortcuts import redirect
5+
6+
from appointment.operations.database_operations import get_user_by_email
7+
from appointment.operations.email_operations import send_verification_email
8+
from logger_config import logger
9+
from appointment.settings import APPOINTMENT_BASE_TEMPLATE
10+
11+
12+
def handle_existing_email(request, client_data, appointment_data, appointment_request_id, id_request):
13+
logger.info("Email already in database, saving info in session and redirecting to enter verification code")
14+
user = get_user_by_email(client_data['email'])
15+
send_verification_email(user=user, email=client_data['email'])
16+
17+
# clean the session variables
18+
session_keys = ['email', 'phone', 'want_reminder', 'address', 'additional_info']
19+
for key in session_keys:
20+
if key in request.session:
21+
del request.session[key]
22+
23+
request.session['email'] = client_data['email']
24+
request.session['phone'] = appointment_data['phone']
25+
request.session['want_reminder'] = appointment_data['want_reminder']
26+
request.session['address'] = appointment_data['address']
27+
request.session['additional_info'] = appointment_data['additional_info']
28+
29+
request.session['APPOINTMENT_BASE_TEMPLATE'] = APPOINTMENT_BASE_TEMPLATE
30+
31+
messages.error(request, f"Email '{client_data['email']}' already exists. Login to your account.")
32+
return redirect('appointment:enter_verification_code', appointment_request_id=appointment_request_id,
33+
id_request=id_request)
34+
35+
36+
def get_appointment_data_from_session(request):
37+
phone = request.session.get('phone')
38+
want_reminder = request.session.get('want_reminder') == 'on'
39+
address = request.session.get('address')
40+
additional_info = request.session.get('additional_info')
41+
42+
return {
43+
'phone': phone,
44+
'want_reminder': want_reminder,
45+
'address': address,
46+
'additional_info': additional_info,
47+
}

appointment/settings.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
APPOINTMENT_PAYMENT_URL = getattr(settings, 'APPOINTMENT_PAYMENT_URL', None)
88
APPOINTMENT_THANK_YOU_URL = getattr(settings, 'APPOINTMENT_THANK_YOU_URL', None)
99
APPOINTMENT_SLOT_DURATION = getattr(settings, 'APPOINTMENT_SLOT_DURATION', 30)
10+
APPOINTMENT_BUFFER_TIME = getattr(settings, 'APPOINTMENT_BUFFER_TIME', 0)
1011
APPOINTMENT_LEAD_TIME = getattr(settings, 'APPOINTMENT_LEAD_TIME', (9, 0))
11-
APPOINTMENT_FINISH_TIME = getattr(settings, 'APPOINTMENT_FINISH_TIME', (16, 30))
12+
APPOINTMENT_FINISH_TIME = getattr(settings, 'APPOINTMENT_FINISH_TIME', (18, 30))
1213
APP_DEFAULT_FROM_EMAIL = getattr(settings, 'DEFAULT_FROM_EMAIL', DEFAULT_FROM_EMAIL)
13-
APP_TIME_ZONE = getattr(settings, 'TIME_ZONE', 'UTC')
14+
APP_TIME_ZONE = getattr(settings, 'TIME_ZONE', 'America/New_York')

0 commit comments

Comments
 (0)