def top_contributors(): users = User.query.order_by(User.contribution.desc()).limit(10) form = SearchForm() return render_template('top_contributors.html', users=users, form=form, title="Top Contributors")
def get_resource(filename): form = SearchForm() try: file_path = os.path.join(current_app.root_path, 'static', 'resource_files') return send_from_directory(directory=file_path, path=filename, as_attachment=True) except FileNotFoundError: abort(404)
def user_posts(username): form = SearchForm() page = request.args.get('page', 1, type=int) user = User.query.filter_by(username=username).first_or_404() posts = (Post.query.filter_by(author=user).order_by( Post.date_posted.desc()).paginate(page=page, per_page=5)) return render_template('user_posts.html', posts=posts, form=form, user=user)
def bookmarked_posts(username): form = SearchForm() page = request.args.get('page', 1, type=int) user = User.query.filter_by(username=username).first_or_404() bookmarked_posts = Bookmark.query.filter_by(user_id=user.id).with_entities( Bookmark.post_id) posts = (Post.query.filter(Post.id.in_(bookmarked_posts)).order_by( Post.date_posted.desc()).paginate(page=page, per_page=5)) return render_template('bookmarked_posts.html', posts=posts, form=form, user=user, title="Bookmarks")
def search(type_sort=0): page = request.args.get('page', 1, type=int) if type_sort == 0: posts = Post.query.paginate(page=page, per_page=5) elif type_sort == 1: posts = Post.query.order_by(Post.date_posted.desc()).paginate( page=page, per_page=5) elif type_sort == 2: posts = db.session.query(Post).join(Upvote).filter( Post.id == Upvote.post_id).group_by(Post.id).order_by( func.count(Post.id).desc()).paginate(page=page, per_page=5) form = SearchForm() if form.validate_on_submit(): searched_val = form.searched.data if searched_val == None: return render_template('searched_post.html', posts=posts, form_val=form.searched.data) return render_template('searched_posts.html', posts=search_result(searched_val, page, 0), form=form, form_val=form.searched.data) searched_val = request.args.get('form_val') if searched_val == None: return render_template('searched_posts.html', posts=posts, form_val=searched_val) type = request.args.get('type') if type == "1": searched_val += ']' searched_val = '[' + searched_val return render_template('searched_posts.html', posts=search_result(searched_val, page, type_sort), form=form, form_val=searched_val, type_sort=type_sort)
def error_404(error): form = SearchForm() return render_template('errors/404.html', form=form), 404 #2nd return value is status code. Default return value is 200.
def error_500(error): form = SearchForm() return render_template('errors/500.html', form=form), 500
def error_403(error): form = SearchForm() return render_template('errors/403.html', form=form), 403
def layout(): form = SearchForm() return dict(form=form)