def paginate(): page = int(request.args['page']) tag = request.args['tag'] status = 'No good' message = "We're having trouble loading our resources right now! Please try again later." try: if tag is not None and tag != "all": tag = tag.lower() posts = list(reversed(list(Post.getManyByTag(tag)))) else: posts = list(reversed(list(Post.getAll()))) first_index = (page - 1) * POST_CONSTANTS.MAX_RESOURCES_PER_PAGE last_index = page * POST_CONSTANTS.MAX_RESOURCES_PER_PAGE posts = posts[first_index:last_index] if posts is not None: status = "OK" message = "Success!!!" for post in posts: post['_id'] = str(post['_id']) post['user_id'] = str(post['user_id']) return jsonify({ "status": status, "message": message, "posts": posts, "page": page }) except Exception as e: if len(str(e)) != 0: message = str(e) return jsonify({"status": status, "message": message})
def all(tag): if tag is not None: posts = list(reversed(list(Post.getManyByTag(tag)))) else: tag = 'all' posts = list(reversed(list(Post.getAll()))) num_pages = math.ceil(len(posts) / POST_CONSTANTS.MAX_RESOURCES_PER_PAGE) max_pages_shown = POST_CONSTANTS.MAX_PAGES_SHOWN if num_pages >= POST_CONSTANTS.MAX_PAGES_SHOWN else num_pages page = 1 if 'page' in request.args.keys(): if int(request.args['page']) <= num_pages: page = int(request.args['page']) else: page = num_pages first_index = (page - 1) * POST_CONSTANTS.MAX_RESOURCES_PER_PAGE last_index = page * POST_CONSTANTS.MAX_RESOURCES_PER_PAGE posts = posts[first_index:last_index] tags = list(Post.getAllTags()) for i in range(len(tags)): tags[i] = tags[i]['tag'].capitalize() css = [{'prefix': 'css/compiled/', 'name': 'resources'}] return render_template('resources.html', posts=posts, tags=tags, css=css, selectedTag=tag, page=page, max_per_page=POST_CONSTANTS.MAX_RESOURCES_PER_PAGE, num_pages=num_pages, max_pages_shown=max_pages_shown)
def search(): input_tag = request.args[ 'input-tag'] if 'input-tag' in request.args else None select_tag = request.args[ 'select-tag'] if 'select-tag' in request.args else None tag = None status = "No Good" message = "We're having trouble loading our resources right now! Please try again later." try: if input_tag is not None: tag = input_tag elif select_tag is not None: tag = select_tag if tag is not None and tag != "all": tag = tag.lower() posts = list(reversed(list(Post.getManyByTag(tag)))) else: posts = list(reversed(list(Post.getAll()))) num_pages = math.ceil( len(posts) / POST_CONSTANTS.MAX_RESOURCES_PER_PAGE) max_pages_shown = POST_CONSTANTS.MAX_PAGES_SHOWN if num_pages >= POST_CONSTANTS.MAX_PAGES_SHOWN else num_pages posts = posts[:POST_CONSTANTS.MAX_RESOURCES_PER_PAGE] if posts is not None: status = "OK" message = "Success!!!" for post in posts: post['_id'] = str(post['_id']) post['user_id'] = str(post['user_id']) return jsonify({ "status": status, "message": message, "posts": posts, "num_pages": num_pages, "max_pages_shown": max_pages_shown }) except Exception: return jsonify({"status": status, "message": message})