-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* #29 Refactor views.py and other modules to OOP struct * #42 Add placeholder text for reservation form & style input width to be consistent - [] Update forms.py * #29 Extract handler class from Views * #29 Update bookings table data with heading * #29 Explicit type handling for views & handler modules Fix some pylint issues * #29 Upgrade requirements.txt * #29 Fix lint issues
- Loading branch information
1 parent
bb70581
commit f3eb150
Showing
10 changed files
with
173 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,27 @@ | ||
asgiref==3.8.1 | ||
astroid==3.2.4 | ||
certifi==2024.7.4 | ||
charset-normalizer==3.3.2 | ||
defusedxml==0.7.1 | ||
dill==0.3.8 | ||
distlib==0.3.8 | ||
Django==5.1.1 | ||
djangorestframework==3.15.2 | ||
djangorestframework-xml==2.0.0 | ||
filelock==3.15.4 | ||
idna==3.10 | ||
isort==5.13.2 | ||
mccabe==0.7.0 | ||
mysqlclient==2.2.4 | ||
pipenv==2024.0.1 | ||
platformdirs==4.2.2 | ||
pylint==3.2.7 | ||
setuptools==73.0.0 | ||
pylint-django==2.5.5 | ||
pylint-plugin-utils==0.8.2 | ||
pytz==2024.2 | ||
requests==2.32.3 | ||
setuptools==75.1.0 | ||
sqlparse==0.5.1 | ||
tomlkit==0.13.2 | ||
virtualenv==20.26.3 | ||
urllib3==2.2.3 | ||
virtualenv==20.26.3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
"""booking-sys handlers module for Mapping & Logic for 4xx to 5xx HTTP status codes | ||
""" | ||
import logging | ||
from django.shortcuts import render | ||
from django.core.handlers.wsgi import WSGIRequest | ||
from django.urls.exceptions import Resolver404 | ||
from django.http import HttpResponse | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
class Handlers(): | ||
"""Handlers for HTTP Error codes like 404 not found, 401 forbidden etc. | ||
""" | ||
|
||
@classmethod | ||
def handler404(cls, request:WSGIRequest, exception:Resolver404) -> HttpResponse: | ||
"""Resolve bad request path | ||
Parameters | ||
---------- | ||
request : WSGIRequest | ||
exception: Resolver404 | ||
""" | ||
uri = request.get_full_path | ||
logger.error('Bad Request URI path: %s', uri) | ||
return render(request, 'error.html', {'uri': uri}, status=404) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,111 +1,116 @@ | ||
"""booking-sys Views Mapping & Logic | ||
"""booking-sys views Mapping & Logic | ||
""" | ||
from datetime import datetime | ||
import logging | ||
from django.http import JsonResponse | ||
from django.shortcuts import render | ||
from django.core.handlers.wsgi import WSGIRequest | ||
from restaurant.forms import ReservationForm | ||
from .models import Reservation | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
def handler404(request, exception): | ||
"""Resolve bad request path | ||
Parameters | ||
---------- | ||
request : Requests https://requests.readthedocs.io/en/latest/ | ||
class Views(): | ||
"""Views class for Views Mapping & Logic | ||
""" | ||
uri = request.get_full_path | ||
logger.error('Bad Request URI path: %s', uri) | ||
return render(request, 'error.html', {'uri': uri}, status=404) | ||
|
||
def table_view(request): | ||
"""GET bookings by date request parameter | ||
Parameters | ||
---------- | ||
request : Requests | ||
""" | ||
date = request.GET.get("date", datetime.today().date()) | ||
return find_bookings_by_date(date) | ||
@classmethod | ||
def table_view(cls, request:WSGIRequest): | ||
"""GET bookings by date request parameter | ||
Parameters | ||
---------- | ||
request : Requests | ||
""" | ||
date = request.GET.get("date", datetime.today().date()) | ||
return cls.__find_bookings_by_date(cls, date) | ||
|
||
def bookings_view(request, date): | ||
"""GET bookings by date request path variable | ||
Parameters | ||
---------- | ||
request : Requests | ||
date: The date in format %y-%m-%d i.e. 2024-09-07 | ||
""" | ||
logger.info('Request information (%s)', request) | ||
return find_bookings_by_date(date) | ||
@classmethod | ||
def bookings_view(cls, request:WSGIRequest, date): | ||
"""GET bookings by date request path variable | ||
Parameters | ||
---------- | ||
request : Requests | ||
date: The date in format %y-%m-%d i.e. 2024-09-07 | ||
""" | ||
logger.info('Request information (%s)', request) | ||
return cls.__find_bookings_by_date(cls, date) | ||
|
||
def find_bookings_by_date(date): | ||
"""Bookings by date and return JSON response | ||
Parameters | ||
---------- | ||
date: The date in format %y-%m-%d i.e. 2024-09-07 | ||
""" | ||
reservations_by_date = Reservation.objects.filter(reservation_date=date) | ||
data = list(reservations_by_date.values('first_name', 'reservation_date', 'reservation_slot')) | ||
logger.info('GET by date (%s) Query set results: %s', date, data) | ||
return JsonResponse({ | ||
'message': 'success', | ||
'reservations': data | ||
}) | ||
@classmethod | ||
def reservations_view(cls, request:WSGIRequest): | ||
"""Resolve all reservations view data request | ||
Parameters | ||
---------- | ||
request : Requests | ||
""" | ||
data = list(Reservation.objects.all().values( | ||
'first_name', 'reservation_date', 'reservation_slot')) | ||
logger.info('GET All Query set results: %s', data) | ||
return render(request, 'reservations.html', {'reservations': data}) | ||
|
||
def reservations_view(request): | ||
"""Resolve all reservations view data request | ||
Parameters | ||
---------- | ||
request : Requests | ||
""" | ||
data = list(Reservation.objects.all().values( | ||
'first_name', 'reservation_date', 'reservation_slot')) | ||
logger.info('GET All Query set results: %s', data) | ||
return render(request, 'reservations.html', {'reservations': data}) | ||
@classmethod | ||
def form_view(cls, request:WSGIRequest): | ||
"""Resolve make a reservation form submit | ||
Parameters | ||
---------- | ||
request : Requests | ||
""" | ||
form = ReservationForm() | ||
# POST request logic | ||
if request.method == 'POST': | ||
form = ReservationForm(request.POST) | ||
logger.info(request.POST) | ||
if form.is_valid(): | ||
# clean & extract form data | ||
form_data = form.cleaned_data | ||
booking_date = form_data['reservation_date'] | ||
booking_slot = form_data['reservation_slot'] | ||
|
||
def form_view(request): | ||
"""Resolve make a reservation form submit | ||
Parameters | ||
---------- | ||
request : Requests | ||
""" | ||
form = ReservationForm() | ||
# POST request logic | ||
if request.method == 'POST': | ||
form = ReservationForm(request.POST) | ||
logger.info(request.POST) | ||
if form.is_valid(): | ||
# clean & extract form data | ||
form_data = form.cleaned_data | ||
booking_date = form_data['reservation_date'] | ||
booking_slot = form_data['reservation_slot'] | ||
# booking flow logic | ||
reservations_by_date = Reservation.objects.filter( | ||
reservation_date=booking_date, reservation_slot=booking_slot) | ||
message = '' | ||
if reservations_by_date.exists(): | ||
message = 'Booking Failed - Already Reserved' | ||
else: | ||
# map form object to model & save to DB | ||
reservation = Reservation( | ||
first_name = form_data['first_name'], | ||
reservation_date = booking_date, | ||
reservation_slot = booking_slot, | ||
) | ||
reservation.save() | ||
message = 'Booking Complete' | ||
# POST response JSON | ||
data = list(Reservation.objects.filter(reservation_date=booking_date).values( | ||
'first_name', 'reservation_date', 'reservation_slot')) | ||
logger.info('POST Query set results: %s', data) | ||
return JsonResponse({ | ||
'message': message, | ||
'reservations': data | ||
}) | ||
else: | ||
# Bookings view default logic | ||
reservations = Reservation.objects.all() | ||
data = list(reservations.values_list('first_name', | ||
'reservation_date', | ||
'reservation_slot')) | ||
logger.info('GET Query set results: %s', data) | ||
return render(request, 'booking.html', {'form': form, | ||
'reservations': reservations}) | ||
|
||
# booking flow logic | ||
reservations_by_date = Reservation.objects.filter( | ||
reservation_date=booking_date, reservation_slot=booking_slot) | ||
message = '' | ||
if reservations_by_date.exists(): | ||
message = 'Booking Failed - Already Reserved' | ||
else: | ||
# map form object to model & save to DB | ||
reservation = Reservation( | ||
first_name = form_data['first_name'], | ||
reservation_date = booking_date, | ||
reservation_slot = booking_slot, | ||
) | ||
reservation.save() | ||
message = 'Booking Complete' | ||
# POST response JSON | ||
data = list(Reservation.objects.filter(reservation_date=booking_date).values( | ||
'first_name', 'reservation_date', 'reservation_slot')) | ||
logger.info('POST Query set results: %s', data) | ||
return JsonResponse({ | ||
'message': message, | ||
'reservations': data | ||
}) | ||
else: | ||
# Bookings view default logic | ||
reservations = Reservation.objects.all() | ||
data = list(reservations.values_list('first_name', 'reservation_date', 'reservation_slot')) | ||
logger.info('GET Query set results: %s', data) | ||
return render(request, 'booking.html', {'form': form, 'reservations': reservations}) | ||
def __find_bookings_by_date(self, date): | ||
"""Bookings by date and return JSON | ||
response (private method) | ||
Parameters | ||
---------- | ||
date: The date in format %y-%m-%d i.e. 2024-09-07 | ||
""" | ||
reservations_by_date = Reservation.objects.filter(reservation_date=date) | ||
data = list(reservations_by_date.values('first_name', | ||
'reservation_date', | ||
'reservation_slot')) | ||
logger.info('GET by date (%s) Query set results: %s', date, data) | ||
return JsonResponse({ | ||
'message': 'success', | ||
'reservations': data | ||
}) |