<gh_stars>0
from sqlalchemy import ForeignKey
from . import db
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import UserMixin
from . import login_manager
from datetime import datetime

@login_manager.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))

class User(UserMixin, db.Model):
    __tablename__ = 'users'

    id = db.Column(db.Integer, primary_key = True)
    username = db.Column(db.String(255),index = True)
    email = db.Column(db.String(255), unique = True, index = True)
    role_id = db.Column(db.Integer, db.ForeignKey('role.id'))
    bio = db.Column(db.String(255))
    profile_pic_path = db.Column(db.String(255))
    pass_secure = db.Column(db.String(255))
    pitches = db.relationship('Pitches',backref = 'pitcher',lazy = "dynamic")
    comments_id = db.relationship('Comments', backref = 'commenter', lazy ='dynamic')

    @property
    def password(self):
        raise AttributeError('You cannot read the password attribute')

    @password.setter
    def password(self, password) :
        self.pass_secure = generate_password_hash(password) 

    def verify_password(self,password):
        return check_password_hash(self.pass_secure, password)     

    def __repr__(self):
        return f'User{self.username}'
    
class Role(db.Model):
    __tablename__ = 'role'  

    id = db.Column(db.Integer, primary_key = True)
    name = db.Column(db.String(255))
    users = db.relationship('User', backref ='role', lazy = 'dynamic')

    def __repr__(self):
        return f'User {self.name}'

class Pitches(db.Model):
    __tablename__ = 'pitches'

    id = db.Column(db.Integer, primary_key = True)
    title = db.Column(db.String(255))
    content = db.Column(db.String(255))
    date_posted = db.Column(db.DateTime,default=datetime.utcnow)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
    comments_id = db.relationship('Comments', backref = 'comments', lazy ='dynamic')
    category_id = db.Column(db.Integer, db.ForeignKey('category.id'))

    def save_pitch(self):
        db.session.add(self)
        db.session.commit() 

    @classmethod    
    def get_pitch (cls, id):
        pitches = Pitches.query.filter_by(user_id = id).all()
        return pitches

class Comments(db.Model):
    __tablename__ = 'comments'

    id = db.Column(db.Integer, primary_key = True)
    comment = db.Column(db.String(255))
    date_posted = db.Column(db.DateTime,default=datetime.utcnow)
    pitch_id = db.Column(db.Integer, db.ForeignKey('pitches.id'))
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'))

    def save_comment(self):
        db.session.add(self)
        db.session.commit()

    @classmethod
    def get_comment(cls, id) :
        comments = Comments.query.filter_by(pitch_id = id).all()
        return comments  

class Category(db.Model) :
    __tablename__ = 'category'

    id = db.Column(db.Integer, primary_key = True)
    category = db.Column(db.String(255))
    pitch_id = db.relationship('Pitches', backref = 'categories', lazy = 'dynamic')

from sqlalchemy import ForeignKey
from . import db
from werkzeug.security import generate_password_hash, check_password_hash
from flask_login import UserMixin
from . import login_manager
from datetime import datetime

@login_manager.user_loader
def load_user(user_id):
    return User.query.get(int(user_id))

class User(UserMixin, db.Model):
    __tablename__ = 'users'

    id = db.Column(db.Integer, primary_key = True)
    username = db.Column(db.String(255),index = True)
    email = db.Column(db.String(255), unique = True, index = True)
    role_id = db.Column(db.Integer, db.ForeignKey('role.id'))
    bio = db.Column(db.String(255))
    profile_pic_path = db.Column(db.String(255))
    pass_secure = db.Column(db.String(255))
    pitches = db.relationship('Pitches',backref = 'pitcher',lazy = "dynamic")
    comments_id = db.relationship('Comments', backref = 'commenter', lazy ='dynamic')

    @property
    def password(self):
        raise AttributeError('You cannot read the password attribute')

    @password.setter
    def password(self, password) :
        self.pass_secure = generate_password_hash(password) 

    def verify_password(self,password):
        return check_password_hash(self.pass_secure, password)     

    def __repr__(self):
        return f'User{self.username}'
    
class Role(db.Model):
    __tablename__ = 'role'  

    id = db.Column(db.Integer, primary_key = True)
    name = db.Column(db.String(255))
    users = db.relationship('User', backref ='role', lazy = 'dynamic')

    def __repr__(self):
        return f'User {self.name}'

class Pitches(db.Model):
    __tablename__ = 'pitches'

    id = db.Column(db.Integer, primary_key = True)
    title = db.Column(db.String(255))
    content = db.Column(db.String(255))
    date_posted = db.Column(db.DateTime,default=datetime.utcnow)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
    comments_id = db.relationship('Comments', backref = 'comments', lazy ='dynamic')
    category_id = db.Column(db.Integer, db.ForeignKey('category.id'))

    def save_pitch(self):
        db.session.add(self)
        db.session.commit() 

    @classmethod    
    def get_pitch (cls, id):
        pitches = Pitches.query.filter_by(user_id = id).all()
        return pitches

class Comments(db.Model):
    __tablename__ = 'comments'

    id = db.Column(db.Integer, primary_key = True)
    comment = db.Column(db.String(255))
    date_posted = db.Column(db.DateTime,default=datetime.utcnow)
    pitch_id = db.Column(db.Integer, db.ForeignKey('pitches.id'))
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'))

    def save_comment(self):
        db.session.add(self)
        db.session.commit()

    @classmethod
    def get_comment(cls, id) :
        comments = Comments.query.filter_by(pitch_id = id).all()
        return comments  

class Category(db.Model) :
    __tablename__ = 'category'

    id = db.Column(db.Integer, primary_key = True)
    category = db.Column(db.String(255))
    pitch_id = db.relationship('Pitches', backref = 'categories', lazy = 'dynamic')