Merge pull request 'Testing' (#1) from Testing into main

Reviewed-on: fuh/PY_ChatBot#1
This commit is contained in:
fuh
2025-10-03 01:26:09 +02:00
12 changed files with 609 additions and 3 deletions

4
.gitignore vendored Normal file
View File

@@ -0,0 +1,4 @@
.gitignore
/.idea/
testing.py
/databases/

79
frontend/app.py Normal file
View File

@@ -0,0 +1,79 @@
from flask import Flask, render_template, request, redirect, url_for, flash
import hashlib
def hash_password(password):
return hashlib.sha512(password.strip().encode('utf-8')).hexdigest()
app = Flask(__name__)
app.secret_key = 'your_secret_key'
@app.route('/')
def home():
return redirect(url_for('login'))
@app.route('/register', methods=['GET', 'POST'])
def register():
import standard.getter as st_getter
import sqlLite.set as setter
if request.method == 'POST':
username = request.form['username']
email = request.form.get('email')
password = request.form.get('password')
pwd_confirm = request.form.get('confirm_password')
if not email or not password or not pwd_confirm or not username:
flash('Please fill out all fields', 'error')
return redirect(url_for('register'))
if password != pwd_confirm:
flash('Passwords do not match', 'error')
return redirect(url_for('register'))
try:
if st_getter.get_validate_email(email):
setter.set_login(username, email, password)
flash('Registration successful! Please log in.', 'success')
return redirect(url_for('login'))
else:
flash('Invalid email format', 'error')
return redirect(url_for('register'))
except Exception as e:
flash(f'Error: {str(e)}', 'error')
return redirect(url_for('register'))
# For GET-requests:
return render_template('register.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
enter_email = request.form.get('email')
enter_password = request.form.get('password')
import hashlib
import sqlLite.get as getter
import standard.getter as st_getter
stored_hash = getter.get_password_by_email(enter_email) # use email here
if stored_hash is None:
flash("User not found!")
return redirect(url_for("login"))
hash_entered = st_getter.get_password_hash(enter_password)
if hash_entered == stored_hash:
return redirect(url_for('dashboard'))
else:
flash('Invalid email or password', 'error')
print("Stored hash:", stored_hash)
print("Entered hash:", hash_entered)
return redirect(url_for('login'))
return render_template('login.html')
@app.route('/dashboard')
def dashboard():
#return "Welcome to the dashboard! Login successful."
return render_template('dashboard.html')

View File

@@ -0,0 +1,119 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Register</title>
<style>
/* Use the same styling as login.html for consistency */
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;600&display=swap');
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Poppins', sans-serif;
}
body {
height: 100vh;
background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
display: flex;
align-items: center;
justify-content: center;
padding: 20px;
}
.login-container {
background: white;
max-width: 400px;
width: 100%;
padding: 40px 30px 50px;
border-radius: 20px;
box-shadow: 0 20px 40px rgba(0,0,0,0.2);
text-align: center;
transition: transform 0.3s ease;
}
.login-container:hover {
transform: translateY(-8px);
box-shadow: 0 30px 60px rgba(0,0,0,0.3);
}
h2 {
font-weight: 600;
color: #333;
margin-bottom: 30px;
font-size: 28px;
letter-spacing: 1.2px;
}
.input-group {
margin-bottom: 25px;
text-align: left;
}
label {
display: block;
margin-bottom: 8px;
color: #555;
font-weight: 600;
font-size: 14px;
}
input[type="username"],
input[type="email"],
input[type="password"] {
width: 100%;
padding: 14px 18px;
font-size: 16px;
border-radius: 12px;
border: 2px solid #ddd;
transition: 0.3s border-color ease;
box-shadow: 0 2px 5px rgba(0,0,0,0.05);
}
input[type="email"]:focus,
input[type="password"]:focus {
border-color: #2575fc;
outline: none;
box-shadow: 0 0 12px rgba(37, 117, 252, 0.5);
}
button {
width: 100%;
padding: 16px 0;
margin-top: 10px;
background-color: #2575fc;
border: none;
border-radius: 14px;
color: white;
font-size: 18px;
font-weight: 700;
letter-spacing: 1px;
cursor: pointer;
box-shadow: 0 10px 20px rgba(37, 117, 252, 0.4);
transition: background-color 0.3s ease, box-shadow 0.3s ease;
}
button:hover {
background-color: #1859d6;
box-shadow: 0 15px 25px rgba(24, 89, 214, 0.6);
}
.error-message {
margin-top: 20px;
color: #ff4d4f;
font-weight: 600;
font-size: 15px;
text-align: center;
background: #ffe6e6;
padding: 10px 15px;
border-radius: 10px;
box-shadow: 0 2px 6px rgba(255,77,79,0.3);
display: none;
}
</style>
</head>
<body>
</body>
</html>

View File

@@ -0,0 +1,141 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Login</title>
<style>
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;600&display=swap');
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Poppins', sans-serif;
}
body {
height: 100vh;
background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
display: flex;
align-items: center;
justify-content: center;
padding: 20px;
}
.login-container {
background: white;
max-width: 400px;
width: 100%;
padding: 40px 30px 50px;
border-radius: 20px;
box-shadow: 0 20px 40px rgba(0,0,0,0.2);
text-align: center;
transition: transform 0.3s ease;
}
.login-container:hover {
transform: translateY(-8px);
box-shadow: 0 30px 60px rgba(0,0,0,0.3);
}
h2 {
font-weight: 600;
color: #333;
margin-bottom: 30px;
font-size: 28px;
letter-spacing: 1.2px;
}
.input-group {
margin-bottom: 25px;
text-align: left;
}
label {
display: block;
margin-bottom: 8px;
color: #555;
font-weight: 600;
font-size: 14px;
}
input[type="email"],
input[type="password"] {
width: 100%;
padding: 14px 18px;
font-size: 16px;
border-radius: 12px;
border: 2px solid #ddd;
transition: 0.3s border-color ease;
box-shadow: 0 2px 5px rgba(0,0,0,0.05);
}
input[type="email"]:focus,
input[type="password"]:focus {
border-color: #2575fc;
outline: none;
box-shadow: 0 0 12px rgba(37, 117, 252, 0.5);
}
button {
width: 100%;
padding: 16px 0;
margin-top: 10px;
background-color: #2575fc;
border: none;
border-radius: 14px;
color: white;
font-size: 18px;
font-weight: 700;
letter-spacing: 1px;
cursor: pointer;
box-shadow: 0 10px 20px rgba(37, 117, 252, 0.4);
transition: background-color 0.3s ease, box-shadow 0.3s ease;
}
button:hover {
background-color: #1859d6;
box-shadow: 0 15px 25px rgba(24, 89, 214, 0.6);
}
.error-message {
margin-top: 20px;
color: #ff4d4f;
font-weight: 600;
font-size: 15px;
text-align: center;
background: #ffe6e6;
padding: 10px 15px;
border-radius: 10px;
box-shadow: 0 2px 6px rgba(255,77,79,0.3);
display: none;
}
</style>
</head>
<body>
<div class="login-container">
<h2>Login to Your Account</h2>
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
<div class="error-message" style="display: block;">
{% for category, message in messages %}
{{ message }}
{% endfor %}
</div>
{% endif %}
{% endwith %}
<form method="POST" action="{{ url_for('login') }}">
<div class="input-group">
<label for="email">Email Address</label>
<input type="email" id="email" name="email" placeholder="you@example.com" required />
</div>
<div class="input-group">
<label for="password">Password</label>
<input type="password" id="password" name="password" placeholder="Your password" required />
</div>
<button type="submit">Log In</button>
</form>
</div>
</body>
</html>

View File

@@ -0,0 +1,152 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Register</title>
<style>
/* Use the same styling as login.html for consistency */
@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@300;600&display=swap');
* {
margin: 0;
padding: 0;
box-sizing: border-box;
font-family: 'Poppins', sans-serif;
}
body {
height: 100vh;
background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
display: flex;
align-items: center;
justify-content: center;
padding: 20px;
}
.login-container {
background: white;
max-width: 400px;
width: 100%;
padding: 40px 30px 50px;
border-radius: 20px;
box-shadow: 0 20px 40px rgba(0,0,0,0.2);
text-align: center;
transition: transform 0.3s ease;
}
.login-container:hover {
transform: translateY(-8px);
box-shadow: 0 30px 60px rgba(0,0,0,0.3);
}
h2 {
font-weight: 600;
color: #333;
margin-bottom: 30px;
font-size: 28px;
letter-spacing: 1.2px;
}
.input-group {
margin-bottom: 25px;
text-align: left;
}
label {
display: block;
margin-bottom: 8px;
color: #555;
font-weight: 600;
font-size: 14px;
}
input[type="username"],
input[type="email"],
input[type="password"] {
width: 100%;
padding: 14px 18px;
font-size: 16px;
border-radius: 12px;
border: 2px solid #ddd;
transition: 0.3s border-color ease;
box-shadow: 0 2px 5px rgba(0,0,0,0.05);
}
input[type="username"]:focus,
input[type="email"]:focus,
input[type="password"]:focus {
border-color: #2575fc;
outline: none;
box-shadow: 0 0 12px rgba(37, 117, 252, 0.5);
}
button {
width: 100%;
padding: 16px 0;
margin-top: 10px;
background-color: #2575fc;
border: none;
border-radius: 14px;
color: white;
font-size: 18px;
font-weight: 700;
letter-spacing: 1px;
cursor: pointer;
box-shadow: 0 10px 20px rgba(37, 117, 252, 0.4);
transition: background-color 0.3s ease, box-shadow 0.3s ease;
}
button:hover {
background-color: #1859d6;
box-shadow: 0 15px 25px rgba(24, 89, 214, 0.6);
}
.error-message {
margin-top: 20px;
color: #ff4d4f;
font-weight: 600;
font-size: 15px;
text-align: center;
background: #ffe6e6;
padding: 10px 15px;
border-radius: 10px;
box-shadow: 0 2px 6px rgba(255,77,79,0.3);
display: none;
}
</style>
</head>
<body>
<div class="login-container">
<h2>Create an Account</h2>
{% with messages = get_flashed_messages(with_categories=true) %}
{% if messages %}
<div class="error-message" style="display:block;">
{% for category, message in messages %}
{{ message }}
{% endfor %}
</div>
{% endif %}
{% endwith %}
<form method="POST" action="{{ url_for('register') }}">
<div class="input-group">
<label for="username">Username</label>
<input type="username" id="username" name="username" placeholder="username" required />
</div>
<div class="input-group">
<label for="email">Email Address</label>
<input type="email" id="email" name="email" placeholder="you@example.com" required />
</div>
<div class="input-group">
<label for="password">Password</label>
<input type="password" id="password" name="password" placeholder="Create a password" required />
</div>
<div class="input-group">
<label for="confirm_password">Confirm Password</label>
<input type="password" id="confirm_password" name="confirm_password" placeholder="Confirm your password" required />
</div>
<button type="submit">Register</button>
<p style="margin-top: 20px; font-size: 14px;">Already have an account? <a href="{{ url_for('login') }}">Log in here</a>.</p>
</form>
</div>
</body>
</html>

13
main.py
View File

@@ -1,5 +1,12 @@
import sqlite3 import sqlLite
DB_CON = sqlite3.connect("database.db") from frontend.app import app
DB_CUR = DB_CON.cursor()
sqlLite.set_db_name("databases/test.db")
import sqlLite.create as create
create.create_table_t_user()
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0', port=8080)

3
sqlLite/__init__.py Normal file
View File

@@ -0,0 +1,3 @@
def set_db_name(name):
global db_name
db_name = name

17
sqlLite/auth.py Normal file
View File

@@ -0,0 +1,17 @@
def login(name_email, password):
import get as getter
password_stored = getter.get_password_by_email(name_email)
if password_stored is None:
return None
elif password_stored != password:
return None
else:
return True
def register(username, email, password):
import standard.getter as st_getter
if st_getter.get_validate_email(email):
hashed_password = st_getter.get_password_hash(password)
import sqlLite.set as sq_setter
sq_setter.set_login(username, email,hashed_password)

15
sqlLite/create.py Normal file
View File

@@ -0,0 +1,15 @@
import sqlite3
from . import db_name
## Create Tables
def create_table_t_user():
db_con = sqlite3.connect(db_name)
db_cur = db_con.cursor()
db_cur.execute("""
CREATE TABLE IF NOT EXISTS T_USERS (
ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
USERNAME TEXT NOT NULL UNIQUE,
EMAIL TEXT NOT NULL UNIQUE,
PASSWORD TEXT NOT NULL
);""")
db_con.commit()

40
sqlLite/get.py Normal file
View File

@@ -0,0 +1,40 @@
import sqlite3
from . import db_name
def get_all_users():
conn = sqlite3.connect(db_name)
cursor = conn.cursor()
cursor.execute('select * from T_USERS')
rows = cursor.fetchall()
for row in rows:
print(row)
def get_userinfo_by_username(username):
conn = sqlite3.connect(db_name)
cursor = conn.cursor()
cursor.execute('select * from T_USERS where username = ?', (username,))
rows = cursor.fetchall()
for row in rows:
print(row)
def get_password_by_username(username):
conn = sqlite3.connect(db_name)
cursor = conn.cursor()
cursor.execute('SELECT PASSWORD FROM T_USERS WHERE username = ?', (username,))
row = cursor.fetchone()
cursor.close()
conn.close()
if row is None:
return None
return row[0] # password string
def get_password_by_email(email):
conn = sqlite3.connect(db_name)
cursor = conn.cursor()
cursor.execute('SELECT PASSWORD FROM T_USERS WHERE EMAIL = ?', (email,))
row = cursor.fetchone()
cursor.close()
conn.close()
if row is None:
return None
return row[0] # password string

17
sqlLite/set.py Normal file
View File

@@ -0,0 +1,17 @@
from hashlib import sha512
import sqlite3
import standard.getter as st_getter
from . import db_name
def set_login(username, email, password):
db_con = sqlite3.connect(db_name)
db_cur = db_con.cursor()
try:
if st_getter.get_validate_email(email):
db_cur.execute("INSERT INTO T_USERS (USERNAME, EMAIL, PASSWORD) VALUES (?,?,?)", (username ,email , st_getter.get_password_hash(password)))
db_con.commit()
else:
print("Email entered is not valid")
except sqlite3.IntegrityError:
print("Username or Email entered is not unique")
db_con.close()

12
standard/getter.py Normal file
View File

@@ -0,0 +1,12 @@
def get_password_hash(password):
from hashlib import sha512
password = password.strip()
return sha512(password.encode('utf-8')).hexdigest()
def get_validate_email(email):
import re
valid = re.match(r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$', email)
if valid:
return True
else:
return False