import streamlit as st from backend.adapters.snowflake import get_current_user_name, get_session, resolve_env from backend.actions.ccpa_upload import upload_ccpa_emails_from_csv from backend.permissions import require_access session = get_session() require_access(session, "CCPA") st.title("CCPA Email Upload") st.markdown( """ Upload a CSV file containing emails and requested dates to the CCPA Fan List. Emails will be validated, lowercased, and stored in the database. **CSV format:** Must contain columns `email` and `requested_date`. Date format should be `MM/DD/YY`. If the same email appears multiple times, only the oldest date is kept. Existing entries are only updated if the new date is older. """ ) uploaded_file = st.file_uploader("Choose a CSV file", type=["csv"]) if st.button("Upload"): if uploaded_file is None: st.warning("Please upload a CSV file first.") else: session = get_session() user = get_current_user_name(session) schema = resolve_env(session) result = upload_ccpa_emails_from_csv(session, uploaded_file, user, schema) if result.errors: st.error( f"Upload rejected — {len(result.errors)} validation error(s). " "Fix all errors and re-upload." ) st.dataframe( {"Validation Errors": result.errors}, width="content", ) else: total_processed = result.inserted_count + result.updated_count st.success(f"Successfully processed {total_processed} email(s)") col1, col2, col3 = st.columns(3) with col1: st.metric("New emails inserted", result.inserted_count) with col2: st.metric("Existing emails updated", result.updated_count) with col3: st.metric("Skipped (date already older)", result.skipped_count)