def unfollow(username): user = User.query.filter_by(username=username).first_or_404() if not current_user.is_following(user): flash('未关注该用户!', 'info') return redirect_back() current_user.unfollow(user) return redirect_back()
def follow(username): user = User.query.filter_by(username=username).first_or_404() if current_user.is_following(user): flash('该用户已关注!', 'info') return redirect_back() current_user.follow(user) return redirect_back()
def uncollect(post_id): post = Post.query.get_or_404(post_id) if not current_user.is_collecting(post): flash('该文章未收藏!', 'info') return redirect_back() current_user.uncollect(post) return redirect_back()
def edit_category(category_id): form = EditCategoryForm() if form.validate_on_submit(): new_category_name = form.name.data category = Category.query.get_or_404(category_id) category.name = new_category_name db.session.commit() flash('分类修改完成!', 'success') return redirect_back() flash('分类名已存在或输入错误', 'warning') return redirect_back()
def search(): # 先查用户,再查文章 q = request.args.get('q', '').strip() if q == '': return redirect_back() result = [] page = request.args.get('page', 1, type=int) per_page = current_app.config['YOUNG_BLOG_PER_PAGE'] users = User.query.whooshee_search(q).all() posts = Post.query.whooshee_search(q).order_by(Post.timestamp.desc()).all() result.extend(users) result.extend(posts) pager_obj = SelfPagination(current_page=page, total_count=len(result), base_url=request.path, params=request.args, per_page_count=per_page) index_list = result[pager_obj.start:pager_obj.end] html = pager_obj.page_html() return render_template('main/search.html', q=q, result=result, index_list=index_list, html=html)
def re_authenticate(): if login_fresh(): return redirect(url_for('main.index')) form = LogInForm() if form.validate_on_submit() and current_user.validate_password( form.password.data): confirm_login() return redirect_back() return render_template('auth/login.html', form=form)
def new_category(): form = CategoryForm() if form.validate_on_submit(): name = form.name.data category = Category(name=name) db.session.add(category) db.session.commit() flash('分类创建成功!', 'success') return redirect_back() flash('分类名已存在或输入错误!', 'warning') return redirect(url_for('.manage_category'))
def login(): if current_user.is_authenticated: return redirect(url_for('main.index')) form = LogInForm() if form.validate_on_submit(): user = User.query.filter_by(email=form.email.data).first() if user is not None and user.validate_password(form.password.data): login_user(user=user, remember=form.remember.data) # flash('登录成功!', 'info') return redirect_back() flash('账号或密码错误!', 'warning') return render_template('auth/login.html', form=form)
def del_post(post_id): post = Post.query.get_or_404(post_id) db.session.delete(post) db.session.commit() flash('文章删除成功', 'success') return redirect_back()
def delete_comment(comment_id): comment = Comment.query.get_or_404(comment_id) db.session.delete(comment) db.session.commit() flash('评论删除成功!', 'success') return redirect_back()
def del_category(category_id): category = Category.query.get_or_404(category_id) db.session.delete(category) db.session.commit() flash('分类删除成功!', 'warning') return redirect_back()