Commit 03834878 by ajil.k

updated integrated with all modules

parent ac5ae7e1
...@@ -23,8 +23,9 @@ class Tickets: ...@@ -23,8 +23,9 @@ class Tickets:
# add new booking details # add new booking details
try: try:
# Check if the data entered # Check if the data entered
# if 5 >= details.no_of_tickets > 0: if 5 < details.no_of_tickets or details.no_of_tickets <= 0:
# raise HTTPException(status_code=400, detail="Can book only 5 tickets at a time.") raise HTTPException(status_code=400, detail="Invalid value for no. of tickets or"
" Can book only 5 tickets at a time.")
if details.preferred_class not in ["gold", "silver"]: if details.preferred_class not in ["gold", "silver"]:
raise HTTPException(status_code=400, detail="Invalid class type") raise HTTPException(status_code=400, detail="Invalid class type")
seats = details.seat_numbers.split(",") seats = details.seat_numbers.split(",")
...@@ -97,10 +98,12 @@ class Tickets: ...@@ -97,10 +98,12 @@ class Tickets:
cleaned_data_dict = clean_record(records) cleaned_data_dict = clean_record(records)
# Use pandas to convert the list of dictionaries to a DataFrame # Use pandas to convert the list of dictionaries to a DataFrame
df = pd.DataFrame(cleaned_data_dict) df = pd.DataFrame(cleaned_data_dict)
print("-------------------------------Booking Details-------------------------------") # Creating excel writer object
print(df) writer = pd.ExcelWriter('external/booking_details.xlsx')
# Use pandas to save the DataFrame to an Excel file # Write dataframe to excel
df.to_excel('external/booking_details.xlsx', index=False) df.to_excel(writer, index=False)
# save the Excel
writer.save()
except Exception as e: except Exception as e:
logger.error(e) logger.error(e)
...@@ -117,30 +120,17 @@ class Tickets: ...@@ -117,30 +120,17 @@ class Tickets:
except Exception as e: except Exception as e:
logger.error(e) logger.error(e)
def json_data(self): @staticmethod
def json_data(dataframe):
try: try:
records = self.session.query(BookingDetails).all() dataframe["date_of_purchase"] = dataframe["date_of_purchase"].dt.strftime('%Y-%m-%d')
cleaned_data_dict = clean_record(records) keys = [f"column{i}" for i in range(len(dataframe.columns))]
# Use pandas to convert the list of dictionaries to a DataFrame header_dict = {'header': dict(zip(keys, dataframe.columns))}
df = pd.DataFrame(cleaned_data_dict) data = {"body": dataframe.to_dict(orient='records')}
df["date_of_purchase"] = df["date_of_purchase"].apply(lambda x: x.strftime('%Y-%m-%d'))
# df = pd.read_excel(filename)
# Print number of rows and columns
# print("Number of rows: ", len(df))
# # Setting "header" dictionary
keys = []
column_names = df.columns.tolist()
for i in range(len(column_names)):
keys.append("column" + str(i))
header_dict = {'header': dict(zip(keys, column_names))}
# Setting 'body' dictionary
data = {"body": df.to_dict(orient='records')}
json_dict = {**header_dict, **data} json_dict = {**header_dict, **data}
# Create the JSON file
with open("external/bookings.json", "w") as json_file: with open("external/bookings.json", "w") as json_file:
# Write data to the file
json.dump(json_dict, json_file, indent=4) json.dump(json_dict, json_file, indent=4)
return json_dict return json_dict
except Exception as e: except Exception as e:
logger.exception(e) logger.exception(e)
print("Data insertion error-", e) print("Reading error-", e)
...@@ -7,6 +7,7 @@ from scripts.core.handlers.create_redis_db import redis_db_create ...@@ -7,6 +7,7 @@ from scripts.core.handlers.create_redis_db import redis_db_create
from scripts.core.handlers.get_all_seats import seat_availability from scripts.core.handlers.get_all_seats import seat_availability
from scripts.database.RedisDB.db_connection import conn from scripts.database.RedisDB.db_connection import conn
from scripts.logging.logger import logger from scripts.logging.logger import logger
import pandas as pd
# Create FastAPI instance # Create FastAPI instance
router = APIRouter() router = APIRouter()
...@@ -61,9 +62,10 @@ def generate_seats(): ...@@ -61,9 +62,10 @@ def generate_seats():
@router.post(EndPoints.booking_details_json, tags=["Create Json File"]) @router.post(EndPoints.booking_details_json, tags=["Create Json File"])
def booking_details_json_format(file: UploadFile): async def booking_details_json_format(file: UploadFile):
try: try:
message = Tickets().json_data() excel_file = pd.read_excel(await file.read())
message = Tickets().json_data(excel_file)
return message return message
except Exception as e: except Exception as e:
logger.error(e) logger.error(e)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment