1
0
mirror of https://github.com/avatao-content/test-tutorial-framework synced 2025-07-06 08:46:23 +00:00

Perform mercilless separation of bussiness logic from request handling

This commit is contained in:
Kristóf Tóth
2018-04-28 19:08:29 +02:00
parent fbd7bff85a
commit 511e16c514
5 changed files with 81 additions and 44 deletions

View File

@ -0,0 +1,28 @@
from crypto import PasswordHasher
from model import Session, User
from exceptions import InvalidCredentialsError, UserExistsError
class UserOps:
def __init__(self, username, password):
self.username = username
self.password = password
def authenticate(self):
with Session() as db:
user = db.query(User).filter(User.username == self.username).first()
if not user or not PasswordHasher.verify(self.password, user.passwordhash):
raise InvalidCredentialsError
def register(self):
with Session() as db:
if db.query(User).filter(User.username == self.username).all():
raise UserExistsError
user = User(username=self.username,
passwordhash=PasswordHasher.hash(self.password))
db.add(user)
db.commit()