SFS/db.py

72 lines
2.4 KiB
Python

from flask_login import UserMixin
from werkzeug.security import generate_password_hash, check_password_hash
from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()
levels_users = {'Inactive':0, 'Simple user':1, 'Admin users':5, 'Admin':10}
#######################################################################
# User class #
#######################################################################
class User(UserMixin, db.Model):
"""Model for user accounts."""
__tablename__ = 'users'
login = db.Column(db.String(100),
primary_key=True)
name = db.Column(db.String(100),
nullable=False,
unique=False)
password = db.Column(db.String(100),
primary_key=False,
unique=False,
nullable=False)
is_admin = db.Column(db.Integer, default=1)
def set_password(self, password):
"""Create hashed password."""
self.password = generate_password_hash(password, method='sha256')
def check_password(self, password):
"""Check hashed password."""
return check_password_hash(self.password, password)
def get_id(self):
return self.login
def __repr__(self):
return '<User {}>'.format(self.login)
#######################################################################
# File class #
#######################################################################
class File(db.Model):
__tablename__ = 'files'
id = db.Column(db.Integer,
primary_key=True)
name = db.Column(db.String(200))
desc = db.Column(db.String(200))
user = db.Column(db.String(100), db.ForeignKey('users.login'))
password = db.Column(db.String(100), default='')
visible = db.Column(db.Boolean, default=False)
date = db.Column(db.DateTime)
def set_password(self, password):
"""Create hashed password."""
if password is None or password == '':
self.password = ''
else:
self.password = generate_password_hash(password, method='sha256')
def check_password(self, password):
"""Check hashed password."""
if self.password == '':
return True
return check_password_hash(self.password, password)