diff --git a/frontend/application/__init__.py b/frontend/application/__init__.py index 22bb4ec191a773320cfa6ba70550e0bc5d6f45c3..7200a3c475e22666367a9eb6147fd208e53d0019 100644 --- a/frontend/application/__init__.py +++ b/frontend/application/__init__.py @@ -1,5 +1,6 @@ # 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(): diff --git a/frontend/application/__pycache__/__init__.cpython-310.pyc b/frontend/application/__pycache__/__init__.cpython-310.pyc index 10d7a685a1e849d20853bb5774f63fa843c93cf1..fc074320b91121f073733f1fb80373225a2b52fb 100644 Binary files a/frontend/application/__pycache__/__init__.cpython-310.pyc and b/frontend/application/__pycache__/__init__.cpython-310.pyc differ diff --git a/frontend/application/frontend/__pycache__/__init__.cpython-310.pyc b/frontend/application/frontend/__pycache__/__init__.cpython-310.pyc index 91cbb9982f1c6a0fbf34d00573e472c87e8378b0..5ff7f5c90e11373cb23a3fe5f785cee4a1579a8e 100644 Binary files a/frontend/application/frontend/__pycache__/__init__.cpython-310.pyc and b/frontend/application/frontend/__pycache__/__init__.cpython-310.pyc differ diff --git a/frontend/application/frontend/__pycache__/forms.cpython-310.pyc b/frontend/application/frontend/__pycache__/forms.cpython-310.pyc index e2020fa6dc2b40e4bd3213af5357e868514bae77..4a22ad63e148a6a76e39717fc98514a9f9677412 100644 Binary files a/frontend/application/frontend/__pycache__/forms.cpython-310.pyc and b/frontend/application/frontend/__pycache__/forms.cpython-310.pyc differ diff --git a/frontend/application/frontend/__pycache__/views.cpython-310.pyc b/frontend/application/frontend/__pycache__/views.cpython-310.pyc index 656458fe082206d1ee38e2f5138f21047dbbe002..f56790c1c496b6811314f58abf343f6845edcc7d 100644 Binary files a/frontend/application/frontend/__pycache__/views.cpython-310.pyc and b/frontend/application/frontend/__pycache__/views.cpython-310.pyc differ diff --git a/frontend/application/frontend/api/PostClient.py b/frontend/application/frontend/api/PostClient.py index a2d87977a7dbc58520e642789901baf3158108d2..f7795777d3def16765ff34c0d92ea6bb23213899 100644 --- a/frontend/application/frontend/api/PostClient.py +++ b/frontend/application/frontend/api/PostClient.py @@ -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 diff --git a/frontend/application/frontend/api/__pycache__/PostClient.cpython-310.pyc b/frontend/application/frontend/api/__pycache__/PostClient.cpython-310.pyc index 1356610a3bddc495d574c9e2ea5f352b51f6b406..1e3590b2d6bdb6a42923fe3e019cf624cbf3b994 100644 Binary files a/frontend/application/frontend/api/__pycache__/PostClient.cpython-310.pyc and b/frontend/application/frontend/api/__pycache__/PostClient.cpython-310.pyc differ diff --git a/frontend/application/frontend/forms.py b/frontend/application/frontend/forms.py index cc1449abf5644693d06ee3b2c8fcac0e61d65cf5..c2249082fef11ed25523c7cb1791639fbed56188 100644 --- a/frontend/application/frontend/forms.py +++ b/frontend/application/frontend/forms.py @@ -1 +1,34 @@ # 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 diff --git a/frontend/application/frontend/views.py b/frontend/application/frontend/views.py index a8e15a77c5ce74b92655bbd5e402535628155410..4a938d78e773679a41db873e967cfb2f8c1d71eb 100644 --- a/frontend/application/frontend/views.py +++ b/frontend/application/frontend/views.py @@ -1,4 +1,5 @@ # 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 diff --git a/post-service/application/__pycache__/__init__.cpython-310.pyc b/post-service/application/__pycache__/__init__.cpython-310.pyc index e27a1d48877094587df3be5819d1ef6d09cbca78..01f59a94537dc3b4f411c5680869877bd387dfbd 100644 Binary files a/post-service/application/__pycache__/__init__.cpython-310.pyc and b/post-service/application/__pycache__/__init__.cpython-310.pyc differ diff --git a/post-service/application/__pycache__/models.cpython-310.pyc b/post-service/application/__pycache__/models.cpython-310.pyc index 1f1af8bfe2ed3567c5f6da2936219a144df51897..6651d0197ba8e2b46e0f0bbe8010326a5dd19023 100644 Binary files a/post-service/application/__pycache__/models.cpython-310.pyc and b/post-service/application/__pycache__/models.cpython-310.pyc differ diff --git a/post-service/application/post_api/__pycache__/__init__.cpython-310.pyc b/post-service/application/post_api/__pycache__/__init__.cpython-310.pyc index b48bf2eb3ed3c7236a56b9d001db170624f06a74..4b05334c59e93218057566979d66ceac6a1f0d23 100644 Binary files a/post-service/application/post_api/__pycache__/__init__.cpython-310.pyc and b/post-service/application/post_api/__pycache__/__init__.cpython-310.pyc differ diff --git a/post-service/application/post_api/__pycache__/routes.cpython-310.pyc b/post-service/application/post_api/__pycache__/routes.cpython-310.pyc index 3e0a7484b02e3c5a7c4aa444a3e6ad5e04e9f4a7..1c46abf130a35003a70104be8e15244d557a2720 100644 Binary files a/post-service/application/post_api/__pycache__/routes.cpython-310.pyc and b/post-service/application/post_api/__pycache__/routes.cpython-310.pyc differ diff --git a/post-service/application/post_api/api/__pycache__/UserClient.cpython-310.pyc b/post-service/application/post_api/api/__pycache__/UserClient.cpython-310.pyc index d1bb5a8c510b1da3b1e39b0472d9e42ff85f420d..eb3390c2a8ae65e61fc17b4fc5f1ee3da66d6ec7 100644 Binary files a/post-service/application/post_api/api/__pycache__/UserClient.cpython-310.pyc and b/post-service/application/post_api/api/__pycache__/UserClient.cpython-310.pyc differ diff --git a/post-service/application/post_api/routes.py b/post-service/application/post_api/routes.py index 19ebffbd6e4f09a00c11cd2dc83801e56674bd9b..116dd30c67247856d1433e8a91b5a5f417f953bd 100644 --- a/post-service/application/post_api/routes.py +++ b/post-service/application/post_api/routes.py @@ -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() diff --git a/post-service/instance/posts-service.db b/post-service/instance/posts-service.db old mode 100644 new mode 100755 index 674332029ee9309152899854474e0910226099a7..c0d65ae2fb58b795e3cce7e9b99df4007ff0b280 Binary files a/post-service/instance/posts-service.db and b/post-service/instance/posts-service.db differ diff --git a/user-service/application/user_api/__pycache__/routes.cpython-310.pyc b/user-service/application/user_api/__pycache__/routes.cpython-310.pyc index 1fbc8bd1140d4af386fe7067fa70c30e10760e1a..59d364b93d1e1a7d2c32b38323fbe86439806d62 100644 Binary files a/user-service/application/user_api/__pycache__/routes.cpython-310.pyc and b/user-service/application/user_api/__pycache__/routes.cpython-310.pyc differ