-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail_alert.py
More file actions
93 lines (80 loc) · 3.59 KB
/
email_alert.py
File metadata and controls
93 lines (80 loc) · 3.59 KB
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
import streamlit as st
import json
import os
# Path to the JSON file
from dotenv import load_dotenv
contact_file = os.getenv("CONTACT_LIST") #'data/contact.json'
print(contact_file)
# Initial data to be saved if the file doesn't exist
initial_data = {
"Maintainers": [],
"Administration": [],
"Management": []
}
# Load existing data from contact.json if it exists, otherwise create it
def load_contacts():
if os.path.exists(contact_file):
with open(contact_file, 'r') as json_file:
contacts = json.load(json_file)
# Convert existing string entries to dictionaries if needed
if isinstance(contacts.get("Maintainers", []), list):
updated_maintainers = []
for item in contacts["Maintainers"]:
if isinstance(item, str): # Old format (just an email string)
updated_maintainers.append({"name": "", "phone": "", "email": item})
elif isinstance(item, dict): # New format (dict with name, phone, email)
updated_maintainers.append(item)
contacts["Maintainers"] = updated_maintainers
return contacts
else:
# Save initial data to file if it doesn't exist
save_contacts(initial_data)
return initial_data
# Save data to contact.json
def save_contacts(contacts):
with open(contact_file, 'w') as json_file:
json.dump(contacts, json_file, indent=4)
# Initialize contacts dictionary
contacts = load_contacts()
# Title of the app
st.title("Email And Contact Alert For Maintenance")
# Function to add a maintainer to the category
def add_maintainer(name, phone, email):
email = email.strip()
name = name.strip()
phone = phone.strip()
if email and not any(contact['email'] == email for contact in contacts["Maintainers"]):
contacts["Maintainers"].append({"name": name, "phone": phone, "email": email})
save_contacts(contacts)
# Function to remove a maintainer from the category
def remove_maintainer(email):
email = email.strip()
contacts["Maintainers"] = [contact for contact in contacts["Maintainers"] if contact['email'] != email]
save_contacts(contacts)
# Input and buttons for Maintainers
st.header("Maintainers")
# Create columns for name, phone, and email input
col1, col2, col3 = st.columns(3)
with col1:
maintainer_name = st.text_input("Enter maintainer's name:")
with col2:
maintainer_phone = st.text_input("Enter maintainer's phone number:")
with col3:
maintainer_email = st.text_input("Enter maintainer's email address:")
if st.button("Add to Maintainers"):
add_maintainer(maintainer_name, maintainer_phone, maintainer_email)
st.success(f"Added: {maintainer_name} ({maintainer_email})")
st.experimental_rerun() # Clear input after adding
# Dropdown and button to remove a maintainer
maintainer_to_remove = st.selectbox("Select a maintainer to remove:",
[f"{contact['name']} ({contact['email']})" for contact in contacts["Maintainers"] if isinstance(contact, dict)])
if st.button("Remove from Maintainers"):
email_to_remove = maintainer_to_remove.split('(')[-1][:-1] # Extract email from selection
remove_maintainer(email_to_remove)
st.success(f"Removed: {maintainer_to_remove}")
st.experimental_rerun()
# Display the current maintainers
st.write(f"**Current Maintainers ({len(contacts['Maintainers'])}):**")
for contact in contacts["Maintainers"]:
if isinstance(contact, dict):
st.markdown(f"- **Name**: {contact['name']} | **Phone**: {contact['phone']} | **Email**: {contact['email']}")