shaheerawan3 commited on
Commit
bf3081b
·
verified ·
1 Parent(s): 503a64f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +125 -80
app.py CHANGED
@@ -1,6 +1,6 @@
1
  import streamlit as st
2
  from cryptography.fernet import Fernet
3
- from datetime import datetime, timedelta
4
  import sqlite3
5
  import bcrypt
6
  import uuid
@@ -10,16 +10,18 @@ import logging
10
  import threading
11
  import schedule
12
  import time
13
- from dataclasses import dataclass
14
- from typing import Optional, Dict, List
15
- import json
 
 
16
 
17
  class EmailConfig:
18
- SMTP_SERVER = "smtp.gmail.com"
19
- SMTP_PORT = 465
20
- SMTP_USERNAME = "Mail" # Replace with your email
21
- SMTP_PASSWORD = "lxau wkjx pzhu krqs" # Replace with your password
22
- SENDER_EMAIL = "shaheerawan001@gmail.com" # Replace with sender email
23
 
24
  class Database:
25
  def __init__(self):
@@ -87,22 +89,14 @@ class DigitalHeir:
87
  def _check_accounts(self):
88
  conn = self.db.conn
89
  cursor = conn.cursor()
90
- cursor.execute("""
91
- SELECT * FROM social_accounts
92
- WHERE status = 'pending'
93
- """)
94
  accounts = cursor.fetchall()
95
 
96
  for account in accounts:
97
  days_inactive = (datetime.now() - datetime.fromisoformat(account[7])).days
98
- if days_inactive >= account[6]: # inactivity_threshold
99
  self._notify_heir(account)
100
-
101
- cursor.execute("""
102
- UPDATE social_accounts
103
- SET status = 'inherited'
104
- WHERE id = ?
105
- """, (account[0],))
106
  conn.commit()
107
 
108
  def _notify_heir(self, account):
@@ -112,12 +106,7 @@ class DigitalHeir:
112
 
113
  if heir:
114
  password = self.decrypt_data(account[4])
115
- self._send_email(
116
- heir[3], # heir email
117
- account[2], # platform
118
- account[3], # username
119
- password
120
- )
121
 
122
  def _send_email(self, recipient: str, platform: str, username: str, password: str):
123
  msg = MIMEText(f"""
@@ -149,17 +138,15 @@ def register_user(username: str, password: str, confirm_password: str):
149
  if password != confirm_password:
150
  st.error("Passwords do not match")
151
  return
152
-
153
  conn = app.db.conn
154
  cursor = conn.cursor()
155
 
156
- # Check if username exists
157
  cursor.execute("SELECT * FROM users WHERE username = ?", (username,))
158
  if cursor.fetchone():
159
  st.error("Username already exists")
160
  return
161
-
162
- # Hash password
163
  salt = bcrypt.gensalt()
164
  hashed_password = bcrypt.hashpw(password.encode('utf-8'), salt)
165
 
@@ -179,29 +166,11 @@ def register_user(username: str, password: str, confirm_password: str):
179
  except Exception as e:
180
  st.error(f"Registration failed: {str(e)}")
181
 
182
- def show_auth():
183
- tab1, tab2 = st.tabs(["Login", "Register"])
184
-
185
- with tab1:
186
- with st.form("login_form"):
187
- username = st.text_input("Username")
188
- password = st.text_input("Password", type="password")
189
- if st.form_submit_button("Login"):
190
- login_user(username, password)
191
-
192
- with tab2:
193
- with st.form("register_form"):
194
- new_username = st.text_input("Username")
195
- new_password = st.text_input("Password", type="password")
196
- confirm_password = st.text_input("Confirm Password", type="password")
197
- if st.form_submit_button("Register"):
198
- register_user(new_username, new_password, confirm_password)
199
-
200
  def login_user(username: str, password: str):
201
  if not username or not password:
202
  st.error("Username and password are required")
203
  return
204
-
205
  conn = app.db.conn
206
  cursor = conn.cursor()
207
 
@@ -219,18 +188,50 @@ def login_user(username: str, password: str):
219
  else:
220
  st.error("Invalid username or password")
221
 
222
- def show_account_management():
223
- st.title("Digital Asset Management")
 
 
224
 
225
- with st.form("add_account"):
226
- platform = st.selectbox("Platform", ["Facebook", "Twitter", "Instagram", "LinkedIn"])
227
- username = st.text_input("Username/Email")
228
- password = st.text_input("Password", type="password")
229
- heir = st.selectbox("Select Heir", get_user_heirs())
230
- inactive_days = st.number_input("Inactivity Period (days)", min_value=30, value=90)
231
-
232
- if st.form_submit_button("Add Account"):
233
- save_social_account(platform, username, password, heir, inactive_days)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
234
 
235
  def save_social_account(platform, username, password, heir, inactive_days):
236
  encrypted_password = app.encrypt_data(password)
@@ -259,25 +260,17 @@ def save_social_account(platform, username, password, heir, inactive_days):
259
  except Exception as e:
260
  st.error(f"Failed to save account: {str(e)}")
261
 
262
- def get_user_heirs():
263
- conn = app.db.conn
264
- cursor = conn.cursor()
265
- cursor.execute("""
266
- SELECT id, name, email, relationship
267
- FROM heirs
268
- WHERE user_id = ?
269
- """, (st.session_state.current_user['id'],))
270
 
271
- # Convert tuple to dictionary
272
- heirs = []
273
- for heir in cursor.fetchall():
274
- heirs.append({
275
- 'id': heir[0],
276
- 'name': heir[1],
277
- 'email': heir[2],
278
- 'relationship': heir[3]
279
- })
280
- return heirs
281
 
282
  def show_account_management():
283
  st.title("Digital Asset Management")
@@ -286,7 +279,7 @@ def show_account_management():
286
  if not heirs:
287
  st.warning("Please add heirs before adding accounts")
288
  return
289
-
290
  with st.form("add_account"):
291
  platform = st.selectbox("Platform", ["Facebook", "Twitter", "Instagram", "LinkedIn"])
292
  username = st.text_input("Username/Email")
@@ -299,13 +292,65 @@ def show_account_management():
299
  if st.form_submit_button("Add Account"):
300
  save_social_account(platform, username, password, heir, inactive_days)
301
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
302
  def main():
303
  st.set_page_config(page_title="Digital Inheritance System", layout="wide")
304
 
305
  if 'current_user' not in st.session_state:
306
  show_auth()
307
  else:
308
- show_account_management()
 
 
 
 
 
 
 
 
 
309
 
310
  app = DigitalHeir()
311
 
 
1
  import streamlit as st
2
  from cryptography.fernet import Fernet
3
+ from datetime import datetime
4
  import sqlite3
5
  import bcrypt
6
  import uuid
 
10
  import threading
11
  import schedule
12
  import time
13
+ from dotenv import load_dotenv
14
+ import os
15
+
16
+ # Load environment variables
17
+ load_dotenv()
18
 
19
  class EmailConfig:
20
+ SMTP_SERVER = os.getenv("SMTP_SERVER", "smtp.gmail.com")
21
+ SMTP_PORT = int(os.getenv("SMTP_PORT", "465"))
22
+ SMTP_USERNAME = os.getenv("SMTP_USERNAME")
23
+ SMTP_PASSWORD = os.getenv("SMTP_PASSWORD")
24
+ SENDER_EMAIL = os.getenv("SENDER_EMAIL")
25
 
26
  class Database:
27
  def __init__(self):
 
89
  def _check_accounts(self):
90
  conn = self.db.conn
91
  cursor = conn.cursor()
92
+ cursor.execute("SELECT * FROM social_accounts WHERE status = 'pending'")
 
 
 
93
  accounts = cursor.fetchall()
94
 
95
  for account in accounts:
96
  days_inactive = (datetime.now() - datetime.fromisoformat(account[7])).days
97
+ if days_inactive >= account[6]:
98
  self._notify_heir(account)
99
+ cursor.execute("UPDATE social_accounts SET status = 'inherited' WHERE id = ?", (account[0],))
 
 
 
 
 
100
  conn.commit()
101
 
102
  def _notify_heir(self, account):
 
106
 
107
  if heir:
108
  password = self.decrypt_data(account[4])
109
+ self._send_email(heir[3], account[2], account[3], password)
 
 
 
 
 
110
 
111
  def _send_email(self, recipient: str, platform: str, username: str, password: str):
112
  msg = MIMEText(f"""
 
138
  if password != confirm_password:
139
  st.error("Passwords do not match")
140
  return
141
+
142
  conn = app.db.conn
143
  cursor = conn.cursor()
144
 
 
145
  cursor.execute("SELECT * FROM users WHERE username = ?", (username,))
146
  if cursor.fetchone():
147
  st.error("Username already exists")
148
  return
149
+
 
150
  salt = bcrypt.gensalt()
151
  hashed_password = bcrypt.hashpw(password.encode('utf-8'), salt)
152
 
 
166
  except Exception as e:
167
  st.error(f"Registration failed: {str(e)}")
168
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169
  def login_user(username: str, password: str):
170
  if not username or not password:
171
  st.error("Username and password are required")
172
  return
173
+
174
  conn = app.db.conn
175
  cursor = conn.cursor()
176
 
 
188
  else:
189
  st.error("Invalid username or password")
190
 
191
+ def save_heir(name, email, relationship):
192
+ if not name or not email:
193
+ st.error("Name and email are required")
194
+ return
195
 
196
+ conn = app.db.conn
197
+ cursor = conn.cursor()
198
+
199
+ try:
200
+ cursor.execute("""
201
+ INSERT INTO heirs (id, user_id, name, email, relationship, created_at)
202
+ VALUES (?, ?, ?, ?, ?, ?)
203
+ """, (
204
+ str(uuid.uuid4()),
205
+ st.session_state.current_user['id'],
206
+ name,
207
+ email,
208
+ relationship,
209
+ datetime.now().isoformat()
210
+ ))
211
+ conn.commit()
212
+ st.success("Heir added successfully!")
213
+ st.rerun()
214
+ except Exception as e:
215
+ st.error(f"Failed to add heir: {str(e)}")
216
+
217
+ def get_user_heirs():
218
+ conn = app.db.conn
219
+ cursor = conn.cursor()
220
+ cursor.execute("""
221
+ SELECT id, name, email, relationship
222
+ FROM heirs
223
+ WHERE user_id = ?
224
+ """, (st.session_state.current_user['id'],))
225
+
226
+ return [
227
+ {
228
+ 'id': heir[0],
229
+ 'name': heir[1],
230
+ 'email': heir[2],
231
+ 'relationship': heir[3]
232
+ }
233
+ for heir in cursor.fetchall()
234
+ ]
235
 
236
  def save_social_account(platform, username, password, heir, inactive_days):
237
  encrypted_password = app.encrypt_data(password)
 
260
  except Exception as e:
261
  st.error(f"Failed to save account: {str(e)}")
262
 
263
+ def show_heir_management():
264
+ st.sidebar.title("Heir Management")
 
 
 
 
 
 
265
 
266
+ with st.sidebar.form("add_heir"):
267
+ heir_name = st.text_input("Heir Name")
268
+ heir_email = st.text_input("Heir Email")
269
+ heir_relationship = st.selectbox("Relationship",
270
+ ["Family", "Friend", "Legal Representative", "Other"])
271
+
272
+ if st.form_submit_button("Add Heir"):
273
+ save_heir(heir_name, heir_email, heir_relationship)
 
 
274
 
275
  def show_account_management():
276
  st.title("Digital Asset Management")
 
279
  if not heirs:
280
  st.warning("Please add heirs before adding accounts")
281
  return
282
+
283
  with st.form("add_account"):
284
  platform = st.selectbox("Platform", ["Facebook", "Twitter", "Instagram", "LinkedIn"])
285
  username = st.text_input("Username/Email")
 
292
  if st.form_submit_button("Add Account"):
293
  save_social_account(platform, username, password, heir, inactive_days)
294
 
295
+ def show_auth():
296
+ tab1, tab2 = st.tabs(["Login", "Register"])
297
+
298
+ with tab1:
299
+ with st.form("login_form"):
300
+ username = st.text_input("Username")
301
+ password = st.text_input("Password", type="password")
302
+ if st.form_submit_button("Login"):
303
+ login_user(username, password)
304
+
305
+ with tab2:
306
+ with st.form("register_form"):
307
+ new_username = st.text_input("Username")
308
+ new_password = st.text_input("Password", type="password")
309
+ confirm_password = st.text_input("Confirm Password", type="password")
310
+ if st.form_submit_button("Register"):
311
+ register_user(new_username, new_password, confirm_password)
312
+
313
+ def show_heirs_list():
314
+ heirs = get_user_heirs()
315
+
316
+ if not heirs:
317
+ st.info("No heirs added yet")
318
+ return
319
+
320
+ for heir in heirs:
321
+ with st.expander(f"{heir['name']} - {heir['relationship']}"):
322
+ st.write(f"Email: {heir['email']}")
323
+ if st.button("Remove", key=f"remove_{heir['id']}"):
324
+ remove_heir(heir['id'])
325
+
326
+ def remove_heir(heir_id):
327
+ conn = app.db.conn
328
+ cursor = conn.cursor()
329
+
330
+ try:
331
+ cursor.execute("DELETE FROM heirs WHERE id = ?", (heir_id,))
332
+ conn.commit()
333
+ st.success("Heir removed successfully!")
334
+ st.rerun()
335
+ except sqlite3.Error:
336
+ st.error("Error removing heir")
337
+
338
  def main():
339
  st.set_page_config(page_title="Digital Inheritance System", layout="wide")
340
 
341
  if 'current_user' not in st.session_state:
342
  show_auth()
343
  else:
344
+ st.title("Digital Inheritance System")
345
+ show_heir_management()
346
+
347
+ tab1, tab2 = st.tabs(["Manage Accounts", "View Heirs"])
348
+
349
+ with tab1:
350
+ show_account_management()
351
+
352
+ with tab2:
353
+ show_heirs_list()
354
 
355
  app = DigitalHeir()
356