-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCore.py
More file actions
181 lines (142 loc) · 6.65 KB
/
Core.py
File metadata and controls
181 lines (142 loc) · 6.65 KB
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
from flask import request, Flask, render_template, redirect, session
from flask_wtf import FlaskForm
from flask_sqlalchemy import SQLAlchemy
from wtforms import StringField, PasswordField, BooleanField, SubmitField, TextAreaField
from wtforms.validators import DataRequired
class LoginForm(FlaskForm):
username = StringField('Логин', validators=[DataRequired()])
password = PasswordField('Пароль', validators=[DataRequired()])
remember_me = BooleanField('Запомнить меня')
submit = SubmitField('Войти')
class RegisterForm(FlaskForm):
username = StringField('Логин', validators=[DataRequired()])
password = PasswordField('Пароль', validators=[DataRequired()])
email = StringField('Email', validators=[DataRequired()])
remember_me = BooleanField('Запомнить меня')
submit = SubmitField('Войти')
class AddTopicForm(FlaskForm):
title = StringField('Название темы', validators=[DataRequired()])
content = TextAreaField('Описание темы', validators=[DataRequired()])
submit = SubmitField('Добавить')
class AddMessageForm(FlaskForm):
text = TextAreaField('Текст', validators=[DataRequired()])
submit = SubmitField('Добавить')
app = Flask(__name__)
app.config['SECRET_KEY'] = 'yandexlyceum_secret_key'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
password = db.Column(db.String(80), unique=False, nullable=False)
email = db.Column(db.String(80), unique=True, nullable=False)
def __repr__(self):
return '<User {} {} {}>'.format(self.id, self.username, self.password)
class Topic(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True, nullable=False)
description = db.Column(db.String(2000), unique=False, nullable=False)
author = db.Column(db.Integer, nullable=False)
author_name = db.Column(db.String(80), nullable=False)
def __repr__(self):
return '<Topic {} {} {}>'.format(self.id, self.name, self.description)
class Message(db.Model):
id = db.Column(db.Integer, primary_key=True)
author = db.Column(db.Integer, nullable=False)
author_name = db.Column(db.String(80), nullable=False)
topic = db.Column(db.Integer, nullable=False)
text = db.Column(db.String(2000), unique=False, nullable=False)
def __repr__(self):
return '<Message {} {} {} {} {}>'.format(self.id, self.author, self.author_name, self.topic, self.text)
db.create_all()
@app.route('/add_topic', methods=['GET', 'POST'])
def add_topic():
if 'username' not in session:
return redirect('/login')
form = AddTopicForm()
if form.validate_on_submit():
name = form.title.data
description = form.content.data
author = session['user_id']
author_name = User.query.get(author).username
topic = Topic(name=name, description=description, author=author, author_name=author_name)
db.session.add(topic)
db.session.commit()
print(Topic.query.all())
return redirect('/index')
return render_template('add_topic.html', title='Добавление новости',
username=session['username'], form=form)
@app.route('/delete_topic/<int:topic_id>', methods=['GET'])
def delete_topic(topic_id):
if 'username' not in session:
return redirect('/login')
for message in Message.query.filter_by(topic=topic_id).all():
db.session.delete(message)
db.session.delete(Topic.query.get(topic_id))
db.session.commit()
return redirect('/index')
@app.route('/topic/<int:topic_id>', methods=['GET', 'POST'])
def topic(topic_id):
form = AddMessageForm()
if form.validate_on_submit() and 'username' in session:
text = form.text.data
message = Message(author=session['user_id'],
author_name=session['username'],
topic=topic_id, text=text)
db.session.add(message)
db.session.commit()
redirect('/index')
topic = Topic.query.filter_by(id=topic_id).first()
messages = [[i.id, i.author, i.author_name, i.topic, i.text]
for i in Message.query.filter_by(topic=topic_id).all()]
return render_template('topic.html',
topic=[topic.id, topic.name, topic.description, topic.author, topic.author_name],
messages=messages, form=form)
@app.route('/delete_message/<int:topic_id>/<int:message_id>', methods=['GET'])
def message_topic(topic_id, message_id):
if 'username' not in session:
return redirect('/login')
db.session.delete(Message.query.get(message_id))
db.session.commit()
return redirect('/topic/' + str(topic_id))
@app.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
user = User.query.filter_by(username=form.username.data, password=form.password.data).first()
if user:
session['username'] = form.username.data
session['user_id'] = user.id
return redirect('/index')
return render_template('login.html', title='Login', form=form)
@app.route('/register', methods=['GET', 'POST'])
def register():
form = RegisterForm()
if form.validate_on_submit():
name = form.username.data
pas = form.password.data
email = form.email.data
user = User(username=name, password=pas, email=email)
db.session.add(user)
db.session.commit()
session['username'] = form.username.data
session['user_id'] = user.id
return redirect('/index')
return render_template('register.html', title='Register', form=form)
@app.route('/index')
def index():
if 'username' in session:
user = session['username']
else:
user = ''
topic=[[i.id, i.name, i.description, i.author, i.author_name] for i in Topic.query.all()]
return render_template('index.html', title='Домашняя страница',
username=user, topics=topic)
@app.route('/logout')
def logout():
session.pop('username', 0)
session.pop('user_id', 0)
return redirect('/login')
if __name__ == '__main__':
app.run(port=8080, host='127.0.0.1')