Booking notes & subscriber
This commit is contained in:
parent
3628283105
commit
76841af575
|
|
@ -1,10 +1,13 @@
|
|||
from abc import ABC, abstractmethod
|
||||
import json
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
import requests
|
||||
from django.conf import settings
|
||||
from rest_framework import status
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from rest_framework import serializers, status
|
||||
from rest_framework.response import Response
|
||||
|
||||
import booking.models.models as models
|
||||
from rest_framework import serializers
|
||||
|
||||
|
||||
class AbstractBookingService(ABC):
|
||||
|
|
@ -24,8 +27,12 @@ class AbstractBookingService(ABC):
|
|||
self.url = settings.LASTABLE_SERVICE
|
||||
|
||||
@staticmethod
|
||||
def get_certain_keys(d: dict, keys_to_preserve: set) -> dict:
|
||||
def get_certain_keys(d: dict, keys_to_preserve: set, required=True) -> dict:
|
||||
""" Helper """
|
||||
if required:
|
||||
diff = keys_to_preserve - d.keys()
|
||||
if diff:
|
||||
raise serializers.ValidationError({field: _(f'This field is required') for field in diff})
|
||||
return {key: d[key] for key in d.keys() & keys_to_preserve}
|
||||
|
||||
@abstractmethod
|
||||
|
|
@ -81,8 +88,11 @@ class GuestonlineService(AbstractBookingService):
|
|||
|
||||
def commit_booking(self, payload, stripe_token = None):
|
||||
url = f'{self.url}v1/pending_bookings/{payload}/commit'
|
||||
r = requests.put(url, headers=self.get_common_headers(),
|
||||
data=json.dumps({'stripe_token': stripe_token} if stripe_token else None))
|
||||
if stripe_token:
|
||||
r = requests.put(url, headers=self.get_common_headers(),
|
||||
data=json.dumps({'stripe_token': stripe_token}))
|
||||
else:
|
||||
r = requests.put(url, headers=self.get_common_headers())
|
||||
self.response = json.loads(r.content)
|
||||
if status.is_success(r.status_code) and self.response is None:
|
||||
raise serializers.ValidationError(detail='Booking already committed.')
|
||||
|
|
@ -94,10 +104,13 @@ class GuestonlineService(AbstractBookingService):
|
|||
payload['lastname'] = payload.pop('last_name')
|
||||
payload['firstname'] = payload.pop('first_name')
|
||||
payload['mobile_phone'] = payload.pop('phone')
|
||||
payload['user_locale'] = payload.pop('country_code')
|
||||
headers = self.get_common_headers()
|
||||
r = requests.put(url, headers=headers, data=json.dumps({'contact_info': payload}))
|
||||
response = r.json()
|
||||
self.response = response.get('prepayment')
|
||||
if not status.is_success(r.status_code):
|
||||
return Response(status=r.status_code, data=response)
|
||||
return status.is_success(r.status_code)
|
||||
|
||||
def create_booking(self, payload):
|
||||
|
|
@ -106,7 +119,10 @@ class GuestonlineService(AbstractBookingService):
|
|||
payload['persons'] = payload.pop('booked_persons_number')
|
||||
payload['date'] = payload.pop('booking_date')
|
||||
r = requests.post(url, headers=self.get_common_headers(), data=json.dumps(payload))
|
||||
return json.loads(r.content)['id'] if status.is_success(r.status_code) else False
|
||||
if status.is_success(r.status_code):
|
||||
return json.loads(r.content)['id']
|
||||
else:
|
||||
return Response(status=r.status_code, data=r.json())
|
||||
|
||||
def cancel_booking(self, payload):
|
||||
url = f'{self.url}v1/pending_bookings/{payload}'
|
||||
|
|
|
|||
|
|
@ -6,6 +6,8 @@ from booking.models.models import Booking, GuestonlineService, LastableService
|
|||
from booking.serializers.web import (PendingBookingSerializer, UpdateBookingSerializer, GetBookingSerializer,
|
||||
CheckBookingSerializer, CommitBookingSerializer)
|
||||
from establishment.models import Establishment
|
||||
from notification.models import Subscriber
|
||||
from utils.methods import get_user_ip
|
||||
|
||||
|
||||
class CheckWhetherBookingAvailable(generics.GenericAPIView):
|
||||
|
|
@ -61,7 +63,9 @@ class CreatePendingBooking(generics.CreateAPIView):
|
|||
}
|
||||
data['pending_booking_id'] = service.create_booking(
|
||||
service.get_certain_keys(data.copy(), service_to_keys[data.get('type')]))
|
||||
if not data['pending_booking_id']:
|
||||
if isinstance(data['pending_booking_id'], Response):
|
||||
return data['pending_booking_id']
|
||||
elif not data['pending_booking_id']:
|
||||
return Response(status=status.HTTP_403_FORBIDDEN, data='Unable to create booking')
|
||||
data['booking_id'] = data['pending_booking_id'] if data.get('type') == Booking.LASTABLE else None
|
||||
serializer = self.get_serializer(data=data)
|
||||
|
|
@ -88,9 +92,15 @@ class UpdatePendingBooking(generics.UpdateAPIView):
|
|||
data = request.data.copy()
|
||||
service = Booking.get_service_by_type(instance.type)
|
||||
data['pending_booking_id'] = instance.pending_booking_id
|
||||
service.update_booking(service.get_certain_keys(data, {
|
||||
'email', 'phone', 'last_name', 'first_name', 'country_code', 'pending_booking_id',
|
||||
r = service.update_booking(service.get_certain_keys(data, {
|
||||
'email', 'phone', 'last_name', 'first_name', 'country_code', 'pending_booking_id', 'note',
|
||||
}))
|
||||
if isinstance(r, Response):
|
||||
return r
|
||||
if data.get('newsletter'):
|
||||
Subscriber.objects.make_subscriber(email=data['email'], country_code=data['country_code'],
|
||||
locale=request.locale, ip_address=get_user_ip(request),
|
||||
user=request.user)
|
||||
if service.response:
|
||||
# если есть предоплата, возвращаем фронту страйп-ключ для совершения оплаты и цену
|
||||
amount = service.response.get('amount')
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user