Spaces:
Build error
Build error
| # 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 | |
| } |