Fixed problem with no required period in guest online service response

This commit is contained in:
Semyon Yekhmenin 2019-11-16 09:38:33 +00:00 committed by d.kuzmenko
parent c725da6a9b
commit 8b656b9bb6

View File

@ -13,10 +13,43 @@ from utils.methods import get_user_ip
class CheckWhetherBookingAvailable(generics.GenericAPIView):
""" Checks which service to use if establishmend is managed by any """
_VALID_GUESTONLINE_PERIODS = {'lunch', 'dinner', 'afternoon', 'breakfast'}
permission_classes = (permissions.AllowAny,)
serializer_class = CheckBookingSerializer
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):
is_booking_available = False
establishment = get_object_or_404(Establishment, pk=kwargs['establishment_id'])
@ -24,12 +57,12 @@ class CheckWhetherBookingAvailable(generics.GenericAPIView):
date = request.query_params.get('date')
g_service = GuestonlineService()
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):
is_booking_available = True
service = l_service
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,
**g_service.get_certain_keys(request.query_params,
{'date', 'persons'})):
@ -41,7 +74,11 @@ class CheckWhetherBookingAvailable(generics.GenericAPIView):
'available': is_booking_available,
'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)
@ -97,8 +134,9 @@ class UpdatePendingBooking(generics.UpdateAPIView):
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',
}))
'email', 'phone', 'last_name', 'first_name',
'country_code', 'pending_booking_id',
}))
if isinstance(r, Response):
return r
if data.get('newsletter'):