-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
183 lines (136 loc) · 5.21 KB
/
app.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
from flask import Flask, render_template, flash, redirect, url_for, session, request, logging
import bcrypt
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.sql import text
from wtforms import Form, StringField, TextAreaField, PasswordField, validators
from md.cl.forms import RegisterForm, RecordForm, InjectionForm
from md._db.insert import Insert
from md._db.select import Select
from md._db.update import Update
from md._db.delete import Delete
from md.cl.verify import *
from md.cl.user import *
from md.cl.view import *
app = Flask(__name__)
@app.route('/')
def home():
return render_template('home.jinja2')
@app.route('/about')
def about():
return render_template('about.jinja2')
@app.route('/register', methods=['GET', 'POST'])
def register():
form = RegisterForm(request.form)
if request.method == 'POST' and form.validate():
hashed = bcrypt.hashpw((form.password.data).encode('utf-8'), bcrypt.gensalt())
new_user = User(None, form.name.data, form.surname.data, form.username.data, hashed.decode(encoding='utf-8'))
Insert.user(new_user)
flash('You are now registered and can log in', 'success')
return redirect(url_for('register'))
return render_template('register.jinja2', form=form)
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
my_list = Select.users()
user_names = my_list.getUserNames()
username = request.form['username']
password_candidate = request.form['password']
if username in user_names:
my_user = my_list.getUser(username)
password = my_user.password
if bcrypt.checkpw(password_candidate.encode('utf-8'), password.encode('utf-8')):
session['logged_in'] = True
session['username'] = username
flash('You are now logged in', 'success')
return redirect(url_for('table'))
else:
error = 'Invalid login'
return render_template('login.jinja2', error=error)
else:
error = 'Username not found'
return render_template('login.jinja2', error=error)
return render_template('login.jinja2')
@app.route('/logout')
@is_logged_in
def logout():
session.clear()
flash('You are now logged out', 'success')
return redirect(url_for('login'))
@app.route('/add_record', methods=['GET', 'POST'])
@is_logged_in
def add_record():
form = RecordForm(request.form)
if request.method == 'POST' and form.validate():
new_user = User(None, form.name.data, form.surname.data, form.username.data, None)
Insert.user(new_user)
flash('Record Created', 'success')
return redirect(url_for('table'))
return render_template('add_record.jinja2', form=form)
@app.route('/edit_record/<string:id>', methods=['GET', 'POST'])
@is_logged_in
def edit_record(id):
record = Select.record_by_id(id)
form = RecordForm(request.form)
form.name.data = record.name
form.surname.data = record.surname
form.username.data = record.username
if request.method == 'POST' and form.validate():
name = request.form['name']
surname = request.form['surname']
username = request.form['username']
new_user = User(None, name, surname, username, None)
app.logger.info(username)
Update.record_update(new_user, id)
flash('Record Updated', 'success')
return redirect(url_for('table'))
return render_template('edit_record.jinja2', form=form)
@app.route('/delete_record/<string:id>', methods=['POST'])
@is_logged_in
def delete_record(id):
Delete.record_from_table(id)
flash('Record Deleted', 'success')
return redirect(url_for('table'))
@app.route('/table')
@is_logged_in
def table():
uzivatelia = Select.users()
my_list = uzivatelia.getUsersInfo()
my_list.reverse()
if len(my_list) > 0:
return render_template('table.jinja2', my_list=my_list)
else:
msg = 'No Data Found'
return render_template('table.jinja2', msg=msg)
@app.route('/view', methods=['GET'])
@is_logged_in
def view():
views = Select.views()
my_list = views.getViewsInfo()
my_list.reverse()
if len(my_list) > 0:
return render_template('view.jinja2', my_list=my_list)
else:
msg = 'No Data Found'
return render_template('view.jinja2', msg=msg)
@app.route('/view', methods=['POST'])
@is_logged_in
def view_sort():
if request.method == 'POST':
nieco = request.form['sort_by']
sorted = Select.viewsSortBy(nieco)
mylist = sorted.getViewsInfo()
return render_template('view.jinja2', mylist=mylist)
@app.route('/injection', methods=['GET', 'POST'])
@is_logged_in
def injection():
form = InjectionForm(request.form)
data = Select.inj()
if request.method == 'POST' and form.validate():
Insert.inj(form.injection.data)
flash('Injection test successful', 'success')
return redirect(url_for('injection'))
return render_template('injection.jinja2', form=form, data=data, len=len(data))
if __name__ == '__main__':
#aplikaciu spustime pomocou prikazu python app.py
app.secret_key='root'
app.run(host='0.0.0.0', port = 5000)