Commit 03834878 by ajil.k

updated integrated with all modules

parent ac5ae7e1
......@@ -23,8 +23,9 @@ class Tickets:
# add new booking details
try:
# Check if the data entered
# if 5 >= details.no_of_tickets > 0:
# raise HTTPException(status_code=400, detail="Can book only 5 tickets at a time.")
if 5 < details.no_of_tickets or details.no_of_tickets <= 0:
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"]:
raise HTTPException(status_code=400, detail="Invalid class type")
seats = details.seat_numbers.split(",")
......@@ -97,10 +98,12 @@ class Tickets:
cleaned_data_dict = clean_record(records)
# Use pandas to convert the list of dictionaries to a DataFrame
df = pd.DataFrame(cleaned_data_dict)
print("-------------------------------Booking Details-------------------------------")
print(df)
# Use pandas to save the DataFrame to an Excel file
df.to_excel('external/booking_details.xlsx', index=False)
# Creating excel writer object
writer = pd.ExcelWriter('external/booking_details.xlsx')
# Write dataframe to excel
df.to_excel(writer, index=False)
# save the Excel
writer.save()
except Exception as e:
logger.error(e)
......@@ -117,30 +120,17 @@ class Tickets:
except Exception as e:
logger.error(e)
def json_data(self):
@staticmethod
def json_data(dataframe):
try:
records = self.session.query(BookingDetails).all()
cleaned_data_dict = clean_record(records)
# Use pandas to convert the list of dictionaries to a DataFrame
df = pd.DataFrame(cleaned_data_dict)
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')}
dataframe["date_of_purchase"] = dataframe["date_of_purchase"].dt.strftime('%Y-%m-%d')
keys = [f"column{i}" for i in range(len(dataframe.columns))]
header_dict = {'header': dict(zip(keys, dataframe.columns))}
data = {"body": dataframe.to_dict(orient='records')}
json_dict = {**header_dict, **data}
# Create the JSON file
with open("external/bookings.json", "w") as json_file:
# Write data to the file
json.dump(json_dict, json_file, indent=4)
return json_dict
except Exception as 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
from scripts.core.handlers.get_all_seats import seat_availability
from scripts.database.RedisDB.db_connection import conn
from scripts.logging.logger import logger
import pandas as pd
# Create FastAPI instance
router = APIRouter()
......@@ -61,9 +62,10 @@ def generate_seats():
@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:
message = Tickets().json_data()
excel_file = pd.read_excel(await file.read())
message = Tickets().json_data(excel_file)
return message
except Exception as 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