Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
Better account system
Added Admin panel
Added Log system
  • Loading branch information
EndermanPC committed Feb 8, 2024
1 parent 4becb82 commit 28bfe9e
Show file tree
Hide file tree
Showing 26 changed files with 306 additions and 48 deletions.
1 change: 1 addition & 0 deletions acc.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python -m streamlit run account/main.py
1 change: 1 addition & 0 deletions acc.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python -m streamlit run account/manager.py
Binary file modified account/__pycache__/loader.cpython-312.pyc
Binary file not shown.
Binary file modified account/__pycache__/reliability.cpython-312.pyc
Binary file not shown.
Binary file added account/__pycache__/userid.cpython-312.pyc
Binary file not shown.
Binary file added account/__pycache__/username.cpython-312.pyc
Binary file not shown.
37 changes: 0 additions & 37 deletions account/create.py

This file was deleted.

2 changes: 1 addition & 1 deletion account/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ def create_users_database():
reliability INTGER DEFAULT 0
)''')
conn.commit()
conn.close()
conn.close()
2 changes: 2 additions & 0 deletions account/loader.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import sqlite3
from log.write import sys_log

def account_database_loader():
conn = sqlite3.connect('./database/users-account.db')
sys_log("Loaded", "users-account.db")
return conn
111 changes: 111 additions & 0 deletions account/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import smtplib
import time
import streamlit as st
from account.loader import account_database_loader
from account.reliability import get_user_reliability
from account.userid import get_user_id
from account.username import get_username
from log.write import sys_log

conn = account_database_loader()
cursor = conn.cursor()

def add_user(email, username, password):
cursor.execute('''INSERT INTO users (email, username, password) VALUES (?, ?, ?)''', (email, username, password))
sys_log("Created User Account", "Username: " + username + " Email: " + email + " Password: " + password)
conn.commit()

def update_password(user_id, email, new_password):
cursor.execute("UPDATE users SET password = ? WHERE id = ?", (new_password, user_id))
sys_log("Changed User Password", "Username: " + username + " User ID: " + str(user_id) + " Email: " + email + " Password: " + password)
conn.commit()

def update_username(user_id, email, new_username):
cursor.execute("UPDATE users SET username = ? WHERE id = ?", (new_username, user_id))
sys_log("Changed Username", "Username: " + username + " User ID: " + str(user_id) + " Email: " + email + " Password: " + password)
conn.commit()

def check_existing_email(email):
cursor.execute("SELECT * FROM users WHERE email=?", (email,))
return cursor.fetchone() is not None

def check_existing_username(username):
cursor.execute("SELECT * FROM users WHERE username=?", (username,))
return cursor.fetchone() is not None

st.title('Account Manager')

st.session_state.setdefault('form_state', True)
AForm = st.session_state.form_state

with st.form(key = 'Account_Form'):
col1, col2, col3 = st.columns([0.1, 0.1, 0.1])

if AForm:
with col1:
submitted1 = st.form_submit_button('Create')
with col2:
submitted2 = st.form_submit_button('Change')
with col3:
submitted3 = st.form_submit_button('Find')

if submitted1:
email = st.text_input('Email:')
username = st.text_input('Username:')
password = st.text_input('Password:', type='password')

if email and username and password:
if check_existing_email(email):
st.error('This email is already registered. Please use a different email.')
elif check_existing_username(username):
st.error('This user name already in use. Please use another username.')
else:
with st.spinner('Checking the given information...'):
add_user(email, username, password)
time.sleep(1)
st.success('Your account has been successfully created.')

if submitted2:
email = st.text_input('Email: ')
username = st.text_input('Username: ')
password = st.text_input('Password: ', type='password')

st.markdown('---')

if get_user_reliability(cursor, username, password) is not None:
user_id = get_user_id(cursor, username)
new_username = st.text_input('New Username: ')
new_password = st.text_input('New Password: ', type='password')
if new_username:
update_username(user_id, email, new_username)
st.success('Your account username has been successfully updated.')
if new_password:
update_password(user_id, email, new_password)
st.success('Your account password has been successfully updated.')
elif get_user_reliability(cursor, username, password) is None:
pass
else:
st.error("Couldn't find your account, please check again.")

close_button = st.form_submit_button('Close')

if close_button:
st.session_state.form_state = False

if submitted3:
username = st.text_input('Username: ')
user_id = st.text_input('User ID: ')

st.markdown('---')

if username and not user_id:
st.write("User ID: " + str(get_user_id(cursor, username)))
elif user_id and not username:
st.write("Username: " + get_username(cursor, user_id))

else:
open_button = st.form_submit_button('Open Form')
if open_button:
st.session_state.form_state = True


2 changes: 0 additions & 2 deletions account/reliability.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
from account.loader import account_database_loader

def get_user_reliability(cursor, username, password):
cursor.execute('SELECT password FROM users WHERE username = ?', (username,))
row = cursor.fetchone()
Expand Down
11 changes: 11 additions & 0 deletions account/userid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import sqlite3

def get_user_id(cursor, username):
cursor.execute("SELECT id FROM users WHERE username = ?", (username,))

rows = cursor.fetchall()

for row in rows:
userid = row[0]

return userid
11 changes: 11 additions & 0 deletions account/username.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import sqlite3

def get_username(cursor, user_id):
cursor.execute("SELECT username FROM users WHERE id = ?", (user_id,))

rows = cursor.fetchall()

for row in rows:
username = row[0]

return username
1 change: 1 addition & 0 deletions adpn.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python adpn.py
129 changes: 129 additions & 0 deletions adpn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import os
import shutil
import sqlite3
from account.userid import get_user_id
from account.username import get_username
from log.write import log, sys_log
from account.loader import account_database_loader
from account.reliability import get_user_reliability

print('Welcome to MonoSearch Administrator Panel')

def compare_databases():
try:
conn1 = sqlite3.connect("./database/search-index.db")
conn2 = sqlite3.connect("./database/censorship.db")

cur1 = conn1.cursor()
cur2 = conn2.cursor()

table_name = "information"

cur1.execute(f"SELECT * FROM {table_name}")
cur2.execute(f"SELECT * FROM {table_name}")

data1 = cur1.fetchall()
data2 = cur2.fetchall()

set1 = set(data1)
set2 = set(data2)

different_list = list(set2 - set1)

result = {
"Censorship:": different_list,
"Search Index:": list(set1-set2)
}

print(result)

conn1.close()
conn2.close()

if different_list:
return False
else:
return True
except Exception as e:
print("Error comparing databases:", str(e))
return False

def synchronization_databases():
try:
if not compare_databases():
print("Databases cannot be synchronized when there are differences between them.")
return
else:
os.remove("./database/censorship.db")
shutil.copy("./database/search-index.db", "./database/censorship.db")
print("Synchronization successful.")
except Exception as e:
print("Error synchronizing databases:", str(e))

account_conn = account_database_loader()
account_cursor = account_conn.cursor()

def list_users_database():
account_cursor.execute("SELECT * FROM users")

rows = account_cursor.fetchall()

for row in rows:
print("ID:", row[0])
print("Email:", row[1])
print("Username:", row[2])
print("Password:", row[3])
print("Reliability:", row[4])
print("--------------------")

def get_reliability_from_id(user_id):
account_cursor.execute("SELECT reliability FROM users WHERE id = ?", (user_id,))

rows = account_cursor.fetchall()

for row in rows:
reliability = row[0]

return reliability

def change_reliability_by_user_id(user_id, new_reliability):
account_cursor.execute("UPDATE users SET reliability = ? WHERE id = ?", (new_reliability, user_id))
account_conn.commit()

sys_log("Changed User Reliability", "Username: " + get_username(account_cursor, user_id) + " Reliability: " + str(new_reliability))

username = input('Username: ')
password = input('Password: ')

reliability = get_user_reliability(account_cursor, username, password)

if reliability is None:
print("Account does not exist.")
elif reliability >= 4:
print('Logged in successfully.')
else:
print('You do not have sufficient rights to access the panel.')
exit()

while(True):
command = input('>>> ')

if command == "exit":
exit()
elif command == "check":
compare_databases()
elif command == "sync":
synchronization_databases()
elif command == "log":
with open('log.txt', 'r') as file:
for line in file:
print(line.strip())
elif command == "users-list":
list_users_database()
elif command == "users-rel":
user_id = input('User ID: ')
print("Current reliability: " + str(get_reliability_from_id(user_id)))
new_reliability = input('Change reliability to: ')
change_reliability_by_user_id(user_id, new_reliability)
print('Changed reliability successfully.')

1 change: 1 addition & 0 deletions adpn.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python adpn.py
Binary file modified initializer/__pycache__/database.cpython-312.pyc
Binary file not shown.
Binary file modified initializer/__pycache__/loader.cpython-312.pyc
Binary file not shown.
5 changes: 4 additions & 1 deletion initializer/database.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sqlite3
from log.write import log, sys_log
from datetime import datetime

from account.database import create_users_database
Expand Down Expand Up @@ -40,4 +41,6 @@ def Initializer_Database():
Initializer_Censorship_Database()
create_users_database()

return conn
sys_log("Initializer Database", "search-index.db")
sys_log("Initializer Censorship Database", "censorship.db")
sys_log("Create Users Account Database", "users-account.db")
3 changes: 3 additions & 0 deletions initializer/loader.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import sqlite3
from log.write import sys_log

def database_loader():
conn = sqlite3.connect('./database/search-index.db')
sys_log("Loaded", "search-index.db")
return conn

def censorship_database_loader():
conn = sqlite3.connect('./database/censorship.db')
sys_log("Loaded", "censorship.db")
return conn
Empty file added log.txt
Empty file.
Binary file added log/__pycache__/write.cpython-312.pyc
Binary file not shown.
14 changes: 14 additions & 0 deletions log/write.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from account.reliability import get_user_reliability
from account.userid import get_user_id

def log(cursor, username, password, job, string):
log_file = open("./log.txt", "a")
log_string = "User: " + username + " (ID:" + str(get_user_id(cursor, username)) + ")/(Reliability:" + str(get_user_reliability(cursor, username, password)) + "): " + job + ": " + string
log_file.write(log_string + '\n')
log_file.close()

def sys_log(job, string):
log_file = open("./log.txt", "a")
log_string = "SYS: " + job + ": " + string
log_file.write(log_string + '\n')
log_file.close()
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@

st.title('MonoSearch')

st.session_state.setdefault('add_state', True)
st.session_state.setdefault('form_state', True)

with st.form('Input_Form'):
col1, col2, col3, col4, col5 = st.columns([3, 0.8, 0.6, 0.6, 0.8])
AForm = st.session_state.add_state
AForm = st.session_state.form_state

with col1:
keyword = st.text_input('Try to search something!', value='Try to search something!', placeholder='Try to search something!', label_visibility='collapsed')
Expand Down
Binary file modified manager/__pycache__/manager.cpython-312.pyc
Binary file not shown.
Loading

0 comments on commit 28bfe9e

Please sign in to comment.