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 new(): if request.method == 'POST': title = request.form['title'] description = request.form['description'] body = request.form['body'].strip() tag = request.form['tag'] videoURL = request.form['videoURL'] status = "No Good" message = "Error: item not inserted" if not title or not description or not tag or (not body and not videoURL): return {"status": status, "message": message} try: post_added = Post.new(title, description, body, tag, videoURL) if post_added: status = "OK" message = "Success!!!" return jsonify({ "status": status, "message": message, "body": body, "tag": tag, "videoURL": videoURL, "timestamp": datetime.datetime.now() }) except PostErrors.PostError as e: return e.message else: tags = list(Post.getAllTags()) for i in range(len(tags)): tags[i] = tags[i]['tag'] return render_template('posts/new.html', tags=tags)
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 new_post(self, title, content, date=datetime.datetime.utcnow()): post = Post(blog_id=self._id, title=title, content=content, author=self.author, created_date=date) post.save_to_mongo()
def create_new_post(blog_id): if request.method == 'GET': return render_template('new_post.html', blog_id=blog_id) else: title = request.form['title'] content = request.form['content'] user = User.get_by_email(session['email']) new_post = Post(blog_id, title, content, user.email) new_post.save_to_mongo() return make_response(blog_posts(blog_id))
def profile(user): user_id = user['_id'] email = session['email'] posts = reversed(list(Post.getAllByUserId(user_id))) # posts = reversed(list(Post.getAll())) tags = list(Post.getAllTags()) for i in range(len(tags)): tags[i] = tags[i]['tag'] return render_template('users/profile.html', email=email, posts=posts, tags=tags)
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})
def delete(id): status = "No Good" message = "Error: could not delete post" try: post_deleted = Post.delete(id) if post_deleted: return redirect(url_for('posts.all')) return jsonify({ "status": status, "message": message, "timestamp": datetime.datetime.now() }) except PostErrors.PostError as e: return e.message
def deleteTag(): status = "No Good" message = "Error: could not delete post" tag = request.form['tag'] try: tag_deleted = Post.deleteTag(tag) if tag_deleted: status = 'OK' message = 'Sucess!!!' return jsonify({ "status": status, "message": message, "timestamp": datetime.datetime.now() }) except PostErrors.PostError as e: return e.message
def addTag(): if request.method == 'POST': tag = request.form['tag'].lower() status = "No Good" message = "Error: tag not inserted" if not tag: return {"status": status, "message": message} try: tag_added = Post.addTag(tag) if tag_added: status = "OK" message = "Success!!!" return jsonify({ "status": status, "message": message, "tag": tag, "timestamp": datetime.datetime.now() }) except PostErrors.PostError as e: return e.message else: return render_template('posts/add-tag.html')
def view(id): post = Post.getOneById(id) if post: css = [{'prefix': 'css/compiled/', 'name': 'resource'}] return render_template('resource.html', post=post, css=css)
def tagEditor(): tags = list(Post.getAllTags()) for i in range(len(tags)): tags[i] = tags[i]['tag'] css = [{'prefix': 'css/compiled/', 'name': 'tag-editor'}] return render_template('posts/tag-editor.html', tags=tags, css=css)
def get_posts(self): return Post.from_blog(self._id)