[31388f42] Feat: Persist drafts and enhance UI warnings
This commit introduces two key improvements to the Lead Engine:
1. **Persistent Email Drafts:**
- Adds a new function to .
- Modifies to save generated email replies directly to the column in the database, ensuring they persist across sessions.
- Removes the previous session-based state for drafts.
2. **Enhanced UI Visibility:**
- Adds a warning icon (⚠️) directly to the lead expander's title if a lead is flagged as low-quality, making it easier to spot.
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import streamlit as st
|
||||
import pandas as pd
|
||||
from db import get_leads, init_db, reset_lead
|
||||
from db import get_leads, init_db, reset_lead, update_lead_draft
|
||||
import json
|
||||
import re
|
||||
import os
|
||||
@@ -149,17 +149,19 @@ if not df.empty:
|
||||
|
||||
# --- DYNAMIC TITLE ---
|
||||
source_icon = "🌐" if row.get('source') == 'Website-Formular' else "🤝"
|
||||
title = f"{source_icon} {row.get('source', 'Lead')} | {date_str} | {row['company_name']}"
|
||||
status_icon = "✅" if row.get('status') == 'synced' else "🆕"
|
||||
|
||||
meta = {}
|
||||
if row.get('lead_metadata'):
|
||||
try: meta = json.loads(row['lead_metadata'])
|
||||
except: pass
|
||||
|
||||
quality_icon = "⚠️ " if meta.get('is_low_quality') else ""
|
||||
|
||||
title = f"{quality_icon}{status_icon} {source_icon} {row.get('source', 'Lead')} | {date_str} | {row['company_name']}"
|
||||
|
||||
with st.expander(title):
|
||||
# Metadata Parsing
|
||||
meta = {}
|
||||
if row.get('lead_metadata'):
|
||||
try: meta = json.loads(row['lead_metadata'])
|
||||
except: pass
|
||||
|
||||
# --- TOP SECTION: QUALITY WARNING ---
|
||||
# Now directly checks the metadata from DB, which is more reliable
|
||||
# The full warning message is still shown inside for clarity
|
||||
if meta.get('is_low_quality'):
|
||||
st.warning("⚠️ **Low Quality Lead detected** (Free-mail provider or missing company name). Please verify manually.")
|
||||
|
||||
@@ -238,10 +240,12 @@ if not df.empty:
|
||||
with st.spinner("Writing email..."):
|
||||
ce_data = enrichment.get('ce_data', {})
|
||||
draft = generate_email_draft(row.to_dict(), ce_data)
|
||||
st.session_state[f"draft_{row['id']}"] = draft
|
||||
update_lead_draft(row['id'], draft) # Save to DB
|
||||
st.rerun() # Rerun to display the new draft from DB
|
||||
|
||||
if f"draft_{row['id']}" in st.session_state:
|
||||
st.text_area("Email Entwurf", value=st.session_state[f"draft_{row['id']}"], height=400)
|
||||
# Always display the draft from the database if it exists
|
||||
if row.get('response_draft'):
|
||||
st.text_area("Email Entwurf", value=row['response_draft'], height=400)
|
||||
st.button("📋 Copy to Clipboard", key=f"copy_{row['id']}", on_click=lambda: st.write("Copy functionality simulated"))
|
||||
else:
|
||||
st.info("Sync with Company Explorer first to generate a response.")
|
||||
|
||||
@@ -127,6 +127,14 @@ def update_lead_status(lead_id, status, response_draft=None):
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
def update_lead_draft(lead_id, draft_text):
|
||||
"""Saves a generated email draft to the database."""
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
c = conn.cursor()
|
||||
c.execute('UPDATE leads SET response_draft = ? WHERE id = ?', (draft_text, lead_id))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
def reset_lead(lead_id):
|
||||
"""Resets a lead to 'new' status and clears enrichment data."""
|
||||
conn = sqlite3.connect(DB_PATH)
|
||||
|
||||
Reference in New Issue
Block a user