Implement a restaurant reservation system including a new model, website form, and controller logic.

This commit is contained in:
Alaguraj0361 2026-02-05 19:03:57 +05:30
parent d0ecc73453
commit 2fee146176
5 changed files with 35 additions and 6 deletions

View File

@ -1,6 +1,7 @@
from odoo import http, _
from odoo.http import request
import datetime
import pytz
class TableReservationController(http.Controller):
@ -24,8 +25,14 @@ class TableReservationController(http.Controller):
start_time_str = post.get('start_time') # Format: 2024-05-20T18:00
num_people = int(post.get('num_people', 1))
# Convert start_time to datetime object
start_time = datetime.datetime.strptime(start_time_str, '%Y-%m-%dT%H:%M')
# Convert start_time to datetime object and localize to restaurant timezone (Brampton)
restaurant_tz = pytz.timezone('America/Toronto')
local_start = datetime.datetime.strptime(start_time_str, '%Y-%m-%dT%H:%M')
local_start = restaurant_tz.localize(local_start)
# Convert to UTC for Odoo
start_time = local_start.astimezone(pytz.utc).replace(tzinfo=None)
# Standard duration of 1 hour for now
end_time = start_time + datetime.timedelta(hours=1)

View File

@ -1,6 +1,7 @@
from odoo import models, fields, api, _
from odoo.exceptions import ValidationError
from datetime import timedelta
import pytz
class RestaurantReservation(models.Model):
_name = 'restaurant.reservation'
@ -23,8 +24,8 @@ class RestaurantReservation(models.Model):
def _compute_whatsapp_url(self):
for rec in self:
if rec.phone:
msg = f"Hello {rec.customer_name}, your reservation {rec.name} for {rec.start_time.strftime('%I:%M %p')} is confirmed!"
if rec.phone and rec.customer_name and rec.start_time:
msg = f"Hello {rec.customer_name}, your reservation {rec.name or ''} for {rec.start_time.strftime('%I:%M %p')} is confirmed!"
rec.whatsapp_url = f"https://wa.me/{rec.phone}?text={msg.replace(' ', '%20')}"
else:
rec.whatsapp_url = False
@ -56,6 +57,24 @@ class RestaurantReservation(models.Model):
if overlap:
raise ValidationError(_('This table is already reserved for the selected time slot.'))
@api.constrains('start_time', 'end_time')
def _check_opening_hours(self):
restaurant_tz = pytz.timezone('America/Toronto')
for rec in self:
local_start = pytz.utc.localize(rec.start_time).astimezone(restaurant_tz)
local_end = pytz.utc.localize(rec.end_time).astimezone(restaurant_tz)
day = local_start.weekday() # 0=Mon, 6=Sun
time_start = local_start.hour + local_start.minute / 60.0
time_end = local_end.hour + local_end.minute / 60.0
if day in [6, 0, 1, 2, 3]: # Sun-Thu
if time_start < 12.0 or time_end > 21.0:
raise ValidationError(_('Reservations for Sunday - Thursday must be between 12:00 PM and 9:00 PM (Local Time).'))
else: # Fri-Sat
if time_start < 12.0 or time_end > 23.0:
raise ValidationError(_('Reservations for Friday & Saturday must be between 12:00 PM and 11:00 PM (Local Time).'))
@api.onchange('start_time')
def _onchange_start_time(self):
if self.start_time:

View File

@ -11,7 +11,10 @@
<div class="card-body p-5">
<div class="text-center mb-5">
<h2 class="display-4 fw-bold mb-2" style="color: #fecd4f;">Table Reservation</h2>
<p class="text-muted">Book your spot for an authentic South Indian dining experience.</p>
<p class="text-muted mb-1">Book your spot for an authentic South Indian dining experience.</p>
<div class="small fw-bold mb-0" style="color: #171422;">
<i class="fa fa-clock-o me-1"></i> Sun - Thu: 12pm - 9pm | Fri &amp; Sat: 12pm - 11pm
</div>
</div>
<t t-if="error">
@ -20,7 +23,7 @@
</div>
</t>
<form action="/reservation/submit" method="post" class="s_website_form">
<form action="/reservation/submit" method="post">
<input type="hidden" name="csrf_token" t-att-value="request.csrf_token()"/>
<div class="row g-4">