Spaces:
Build error
Build error
File size: 7,557 Bytes
971b0ff cf6b61d 971b0ff cf6b61d 971b0ff cf6b61d 971b0ff cf6b61d 971b0ff cf6b61d 971b0ff cf6b61d 971b0ff cf6b61d 971b0ff cf6b61d 971b0ff cf6b61d |
1 2 3 4 5 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 37 38 39 40 41 42 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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# profile_management.py
# Filepath: ai-email-assistant/profile_management.py
# Handles sender and receiver profile management
import gradio as gr
from google_sheets_utils import (
get_profiles_from_sheet,
save_profile,
delete_profile_from_sheet,
update_profile_in_sheet,
generate_profile_id,
)
import logging
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
def create_profile_management_interface(spreadsheet, profile_type, current_profile):
with gr.Blocks() as profile_management_block:
gr.Markdown(f"## {profile_type} Management")
prefix = "s_" if profile_type == "Sender Profiles" else "r_"
name = gr.Textbox(label="Name", elem_id=f"{prefix}name")
email = gr.Textbox(label="Email", elem_id=f"{prefix}email")
position = gr.Textbox(label="Position", elem_id=f"{prefix}position")
company = gr.Textbox(label="Company", elem_id=f"{prefix}company")
context = gr.Dropdown(["Professional", "Personal", "Other"], label="Context", allow_custom_value=True, elem_id=f"{prefix}context")
profile_id = gr.Textbox(label="Profile ID", interactive=False, elem_id=f"{prefix}profile_id")
worksheet = spreadsheet.worksheet(profile_type)
headers = ["Type", "ID", "Name", "Email", "Position", "Company", "Context"]
profiles_list = gr.Dataframe(
headers=headers,
label=f"{profile_type} List",
interactive=False,
elem_id=f"{prefix}profiles_list"
)
add_btn = gr.Button("Add New Profile", elem_id=f"{prefix}add_btn")
update_btn = gr.Button("Update Existing Profile", elem_id=f"{prefix}update_btn")
delete_btn = gr.Button("Delete Profile", elem_id=f"{prefix}delete_btn")
clear_btn = gr.Button("Clear Fields", elem_id=f"{prefix}clear_btn")
message_output = gr.HTML(elem_id=f"{prefix}message_output")
def refresh_profiles():
profiles = get_profiles_from_sheet(worksheet)
return [[profile.get(h, "") for h in headers] for profile in profiles]
def add_profile(name, email, position, company, context):
# Check for empty fields
if not name or not email or not position or not company or not context:
gr.Error("All fields are required. Please fill in all details before saving.")
return refresh_profiles()
new_id = generate_profile_id()
profile_data = [
profile_type, # Type
new_id, # ID
name,
email,
position,
company,
context
]
result = save_profile(worksheet, profile_data)
message = gr.Info("Profile added successfully.") if result else gr.Error("Failed to add profile.")
return gr.update(), message, refresh_profiles()
def update_profile(profile_id, name, email, position, company, context):
# Check for empty fields
if not profile_id:
return gr.update(), gr.Error("No profile selected. Please select a profile to update."), refresh_profiles()
if not name or not email or not position or not company or not context:
return gr.update(), gr.Error("All fields are required. Please fill in all details before updating."), refresh_profiles()
updated_data = [
profile_type, # Type
profile_id,
name,
email,
position,
company,
context
]
result = update_profile_in_sheet(worksheet, profile_id, updated_data)
message = gr.Info("Profile updated successfully.") if result else gr.Error("Failed to update profile.")
return gr.update(), message, refresh_profiles()
def delete_profile(profile_id):
result = delete_profile_from_sheet(worksheet, profile_id)
gr.Info("Profile deleted successfully" if result else "Failed to delete profile")
return refresh_profiles()
def clear_fields():
return [""] * 6 # Clear all 6 fields
def load_profile(evt: gr.SelectData, current_name, current_email, current_position,
current_company, current_context, current_profile_id):
profiles = get_profiles_from_sheet(worksheet)
selected_row = evt.index[0]
selected_profile = profiles[selected_row]
logger.debug(f"Selected {profile_type} Profile: {selected_profile}")
# Check if ANY field (including profile_id) is non-empty
fields = [current_name, current_email, current_position, current_company, current_context, current_profile_id]
non_empty_fields = any(field and field.strip() != "" for field in fields)
logger.debug(f"Any {profile_type} field non-empty: {non_empty_fields}")
if non_empty_fields:
logger.warning(f"{profile_type} fields not empty, showing warning")
gr.Warning(f"Please clear all fields before loading a {profile_type[:-1]} profile.")
return [gr.update() for _ in range(6)] # Don't update any fields
logger.info(f"Loading selected {profile_type} profile")
try:
return (
selected_profile["Name"],
selected_profile["Email"],
selected_profile["Position"],
selected_profile["Company"],
selected_profile["Context"],
selected_profile["ID"]
)
except KeyError as e:
logger.error(f"Error loading profile: missing key {e}")
gr.Error(f"Error loading profile: missing information")
return [gr.update() for _ in range(6)]
# Attach event handlers
add_btn.click(
add_profile,
inputs=[name, email, position, company, context],
outputs=[message_output, profiles_list]
)
update_btn.click(
update_profile,
inputs=[profile_id, name, email, position, company, context],
outputs=[message_output, profiles_list]
)
delete_btn.click(
delete_profile,
inputs=[profile_id],
outputs=[profiles_list]
)
clear_btn.click(clear_fields, outputs=[name, email, position, company, context, profile_id])
profiles_list.select(
load_profile,
inputs=[name, email, position, company, context, profile_id],
outputs=[name, email, position, company, context, profile_id]
)
# Refresh profiles on component creation
profiles_list.value = refresh_profiles()
# Return a dictionary containing the block and required components/functions
return {
"block": profile_management_block,
"name": name,
"email": email,
"position": position,
"company": company,
"context": context,
"profile_id": profile_id,
"profiles_list": profiles_list,
"refresh_profiles": refresh_profiles,
"load_profile": load_profile, # Add other components/functions as needed
} |