-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
132 lines (104 loc) · 3.28 KB
/
main.py
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
from types import MethodType
from flask import Flask, render_template, request, url_for, redirect, flash, session
import sqlite3
import os
app = Flask(__name__)
app.secret_key = 'asdfasdf'
def init_db():
if not os.path.exists('data.db'):
conn = sqlite3.connect('data.db')
c = conn.cursor()
c.execute('''
CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
email TEXT NOT NULL UNIQUE,
password TEXT NOT NULL,
text TEXT
)
''')
conn.commit()
conn.close()
@app.route("/")
def hello_world():
return render_template('index.html')
@app.route("/signup", methods=['GET', 'POST'])
def signup():
if request.method == 'POST':
email = request.form['email']
password = request.form['password']
text = 'test test'
try:
conn = sqlite3.connect('data.db')
c = conn.cursor()
c.execute(
"""INSERT INTO users (
email,
password,
text
) VALUES (?, ?, ?)""",
(email, password, text))
conn.commit()
conn.close()
except Exception as e:
print(e)
flash('Email already exists')
return redirect(url_for('signup'))
print(email + ' ' +password)
return redirect(url_for('login'))
return render_template('signup.html')
@app.route("/login", methods=['POST', 'GET'])
def login():
if request.method == 'POST':
email = request.form['email']
password = request.form['password']
# Verbindung zur Datenbank
conn = sqlite3.connect('data.db')
cursor = conn.cursor()
query = "SELECT * FROM users WHERE email = ? AND password = ?"
cursor.execute(query, (email, password))
result = cursor.fetchone()
conn.close()
if result:
# Speichern von Nutzerdaten in der Session
session['email'] = email
return redirect(url_for('home'))
else:
flash('Falsche E-Mail oder Passwort')
return render_template('login.html')
return render_template('login.html')
@app.route("/home")
def home():
if 'email' in session:
email = session['email']
conn = sqlite3.connect('data.db')
cursor = conn.cursor()
query = "SELECT * FROM users WHERE email = ?"
cursor.execute(query, (email,))
result = cursor.fetchone()
conn.close()
return render_template('home.html', data=result)
else:
flash('Du bist nicht eingeloggt!')
return redirect(url_for('login'))
@app.route("/logout")
def logout():
session.pop('email', None) # Entfernt die E-Mail aus der Session
flash('Erfolgreich ausgeloggt')
return redirect(url_for('login'))
@app.route("/admin", methods=['POST', 'GET'])
def admin():
if request.method == 'POST':
password = request.form['password']
if password == 'test':
conn = sqlite3.connect('data.db')
c = conn.cursor()
c.execute("SELECT * FROM users")
users = c.fetchall()
conn.close()
return render_template('admin_login.html', users=users)
return render_template('admin.html')
else:
return render_template('admin.html')
if __name__ == "__main__":
init_db()
app.run(debug=False, port=8080, host='0.0.0.0')