ska-referee-test/ref-test/app/models/user.py

108 lines
4.5 KiB
Python
Raw Normal View History

2022-06-11 18:26:39 +01:00
from ..modules import db
from ..tools.encryption import decrypt, encrypt
from ..tools.logs import write
from flask import flash, jsonify, session
from flask.helpers import url_for
2022-06-12 21:03:51 +01:00
from flask_login import current_user, login_user, logout_user, UserMixin
2022-06-11 18:26:39 +01:00
from werkzeug.security import check_password_hash, generate_password_hash
2022-06-12 21:03:51 +01:00
import secrets
from uuid import uuid4
2022-06-11 18:26:39 +01:00
class User(UserMixin, db.Model):
id = db.Column(db.String(36), primary_key=True)
username = db.Column(db.String(128), nullable=False)
password = db.Column(db.String(128), nullable=False)
email = db.Column(db.String(128), nullable=False)
reset_token = db.Column(db.String(20), nullable=True)
verification_token = db.Column(db.String(20), nullable=True)
tests = db.relationship('Test', backref='creator')
datasets = db.relationship('Dataset', backref='creator')
2022-06-11 18:26:39 +01:00
def __repr__(self):
return f'<user {self.username}> was added with <id {self.id}>.'
2022-06-12 21:03:51 +01:00
@property
def generate_id(self): raise AttributeError('generate_id is not a readable attribute.')
generate_id.setter
2022-06-15 23:54:44 +01:00
def generate_id(self): self.id = uuid4().hex
2022-06-12 21:03:51 +01:00
2022-06-11 18:26:39 +01:00
@property
def set_username(self): raise AttributeError('set_username is not a readable attribute.')
set_username.setter
def set_username(self, username:str): self.username = encrypt(username)
def get_username(self): return decrypt(self.username)
@property
def set_password(self): raise AttributeError('set_password is not a readable attribute.')
set_password.setter
def set_password(self, password:str): self.password = generate_password_hash(password, method="sha256")
def verify_password(self, password:str): return check_password_hash(self.password, password)
@property
def set_email(self): raise AttributeError('set_email is not a readable attribute.')
set_email.setter
def set_email(self, email:str): self.email = encrypt(email)
def get_email(self): return decrypt(self.email)
2022-06-12 22:48:13 +01:00
def register(self, notify:bool=False):
self.generate_id()
2022-06-11 18:26:39 +01:00
users = User.query.all()
for user in users:
2022-06-12 22:48:13 +01:00
if user.get_username() == self.get_username(): return False, f'Username {self.get_username()} already in use.'
if user.get_email() == self.get_email(): return False, f'Email address {self.get_email()} already in use.'
2022-06-11 18:26:39 +01:00
db.session.add(self)
db.session.commit()
write('users.log', f'User \'{self.get_username()}\' was created with id \'{self.id}\'.')
return True, f'User {self.get_username()} was created successfully.'
def login(self, remember:bool=False):
login_user(self, remember = remember)
write('users.log', f'User \'{self.get_username()}\' has logged in.')
flash(message=f'Welcome {self.get_username()}', category='success')
def logout(self):
session['remembered_username'] = self.get_username()
logout_user()
write('users.log', f'User \'{self.get_username()}\' has logged out.')
flash(message='You have successfully logged out.', category='success')
def reset_password(self):
new_password = secrets.token_hex(12)
self.set_password(new_password)
self.reset_token = secrets.token_urlsafe(16)
self.verification_token = secrets.token_urlsafe(16)
db.session.commit()
print('Password', new_password)
print('Reset Token', self.reset_token)
print('Verification Token', self.verification_token)
2022-06-15 23:54:44 +01:00
print('Reset Link', f'{url_for("admin._reset", token=self.reset_token, verification=self.verification_token, _external=True)}')
2022-06-11 18:26:39 +01:00
return jsonify({'success': 'Your password reset link has been generated.'}), 200
def clear_reset_tokens(self):
self.reset_token = self.verification_token = None
db.session.commit()
def delete(self, notify:bool=False):
2022-06-11 18:26:39 +01:00
username = self.get_username()
db.session.delete(self)
db.session.commit()
message = f'User \'{username}\' was deleted by \'{current_user.get_username()}\'.'
write('users.log', message)
return True, message
2022-06-12 21:03:51 +01:00
def update(self, password:str=None, email:str=None, notify:bool=False):
if not password and not email: return False, 'There were no changes requested.'
2022-06-12 21:03:51 +01:00
if password: self.set_password(password)
if email: self.set_email(email)
db.session.commit()
2022-06-15 23:54:44 +01:00
write('system.log', f'Information for user {self.get_username()} has been updated by {current_user.get_username()}.')
return True, f'Account {self.get_username()} has been updated.'