Skip to content

Commit db269f3

Browse files
committed
use a form for client data
1 parent 93c4e0d commit db269f3

File tree

4 files changed

+16
-83
lines changed

4 files changed

+16
-83
lines changed

appointment/forms.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ def __init__(self, *args, **kwargs):
7070
})
7171

7272

73+
class ClientDataForm(forms.Form):
74+
name = forms.CharField(max_length=50, widget=forms.TextInput(attrs={'class': 'form-control'}))
75+
email = forms.EmailField(widget=forms.EmailInput(attrs={'class': 'form-control'}))
76+
77+
7378
class PersonalInformationForm(forms.Form):
7479
# first_name, last_name, email
7580
first_name = forms.CharField(max_length=50, widget=forms.TextInput(attrs={'class': 'form-control'}))

appointment/templates/appointment/appointment_client_information.html

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,11 @@ <h1 class="description-title">{% trans "Tell us a bit about yourself" %}</h1>
4444
</div>
4545
</div>
4646
<div class="name-email">
47-
<label for="name" class="name">{% trans "Full Name" %} *<br>
48-
<input type="text" placeholder="John DOE" id="name" class="client-name"
49-
maxlength="100" minlength="3" required name="name">
47+
<label for="{{ form.name.id_for_label }}" class="name">{% trans "Full Name" %} *<br>
48+
{{ client_data_form.name }}
5049
</label>
51-
<label for="email" class="email">{% trans "Email" %} *<br>
52-
<input type="email" placeholder="[email protected]" id="email"
53-
class="client-email" name="email"
54-
maxlength="100" required>
50+
<label for="{{ form.email.id_for_label }}" class="email">{% trans "Email" %} *<br>
51+
{{ client_data_form.email }}
5552
</label>
5653
</div>
5754
<div class="receive-email">

appointment/tests/test_views.py

Lines changed: 1 addition & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@
2929
from appointment.tests.base.base_test import BaseTest
3030
from appointment.utils.db_helpers import Service, WorkingHours, create_user_with_username
3131
from appointment.utils.error_codes import ErrorCode
32-
from appointment.views import (
33-
create_appointment, get_client_data_from_post,
34-
redirect_to_payment_or_thank_you_page, verify_user_and_login
35-
)
32+
from appointment.views import create_appointment, redirect_to_payment_or_thank_you_page, verify_user_and_login
3633

3734

3835
class SlotTestCase(BaseTest):
@@ -1045,63 +1042,6 @@ def test_notify_admin_about_reschedule_called(self, mock_notify_admin):
10451042
self.assertTrue(mock_notify_admin.called)
10461043

10471044

1048-
class GetClientDataFromPostTests(BaseTest):
1049-
def setUp(self):
1050-
self.factory = RequestFactory()
1051-
1052-
def test_get_client_data_with_full_data(self):
1053-
"""Test retrieving client data from a POST request with all fields provided."""
1054-
post_data = {
1055-
'name': 'John Doe',
1056-
'email': '[email protected]',
1057-
}
1058-
request = self.factory.post('/fake-url/', post_data)
1059-
1060-
client_data = get_client_data_from_post(request)
1061-
1062-
self.assertEqual(client_data['name'], post_data['name'])
1063-
self.assertEqual(client_data['email'], post_data['email'])
1064-
1065-
def test_get_client_data_with_missing_name(self):
1066-
"""Test retrieving client data from a POST request with the name missing."""
1067-
post_data = {
1068-
# 'name' is missing
1069-
'email': '[email protected]',
1070-
}
1071-
request = self.factory.post('/fake-url/', post_data)
1072-
1073-
client_data = get_client_data_from_post(request)
1074-
1075-
self.assertIsNone(client_data['name'], "name should be None if not provided")
1076-
self.assertEqual(client_data['email'], post_data['email'])
1077-
1078-
def test_get_client_data_with_missing_email(self):
1079-
"""Test retrieving client data from a POST request with the email missing."""
1080-
post_data = {
1081-
'name': 'John Doe',
1082-
# 'email' is missing
1083-
}
1084-
request = self.factory.post('/fake-url/', post_data)
1085-
1086-
client_data = get_client_data_from_post(request)
1087-
1088-
self.assertEqual(client_data['name'], post_data['name'])
1089-
self.assertIsNone(client_data['email'], "email should be None if not provided")
1090-
1091-
def test_get_client_data_with_empty_fields(self):
1092-
"""Test retrieving client data from a POST request with empty fields."""
1093-
post_data = {
1094-
'name': '',
1095-
'email': '',
1096-
}
1097-
request = self.factory.post('/fake-url/', post_data)
1098-
1099-
client_data = get_client_data_from_post(request)
1100-
1101-
self.assertEqual(client_data['name'], '', "name should be empty string if provided as such")
1102-
self.assertEqual(client_data['email'], '', "email should be empty string if provided as such")
1103-
1104-
11051045
class RedirectToPaymentOrThankYouPageTests(BaseTest):
11061046
def setUp(self):
11071047
super().setUp()

appointment/views.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from django.utils.timezone import get_current_timezone_name
2222
from django.utils.translation import gettext as _
2323

24-
from appointment.forms import AppointmentForm, AppointmentRequestForm, SlotForm
24+
from appointment.forms import AppointmentForm, AppointmentRequestForm, SlotForm, ClientDataForm
2525
from appointment.logger_config import logger
2626
from appointment.models import (
2727
Appointment, AppointmentRequest, AppointmentRescheduleHistory, Config, DayOff, EmailVerificationCode,
@@ -290,18 +290,6 @@ def create_appointment(request, appointment_request_obj, client_data, appointmen
290290
return redirect_to_payment_or_thank_you_page(appointment)
291291

292292

293-
def get_client_data_from_post(request):
294-
"""This function retrieves client data from the POST request.
295-
296-
:param request: The request instance.
297-
:return: The client data.
298-
"""
299-
return {
300-
'name': request.POST.get('name'),
301-
'email': request.POST.get('email'),
302-
}
303-
304-
305293
def appointment_client_information(request, appointment_request_id, id_request):
306294
"""This view function handles client information submission for an appointment.
307295
@@ -317,10 +305,11 @@ def appointment_client_information(request, appointment_request_id, id_request):
317305

318306
if request.method == 'POST':
319307
appointment_form = AppointmentForm(request.POST)
308+
client_data_form = ClientDataForm(request.POST)
320309

321-
if appointment_form.is_valid():
310+
if appointment_form.is_valid() and client_data_form.is_valid():
322311
appointment_data = appointment_form.cleaned_data
323-
client_data = get_client_data_from_post(request)
312+
client_data = client_data_form.cleaned_data
324313
payment_type = request.POST.get('payment_type')
325314
ar.payment_type = payment_type
326315
ar.save()
@@ -340,11 +329,13 @@ def appointment_client_information(request, appointment_request_id, id_request):
340329
return response
341330
else:
342331
appointment_form = AppointmentForm()
332+
client_data_form = ClientDataForm()
343333

344334
extra_context = {
345335
'ar': ar,
346336
'APPOINTMENT_PAYMENT_URL': APPOINTMENT_PAYMENT_URL,
347337
'form': appointment_form,
338+
'client_data_form': client_data_form,
348339
'service_name': ar.service.name,
349340
}
350341
context = get_generic_context_with_extra(request, extra_context, admin=False)

0 commit comments

Comments
 (0)