# project/server/models.py


from flask import current_app

from project.server import db, bcrypt


class User(db.Model):

    __tablename__ = 'users'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    email = db.Column(db.String(255), unique=True, nullable=False)
    password = db.Column(db.String(255), nullable=False)
    admin = db.Column(db.Boolean, nullable=False, default=False)
    projects = db.relationship('Project', backref='users', lazy=True)

    def __init__(self, email, password, admin=False):
        self.email = email
        self.password = <PASSWORD>.generate_password_hash(
            password, current_app.config.get('BCRYPT_LOG_ROUNDS')
        ).decode('utf-8')
        self.admin = admin

    def is_authenticated(self):
        return True

    def is_active(self):
        return True

    def is_anonymous(self):
        return False

    def get_id(self):
        return self.id

    def __repr__(self):
        return '<User {0}>'.format(self.email)


class Project(db.Model):

    __tablename__ = 'projects'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(120), nullable=False)
    url = db.Column(db.String, nullable=False)
    status = db.Column(db.Boolean, nullable=False, default=False)
    user_id = db.Column(
        db.Integer,
        db.ForeignKey('users.id'),
        nullable=False
    )
    builds = db.relationship('Build', backref='builds', lazy=True)

    def __init__(self, user_id, name, url, status=False):
        self.user_id = user_id
        self.name = name
        self.url = url
        self.status = status


class Build(db.Model):

    __tablename__ = 'builds'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    status = db.Column(db.Boolean, nullable=False)
    datetime = db.Column(db.DateTime, nullable=False)
    project_id = db.Column(
        db.Integer,
        db.ForeignKey('projects.id'),
        nullable=False
    )

    def __init__(self, project_id, status, datetime):
        self.project_id = project_id
        self.status = status
        self.datetime = datetime

    def to_json(self):
        return {
            'id': self.id,
            'project_id': self.project_id,
            'status': self.status,
            'datetime': self.datetime
        }

# project/server/models.py


from flask import current_app

from project.server import db, bcrypt


class User(db.Model):

    __tablename__ = 'users'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    email = db.Column(db.String(255), unique=True, nullable=False)
    password = db.Column(db.String(255), nullable=False)
    admin = db.Column(db.Boolean, nullable=False, default=False)
    projects = db.relationship('Project', backref='users', lazy=True)

    def __init__(self, email, password, admin=False):
        self.email = email
        self.password = <PASSWORD>.generate_password_hash(
            password, current_app.config.get('BCRYPT_LOG_ROUNDS')
        ).decode('utf-8')
        self.admin = admin

    def is_authenticated(self):
        return True

    def is_active(self):
        return True

    def is_anonymous(self):
        return False

    def get_id(self):
        return self.id

    def __repr__(self):
        return '<User {0}>'.format(self.email)


class Project(db.Model):

    __tablename__ = 'projects'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    name = db.Column(db.String(120), nullable=False)
    url = db.Column(db.String, nullable=False)
    status = db.Column(db.Boolean, nullable=False, default=False)
    user_id = db.Column(
        db.Integer,
        db.ForeignKey('users.id'),
        nullable=False
    )
    builds = db.relationship('Build', backref='builds', lazy=True)

    def __init__(self, user_id, name, url, status=False):
        self.user_id = user_id
        self.name = name
        self.url = url
        self.status = status


class Build(db.Model):

    __tablename__ = 'builds'

    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    status = db.Column(db.Boolean, nullable=False)
    datetime = db.Column(db.DateTime, nullable=False)
    project_id = db.Column(
        db.Integer,
        db.ForeignKey('projects.id'),
        nullable=False
    )

    def __init__(self, project_id, status, datetime):
        self.project_id = project_id
        self.status = status
        self.datetime = datetime

    def to_json(self):
        return {
            'id': self.id,
            'project_id': self.project_id,
            'status': self.status,
            'datetime': self.datetime
        }

# project/server/models.py