-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
307 lines (250 loc) · 8.78 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
from datetime import datetime
import datetime
import os
import hashlib
from typing import TextIO
from flask_moment import Moment
from flask import Flask, render_template, request, redirect, session
from flask_sqlalchemy import SQLAlchemy
# open('dataHandler.py')
# import json
# hash = hashlib.md5(b'Holka19129')
# print(hash.hexdigest())
app = Flask(__name__, template_folder="client-side")
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://{}:{}@{}/{}'.format(
os.getenv('DB_USER', 'a0219210_flask'),
os.getenv('DB_PASSWORD', '19129'),
os.getenv('DB_HOST', 'artik-life.ru'),
os.getenv('DB_NAME', 'a0219210_flask')
)
db = SQLAlchemy(app)
app.secret_key = 'jjjdhgasgjkdfwehfsdhfjsdf'
app.permanent_session_lifetime = datetime.timedelta(hours=1)
moment = Moment(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True, nullable=False)
password = db.Column(db.String(36), nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
age = db.Column(db.Integer, nullable=False)
sex = db.Column(db.Integer, nullable=False)
developer = db.Column(db.Integer, nullable=False)
language = db.Column(db.String(36))
avatar = db.Column(db.String(400))
def __repr__(self):
return '<User %r>' % self.name
global three
three = datetime.timedelta(hours=3)
from datetime import datetime
blogtime = datetime.utcnow() + three
class Blog(db.Model):
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, nullable=False)
title = db.Column(db.String(80), unique=True, nullable=False)
tag = db.Column(db.String(80))
text = db.Column(db.TEXT, nullable=False)
data = db.Column(db.DateTime, default=blogtime)
def __repr__(self):
return '<Post %r>' % self.id
def log(type, text):
log: TextIO = open('log.txt', 'a')
if type == 1:
log.write('[ERROR] {}\n'.format(text))
print('ok')
def is_auth():
if session.get('is_auth') == True:
return True
else:
return False
@app.route('/')
def index():
title = 'Главная'
return render_template('index.html', title=title)
@app.route('/users')
def users():
global users
title = 'Пользователи'
try:
users = User.query.all()
except db.except_:
log(1, 'Ошибка при запросе в бд (67 line)')
return render_template('users.html', users=users, title=title)
@app.route('/user/<int:id>')
def user(id):
global user
try:
user = User.query.filter(User.id == id).first()
except db.except_:
log(1, 'Ошибка при запросе в бд (71 line)')
return render_template('user.html', user=user)
@app.route('/search', methods=['POST', 'GET'])
def search():
users = User.query.all()
if request.method == 'POST':
name = request.form['name']
for user in users:
if user.name == name:
return redirect('/user/{}'.format(user.id))
return redirect('/users')
else:
return redirect('/users')
@app.route('/auth', methods=['POST', 'GET'])
def auth():
global user
if request.method == 'POST':
try:
user = User.query.filter(User.name == request.form['name']).first()
except db.except_ as error:
log(1, 'Error in auth query! (114 line)')
formpass = request.form['password']
hash_pass = hashlib.md5(str(formpass).encode())
password = hash_pass.hexdigest()
if user is None:
message = 'Такого пользователя не существует!'
return render_template('auth.html', message=message)
elif user.password == password:
session['is_auth'] = True
session['user_name'] = user.name
session['user_id'] = user.id
return redirect('/ucp')
else:
message = 'Пароль неверный!'
return render_template('auth.html', message=message)
else:
title = 'Авторизация'
return render_template('auth.html', title=title)
@app.route('/reg', methods=['POST', 'GET'])
def reg():
if request.method == 'POST':
name = request.form['name']
age = request.form['age']
sex = request.form['sex']
developer = request.form['developer']
language = request.form['language']
email = request.form['email']
hash_pass = hashlib.md5(request.form['password'])
password = hash_pass.hexdigest()
user: User = User(name=name, password=password, email=email, age=age, sex=sex, developer=developer,
language=language)
try:
db.session.add(user)
db.session.commit()
return redirect('/auth')
except db.except_:
log(1, '133 line ERROR')
else:
title = 'Регистрация'
return render_template('reg.html', title=title)
@app.route('/ucp')
def ucp():
if not is_auth():
return redirect('/auth')
else:
user: object = User.query.filter(User.id == session['user_id']).first()
return render_template('ucp/index.html', user=user)
@app.route('/update-user', methods=['POST', 'GET'])
def update_user():
if request.method == 'POST':
name = request.form['name']
age = request.form['age']
email = request.form['email']
hash_pass = hashlib.md5(request.form['password'])
password = hash_pass.hexdigest()
try:
user = User.query.get(session['user_id'])
user.name = name
user.age = age
user.email = email
user.password = password
db.session.add(user)
db.session.commit()
session['user_name'] = name
return redirect('/ucp')
except db.except_ as error:
log(1, '154 line | ' + error)
print(error)
@app.route('/ucp/logout')
def logout():
session['is_auth'] = False
session.pop('user_name', None)
session.pop('user_id', None)
return redirect('/')
@app.route('/blog')
def posts():
global posts
global users
title = 'Блог'
try:
posts = Blog.query.order_by(Blog.id.desc()).all()
users = User.query.all()
except db.except_:
log(1, 'Ошибка при запросе в бд (206 line)')
return render_template('blog/posts.html', posts=posts, title=title, three=three, users=users)
@app.route('/blog/<int:id>')
def post(id):
global post
try:
post = Blog.query.filter(Blog.id == id).first()
except db.except_:
log(1, 'Ошибка при запросе в бд (215 line)')
return render_template('blog/post.html', post=post, title=post.title)
@app.route('/blog/<int:id>/edit', methods=['POST', 'GET'])
def edit_post(id):
if not is_auth():
return redirect('/auth')
else:
global post
try:
post = Blog.query.filter(Blog.id == id).first()
except db.except_:
log(1, 'Ошибка при запросе в бд (228 line)')
if request.method == 'POST':
title = request.form['title']
text = request.form['text']
tag = request.form['tag']
blog = Blog.query.get(id)
blog.title = title
blog.text = text
blog.tag = tag
try:
db.session.add(blog)
db.session.commit()
return redirect('/blog')
except db.except_:
log(1, '245 line ERROR')
if session['user_id'] == post.user_id:
return render_template('blog/edit.html', post=post, title='Редактирование')
else:
return redirect('/blog/{}'.format(id))
@app.route('/blog/<int:id>/delete')
def del_post(id):
try:
blog = Blog.query.get(id)
db.session.delete(blog)
db.session.commit()
return redirect('/blog')
except:
return 'delete post' + id
@app.route('/blog/add', methods=['POST', 'GET'])
def add_post():
if not is_auth():
return redirect('/auth')
else:
if request.method == 'POST':
title = request.form['title']
text = request.form['text']
tag = request.form['tag']
global Blog
blog = Blog(title=title, text=text, tag=tag,
user_id=session['user_id'])
try:
db.session.add(blog)
db.session.commit()
return redirect('/blog')
except db.except_:
log(1, '252 line ERROR')
else:
return render_template('blog/add.html', title='Новый пост')
if __name__ == "__main__":
app.run(debug=True)