def dispatch_request(self, post_id: int): post = db_posts(id=post_id) comments = db_comments(post_id=post_id) user_loggedin = user_authenticated() form = CommentForm() if user_loggedin: if request.method == 'POST': if form.validate_on_submit(): comment = form.comment.data try: username = session['active_user']['username'] author = db_user(username=username) parent_post = db_posts(id=post_id) new_comment = Comment(comment=comment, parent_post=parent_post, author=author) db.session.add(new_comment) db.session.commit() flash('Your comment is successfully posted.', 'success') return redirect( url_for(request.endpoint, post_id=post_id)) except: flash('Some errors occurred.', 'error') return redirect( url_for(request.endpoint, post_id=post_id)) return render_template('post.html', post=post, comments=comments, form=form, user_loggedin=user_loggedin)
def dispatch_request(self, username: str): current_username = session['active_user']['username'] current_user = db_user(username=current_username) user_account = db_user(username=username) if not user_account: abort(403) #getting back to this later. This will delete everything. Need better functions to maintain content even users are deleted. if current_user.username == user_account.username or current_user.is_admin: user_comments = db_comments(author=user_account) user_posts = db_posts(author=user_account) for comment in user_comments: db.session.delete(comment) for post in user_posts: db.session.delete(post) db.session.delete(user_account) db.session.commit() if current_user.is_admin: flash(f'Account { username } is successfully deleted.', 'success') return redirect(url_for('users.Dashboard')) session.clear() flash(f'Account { username } is successfully deleted.', 'success') return redirect(url_for('users.Login'))
def dispatch_request(self): form = PostForm() username = session['active_user']['username'] author = db_user(username=username) posts = db_posts(author=author) if request.method == 'POST': if form.validate_on_submit(): title = form.title.data content = form.content.data tag = form.tag.data try: new_post = Post(author=author, title=title, content=content, tag=tag) db.session.add(new_post) db.session.commit() flash('Your post is successfully submitted', 'success') return redirect(url_for(request.endpoint)) except Exception as e: flash('Invalid inputs.', 'error') return redirect(url_for(request.endpoint)) return render_template('admin.html', username=username, form=form, posts=posts)
def dispatch_request(self, post_id: int): username = session['active_user']['username'] post = db_posts(id=post_id) if username != post.author.username: abort(403) form = PostForm() if request.method == 'POST': if form.validate_on_submit(): try: post.title = form.title.data post.content = form.content.data post.tag = form.tag.data db.session.commit() flash('Your post is successfully updated.', 'success') return redirect(url_for(request.endpoint, post_id=post_id)) except Exception as e: flash('Invalid update', 'error') return redirect(url_for(request.endpoint, post_id=post_id)) form.title.data = post.title form.content.data = post.content form.tag.data = post.tag return render_template('editpost.html', form=form)
def dispatch_request(self): f = textwrap posts = db_posts() return render_template("index.html", posts=posts, textwrap=f, is_homepage=True)
def get(self): # limit the number of post and starting ID with limit and offset arguments in the GET request # example: http:localhost:8000/api/rest/v1/blog?limit=10&offset=3 try: limit = request.args.get("limit") if request.args.get( "limit") else 5 offset = request.args.get("offset") if request.args.get( "offset") else 0 limit = int(limit) offset = int(offset) except: return jsonify({'error': 'Invalid API query'}), 400 posts = db_posts(limit=limit, offset=offset) api_response = [] for post in posts: post_payload = { 'id': post.id, 'author': post.author.username, 'title': post.title, 'content': post.content, 'date_posted': post.date_posted, 'comments': [] } for comment in post.comments: comment_payload = { 'author': comment.author.username, 'comment': comment.comment, 'date_posted': comment.date_posted } post_payload['comments'].append(comment_payload) api_response.append(post_payload) return jsonify(api_response), 200
def get(self, post_id: int): try: post = db_posts(id=post_id) api_response = [] post_payload = { 'id': post.id, 'author': post.author.username, 'title': post.title, 'content': post.content, 'date_posted': post.date_posted, 'comments': [] } for comment in post.comments: comment_payload = { 'author': comment.author.username, 'comment': comment.comment, 'date_posted': comment.date_posted } post_payload['comments'].append(comment_payload) api_response.append(post_payload) return jsonify(api_response), 200 except: return jsonify({'error': 'post not found'}), 400
def dispatch_request(self, post_id: int): username = session['active_user']['username'] post = db_posts(id=post_id) if username != post.author.username: abort(403) for comment in post.comments: db.session.delete(comment) db.session.delete(post) db.session.commit() flash('Your post is successfully deleted.', 'success') return redirect(url_for('users.Dashboard'))
def dispatch_request(self): posts = db_posts() return render_template('blog.html', posts=posts)