Skip to content

Commit

Permalink
use a form for client data
Browse files Browse the repository at this point in the history
  • Loading branch information
deronnax committed Apr 15, 2024
1 parent f5b144b commit a9f1b6d
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 83 deletions.
5 changes: 5 additions & 0 deletions appointment/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ def __init__(self, *args, **kwargs):
})


class ClientDataForm(forms.Form):
name = forms.CharField(max_length=50, widget=forms.TextInput(attrs={'class': 'form-control'}))
email = forms.EmailField(widget=forms.EmailInput(attrs={'class': 'form-control'}))


class PersonalInformationForm(forms.Form):
# first_name, last_name, email
first_name = forms.CharField(max_length=50, widget=forms.TextInput(attrs={'class': 'form-control'}))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,11 @@ <h1 class="description-title">{% trans "Tell us a bit about yourself" %}</h1>
</div>
</div>
<div class="name-email">
<label for="name" class="name">{% trans "Full Name" %} *<br>
<input type="text" placeholder="John DOE" id="name" class="client-name"
maxlength="100" minlength="3" required name="name">
<label for="{{ form.name.id_for_label }}" class="name">{% trans "Full Name" %} *<br>
{{ client_data_form.name }}
</label>
<label for="email" class="email">{% trans "Email" %} *<br>
<input type="email" placeholder="[email protected]" id="email"
class="client-email" name="email"
maxlength="100" required>
<label for="{{ form.email.id_for_label }}" class="email">{% trans "Email" %} *<br>
{{ client_data_form.email }}
</label>
</div>
<div class="receive-email">
Expand Down
62 changes: 1 addition & 61 deletions appointment/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@
from appointment.tests.base.base_test import BaseTest
from appointment.utils.db_helpers import Service, WorkingHours, create_user_with_username
from appointment.utils.error_codes import ErrorCode
from appointment.views import (
create_appointment, get_client_data_from_post,
redirect_to_payment_or_thank_you_page, verify_user_and_login
)
from appointment.views import create_appointment, redirect_to_payment_or_thank_you_page, verify_user_and_login


class SlotTestCase(BaseTest):
Expand Down Expand Up @@ -1048,63 +1045,6 @@ def test_notify_admin_about_reschedule_called(self, mock_notify_admin):
self.assertTrue(mock_notify_admin.called)


class GetClientDataFromPostTests(BaseTest):
def setUp(self):
self.factory = RequestFactory()

def test_get_client_data_with_full_data(self):
"""Test retrieving client data from a POST request with all fields provided."""
post_data = {
'name': 'John Doe',
'email': '[email protected]',
}
request = self.factory.post('/fake-url/', post_data)

client_data = get_client_data_from_post(request)

self.assertEqual(client_data['name'], post_data['name'])
self.assertEqual(client_data['email'], post_data['email'])

def test_get_client_data_with_missing_name(self):
"""Test retrieving client data from a POST request with the name missing."""
post_data = {
# 'name' is missing
'email': '[email protected]',
}
request = self.factory.post('/fake-url/', post_data)

client_data = get_client_data_from_post(request)

self.assertIsNone(client_data['name'], "name should be None if not provided")
self.assertEqual(client_data['email'], post_data['email'])

def test_get_client_data_with_missing_email(self):
"""Test retrieving client data from a POST request with the email missing."""
post_data = {
'name': 'John Doe',
# 'email' is missing
}
request = self.factory.post('/fake-url/', post_data)

client_data = get_client_data_from_post(request)

self.assertEqual(client_data['name'], post_data['name'])
self.assertIsNone(client_data['email'], "email should be None if not provided")

def test_get_client_data_with_empty_fields(self):
"""Test retrieving client data from a POST request with empty fields."""
post_data = {
'name': '',
'email': '',
}
request = self.factory.post('/fake-url/', post_data)

client_data = get_client_data_from_post(request)

self.assertEqual(client_data['name'], '', "name should be empty string if provided as such")
self.assertEqual(client_data['email'], '', "email should be empty string if provided as such")


class RedirectToPaymentOrThankYouPageTests(BaseTest):
def setUp(self):
super().setUp()
Expand Down
21 changes: 6 additions & 15 deletions appointment/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from django.utils.timezone import get_current_timezone_name
from django.utils.translation import gettext as _

from appointment.forms import AppointmentForm, AppointmentRequestForm, SlotForm
from appointment.forms import AppointmentForm, AppointmentRequestForm, SlotForm, ClientDataForm
from appointment.logger_config import logger
from appointment.models import (
Appointment, AppointmentRequest, AppointmentRescheduleHistory, Config, DayOff, EmailVerificationCode,
Expand Down Expand Up @@ -290,18 +290,6 @@ def create_appointment(request, appointment_request_obj, client_data, appointmen
return redirect_to_payment_or_thank_you_page(appointment)


def get_client_data_from_post(request):
"""This function retrieves client data from the POST request.
:param request: The request instance.
:return: The client data.
"""
return {
'name': request.POST.get('name'),
'email': request.POST.get('email'),
}


def appointment_client_information(request, appointment_request_id, id_request):
"""This view function handles client information submission for an appointment.
Expand All @@ -317,10 +305,11 @@ def appointment_client_information(request, appointment_request_id, id_request):

if request.method == 'POST':
appointment_form = AppointmentForm(request.POST)
client_data_form = ClientDataForm(request.POST)

if appointment_form.is_valid():
if appointment_form.is_valid() and client_data_form.is_valid():
appointment_data = appointment_form.cleaned_data
client_data = get_client_data_from_post(request)
client_data = client_data_form.cleaned_data
payment_type = request.POST.get('payment_type')
ar.payment_type = payment_type
ar.save()
Expand All @@ -340,11 +329,13 @@ def appointment_client_information(request, appointment_request_id, id_request):
return response
else:
appointment_form = AppointmentForm()
client_data_form = ClientDataForm()

extra_context = {
'ar': ar,
'APPOINTMENT_PAYMENT_URL': APPOINTMENT_PAYMENT_URL,
'form': appointment_form,
'client_data_form': client_data_form,
'service_name': ar.service.name,
}
context = get_generic_context_with_extra(request, extra_context, admin=False)
Expand Down

0 comments on commit a9f1b6d

Please sign in to comment.