Files
myapp-full/myapp/ns_api/login.py
2022-05-03 22:34:52 +03:00

61 lines
1.6 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
__author__ = 'RemiZOffAlex'
__email__ = 'remizoffalex@mail.ru'
__url__ = 'https://remizoffalex.ru/'
import string
from flask import session
from . import jsonrpc
from .. import app, lib, models
@jsonrpc.method('login')
def login(username: str, password: str) -> bool:
user = models.db_session.query(
models.User
).filter(
models.User.name == username,
models.User.password == lib.get_hash_password(
password,
app.config['SECRET_KEY']
),
models.User.disabled == False # noqa E712
).first()
if user is None:
raise ValueError
session['logged_in'] = True
session['user_id'] = user.id
return True
@jsonrpc.method('login.register')
def login_register(username: str, password: str) -> bool:
"""Регистрация
"""
if len(username) < 4 or len(username) > 25:
raise ValueError('Длина логина от 4 до 25 символов')
if len(password) < 4 or len(password) > 150:
raise ValueError('Длина пароля от 4 до 150 символов')
for char in username:
if char not in string.ascii_letters + string.digits:
raise ValueError
user = models.db_session.query(
models.User
).filter(
models.User.name == username
).first()
if user:
raise ValueError('Пользователь с таким логином уже существует')
newuser = models.User(username)
newuser.password = lib.get_hash_password(
password,
app.config['SECRET_KEY']
)
models.db_session.add(newuser)
models.db_session.commit()
return True