Fixed problem with no required period in guest online service response
This commit is contained in:
parent
c725da6a9b
commit
8b656b9bb6
|
|
@ -13,10 +13,43 @@ from utils.methods import get_user_ip
|
||||||
class CheckWhetherBookingAvailable(generics.GenericAPIView):
|
class CheckWhetherBookingAvailable(generics.GenericAPIView):
|
||||||
""" Checks which service to use if establishmend is managed by any """
|
""" Checks which service to use if establishmend is managed by any """
|
||||||
|
|
||||||
|
_VALID_GUESTONLINE_PERIODS = {'lunch', 'dinner', 'afternoon', 'breakfast'}
|
||||||
|
|
||||||
permission_classes = (permissions.AllowAny,)
|
permission_classes = (permissions.AllowAny,)
|
||||||
serializer_class = CheckBookingSerializer
|
serializer_class = CheckBookingSerializer
|
||||||
pagination_class = None
|
pagination_class = None
|
||||||
|
|
||||||
|
def _fill_period_template(self, period_template, period_name):
|
||||||
|
period_template_copy = period_template.copy()
|
||||||
|
period_template_copy['period'] = period_name
|
||||||
|
return period_template_copy
|
||||||
|
|
||||||
|
def _preprocess_guestonline_response(self, response):
|
||||||
|
periods = response['periods']
|
||||||
|
periods_by_name = {period['period']: period for period in periods if 'period' in period}
|
||||||
|
if not periods_by_name:
|
||||||
|
raise ValueError('Empty guestonline response')
|
||||||
|
|
||||||
|
period_template = iter(periods_by_name.values()).__next__().copy()
|
||||||
|
period_template.pop('total_left_seats')
|
||||||
|
period_template.pop('hours')
|
||||||
|
period_template.pop('period')
|
||||||
|
|
||||||
|
processed_periods = [
|
||||||
|
periods_by_name[period_name]
|
||||||
|
if period_name in periods_by_name
|
||||||
|
else self._fill_period_template(period_template, period_name)
|
||||||
|
for period_name in CheckWhetherBookingAvailable._VALID_GUESTONLINE_PERIODS
|
||||||
|
]
|
||||||
|
|
||||||
|
unnamed_periods = filter(lambda period: 'period' not in period, periods)
|
||||||
|
for unnamed_period in unnamed_periods:
|
||||||
|
processed_periods.append(unnamed_period)
|
||||||
|
|
||||||
|
response['periods'] = processed_periods
|
||||||
|
|
||||||
|
return response
|
||||||
|
|
||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
is_booking_available = False
|
is_booking_available = False
|
||||||
establishment = get_object_or_404(Establishment, pk=kwargs['establishment_id'])
|
establishment = get_object_or_404(Establishment, pk=kwargs['establishment_id'])
|
||||||
|
|
@ -24,12 +57,12 @@ class CheckWhetherBookingAvailable(generics.GenericAPIView):
|
||||||
date = request.query_params.get('date')
|
date = request.query_params.get('date')
|
||||||
g_service = GuestonlineService()
|
g_service = GuestonlineService()
|
||||||
l_service = LastableService()
|
l_service = LastableService()
|
||||||
if (not establishment.lastable_id is None) and l_service \
|
if establishment.lastable_id is not None and l_service \
|
||||||
.check_whether_booking_available(establishment.lastable_id, date):
|
.check_whether_booking_available(establishment.lastable_id, date):
|
||||||
is_booking_available = True
|
is_booking_available = True
|
||||||
service = l_service
|
service = l_service
|
||||||
service.service_id = establishment.lastable_id
|
service.service_id = establishment.lastable_id
|
||||||
elif (not establishment.guestonline_id is None) and g_service \
|
elif establishment.guestonline_id is not None and g_service \
|
||||||
.check_whether_booking_available(establishment.guestonline_id,
|
.check_whether_booking_available(establishment.guestonline_id,
|
||||||
**g_service.get_certain_keys(request.query_params,
|
**g_service.get_certain_keys(request.query_params,
|
||||||
{'date', 'persons'})):
|
{'date', 'persons'})):
|
||||||
|
|
@ -41,7 +74,11 @@ class CheckWhetherBookingAvailable(generics.GenericAPIView):
|
||||||
'available': is_booking_available,
|
'available': is_booking_available,
|
||||||
'type': service.service if service else None,
|
'type': service.service if service else None,
|
||||||
}
|
}
|
||||||
response.update({'details': service.response} if service and service.response else {})
|
|
||||||
|
service_response = self._preprocess_guestonline_response(service.response) \
|
||||||
|
if establishment.guestonline_id is not None \
|
||||||
|
else service.response
|
||||||
|
response.update({'details': service_response} if service and service.response else {})
|
||||||
return Response(data=response, status=200)
|
return Response(data=response, status=200)
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -97,8 +134,9 @@ class UpdatePendingBooking(generics.UpdateAPIView):
|
||||||
r = service.update_booking(service.get_certain_keys(data, {
|
r = service.update_booking(service.get_certain_keys(data, {
|
||||||
'email', 'phone', 'last_name', 'first_name', 'country_code', 'pending_booking_id', 'note',
|
'email', 'phone', 'last_name', 'first_name', 'country_code', 'pending_booking_id', 'note',
|
||||||
}, {
|
}, {
|
||||||
'email', 'phone', 'last_name', 'first_name', 'country_code', 'pending_booking_id',
|
'email', 'phone', 'last_name', 'first_name',
|
||||||
}))
|
'country_code', 'pending_booking_id',
|
||||||
|
}))
|
||||||
if isinstance(r, Response):
|
if isinstance(r, Response):
|
||||||
return r
|
return r
|
||||||
if data.get('newsletter'):
|
if data.get('newsletter'):
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user