-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
368 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
""" | ||
Система авторизации пользователей. | ||
""" | ||
|
||
from app import login_manager | ||
from app.models import User | ||
|
||
|
||
@login_manager.user_loader | ||
def load_user(id): | ||
return User.query.get(int(id)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
""" | ||
Формы приложения. | ||
""" | ||
|
||
from flask_wtf import FlaskForm | ||
from wtforms import StringField, PasswordField, BooleanField, SubmitField, validators | ||
from wtforms.validators import DataRequired, EqualTo, ValidationError | ||
from app.models import User | ||
|
||
|
||
class LoginForm(FlaskForm): | ||
""" | ||
Форма авторизации. | ||
""" | ||
nickname = StringField('Никнейм', validators=[DataRequired()]) | ||
password = PasswordField('Пароль', validators=[DataRequired()]) | ||
remember_me = BooleanField('Запомнить меня') | ||
submit = SubmitField('Войти') | ||
|
||
def validate_nickname(self, nickname): | ||
user = User.query.filter_by(nickname=nickname.data).first() | ||
if user and not user.active: | ||
raise ValidationError('Учётная запись неактивна.') | ||
|
||
|
||
class RegForm(FlaskForm): | ||
""" | ||
Форма регистрации пользователя. | ||
""" | ||
nickname = StringField('Никнейм', validators=[DataRequired()]) | ||
password1 = PasswordField('Пароль', [ | ||
validators.DataRequired(), | ||
validators.EqualTo('password2', message='Пароли должны совпадать!') | ||
]) | ||
password2 = PasswordField('Пароль повторно', [ | ||
validators.DataRequired() | ||
]) | ||
submit = SubmitField('Зарегистрировать') | ||
|
||
def validate_nickname(self, nickname): | ||
user = User.query.filter_by(nickname=nickname.data).first() | ||
if user is not None: | ||
raise ValidationError('Ник занят. Выберите другой, пожалуйста.') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,50 +1,127 @@ | ||
#!/usr/bin/env python3 | ||
|
||
from flask import render_template, redirect, request | ||
from flask import flash, redirect, render_template, request, url_for | ||
from flask_login import current_user, login_required, login_user | ||
from werkzeug.urls import url_parse | ||
|
||
from app import application | ||
from app.generator import start_module | ||
from app.dbpanel import DBWork | ||
from app.forms import LoginForm, RegForm | ||
from app.generator import start_module | ||
from app.models import User | ||
|
||
active_output: str = '' | ||
from app import db_lib | ||
|
||
# Дополнительные настройки | ||
application.add_template_global(DBWork().get_list_category, 'list_category') | ||
|
||
@application.route('/', methods=['GET', 'POST']) | ||
@application.route('/index', methods=['GET', 'POST']) | ||
def index(): | ||
""" | ||
Вывод главной страницы проекта. | ||
""" | ||
global active_output | ||
dbw = DBWork() | ||
output: str = '' | ||
tag_links: list = list() | ||
form_login = LoginForm() | ||
data_render = dict( | ||
title='online', | ||
output='', | ||
category_links=dbw.get_list_category, # chat_string | ||
tag_links=list(), # tag_string | ||
exoutput='', | ||
form_login=form_login | ||
) | ||
|
||
# Обработчик запроса пользователя GET | ||
if request.method == 'GET': | ||
chatstring = request.args.get(key="chatstring", default="") | ||
tagstring = request.args.get(key="tagstring", default="") | ||
question_to_base = f'{chatstring} {tagstring}' | ||
data_render['output'] = start_module(question_to_base) | ||
|
||
if request.args.get(key='chatstring'): | ||
output = start_module(request.args['chatstring']) | ||
active_output = request.args['chatstring'] | ||
data_render['exoutput'] = request.args.get(key='chatstring', default='') | ||
|
||
elif request.args.get(key='tagstring'): | ||
output = start_module(f"{active_output} {request.args['tagstring']}") | ||
else: | ||
output = start_module('all') | ||
active_output = '' | ||
tag_links = dbw.get_choices_types(category=data_render['exoutput']) | ||
data_render['tag_links'] = tag_links if len(tag_links) > 1 else list() | ||
|
||
tag_links = dbw.get_choices_types(category=active_output) | ||
if len(tag_links) < 2: | ||
tag_links = list() | ||
if request.method == 'POST': | ||
if form_login.validate_on_submit() and form_login.submit.data: | ||
user = User.query.filter_by(nickname=form_login.nickname.data).first() | ||
if user is None or not user.check_password(form_login.password.data): | ||
flash('Неверные данные.', category='error') | ||
return render_template('index.html', **data_render) | ||
|
||
login_user(user, remember=True) | ||
|
||
if request.method == 'GET': | ||
pass | ||
|
||
return render_template('index.html', | ||
title='online', | ||
output=output, | ||
exoutput=active_output, | ||
category_links=dbw.get_list_category, | ||
tag_links=tag_links) | ||
return render_template('index.html', **data_render) | ||
|
||
|
||
@application.route('/db') | ||
def createbase(): | ||
pass | ||
@login_required | ||
def db_view(): | ||
""" | ||
Для авторизованных пользователей работа с записями в базе данных. | ||
:return: HttpResponse, форма. | ||
""" | ||
|
||
if not current_user.isadmin: | ||
return f'Техническая страница недоступна для вашей учётной записи. <a href={url_for("index")}>На главную</a>.' | ||
|
||
return 'В стадии разработки.' | ||
|
||
|
||
@application.route('/login', methods=['GET', 'POST']) | ||
@application.template_global() | ||
def login(): | ||
""" | ||
Форма регистрации и авторизации пользователя. | ||
:return: HttpResponse, форма. | ||
""" | ||
# https://habr.com/ru/post/346346/ | ||
|
||
if current_user.is_authenticated: | ||
return redirect(url_for('index')) | ||
|
||
form_login = LoginForm(prefix='login') | ||
|
||
if form_login.validate_on_submit(): | ||
user = User.query.filter_by(nickname=form_login.nickname.data).first() | ||
if user is None or not user.check_password(form_login.password.data): | ||
flash('Неверные данные.', category='error') | ||
return redirect(url_for('login')) | ||
login_user(user, remember=True) | ||
|
||
next_page = request.args.get('next') | ||
if not next_page or url_parse(next_page).netloc != '': | ||
next_page = url_for('index') | ||
return redirect(next_page) | ||
|
||
return render_template('auth/login.html', title='Авторизация', | ||
form=form_login) | ||
|
||
@application.route('/singin', methods=['GET', 'POST']) | ||
@application.template_global() | ||
def singin(): | ||
|
||
if current_user.is_authenticated: | ||
return redirect(url_for('index')) | ||
|
||
form_reg = RegForm() | ||
|
||
if form_reg.validate_on_submit(): | ||
user = User( | ||
nickname=form_reg.nickname.data | ||
) | ||
user.set_password(form_reg.password1.data) | ||
db_lib.session.add(user) | ||
db_lib.session.commit() | ||
|
||
login_user(user, remember=True) | ||
|
||
return redirect(url_for('index')) | ||
|
||
return render_template('auth/registration.html', title='Регистрация', | ||
form_reg=form_reg) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{% extends "base.html" %} | ||
{% block content %} | ||
|
||
<h1>Портал в закулисье</h1> | ||
|
||
{% with messages = get_flashed_messages() %} | ||
{% if messages %} | ||
<ul> | ||
{% for message in messages %} | ||
<li>{{ message }}</li> | ||
{% endfor %} | ||
</ul> | ||
{% endif %} | ||
{% endwith %} | ||
|
||
<form method="POST" novalidate> | ||
{{ form.hidden_tag() }} | ||
|
||
<p> | ||
{{ form.nickname.label }}*<br> | ||
{{ form.nickname(size=32) }}<br> | ||
{% for error in form.nickname.errors %} | ||
<span style="color: #ff0000;">[{{ error }}]</span> | ||
{% endfor %} | ||
</p> | ||
<p> | ||
{{ form.password.label }}*<br> | ||
{{ form.password(size=32) }}<br> | ||
{% for error in form.password.errors %} | ||
<span style="color: red;">[{{ error }}]</span> | ||
{% endfor %} | ||
</p> | ||
<p> | ||
{{ form.submit() }} | ||
</p> | ||
</form> | ||
|
||
{% endblock %} |
Oops, something went wrong.