-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
143 lines (97 loc) · 3.79 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
from os import path
from typing import Union
from datetime import datetime
from flask import Flask, request, redirect, render_template
from flask_wtf import CSRFProtect
from werkzeug.utils import secure_filename
from data import db_session
from data.posts import Posts
from forms.edit_post_form import EditPostForm
app = Flask(__name__)
app.config['SECRET_KEY'] = 'SECRET_KEY'
csrf_protect = CSRFProtect(app)
UPLOAD_FOLDER = 'static/posts_img/'
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER
DATA_BASE = 'db/blog.sqlite'
app.config['DATA_BASE'] = DATA_BASE
def edit_post_in_data_base(form: EditPostForm, post: Union[Posts, None]):
db_sess = db_session.create_session()
post_title = form.title.data
post_text = form.text.data
post_author = form.author.data
post_image = form.image.data
# --- Фотография ---
if not post_image:
post_image_name = '' # Картинки нет
else:
current_id = db_sess.query(Posts).order_by(Posts.id.desc()).first()
current_id = current_id.id + 1 if current_id else 1
real_image_name = secure_filename(post_image.filename)
post_image_name = f'{current_id}{real_image_name[real_image_name.rfind("."):]}'
post_image.save(path.join(app.config['UPLOAD_FOLDER'], post_image_name))
# --- Фотография ---
if not post: # Добавление поста
post = Posts()
post.title = post_title
post.image_name = post_image_name
post.text = post_text
post.author = post_author
post.date = datetime.now()
db_sess.add(post)
else: # редактирование
post.title = post_title
post.image_name = post_image_name
post.text = post_text
post.author = post_author
post.date = datetime.now()
db_sess.merge(post)
db_sess.commit()
db_sess.close()
return redirect('/')
@app.route('/')
def index():
params = {'title': 'Blog', 'UPLOAD_FOLDER': app.config['UPLOAD_FOLDER']}
db_sess = db_session.create_session()
posts = db_sess.query(Posts).order_by(Posts.id.desc()).all()
view = render_template('blog.html', **params, posts=posts)
db_sess.close()
return view
@app.route('/add_post', methods=['GET', 'POST'])
def add_post():
params = {'title': 'Добавление поста', 'action_type': 'Добавление поста', 'submit_text': 'Добавить'}
form = EditPostForm()
params['form'] = form
if form.validate_on_submit():
return edit_post_in_data_base(form, None)
return render_template('edit_post.html', **params)
@app.route('/edit_post/<int:post_id>', methods=['GET', 'POST'])
def edit_post(post_id: int):
params = {'title': 'Редактирование поста', 'action_type': 'Редактирование поста', 'submit_text': 'Редактировать'}
form = EditPostForm()
params['form'] = form
db_sess = db_session.create_session()
post: Posts = db_sess.query(Posts).filter(Posts.id == post_id).first()
db_sess.close()
if not post:
return redirect('/')
if request.method == 'GET':
form.title.data = post.title
form.text.data = post.text
form.author.data = post.author
elif form.validate_on_submit():
return edit_post_in_data_base(form, post)
return render_template('edit_post.html', **params)
@app.route('/delete_post/<int:post_id>')
def delete_post(post_id: int):
db_sess = db_session.create_session()
post = db_sess.query(Posts).filter(Posts.id == post_id).first()
if post:
db_sess.delete(post)
db_sess.commit()
db_sess.close()
return redirect('/')
def main():
db_session.global_init(app.config['DATA_BASE'])
app.run('127.0.0.1', 8080)
if __name__ == '__main__':
main()