lokeshloki143 commited on
Commit
5cfa01e
·
verified ·
1 Parent(s): 8546d53

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -5
app.py CHANGED
@@ -10,6 +10,10 @@ from io import BytesIO
10
  from simple_salesforce import Salesforce
11
  from sendgrid import SendGridAPIClient
12
  from sendgrid.helpers.mail import Mail
 
 
 
 
13
 
14
  # Set up logging to capture errors and debug information
15
  logging.basicConfig(level=logging.INFO)
@@ -24,18 +28,20 @@ SF_SECURITY_TOKEN = os.getenv("SF_SECURITY_TOKEN", "NbUKcTx45azba5HEdntE9YAh")
24
  SF_DOMAIN = os.getenv("SF_DOMAIN", "login")
25
  SENDGRID_API_KEY = os.getenv("SENDGRID_API_KEY")
26
 
27
- # Validate environment variables
28
  required_vars = {
29
  'SF_USERNAME': SF_USERNAME,
30
  'SF_PASSWORD': SF_PASSWORD,
31
- 'SF_SECURITY_TOKEN': SF_SECURITY_TOKEN,
32
- 'SENDGRID_API_KEY': SENDGRID_API_KEY
33
  }
34
  missing_vars = [var for var, value in required_vars.items() if not value]
35
  if missing_vars:
36
  logger.error(f"Missing required environment variables: {', '.join(missing_vars)}")
37
  raise ValueError(f"Missing required environment variables: {', '.join(missing_vars)}")
38
 
 
 
 
39
  # Connect to Salesforce
40
  try:
41
  sf = Salesforce(
@@ -311,6 +317,10 @@ def generate_pdf(vendor_scores):
311
 
312
  def send_email_alert(vendor_scores, pdf_urls):
313
  """Send email alerts to vendors with low scores using SendGrid."""
 
 
 
 
314
  sg = SendGridAPIClient(SENDGRID_API_KEY)
315
  for scores in vendor_scores:
316
  if not scores['alert_flag']:
@@ -402,7 +412,7 @@ def get_dashboard():
402
  vendor_data = fetch_vendor_logs()
403
  if not vendor_data:
404
  logger.warning("No vendor logs found")
405
- last_updated = "12:32 PM IST on Monday, May 19, 2025"
406
  return render_template('index.html', processed_logs=[], last_updated=last_updated)
407
 
408
  vendor_scores = calculate_scores(vendor_data)
@@ -444,7 +454,7 @@ def get_dashboard():
444
  vendor_logs.append(log_entry)
445
 
446
  logger.info(f"Rendering dashboard with {len(processed_logs)} processed logs")
447
- last_updated = "12:32 PM IST on Monday, May 19, 2025"
448
  return render_template('index.html', processed_logs=processed_logs, last_updated=last_updated)
449
  except Exception as e:
450
  logger.error(f"Error in / endpoint: {str(e)}")
 
10
  from simple_salesforce import Salesforce
11
  from sendgrid import SendGridAPIClient
12
  from sendgrid.helpers.mail import Mail
13
+ from dotenv import load_dotenv
14
+
15
+ # Load environment variables from .env file
16
+ load_dotenv()
17
 
18
  # Set up logging to capture errors and debug information
19
  logging.basicConfig(level=logging.INFO)
 
28
  SF_DOMAIN = os.getenv("SF_DOMAIN", "login")
29
  SENDGRID_API_KEY = os.getenv("SENDGRID_API_KEY")
30
 
31
+ # Validate required environment variables (exclude SENDGRID_API_KEY)
32
  required_vars = {
33
  'SF_USERNAME': SF_USERNAME,
34
  'SF_PASSWORD': SF_PASSWORD,
35
+ 'SF_SECURITY_TOKEN': SF_SECURITY_TOKEN
 
36
  }
37
  missing_vars = [var for var, value in required_vars.items() if not value]
38
  if missing_vars:
39
  logger.error(f"Missing required environment variables: {', '.join(missing_vars)}")
40
  raise ValueError(f"Missing required environment variables: {', '.join(missing_vars)}")
41
 
42
+ if not SENDGRID_API_KEY:
43
+ logger.warning("SENDGRID_API_KEY not set. Email alerts will be disabled.")
44
+
45
  # Connect to Salesforce
46
  try:
47
  sf = Salesforce(
 
317
 
318
  def send_email_alert(vendor_scores, pdf_urls):
319
  """Send email alerts to vendors with low scores using SendGrid."""
320
+ if not SENDGRID_API_KEY:
321
+ logger.info("Skipping email alerts due to missing SENDGRID_API_KEY")
322
+ return
323
+
324
  sg = SendGridAPIClient(SENDGRID_API_KEY)
325
  for scores in vendor_scores:
326
  if not scores['alert_flag']:
 
412
  vendor_data = fetch_vendor_logs()
413
  if not vendor_data:
414
  logger.warning("No vendor logs found")
415
+ last_updated = "12:43 PM IST on Monday, May 19, 2025"
416
  return render_template('index.html', processed_logs=[], last_updated=last_updated)
417
 
418
  vendor_scores = calculate_scores(vendor_data)
 
454
  vendor_logs.append(log_entry)
455
 
456
  logger.info(f"Rendering dashboard with {len(processed_logs)} processed logs")
457
+ last_updated = "12:43 PM IST on Monday, May 19, 2025"
458
  return render_template('index.html', processed_logs=processed_logs, last_updated=last_updated)
459
  except Exception as e:
460
  logger.error(f"Error in / endpoint: {str(e)}")