コード例 #1
0
ファイル: models.py プロジェクト: carlosbelen/Thrive
class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key = True)
    # each of our usernames have to be unique and it cant be empty:
    username = db.Column(db.String(150), nullable = False, unique=True)
    email = db.Column(db.String(150), nullable = False, unique = True)
    password = db.Column(db.String(256), nullable = False)
    # 'lazy' this is only loaded when "we need it" or reqquested
    post = db.relationship('Post', backref = 'author', lazy = True)

    def __init__(self,username,email,password):
        self.username = username
        self.email = email
        # this will start our encryption method:
        self.password = self.set_password(password)
    
    def set_password(self,password):
        """
            Grab the password that is passed into the method
            return the hashed verion of the passowrd
            which will be stored inside the database

        """
        self.pw_hash = generate_password_hash(password)
        return self.pw_hash
    
    # The following is needed to see when things go into our database, or object gets created so we can than use it:
    def __repr__(self):
        return f'{self.username} has been created with the following email: {self.email}'
コード例 #2
0
class Record(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    calories =  db.Column(db.String(20))
    food_item = db.Column(db.String(20), nullable=False)
    date_posted = db.Column(db.DateTime)
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'))

    def __repr__(self):
        # return f "Record({self.id}, food_item : {self.food_item}, calories : {self.calories}, time : {self.time})"
        return self.food_item
コード例 #3
0
ファイル: models.py プロジェクト: carlosbelen/Thrive
class Post(db.Model):
    id = db.Column(db.Integer, primary_key = True)
    title = db.Column(db.String(100))
    content = db.Column(db.String(300))
    date_created = db.Column(db.DateTime, nullable = False, default = datetime.utcnow)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable = False)

    def __init__(self,title,content,user_id):
        self.title = title
        self.content = content
        self.user_id = user_id

    def __repr__(self):
        return f'The title of the post is {self.title} \n and the content is {self.content}'
コード例 #4
0
class User(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    #TODO : add firstname and lastname instead of username
    username = db.Column(db.String(20), unique=True, nullable=False)
    email = db.Column(db.String(20), unique=True, nullable=False)
    password = db.Column(db.String(60), nullable=False)
    image_file = db.Column(db.String(20))
    no_of_failed_attemps = db.Column(db.Integer)
    is_account_blocked = db.Column(db.Integer)
    is_account_verified = db.Column(db.Integer)
    # Relationships
    roles = db.relationship('Role', secondary='user_roles')
    records = db.relationship('Record', backref='user', lazy='dynamic')


    def __repr__(self):
        # return f"User({self.username}, {self.email}, {self.image_file})"\
        return self.username


    def __str__(self):
        # return f"User({self.username}, {self.email}, {self.image_file})"\
        return self.username
コード例 #5
0
class User(UserMixin, Base):
    __tablename__ = 'users'
    id            = db.Column(db.Integer, primary_key = True)
    first_name    = db.Column(db.String(256), nullable = False)
    last_name     = db.Column(db.String(256), nullable = False)
    email         = db.Column(db.String(256), unique = True, nullable = False)
    password_hash = db.Column(db.String(1024), nullable = False)

    @property
    def password(self):
        # Makes password inaccessible
        raise AttributeError('password is not readable')

    @password.setter
    def password(self, password):
        # Set password to hash
        self.password_hash = generate_password_hash(password)
    
    def check_password(self, password):
        # Checks if password is actually equal to each other through hash
        return check_password_hash(self.password_hash, password)

    def __repr__(self):
        return '<Email: {}>'.format(self.email)
コード例 #6
0
class Role(db.Model):
    __tablename__ = 'roles'
    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String(50), unique=True)