Spaces:
Runtime error
Runtime error
Update app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172731 unmodified line75767778798081828384858687889 unmodified lines9899100101102from fastapi import FastAPI, Requestfrom fastapi.responses import FileResponsefrom fastapi.templating import Jinja2Templatesfrom celery import Celeryimport pandas as pdfrom simple_salesforce import Salesforceimport os# Salesforce credentialsSALESFORCE_USERNAME = "venkatramana@sandbox.com"SALESFORCE_PASSWORD = "Seta12345@"SALESFORCE_SECURITY_TOKEN = "ZOnDC5se0y3CACNjNSsBWQPl"# Initialize FastAPI appapp = FastAPI()# Initialize Celerycelery = Celery("app", broker="redis://localhost:6379/0")# Set up Jinja2 templatestemplates = Jinja2Templates(directory="templates")# Salesforce connectiondef connect_to_salesforce():try:sf = Salesforce(username=SALESFORCE_USERNAME,password=SALESFORCE_PASSWORD,security_token=SALESFORCE_SECURITY_TOKEN,)return sfexcept Exception as e:print(f"Error connecting to Salesforce: {e}")return None# Fetch quote line items from Salesforcedef fetch_quote_line_items(sf):try:query = """SELECT Id, Product__c, Quantity__c, Price_c__cFROM Quote_Line_Item__cLIMIT 50"""line_items = sf.query(query)return line_items["records"]except Exception as e:print(f"Error fetching quote line items: {e}")return None# Fetch products and calculate totalsdef fetch_products():sf = connect_to_salesforce()if not sf:return []line_items = fetch_quote_line_items(sf)products = []for item in line_items:product = item.get("Product__c", "N/A")quantity = item.get("Quantity__c", "N/A")price = item.get("Price_c__c", "N/A")total = float(quantity) * float(price) if quantity and price else "N/A"products.append({"Product": product,"Quantity": quantity,"Price": price,"Total": total,})return products# Save products to Excel1 unmodified linedf = pd.DataFrame(products)df.to_excel(file_path, index=False)# Celery task for Excel generation@celery.taskdef generate_excel_task():products = fetch_products()file_path = "products_data.xlsx"save_to_excel(products, file_path)print(f"Excel file generated at {file_path}")# FastAPI route: Home page@app.get("/")async def index(request: Request):9 unmodified lines# FastAPI route: Generate Excel@app.get("/generate_excel")async def generate_excel():generate_excel_task.delay() # Trigger Celery taskreturn {"message": "Excel generation started!"}1234567891011121314151617181920211 unmodified line2324252627289 unmodified lines38394041424344from fastapi import FastAPI, Requestfrom fastapi.responses import FileResponsefrom fastapi.templating import Jinja2Templatesimport pandas as pdimport os# Initialize FastAPI appapp = FastAPI()# Set up Jinja2 templatestemplates = Jinja2Templates(directory="templates")# Dummy function to simulate fetching productsdef fetch_products():products = [{"Product": "Product A", "Quantity": 10, "Price": 20.5, "Total": 205.0},{"Product": "Product B", "Quantity": 5, "Price": 15.0, "Total": 75.0},]return products# Save products to Excel1 unmodified linedf = pd.DataFrame(products)df.to_excel(file_path, index=False)# FastAPI route: Home page@app.get("/")async def index(request: Request):9 unmodified lines# FastAPI route: Generate Excel@app.get("/generate_excel")async def generate_excel():products = fetch_products()file_path = "products_data.xlsx"save_to_excel(products, file_path)return {"message": f"Excel file generated: {file_path}"}
app.py+9-67