Skip to content
Snippets Groups Projects
Commit cfe79f32 authored by Aydin, Mehmet Can (PG/T - Comp Sci & Elec Eng)'s avatar Aydin, Mehmet Can (PG/T - Comp Sci & Elec Eng)
Browse files

Merge branch '31/03-updates' into 'main'

frontend display routes is added, post-service API connection is mostly done....

See merge request group20/cwblog!8
parents 9c69888d 64957f67
Branches main
No related tags found
No related merge requests found
Showing
with 213 additions and 23 deletions
# application/__init__.py
from flask import Flask
from os import environ
from flask_login import LoginManager
login_manager = LoginManager()
......@@ -8,6 +9,8 @@ login_manager = LoginManager()
def create_app():
app = Flask(__name__)
app.config['SECRET_KEY'] = '3834j2724827'
login_manager.init_app(app)
with app.app_context():
......
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
......@@ -25,7 +25,60 @@ class PostClient:
return response
def get_comment(post_id, comment_id):
url = 'http://172.16.238.130:5002/api/' + str(post_id) + '/' + str(comment_id)
response = requests.request(method="GET", url=url)
if response.status_code == 404:
return response.status_code
response = response.json()
return response
def create_post(form):
payload = {'title':form.title.data, 'category':form.category.data,
'content':form.content.data, 'user':current_user}
url = 'http://172.16.238.130:5002/api/new-post'
response = requests.request(method="POST", url=url, data=payload)
if response:
return response
# def create_post(form):
# payload = {'title':form.title, 'category':form.category,
# 'content':form.content, 'user':form.user_id}
# url = 'http://172.16.238.130:5002/api/new-post'
# response = requests.request(method="POST", url=url, data=payload)
# if response:
# return response
def delete_post(post_id):
url = 'http://172.16.238.130:5002/api/' + str(post_id) + '/delete'
response = requests.request(method="POST", url=url)
if response:
return response
def create_comment(form, post_id):
payload = {'content':form.content.data, 'user':current_user,
'post_id':post_id}
url = 'http://172.16.238.130:5002/api/new-comment'
response = requests.request(method="POST", url=url, data=payload)
if response:
return response
def delete_comment(post_id,comment_id):
url = 'http://172.16.238.130:5002/api/' + str(post_id) + '/' + str(comment_id) + '/delete'
response = requests.request(method="POST", url=url)
# headers = {
# 'Authorization': 'Basic ' + session['user_api_key']
# }
\ No newline at end of file
if response:
return response
No preview for this file type
# application/frontend/forms.py
from flask_wtf import FlaskForm
from flask_wtf.file import FileField, FileAllowed
from flask_login import current_user
from wtforms import (StringField, PasswordField,
SubmitField, SelectField)
from wtforms.validators import (InputRequired, Email, EqualTo,
ValidationError, Length)
from flask_ckeditor import CKEditorField
# from application.models import User
class CreatePostForm(FlaskForm):
'''Create post form'''
title = StringField('Title', validators=[InputRequired()])
category = SelectField('Category', choices=[
('help', 'Help'),
('python', 'Python'),
('nodejs', 'Nodejs'),
('general', 'General'),
('feedback', 'Feedback'),
('html-css', 'HTML CSS'),
('support', 'Support'),
('javascript', 'Javascript')
])
content = CKEditorField('Content', validators=[
InputRequired(), Length(min=20)])
submit = SubmitField('CREATE POST')
class CommentForm(FlaskForm):
'''Comment post form'''
content = CKEditorField('Comment', validators=[InputRequired()])
submit = SubmitField('SUBMIT')
\ No newline at end of file
# application/frontend/views.py
from flask_wtf import FlaskForm
import requests
from . import forms
from . import frontend_blueprint
......@@ -6,24 +7,128 @@ from .. import login_manager
# from .api.UserClient import UserClient
from .api.PostClient import PostClient
from flask import render_template, session, redirect, url_for, flash, request, abort
from flask_login import current_user
@frontend_blueprint.route('/', methods=['GET'])
#it is going to change to home page, which categories will be seen in
def get_posts():
posts = PostClient.get_posts()
if posts == 404:
abort(404)
return posts
@frontend_blueprint.route('/post/new', methods=['GET','POST'])
# @login_required
def create_post():
form = forms.CreatePostForm(request.form)
if request.method == "POST":
if form.validate_on_submit():
post = PostClient.create_post(form)
if post:
flash('Post created successfully', 'success')
return redirect(url_for('forum.create_post', _external=True))
else :
flash('Post not successful', 'fail')
return redirect(url_for('forum.forum_route', _external=True))
@frontend_blueprint.route('/post/<int:post_id>', methods=['GET','POST'])
# @login_required
def display_post(post_id):
response = PostClient.get_post(post_id)
if response == 404:
abort(404)
post = response[0]
comments = response[1:]
form = forms.CommentForm(request.form)
if request.method == "POST":
if form.validate_on_submit():
comment = PostClient.create_comment(form, post_id)
if comment:
flash('Comment created successfully', 'success')
return redirect(url_for('forum.display_post', post_id=post.id))
else:
flash('Comment not successful', 'fail')
return redirect(url_for('forum.display_post', form=form, post_id=post.id))
content = {
'post': post,
'form': form,
'comments': comments
}
return render_template('forum/post.html', **content)
@frontend_blueprint.route('/post/<int:post_id>/<int:comment_id>/delete', methods=['GET', 'POST'])
# @login_required
def delete_comment(post_id,comment_id):
user_id = 2
comment = PostClient.get_comment(post_id,comment_id)
if comment == 404:
abort(404)
comment_user_id = comment[0]['user_id']
# if comment_user_id != current_user:
if comment_user_id != user_id:
abort(403)
delete_comment = PostClient.delete_comment(post_id, comment_id)
if delete_comment:
flash(' Your comment has been deleted', 'success')
else:
flash(' Comment deletion not successful', 'fail')
return redirect(url_for('forum.forum_route'))
@frontend_blueprint.route('/<int:post_id>', methods=['GET','POST'])
def get_post(post_id):
@frontend_blueprint.route('/post/<int:post_id>/delete', methods=['GET', 'POST'])
# @login_required
def delete_post(post_id):
user_id = 1
post = PostClient.get_post(post_id)
if post == 404:
abort(404)
return post
\ No newline at end of file
post_user_id = post[0]['user_id']
# if post_user_id != current_user:
if post_user_id != user_id:
abort(403)
delete_post = PostClient.delete_post(post_id)
if delete_post:
flash(' Your post has been deleted', 'success')
else:
flash(' Post deletion not successful', 'fail')
return redirect(url_for('forum.forum_route'))
# @frontend_blueprint.route('/post/new', methods=['GET','POST'])
# # @login_required
# def create_post():
# # form = forms.CreatePostForm()
# # if request.method == "POST":
# # if form.validate_on_submit():
# form = FlaskForm(meta={'csrf': False})
# form.title = 'hello world v2'
# form.category = 'social'
# form.content = 'testing from frontend'
# form.user_id = 1
# post = PostClient.create_post(form)
# print(post)
# if post:
# print('Post created successfully', 'success')
# # return redirect(url_for('forum.create_post', _external=True))
\ No newline at end of file
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
No preview for this file type
......@@ -96,11 +96,11 @@ def get_comment(post_id,comment_id):
return response
@post_api_blueprint.route('/api/new-post', methods=['POST'])
# @login_required
def post_newpost():
api_key = request.headers.get('Authorization')
response = UserClient.get_user(api_key)
user = response['result']
# user_id = request.form['user']
user = request.form['current_user']
u_id = int(user['id'])
title = request.form['title']
......@@ -109,6 +109,7 @@ def post_newpost():
post = Post()
post.user_id = u_id
# post.user_id = user_id
post.title = title
post.category = category
post.content = content
......@@ -122,12 +123,9 @@ def post_newpost():
@post_api_blueprint.route('/api/<int:post_id>/delete', methods=['GET','POST'])
# @login_required
def delete_post(post_id):
current_user_id = 1
def delete_post(post_id):
post = Post.query.get_or_404(post_id)
if post.user_id != current_user_id:
abort(403)
db.session.delete(post)
db.session.commit()
......@@ -135,16 +133,16 @@ def delete_post(post_id):
return response
@post_api_blueprint.route('/api/<int:post_id>/new-comment', methods=['POST'])
# @post_api_blueprint.route('/api/<int:post_id>/new-comment', methods=['POST'])
@post_api_blueprint.route('/api/new-comment', methods=['POST'])
# @login_required
def add_comment(post_id):
api_key = request.headers.get('Authorization')
response = UserClient.get_user(api_key)
def post_comment():
user = response['result']
user = request.form['current_user']
u_id = int(user['id'])
content = request.form['content']
post_id = request.from_object['post_id']
comment = Comment()
comment.user_id = u_id
......@@ -156,18 +154,16 @@ def add_comment(post_id):
response = jsonify({'message': 'Comment added', 'result': comment.to_json()})
return response
@post_api_blueprint.route('/api/<int:post_id>/<int:comment_id>/delete', methods=['GET','POST'])
# @login_required
def delete_comment(post_id,comment_id):
current_user_id = 2
comment = Comment.query.filter(Comment.post_id == post_id,
Comment.id == comment_id).first()
if comment.user_id != current_user_id:
abort(403)
db.session.delete(comment)
db.session.commit()
......
No preview for this file type
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment