diff --git a/Django_api/airline/api_common/views.py b/Django_api/airline/api_common/views.py index d34feee..7cb93dd 100644 --- a/Django_api/airline/api_common/views.py +++ b/Django_api/airline/api_common/views.py @@ -28,23 +28,24 @@ def post(self, request, *args, **kwargs): password = request.data.get('password') user = authenticate(username=username, password=password) if user: + user_id_bank = user.client.id token, created = Token.objects.get_or_create(user=user) + asyncio.run(self.banque_account_create(user_id_bank)) return Response({'token': token.key}) - self.banque_account_create(Client.user.id) else: return Response({'error': 'Invalid Credentials'}, status=status.HTTP_400_BAD_REQUEST) async def banque_account_create(self, user_id_bank): global nc env = os.getenv('DJANGO_ENVIRONMENT', 'development') - user = os.getenv('NATS_USER', '') - password = os.getenv('NATS_PASSWORD', '') + user = os.getenv('NATS_USER', 'user') + password = os.getenv('NATS_PASSWORD', 'password') if env == 'development': nc = await nats.connect("nats://localhost:4222") else: nc = await nats.connect("nats://nats:4222",user=user,password=password) try: - response = await nc.request(f"banque.creation",f"{request.data.get('username')}:1000",timeout=10) + response = await nc.request(f"banque.creation",f"{str(user_id_bank)}:1000",timeout=10) response_data = response.data.decode() data = response_data.split(",") status=data[0] @@ -329,8 +330,8 @@ class PaymentView(APIView): async def valid_payment(self, user_reserv, price_seat): global nc env = os.getenv('DJANGO_ENVIRONMENT', 'development') - user = os.getenv('NATS_USER', '') - password = os.getenv('NATS_PASSWORD', '') + user = os.getenv('NATS_USER', 'user') + password = os.getenv('NATS_PASSWORD', 'password') if env == 'development': nc = await nats.connect("nats://localhost:4222") else: @@ -338,7 +339,7 @@ async def valid_payment(self, user_reserv, price_seat): try: client = user_reserv if payment == "True": - response = await nc.request(f"banque.validation.{client}",str(price_seat),timeout=10) + response = await nc.request(f"banque.validation.{client}", str(price_seat), timeout=10) response_data = response.data.decode() data = response_data.split(",") payment=data[0] @@ -356,7 +357,7 @@ def post(self, request, *args, **kwargs): client = get_object_or_404(User, id=client_id) price_seat = booking.booking_type.price # Simulate payment process. In a real scenario, you would integrate with a payment gateway. - payment_successful = self.valid_payment(client_id,price_seat) # This should be replaced with actual payment verification logic + payment_successful = asyncio.run(self.valid_payment(client_id,price_seat)) # This should be replaced with actual payment verification logic if payment_successful: booking.status = 'confirmed' @@ -374,7 +375,6 @@ def post(self, request, *args, **kwargs): return Response({'status': 'Payment successful and booking confirmed'}, status=status.HTTP_200_OK) else: return Response({'error': 'Payment failed'}, status=status.HTTP_400_BAD_REQUEST) - # new-2 class PaymentGatewayListView(generics.ListCreateAPIView): diff --git a/Docker-test/Frontend/Django/populate_db_tables.sql b/Docker-test/Frontend/Django/populate_db_tables.sql index d66aa93..1b6f3d3 100644 --- a/Docker-test/Frontend/Django/populate_db_tables.sql +++ b/Docker-test/Frontend/Django/populate_db_tables.sql @@ -204,9 +204,9 @@ SELECT flight_number, departure::timestamp with time zone, arrival::timestamp wi -- Insert booking_types INSERT INTO booking_type (type, price) SELECT type, price FROM ( - SELECT 'Second Class' AS type, 100 AS price + SELECT 'Second Class' AS type, 100.00 AS price WHERE NOT EXISTS (SELECT 1 FROM booking_type WHERE type = 'Second Class') UNION ALL - SELECT 'First Class', 200 + SELECT 'First Class', 200.00 WHERE NOT EXISTS (SELECT 1 FROM booking_type WHERE type = 'First Class') ) AS subquery; \ No newline at end of file