Skip to content

Commit

Permalink
#29 Refactor views.py and other modules to OOP struct
Browse files Browse the repository at this point in the history
  • Loading branch information
conorheffron committed Sep 18, 2024
1 parent bb70581 commit f3674eb
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 106 deletions.
2 changes: 1 addition & 1 deletion booking-sys/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from django.views.static import serve
from restaurant import views

handler404 = views.handler404
handler404 = views.Views.handler404

urlpatterns = [
path('admin/', admin.site.urls),
Expand Down
6 changes: 3 additions & 3 deletions restaurant/static/js/reservations.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ function renderTableBookings(bookings) {
if (bookings.length > 0) {
var dynamoT = '<tbody>'
dynamoT += '<tr>';
dynamoT += '<th>First Name</td>';
dynamoT += '<th>Reservation Date</td>';
dynamoT += '<th>Reservation Time</td>';
dynamoT += '<th>Name</td>';
dynamoT += '<th>Booking Date</td>';
dynamoT += '<th>Booking Time</td>';
dynamoT += '</tr>';
for (i = 0; i < bookings.length; i++) {
dynamoT += '<tr>';
Expand Down
2 changes: 1 addition & 1 deletion restaurant/templates/booking.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
</div>

<div id="bookings" class="container pt-5">
<h1>Make a Reservation</h1>
<h3>Make a Reservation</h1>
<form method="POST" id="form">
{% csrf_token %}
{{ form.as_p }}
Expand Down
2 changes: 1 addition & 1 deletion restaurant/templates/navbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<div class="navbar-nav">
<a class="nav-link" href="/">Home</a>
<a class="nav-link" href="/reservations">Reservations</a>
<a class="nav-link" href="/admin">Login</a>
<a class="nav-link" target="_blank" href="/admin">Login</a>
</div>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions restaurant/templates/reservations.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
</tr>
{% endfor %}
</table>
<br />
<button type="button" class="btn btn-primary" onClick="refresh()">Refresh</button>
</div>
<body>
12 changes: 6 additions & 6 deletions restaurant/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
booking-sys Views Mapping & Logic
"""
from django.urls import path
from restaurant import views
from restaurant.views import Views

urlpatterns = [
path('', views.form_view, name="home"),
path('book/', views.form_view, name="book"),
path('reservations/', views.reservations_view, name="reservations"),
path('bookings/<str:date>/', views.bookings_view, name='bookings_by_date'),
path('bookings', views.table_view, name='bookings_by_date')
path('', Views.form_view, name="home"),
path('book/', Views.form_view, name="book"),
path('reservations/', Views.reservations_view, name="reservations"),
path('bookings/<str:date>/', Views.bookings_view, name='bookings_by_date'),
path('bookings', Views.table_view, name='bookings_by_date')
]
195 changes: 101 additions & 94 deletions restaurant/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,103 +9,110 @@

logger = logging.getLogger(__name__)

def handler404(request, exception):
"""Resolve bad request path
Parameters
----------
request : Requests https://requests.readthedocs.io/en/latest/
"""
uri = request.get_full_path
logger.error('Bad Request URI path: %s', uri)
return render(request, 'error.html', {'uri': uri}, status=404)
class Views(object):

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 handler404(self, request, exception):
"""Resolve bad request path
Parameters
----------
request : Requests https://requests.readthedocs.io/en/latest/
"""
uri = request.get_full_path
logger.error('Bad Request URI path: %s', uri)
return render(request, 'error.html', {'uri': uri}, status=404)

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 table_view(self, request):
"""GET bookings by date request parameter
Parameters
----------
request : Requests
"""
date = request.GET.get("date", datetime.today().date())
return self.__find_bookings_by_date(self, 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 bookings_view(self, 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 self.__find_bookings_by_date(self, date)

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 reservations_view(self, 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})

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']
@classmethod
def form_view(self, 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
})

0 comments on commit f3674eb

Please sign in to comment.