コード例 #1
0
ファイル: models.py プロジェクト: nuagesdencre/CI_DataCentric
class Story(db.Model):
    __tablename__ = 'stories'
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(60), nullable=False, unique=True, index=True)
    meaning = db.Column(db.Text, nullable=False)
    text = db.Column(db.Text)
    source = db.Column(db.String(100), default='None provided')
    # relationships
    # one user_id per entry
    users = db.relationship('User')
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
    # one category per entry(story, being)
    categories = db.relationship('Category')
    category_id = db.Column(db.Integer, db.ForeignKey('categories.id'))
    # many comments per entry
    comments = db.relationship('Comment', backref='story', lazy=True)

    def __init__(self, title, text, meaning, category_id, source, user_id):
        """
        Initialize story object
        """
        self.title = title
        self.text = text
        self.meaning = meaning
        self.category_id = category_id
        self.source = source
        self.user_id = user_id

    def __repr__(self):
        """
        Set story self-representation
        """
        return "Story title: {}".format(self.title)
コード例 #2
0
ファイル: models.py プロジェクト: nuagesdencre/CI_DataCentric
class Comment(db.Model):
    __tablename__ = 'comments'
    id = db.Column(db.Integer, primary_key=True)
    subject = db.Column(db.String(60), nullable=False)
    content = db.Column(db.Text)
    # relationships
    # one user_id per comment
    users = db.relationship('User')
    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
    # one entry per comment
    stories = db.relationship('Story')
    story_id = db.Column(db.Integer, db.ForeignKey('stories.id'))
    beings = db.relationship('Being')
    being_id = db.Column(db.Integer, db.ForeignKey('beings.id'))

    def __init__(self, subject, content, user_id, story_id, being_id):
        """
        Initialize comment object
        """
        self.subject = subject
        self.content = content
        self.user_id = user_id
        self.story_id = story_id if story_id is not None else None
        self.being_id = being_id if being_id is not None else None

    def __repr__(self):
        """
        Set comment self-representation
        """
        return "Comment '{}' made by {}".format(self.subject, self.user_id)
コード例 #3
0
ファイル: models.py プロジェクト: nuagesdencre/CI_DataCentric
class Category(db.Model):
    __tablename__ = 'categories'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(60), nullable=False, unique=True, index=True)
    description = db.Column(db.Text)
    # relationships
    # many entries(story, being) per category
    stories = db.relationship('Story', backref='category', lazy=True)
    beings = db.relationship('Being', backref='category', lazy=True)

    def __init__(self, name, description):
        """
        Initialize being object
        """
        self.name = name
        self.description = description

    def __repr__(self):
        """
        Set category self-representation
        """
        return "Category name: {}".format(self.name)
コード例 #4
0
ファイル: models.py プロジェクト: nuagesdencre/CI_DataCentric
class User(db.Model, UserMixin):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(36),
                         nullable=False,
                         unique=True,
                         index=True)
    email = db.Column(db.String(72), unique=True, index=True)
    psw_hash = db.Column(db.String(128))
    # relationships
    # One author for many entries - stories & beings - and comments
    stories = db.relationship('Story', backref='user', lazy=True)
    beings = db.relationship('Being', backref='user', lazy=True)
    comments = db.relationship('Comment', backref='user', lazy=True)

    def __init__(self, username, email, psw):
        """
        Initialize user object
        """
        self.username = username
        self.email = email
        self.psw_hash = generate_password_hash(psw)

    def __repr__(self):
        """
        Set user self-representation
        """
        return "Username {}".format(self.username)

    def set_password(self, psw):
        """
        Set a new password (useful for psw reset)
        """
        self.psw_hash = generate_password_hash(psw)

    # password check
    def psw_check(self, psw):
        """
        Check if the password hash of the inputted password
        matches the hash on file
        """
        return check_password_hash(self.psw_hash, psw)

    #
    def get_reset_password_token(self, expires_in=600):
        """
        Generate a JWT token as a string following a password reset request
        """
        return jwt.encode(
            {
                'reset_password': self.id,
                'exp': time() + expires_in
            },
            app.config['SECRET_KEY'],
            algorithm='HS256').decode('utf-8')

    @staticmethod
    def verify_reset_password_token(token):
        """
        Decode the JWT token associated to the password reset request
        """
        try:
            id = jwt.decode(token,
                            app.config['SECRET_KEY'],
                            algorithms=['HS256'])['reset_password']
        except:
            return
        return User.query.get(id)

    def avatar(self, size):
        """
        Import user avatars using gravatar
        """
        digest = md5(self.email.lower().encode('utf-8')).hexdigest()
        return 'https://www.gravatar.com/avatar/{}?d=identicon&s={}'.format(
            digest, size)