-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
95 lines (76 loc) · 3.13 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
import os
from flask import Flask, render_template, redirect, make_response, jsonify
from flask_login import LoginManager, login_user, login_required, logout_user, current_user
from data import db_session, jobs_api
from data.users import User
from data.jobs import Jobs
from forms.login import LoginForm
from forms.register import RegisterForm
app = Flask(__name__)
login_manager = LoginManager()
login_manager.init_app(app)
app.config['SECRET_KEY'] = 'yandexlyceum_secret_key'
def main():
port = int(os.environ.get("PORT", 5000))
app.run(host='0.0.0.0', port=port)
db_session.global_init("db/mars_jobs.db")
app.register_blueprint(jobs_api.blueprint)
app.run()
@login_manager.user_loader
def load_user(user_id):
db_sess = db_session.create_session()
return db_sess.query(User).get(user_id)
@app.route("/")
def index():
db_sess = db_session.create_session()
jobs = db_sess.query(Jobs).all()
users = db_sess.query(User).all()
names = {name.id: (name.surname, name.name) for name in users}
return render_template("index.html", title='Works Log', jobs=jobs, names=names)
@app.route('/login', methods=['GET', 'POST'])
def login():
form = LoginForm()
if form.validate_on_submit():
db_sess = db_session.create_session()
user = db_sess.query(User).filter(User.email == form.email.data).first()
if user and user.check_password(form.password.data):
login_user(user, remember=form.remember_me.data)
return redirect("/")
return render_template('login.html',
message="Неправильный логин или пароль",
form=form)
return render_template('login.html', title='Авторизация', form=form)
@app.route('/logout')
@login_required
def logout():
logout_user()
return redirect("/")
@app.route('/register', methods=['GET', 'POST'])
def register():
form = RegisterForm()
if form.validate_on_submit():
if form.password.data != form.password_again.data:
return render_template('register.html', title='Регистрация', form=form,
message='Пароли не совпадают')
session = db_session.create_session()
if session.query(User).filter(User.email == form.email.data).first():
return render_template('register.html', title='Регистрация', form=form,
message='Такой пользователь уже есть')
user = User(
surname=form.surname.data,
name=form.name.data,
age=form.age.data,
position=form.position.data,
speciality=form.speciality.data,
address=form.address.data,
email=form.email.data
)
user.set_password(form.password.data)
session.add(user)
return redirect('/login')
return render_template("register.html", title='Регистрация', form=form)
@app.errorhandler(404)
def not_found(error):
return make_response(jsonify({'error': 'Not found'}), 404)
if __name__ == '__main__':
main()