Exemple #1
0
class Posts(db.Model):
    """ This class represents Posts in blog
    post_id: is a unique identification of a Post in DB, integer, auto increment.
    post_date_creation: datetime
    post_reading_time: string
    author_id: integer, relation type with User class one to many
    category_id: integer, relation type with PostsCategory class one to many
    post_title: string, 120 characters max
    post_text: text
    is_post_published: Boolean, False as default value
    posts_claps: integer, amount of "likes" of this post's.

    Posts have a many2many relation with Tags class.
    """
    __tablename__ = "posts"

    post_id = db.Column(db.Integer, nullable=False, primary_key=True)
    post_date_creation = db.Column(db.Date, nullable=False, default=datetime.now())
    post_reading_time = db.Column(db.String)
    author_id = db.Column(db.Integer, db.ForeignKey(User.id), nullable=False)
    category_id = db.Column(db.Integer, db.ForeignKey(PostsCategory.category_id))
    post_title = db.Column(db.String(120), nullable=False)
    short_description = db.Column(db.String(300), default="")
    post_text = db.Column(db.String, nullable=False)
    is_post_published = db.Column(db.Boolean, nullable=False, default=False)
    posts_claps = db.Column(db.Integer, default=0)

    user = db.relationship(User, back_populates="posts", lazy="joined")
    post_category = db.relationship(PostsCategory, back_populates="posts")
    tags = db.relationship("Tags", secondary=tag_posts_relation, back_populates="posts")

    def __repr__(self):
        return f"Post{self.post_id} {self.post_title}"
Exemple #2
0
class User(db.Model, UserMixin):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    password = db.Column(db.String(255), nullable=False, server_default='')
    email = db.Column(db.String(120), unique=True, nullable=False)
    posts = db.relationship('Post')
    comments = db.relationship('Comments')

    roles = db.relationship('Role', secondary='user_roles')

    def __repr__(self):
        return f'User - {self.username}'
Exemple #3
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.png')
    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 "User {}, {}, {}".format(self.username, self.email,
                                        self.image_file)
Exemple #4
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 __repr__(self):
        return f"User('{self.username}', '{self.email}', '{self.image_file}')"
Exemple #5
0
class Post(db.Model):
    __tablename__ = 'posts'
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(80), nullable=False)
    body = db.Column(db.Text, nullable=False)
    pub_data = db.Column(db.DateTime, nullable=False,
                         default=datetime.utcnow)
    author = db.Column(db.String, db.ForeignKey('users.username'), nullable=False)

    comments = db.relationship('Comments')

    def __repr__(self):
        return f'Post - {self.title}'
Exemple #6
0
class Tags(db.Model):
    """ This class represents Tags in blog.
    tag_id: is a unique identification of a Tag in DB, integer, auto increment.
    tag_name: string, 120 characters max
    """
    __tablename__ = "tags"

    tag_id = db.Column(db.Integer, nullable=False, primary_key=True)
    tag_name = db.Column(db.String(120), nullable=False)

    posts = db.relationship(Posts, secondary=tag_posts_relation, back_populates="tags")

    def __repr__(self):
        return f"{self.tag_name}"
Exemple #7
0
class PostsCategory(db.Model):
    """ This class represents Posts categories available in blog.
    category_id: is a unique identification of a Post's Category in DB, integer, auto increment.
    category_name: string, can be repeated in DB
    """
    __tablename__ = "post_category"

    category_id = db.Column(db.Integer, nullable=False, primary_key=True)
    category_name = db.Column(db.String, nullable=False)

    posts = db.relationship("Posts", back_populates="post_category")

    def __repr__(self):
        return f"Post's Category Id #{self.category_id}, Category name {self.category_name}"
Exemple #8
0
class User(UserMixin, db.Model):
    """This class represents User in blog.
    id: is a unique identification of a User in DB, integer, auto increment.
    user_name: string max 30 characters, can be repeated in DB
    """
    __tablename__ = "users"

    id = db.Column(db.Integer, nullable=False, primary_key=True)
    user_name = db.Column(db.String(30), nullable=False)
    email = db.Column(db.String(100), unique=True)
    is_admin = db.Column(db.Boolean, default=False)
    password_hash = db.Column(db.String(100), nullable=False)

    posts = db.relationship("Posts", back_populates="user")

    def set_password_hash(self, password):
        self.password_hash = generate_password_hash(password)

    def check_password(self, password):
        return check_password_hash(self.password_hash, password)

    def __str__(self):
        return f"{self.user_name}"
Exemple #9
0
class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), index=True, unique=True)
    email = db.Column(db.String(120), index=True, unique=True)
    password_hash = db.Column(db.String(128))
    # to get all posts of a user
    posts = db.relationship('Post', backref='author', lazy='dynamic')
    about_me = db.Column(db.String(500))
    last_seen = db.Column(db.DateTime, default=datetime.utcnow)

    followed = db.relationship('User',
                               secondary=followers,
                               primaryjoin=(followers.c.follower_id == id),
                               secondaryjoin=(followers.c.followed_id == id),
                               backref=db.backref('followers', lazy='dynamic'),
                               lazy='dynamic')

    def set_password(self, password):
        self.password_hash = generate_password_hash(password)

    def check_password(self, password):
        return check_password_hash(self.password_hash, password)

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

    def follow(self, user):
        if not self.is_following(user):
            self.followed.append(user)

    def unfollow(self, user):
        if self.is_following(user):
            self.followed.remove(user)

    def is_following(self, user):
        return self.followed.filter(
            followers.c.followed_id == user.id).count() > 0

    def followed_posts(self):
        followed = Post.query.join(
            followers, (followers.c.followed_id == Post.user_id)).filter(
                followers.c.follower_id == self.id)
        own = Post.query.filter_by(user_id=self.id)
        return followed.union(own).order_by(Post.timestamp.desc())

    def get_reset_password_token(self, expires_in=600):
        return jwt.encode(
            {
                'reset_password': self.id,
                'exp': time() + expires_in
            },
            current_app.config['SECRET_KEY'],
            algorithm='HS256').decode('utf-8')

    @staticmethod
    def verify_reset_password_token(token):
        try:
            id = jwt.decode(token,
                            current_app.config['SECRET_KEY'],
                            algorithms='HS256')['reset_password']
        except:
            return
        return User.query.get(id)

    def __repr__(self):
        return '<User {}>'.format(self.username)
Exemple #10
0
class User(PaginatedAPIMixin, UserMixin,
           db.Model):  # 数据库表命是snake case 如user,GoodList-->good_list
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), index=True, unique=True)
    password_hash = db.Column(db.String(128))
    email = db.Column(db.String(120), index=True, unique=True)
    about_me = db.Column(db.String(100))
    last_seen = db.Column(db.DateTime, default=datetime.utcnow)
    token = db.Column(db.String(32), index=True, unique=True)
    token_expiration = db.Column(db.DateTime)
    posts = db.relationship(
        'Post', backref='author',
        lazy='dynamic')  # 声明一对多的关系 arg1代表many方,backref p.author=the user
    # 声明多对多的关系,followed并不是user实例的属性,是sqlAlchemy对象
    # A---followed---followers---follower--->B(暂定)
    # arg1=B,class User=A
    # secondary:the relationship
    # backref定义了如何从右侧实体来访问relationship
    # lazy就是右侧对象sql查询模式,直到具体请求才查询
    # 最后一个参数是左侧的查询模式
    # user1.followed.append(user2) 1关注了2
    followed = db.relationship('User',
                               secondary=followers,
                               primaryjoin=(followers.c.follower_id == id),
                               secondaryjoin=(followers.c.followed_id == id),
                               backref=db.backref('followers', lazy='dynamic'),
                               lazy='dynamic')
    message_sent = db.relationship('Message',
                                   foreign_keys='Message.sender_id',
                                   backref='author',
                                   lazy='dynamic')
    message_received = db.relationship('Message',
                                       foreign_keys='Message.receiver_id',
                                       backref='recipient',
                                       lazy='dynamic')
    last_message_read_time = db.Column(db.DateTime)
    notifications = db.relationship('Notification',
                                    backref='user',
                                    lazy='dynamic')

    tasks = db.relationship('Task', backref='user', lazy='dynamic')

    def set_password(self, password):
        self.password_hash = generate_password_hash(password)

    def check_password(self, password):
        return check_password_hash(self.password_hash, password)

    def get_token(self, expires_in=3600):
        """
        返回当前用户的token
        :param expires_in:
        :return:
        """
        now = datetime.utcnow()
        if self.token and self.token_expiration > now + timedelta(seconds=60):
            return self.token
        self.token = base64.b64decode(os.urandom(24)).decode('utf-8')
        self.token_expiration = now + timedelta(seconds=expires_in)
        db.session.add(self)
        return self.token

    def revoke_token(self):
        self.token_expiration = datetime.utcnow() - timedelta(seconds=1)

    def avatar(self, size):  # 利用这个网址生成头像,默认是identicon
        digest = md5(self.email.lower().encode('utf-8')).hexdigest()
        return 'https://www.gravatar.com/avatar/{}?d=identicon&s={}'.format(
            digest, size)

    def follow(self, user):
        if not self.is_following(user):
            self.followed.append(user)

    def unfollow(self, user):
        if self.is_following(user):
            self.followed.remove(user)

    def is_following(self, user):
        return self.followed.filter(
            followers.c.followed_id == user.id).count() > 0

    def followed_posts(self):
        """
        查询关注的所有人的博客以及自己的博客
        """
        followed_posts = Post.query.join(
            followers, (followers.c.followed_id == Post.user_id)).filter(
                followers.c.follower_id == self.id)
        own_posts = Post.query.filter_by(user_id=self.id)
        return followed_posts.union(own_posts).order_by(Post.timestamp.desc())

    def get_reset_password_token(self, expires_in=600):
        """
        生成token,有效期10分钟
        """
        return jwt.encode(
            {
                'reset_password': self.id,
                'exp': expires_in + time()
            },
            current_app.config['SECRET_KEY'],
            algorithm='HS256').decode('utf-8')

    def new_messages(self):
        last_read_time = self.last_message_read_time or datetime(1900, 1, 1)
        return Message.query.filter_by(recipient=self).filter(
            Message.timestamp > last_read_time).count()

    def add_notification(self, name, data):
        self.notifications.filter_by(name=name).delete()
        n = Notification(name=name, payload_json=json.dumps(data), user=self)
        db.session.add(n)
        return n

    def launch_task(self, name, description, *args, **kwargs):
        """
        添加一个任务到队列中
        :param name: 任务函数名,如blog_app.tasks.example
        :param description: 呈现给用户的简要描述
        :param args: 任务函数需要的参数
        :param kwargs:
        :return:
        """
        rq_job = current_app.task_queue.enqueue('blog_app.tasks.' + name,
                                                self.id, *args, **kwargs)
        task = Task(id=rq_job.get_id(),
                    name=name,
                    description=description,
                    user=self)
        db.session.add(task)
        return task

    def get_tasks_in_progress(self):
        return Task.query.filter_by(user=self, complete=False).all()

    def get_task_in_progress(self, name):
        return Task.query.filter_by(name=name, user=self,
                                    complete=False).first()

    def to_dict(self, include_email=False):
        last_seen = self.last_seen.isoformat(
        ) + 'Z' if self.last_seen else 'no data'

        data = {
            'id': self.id,
            'username': self.username,
            'last_seen': last_seen,
            'about_me': self.about_me,
            'post_count': self.posts.count(),
            'follower_count': self.followers.count(),
            'followed_count': self.followed.count(),
            '_links': {
                'self': url_for('api.get_user', id=self.id),
                'followers': url_for('api.get_followers', id=self.id),
                'followed': url_for('api.get_followed', id=self.id),
                'avatar': self.avatar(128)
            }
        }
        if include_email:
            data['email'] = self.email
        return data

    def from_dict(self, data, new_user=False):
        for filed in ['username', 'email', 'about_me']:
            if filed in data:
                setattr(self, filed, data[filed])  # 修改这些值
        if new_user and 'password' in data:
            self.set_password(data['password'])

    @staticmethod
    def verify_reset_password_token(token):
        """
        验证token是否有效
        """
        try:
            id = jwt.decode(token,
                            current_app.config['SECRET_KEY'],
                            algorithms=['HS256'])['reset_password']
        except:
            return
        return User.query.get(id)

    @staticmethod
    def check_token(token):
        user = User.query.filter_by(token=token).first()
        if user is None or user.token_expiration < datetime.utcnow():
            return None
        return user

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