def get_labels(): """ 请求文章分类标签 """ labels = Label.query.all() data = {'labels': [label.to_json() for label in labels]} return response(data)
def cancel_favourite_blog(blog_id): """ 取消喜欢某篇文章 """ favourite = Favourite.query.filter_by(blog_id=blog_id).first_or_404() db.session.delete(favourite) db.session.commit() return response()
def favourite_blog(blog_id): """ 喜欢某篇文章 """ blog = Blog.query.get_or_404(blog_id) favourite = Favourite(user=g.current_user, blog=blog) db.session.add(favourite) db.session.commit() return response()
def add_comment(blog_id): """ 提交评论 """ content = request.args.get('content') if content is None or content == '': raise ValidationError('评论内容不能为空') blog = Blog.query.get_or_404(blog_id) comment = Comment(content=content, blog=blog, user=g.current_user) db.session.add(comment) db.session.commit() return response()
def get_favourites(user_id): """ 分页请求对应id的用户喜欢的文章 """ page = request.args.get('page', 1, type=int) user = User.query.get_or_404(user_id) pagination = user.favourites.order_by(Favourite.timestamp.desc()).paginate( page=page, per_page=current_app.config['PER_PAGE_10'], error_out=False) favourites = pagination.items data = { 'favourites': [favourite.to_json() for favourite in favourites], 'current_page': page, 'total_page': pagination.total } return response(data)
def get_user_comments(user_id): """ 分页请求对应id的用户发表的评论 """ page = request.args.get('page', 1, type=int) user = User.query.get_or_404(user_id) pagination = user.comments.order_by(Comment.timestamp.desc()).paginate( page=page, per_page=current_app.config['PER_PAGE_10'], error_out=False) comments = pagination.items data = { 'comments': [comment.to_json() for comment in comments], 'current_page': page, 'total_page': pagination.total } return response(data)
def get_blogs(): """ 移动端分页请求blog列表的api """ page = request.args.get('page', 1, type=int) pagination = Blog.query.filter_by(draft=False).order_by( Blog.publish_date.desc()).paginate( page=page, per_page=current_app.config['PER_PAGE_10'], error_out=False) blogs = pagination.items data = { 'blogs': [blog.to_json() for blog in blogs], 'current_page': page, 'total_page': pagination.total } return response(data)
def login(): """ 登录 """ email = request.args.get('email') password = request.args.get('password') if email is None: raise ValidationError('邮箱不能为空') if password is None: raise ValidationError('密码不能为空') user = User.query.filter_by(email=email).first() if user is None: raise ValidationError('邮箱不存在') if user.verify_password(password): return response(user.to_json()) raise ValidationError('密码错误')
def get_blogs_by_label(label_id): """ 按照标签分页请求blog """ page = request.args.get('page', 1, type=int) label = Label.query.filter_by(id=label_id).first() pagination = label.blogs.filter_by(draft=False).order_by( Blog.publish_date.desc()).paginate( page=page, per_page=current_app.config['PER_PAGE_10'], error_out=False) blogs = pagination.items data = { 'blogs': [blog.to_json() for blog in blogs], 'current_page': page, 'total_page': pagination.total } return response(data)
def register(): """ 注册,省去了PC端注册时邮件验证的步骤 """ email = request.args.get('email') password = request.args.get('password') username = request.args.get('username') if email is None: raise ValidationError('邮箱不能为空') if password is None: raise ValidationError('密码不能为空') if username is None: raise ValidationError('昵称不能为空') user = User(email=email, username=username, password=password, confirmed=True) db.session.add(user) db.session.commit() return response()
def get_blog(blog_id): """ 根据id请求文章详情 """ blog = Blog.query.get_or_404(blog_id) return response(blog.to_json())