-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathapp.py
116 lines (100 loc) · 3.87 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
# coding=utf8
"""
Main module run it end run application
"""
import bcrypt
from flask import Flask, render_template, url_for, request, session, redirect, flash
from forms import LoginForm, RegistrationForm
import config
from db_connect import mongo
from classes.user import to_class, User
from classes.keyword import ukrainian
app = Flask(__name__)
app.secret_key = config.flask_key
app.config['SECRET_KEY'] = config.secret_key
app.config['MONGO_DBNAME'] = config.mongoname
@app.route('/')
def index():
"""
Home page
:return: html_documentation
"""
if 'user' in session:
try:
user = User(session['user'])
except NameError:
return redirect(url_for('login'))
data = {'username': session['user'], 'keywords': user.get_full_data(),
'telegram_links': user.get_pretty_links('telegram'),
'twitter_links': user.get_pretty_links('twitter')}
return render_template('index.html', username=session['user'], data=data)
return redirect(url_for('login'))
@app.route('/register', methods=['POST', 'GET'])
def register():
"""
Register page
:return: html_documentation
"""
form = RegistrationForm()
print()
if form.validate_on_submit():
users = mongo.db.users
hashpass = bcrypt.hashpw(form.password.data.encode('utf-8'), bcrypt.gensalt())
users.insert({'name': form.username.data,
'email': form.email.data, 'password': hashpass,
'keywords': [], 'links_twitter': [],
'links_telegram': []})
session['user'] = form.username.data
print(session['user'])
return redirect(url_for('index'))
if request.method == 'POST':
flash("Невдалось стоворити користувача,\n перевірте чи усі дані введено коректно", 'danger')
return render_template('main_register.html', title='Register', form=form)
@app.route('/login', methods=['POST', 'GET'])
def login():
"""
Login Page
:return: html_documentation
"""
form = LoginForm()
if form.validate_on_submit():
users = mongo.db.users
login_user = users.find_one({'email': form.email.data})
if login_user:
if bcrypt.hashpw(form.password.data.encode('utf-8'), login_user['password']) \
== login_user['password']:
session['user'] = login_user['name']
return redirect(url_for('index'))
flash("Неправильна пошта або пароль!", 'danger')
elif not form.validate_on_submit() and request.method == 'POST':
flash("Неправильна пошта або пароль!", 'danger')
return render_template('main_login.html', title='Register', form=form)
@app.route('/add', methods=['POST'])
def add():
"""
Add new keyword for user
:return: redirect end Home page
"""
if request.method == 'POST':
user = to_class(session['user'])
if request.form['keyword'] in user.keywords:
flash("Це слово уже є у вашому словнику", 'danger')
elif len(request.form['keyword'].split()) > 1:
flash("Введіть рівно одне слово!", 'danger')
elif not ukrainian(request.form['keyword']):
flash("Цей сайт працює тільки з українськими словами", 'danger')
else:
user.add_keyword(request.form['keyword'])
flash("Це слово додано у ваш словник", 'success')
return redirect(url_for('index'))
@app.route('/logout', methods=['POST'])
def logout():
"""
Logout user
:return: redirect end Login Page
"""
if request.method == 'POST':
del session['user']
return redirect(url_for('login'))
if __name__ == '__main__':
app.run()