Example #1
0
class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20), unique=True, nullable=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    image_file = db.Column(db.String(20),
                           nullable=False,
                           default='default.jpg')
    password = db.Column(db.String(60), nullable=False)
    posts = db.relationship('Post', backref='author', lazy=True)

    def get_reset_token(self, expires_sec=1800):
        s = Serializer(current_app.config['SECRET_KEY'], expires_sec)
        return s.dumps({'user_id': self.id}).decode('utf-8')

    @staticmethod
    def verify_reset_token(token):
        s = Serializer(current_app.config['SECRET_KEY'])
        try:
            user_id = s.loads(token)['user_id']
        except:
            return None
        return User.query.get(user_id)

    def __repr__(self):
        return f"User('{self.username}, {self.email}, {self.image_file}')"
Example #2
0
class Log(db.Model):
    __tablename__ = 'log'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), db.ForeignKey('user.username'))
    query = db.Column(db.String(140), nullable=False)

    #timestamp = db.Column(db.DateTime, primary_key=True)

    def __repr__(self):
        return '{}'.format(self.username)
Example #3
0
class User(db.Model):
    #id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64),
                         index=True,
                         primary_key=True,
                         nullable=False)
    email = db.Column(db.String(120), index=True, unique=True, nullable=False)
    password_hash = db.Column(db.String(64), nullable=False)

    def __repr__(self):
        return '<User {}>'.format(self.username)
Example #4
0
class User(db.Model):
    __tablename__ = 'User'

    Id = db.Column(GUID(), primary_key=True)
    Login = db.Column(db.String(140), index=True)
    password_hash = db.Column(db.String(200))
    FirstName = db.Column(db.String(140))
    LastName = db.Column(db.String(140))

    CreationDate = db.Column(db.DateTime)
    LastModificationDate = db.Column(db.DateTime)

    def hash_password(self, password):
        self.password_hash = pwd_context.encrypt(password)

    def verify_password(self, password):
        return pwd_context.verify(password, self.password_hash)

    def generate_auth_token(self, expiration=3600):
        s = Serializer(app.config['SECRET_KEY'], expires_in=expiration)
        IdString = "{0}".format(self.Id)
        return s.dumps({'Id': IdString})

    def __unicode__(self):
        return self.Login

    @staticmethod
    def verify_auth_token(token):
        s = Serializer(app.config['SECRET_KEY'])
        try:
            data = s.loads(token)
        except SignatureExpired:
            return None  # valid token, but expired
        except BadSignature:
            return None  # invalid token
        user = User.query.get(data['Id'])
        return user

    def __init__(self, *args, **kwargs):

        self.Login = kwargs.get('Login')
        self.FirstName = kwargs.get('FirstName')
        self.LastName = kwargs.get('LastName')

    def dict_format(self):
        return dict(Id=self.Id,
                    Login=self.Login,
                    FirstName=self.FirstName,
                    LastName=self.LastName,
                    CreationDate=self.CreationDate,
                    LastModificationDate=self.LastModificationDate)
class Role(db.Model):
    """
    Create a Role table
    """

    __tablename__ = 'roles'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(60), unique=True)
    description = db.Column(db.String(200))
    employees = db.relationship('Employee', backref='role', lazy='dynamic')

    def __repr__(self):
        return '<Role: {}>'.format(self.name)
Example #6
0
class User(UserMixin, db.Model):
    __tablename__ = 'user'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), index=True, nullable=False)
    email = db.Column(db.String(120), index=True, nullable=False)
    password_hash = db.Column(db.String(64), nullable=False)

    @staticmethod
    def set_password(password):
        key = sha256(password.encode()).hexdigest()
        password_hash = key
        return key

    def __repr__(self):
        return '<User {}>'.format(self.username)
Example #7
0
class History(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    search_query = db.Column(db.String(140))
    timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
    username = db.Column(db.Integer, db.ForeignKey('user.username'))

    def __repr__(self):
        return '<Query : {}>'.format(self.search_query)
Example #8
0
class Post(db.Model):
    """ Posts Details Class"""
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(50))
    content = db.Column(db.String(250))
    member_id = db.Column(db.Integer, db.ForeignKey("member.id"))

    def __repr__(self):
        return f'Post id : {self.id}\nPost title : {self.title}\nPost content : {self.content}'

    def as_dict(self):
        return {
            "id": self.id,
            "title": self.title,
            "content": self.content,
            "member_id": self.member_id,
            "date": self.date,
        }
Example #9
0
class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100), nullable=False)
    date_posted = db.Column(db.DateTime,
                            nullable=False,
                            default=datetime.utcnow)
    content = db.Column(db.Text, nullable=False)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)

    def __repr__(self):
        return f"User('{self.title}, {self.date_posted}')"
class Employee(UserMixin, db.Model):
    """
    Create an Employee table
    """

    # Ensures table will be named in plural and not in singular
    # as is the name of the model
    __tablename__ = 'employees'

    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(60), index=True, unique=True)
    username = db.Column(db.String(60), index=True, unique=True)
    first_name = db.Column(db.String(60), index=True)
    last_name = db.Column(db.String(60), index=True)
    password_hash = db.Column(db.String(128))
    department_id = db.Column(db.Integer, db.ForeignKey('departments.id'))
    role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))
    is_admin = db.Column(db.Boolean, default=False)

    @property
    def password(self):
        """
        Prevent pasword from being accessed
        """
        raise AttributeError('password is not a readable attribute.')

    @password.setter
    def password(self, password):
        """
        Set password to a hashed password
        """
        self.password_hash = generate_password_hash(password)

    def verify_password(self, password):
        """
        Check if hashed password matches actual password
        """
        return check_password_hash(self.password_hash, password)

    def __repr__(self):
        return '<Employee: {}>'.format(self.username)
Example #11
0
class Member(db.Model):
    """ Members Details Class"""

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50))
    age = db.Column(db.Integer)
    posts = db.relationship("Post", backref="members")

    def __repr__(self):
        return f'Member id : {self.id}\nMember Name : {self.name}\nMember Age : {self.age}'

    def as_dict(self):
        return {
            "id": self.id,
            "name": self.name,
            "age": self.age,
            "posts": self.posts,
        }