Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,91 +1,107 @@
|
|
| 1 |
-
from
|
| 2 |
-
from
|
| 3 |
-
|
| 4 |
-
from
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
return
|
| 37 |
-
except Exception as e:
|
| 38 |
-
print(f"Error
|
| 39 |
-
return None
|
| 40 |
-
|
| 41 |
-
# Fetch
|
| 42 |
-
def
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from fastapi.responses import FileResponse
|
| 3 |
+
from fastapi.templating import Jinja2Templates
|
| 4 |
+
from fastapi.staticfiles import StaticFiles
|
| 5 |
+
from starlette.requests import Request
|
| 6 |
+
from celery import Celery
|
| 7 |
+
import pandas as pd
|
| 8 |
+
from simple_salesforce import Salesforce
|
| 9 |
+
import os
|
| 10 |
+
|
| 11 |
+
# Salesforce credentials
|
| 12 |
+
SALESFORCE_USERNAME = "venkatramana@sandbox.com"
|
| 13 |
+
SALESFORCE_PASSWORD = "Seta12345@"
|
| 14 |
+
SALESFORCE_SECURITY_TOKEN = "ZOnDC5se0y3CACNjNSsBWQPl"
|
| 15 |
+
|
| 16 |
+
# Initialize FastAPI app
|
| 17 |
+
app = FastAPI()
|
| 18 |
+
|
| 19 |
+
# Initialize Celery
|
| 20 |
+
celery = Celery("app", broker="redis://localhost:6379/0")
|
| 21 |
+
|
| 22 |
+
# Set up Jinja2 templates
|
| 23 |
+
templates = Jinja2Templates(directory="templates")
|
| 24 |
+
|
| 25 |
+
# Serve static files (if needed)
|
| 26 |
+
app.mount("/static", StaticFiles(directory="static"), name="static")
|
| 27 |
+
|
| 28 |
+
# Salesforce connection
|
| 29 |
+
def connect_to_salesforce():
|
| 30 |
+
try:
|
| 31 |
+
sf = Salesforce(
|
| 32 |
+
username=SALESFORCE_USERNAME,
|
| 33 |
+
password=SALESFORCE_PASSWORD,
|
| 34 |
+
security_token=SALESFORCE_SECURITY_TOKEN,
|
| 35 |
+
)
|
| 36 |
+
return sf
|
| 37 |
+
except Exception as e:
|
| 38 |
+
print(f"Error connecting to Salesforce: {e}")
|
| 39 |
+
return None
|
| 40 |
+
|
| 41 |
+
# Fetch quote line items from Salesforce
|
| 42 |
+
def fetch_quote_line_items(sf):
|
| 43 |
+
try:
|
| 44 |
+
query = """
|
| 45 |
+
SELECT Id, Product__c, Quantity__c, Price_c__c
|
| 46 |
+
FROM Quote_Line_Item__c
|
| 47 |
+
LIMIT 50
|
| 48 |
+
"""
|
| 49 |
+
line_items = sf.query(query)
|
| 50 |
+
return line_items["records"]
|
| 51 |
+
except Exception as e:
|
| 52 |
+
print(f"Error fetching quote line items: {e}")
|
| 53 |
+
return None
|
| 54 |
+
|
| 55 |
+
# Fetch products and calculate totals
|
| 56 |
+
def fetch_products():
|
| 57 |
+
sf = connect_to_salesforce()
|
| 58 |
+
if not sf:
|
| 59 |
+
return []
|
| 60 |
+
|
| 61 |
+
line_items = fetch_quote_line_items(sf)
|
| 62 |
+
products = []
|
| 63 |
+
for item in line_items:
|
| 64 |
+
product = item.get("Product__c", "N/A")
|
| 65 |
+
quantity = item.get("Quantity__c", "N/A")
|
| 66 |
+
price = item.get("Price_c__c", "N/A")
|
| 67 |
+
total = float(quantity) * float(price) if quantity and price else "N/A"
|
| 68 |
+
products.append(
|
| 69 |
+
{
|
| 70 |
+
"Product": product,
|
| 71 |
+
"Quantity": quantity,
|
| 72 |
+
"Price": price,
|
| 73 |
+
"Total": total,
|
| 74 |
+
}
|
| 75 |
+
)
|
| 76 |
+
return products
|
| 77 |
+
|
| 78 |
+
# Save products to Excel
|
| 79 |
+
def save_to_excel(products, file_path):
|
| 80 |
+
df = pd.DataFrame(products)
|
| 81 |
+
df.to_excel(file_path, index=False)
|
| 82 |
+
|
| 83 |
+
# Celery task for Excel generation
|
| 84 |
+
@celery.task
|
| 85 |
+
def generate_excel_task():
|
| 86 |
+
products = fetch_products()
|
| 87 |
+
file_path = "products_data.xlsx"
|
| 88 |
+
save_to_excel(products, file_path)
|
| 89 |
+
print(f"Excel file generated at {file_path}")
|
| 90 |
+
|
| 91 |
+
# FastAPI route: Home page
|
| 92 |
+
@app.get("/")
|
| 93 |
+
async def index(request: Request):
|
| 94 |
+
files = [f for f in os.listdir(".") if f.endswith(".xlsx")]
|
| 95 |
+
return templates.TemplateResponse("index.html", {"request": request, "files": files})
|
| 96 |
+
|
| 97 |
+
# FastAPI route: Download file
|
| 98 |
+
@app.get("/download/{filename}")
|
| 99 |
+
async def download_file(filename: str):
|
| 100 |
+
file_path = os.path.join(".", filename)
|
| 101 |
+
return FileResponse(file_path, media_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|
| 102 |
+
|
| 103 |
+
# FastAPI route: Generate Excel
|
| 104 |
+
@app.get("/generate_excel")
|
| 105 |
+
async def generate_excel():
|
| 106 |
+
generate_excel_task.delay() # Trigger Celery task
|
| 107 |
+
return {"message": "Excel generation started!"}
|