Commit e035cbd7 by ajil.k

updateded

parent f05403dc
...@@ -16,15 +16,18 @@ def create_excel(app_api, collection): ...@@ -16,15 +16,18 @@ def create_excel(app_api, collection):
} }
} }
] ]
records = list(collection.aggregate(pipeline)) records = list(collection.aggregate(pipeline))
# Create an Excel Workbook and Worksheet # Create an Excel Workbook and Worksheet
wb = openpyxl.Workbook() wb = openpyxl.Workbook()
ws = wb.active ws = wb.active
# Write column headers to Excel Worksheet # Write column headers to Excel Worksheet
column_headers = list(records[0].keys()) column_headers = list(records[0].keys())
for i, header in enumerate(column_headers): for i, header in enumerate(column_headers):
ws.cell(row=1, column=i + 1, value=header) ws.cell(row=1, column=i + 1, value=header)
# Write data from MongoDB to Excel Worksheet # Write data from MongoDB to Excel Worksheet
for i, record in enumerate(records): for i, record in enumerate(records):
for j, key in enumerate(record.keys()): for j, key in enumerate(record.keys()):
......
...@@ -8,14 +8,18 @@ def upload_and_insert_into_db(app_api, collection): ...@@ -8,14 +8,18 @@ def upload_and_insert_into_db(app_api, collection):
async def create_upload_file(file: UploadFile): async def create_upload_file(file: UploadFile):
data_frame = file.file.read() data_frame = file.file.read()
data_frame = data_frame.decode() data_frame = data_frame.decode()
uploaded_path = "temp/uploaded.json" uploaded_path = "temp/uploaded.json"
with open(uploaded_path, "w") as json_file: with open(uploaded_path, "w") as json_file:
# Write data to the file # Write data to the file
json_file.write(data_frame) json_file.write(data_frame)
with open(uploaded_path, "r") as uploaded_json: with open(uploaded_path, "r") as uploaded_json:
# Load the data from the file # Load the data from the file
data = json.load(uploaded_json) data = json.load(uploaded_json)
inserted = insert_data_to_mongodb(collection, data) inserted = insert_data_to_mongodb(collection, data)
if inserted: if inserted:
print("Data inserted to DB") print("Data inserted to DB")
return {"filename": file.filename} return {"filename": file.filename}
# Importing Libraries
from fastapi import FastAPI from fastapi import FastAPI
from scripts.constants.db_connection import db_connect from scripts.constants.db_connection import db_connect
from scripts.core.handlers.aggregate_operations import violated_companies, no_violated_companies from scripts.core.handlers.aggregate_operations import violated_companies, no_violated_companies
......
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