diff --git a/frontend/application/__init__.py b/frontend/application/__init__.py index 7200a3c475e22666367a9eb6147fd208e53d0019..cda22c2afa307c977bef17a1f5df3e08262e7412 100644 --- a/frontend/application/__init__.py +++ b/frontend/application/__init__.py @@ -2,16 +2,17 @@ from flask import Flask from os import environ from flask_login import LoginManager +from flask_ckeditor import CKEditor login_manager = LoginManager() - def create_app(): app = Flask(__name__) + ckeditor = CKEditor(app) app.config['SECRET_KEY'] = '3834j2724827' - login_manager.init_app(app) + # 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 fc074320b91121f073733f1fb80373225a2b52fb..3c389dc4b0d64f45b241c79d6ad942efecccb9c8 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__/forms.cpython-310.pyc b/frontend/application/frontend/__pycache__/forms.cpython-310.pyc index 4a22ad63e148a6a76e39717fc98514a9f9677412..a81f78029877e5827f4958a5ac6455a9b0e5e797 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 f56790c1c496b6811314f58abf343f6845edcc7d..2f86372d518740b140f44703d650e258eefb7e29 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 f7795777d3def16765ff34c0d92ea6bb23213899..24155e038111f3fafa2f7ea1e9673152033c7947 100644 --- a/frontend/application/frontend/api/PostClient.py +++ b/frontend/application/frontend/api/PostClient.py @@ -36,10 +36,11 @@ class PostClient: return response - def create_post(form): + # payload = {'title':form.title.data, 'category':form.category.data, + # 'content':form.content.data, 'user':current_user} payload = {'title':form.title.data, 'category':form.category.data, - 'content':form.content.data, 'user':current_user} + 'content':form.content.data} url = 'http://172.16.238.130:5002/api/new-post' response = requests.request(method="POST", url=url, data=payload) @@ -47,16 +48,6 @@ class PostClient: 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' @@ -66,8 +57,9 @@ class PostClient: return response def create_comment(form, post_id): - payload = {'content':form.content.data, 'user':current_user, - 'post_id':post_id} + # payload = {'content':form.content.data, 'user':current_user, + # 'post_id':post_id} + payload = {'content':form.content.data, 'post_id':post_id} url = 'http://172.16.238.130:5002/api/new-comment' response = requests.request(method="POST", url=url, data=payload) diff --git a/frontend/application/frontend/api/__pycache__/PostClient.cpython-310.pyc b/frontend/application/frontend/api/__pycache__/PostClient.cpython-310.pyc index 1e3590b2d6bdb6a42923fe3e019cf624cbf3b994..c104fb547b7b4b247ef6fd088662cfa373ba4971 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 c2249082fef11ed25523c7cb1791639fbed56188..672524549b99d2101d2041e39c901ac68e929569 100644 --- a/frontend/application/frontend/forms.py +++ b/frontend/application/frontend/forms.py @@ -14,14 +14,10 @@ class CreatePostForm(FlaskForm): 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') + ('social', 'Social'), + ('lost&found', 'Lost&Found'), + ('faculty', 'Faculty'), + ('buy&sell', 'Buy&Sell') ]) content = CKEditorField('Content', validators=[ InputRequired(), Length(min=20)]) diff --git a/frontend/application/frontend/views.py b/frontend/application/frontend/views.py index 4a938d78e773679a41db873e967cfb2f8c1d71eb..240fc6452abf0e6abf85070d745bb7c2be7b230c 100644 --- a/frontend/application/frontend/views.py +++ b/frontend/application/frontend/views.py @@ -16,8 +16,9 @@ def get_posts(): if posts == 404: abort(404) - return posts - + + return render_template('forum/index.html', + posts=posts) @frontend_blueprint.route('/post/new', methods=['GET','POST']) # @login_required @@ -28,10 +29,16 @@ def create_post(): post = PostClient.create_post(form) if post: flash('Post created successfully', 'success') - return redirect(url_for('forum.create_post', _external=True)) + return redirect(url_for('frontend.get_posts', _external=True)) else : flash('Post not successful', 'fail') - return redirect(url_for('forum.forum_route', _external=True)) + return redirect(url_for('frontend.get_posts', _external=True)) + content = { + 'page_title': 'New', + 'form': form, + 'post': None + } + return render_template('forum/create_post.html', **content) @frontend_blueprint.route('/post/<int:post_id>', methods=['GET','POST']) @@ -49,14 +56,15 @@ def display_post(post_id): if request.method == "POST": if form.validate_on_submit(): + print(form.content) 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)) + return redirect(url_for('frontend.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)) - + return redirect(url_for('frontend.display_post', form=form, post_id=post_id)) + content = { 'post': post, 'form': form, @@ -69,7 +77,6 @@ def display_post(post_id): @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: @@ -77,7 +84,8 @@ def delete_comment(post_id,comment_id): comment_user_id = comment[0]['user_id'] - # if comment_user_id != current_user: + # if comment_user_id != int(current_user['id']): + user_id = 1 if comment_user_id != user_id: abort(403) @@ -88,13 +96,12 @@ def delete_comment(post_id,comment_id): else: flash(' Comment deletion not successful', 'fail') - return redirect(url_for('forum.forum_route')) + return redirect(url_for('frontend.display_post',post_id=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: @@ -102,7 +109,8 @@ def delete_post(post_id): post_user_id = post[0]['user_id'] - # if post_user_id != current_user: + # if post_user_id != int(current_user['id']): + user_id = 1 if post_user_id != user_id: abort(403) @@ -112,23 +120,4 @@ def delete_post(post_id): 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 + return redirect(url_for('frontend.get_posts')) \ No newline at end of file diff --git a/frontend/application/css.css b/frontend/application/static/css.css similarity index 100% rename from frontend/application/css.css rename to frontend/application/static/css.css diff --git a/frontend/application/templates/forum/create_post.html b/frontend/application/templates/forum/create_post.html new file mode 100644 index 0000000000000000000000000000000000000000..e30d80e6d102b7064f46413ad80d6ad31946d0e2 --- /dev/null +++ b/frontend/application/templates/forum/create_post.html @@ -0,0 +1,32 @@ +{# application/templates/forum/create_post.html #} + +{% from 'macros.html' import displayField %} + +{% block title %}Create Post{% endblock %} + +{% block content %} + + <h1>{{ page_title }} Post</h1> + + <form class="post__form" action="" method="POST"> + + {{ displayField(form.title, 'Enter post title') }} + <select id="category" name="category"> + <option value="social">Social</option> + <option value="lost&found">Lost&Found</option> + <option value="faculty">Faculty</option> + <option value="buy&sell">Buy&Sell</option> + </select> + {{ displayField(form.content, 'Type content here') }} + + <button class="btn btn--solid post-btn" aria-label="Create" type="submit">CREATE</button> + <a class="btn btn--cancel" href="{{ url_for('frontend.get_posts') }}">CANCEL</a> + + {{ form.csrf_token }} + + </form> + + {{ ckeditor.load() }} + {{ ckeditor.config(name='content') }} + +{% endblock %} diff --git a/frontend/application/templates/forum/index.html b/frontend/application/templates/forum/index.html new file mode 100644 index 0000000000000000000000000000000000000000..4055d45a74892b34bae8eaebde44fdccdeb3bc1a --- /dev/null +++ b/frontend/application/templates/forum/index.html @@ -0,0 +1,28 @@ +<!DOCTYPE html> +<html> +<body> + +<h1>Posts<h1> + +<a class="btn btn--link" href="{{ url_for('frontend.create_post') }}">New Post</a> + +{% for post in posts %} + + <div class="tile tile--wide"> + + <div class="tile__content"> + + <header class="tile__header"> + <h2 class="tile__title"><a href="{{ url_for('frontend.display_post', post_id=post.id) }}">{{ post['title'] }}</a></h2> + <span class="tile__date">{{ post['date_added']}}</span> + </header> + + <div class="tile__text"> + <p>{{ post['content'] | safe }}</p> + </div> + + </div> + + </div> + + {% endfor %} \ No newline at end of file diff --git a/frontend/application/templates/forum/post.html b/frontend/application/templates/forum/post.html new file mode 100644 index 0000000000000000000000000000000000000000..80eaf32297495b3ac8ebac7f8df9fbde29184994 --- /dev/null +++ b/frontend/application/templates/forum/post.html @@ -0,0 +1,95 @@ +{# application/templates/forum/post.html #} + +{% from 'macros.html' import displayField %} + +{% block title %}Post{% endblock %} + + <section class="row latest"> + + <!-- START posts --> + + <div class="tile tile--wide"> + + <div class="tile__content"> + + <div class="tile__header"> + <h2 class="tile__title"><a href="{{ url_for('frontend.display_post', post_id=post.id) }}">{{ post['title'] }}</a></h2> + <p class="tile__date">{{ post['date_added'] }}</p> + </div> + + <div class="tile__text"> + <p>{{ post['content'] | safe }}</p> + </div> + + <div class="tile__footer"> + + <div class="tile__author"> + {{ post['user_id']}} + </div> + + <div class="tile__buttons"> + <a class="tile__btn" href="{{ url_for('frontend.delete_post', post_id=post.id) }}">Delete</a> + </div> + + </div> + + </div> + + </div> + + <!-- END posts --> + + <!-- START comments --> + + {% if comments %} + + {% for comment in comments %} + + <div class="comment comment--wide"> + + <div class="comment__content"> + + <div class="comment__header"> + <h2 class="comment__title">{{ comment['title'] }}</h2> + <p class="tile__date">{{ comment['date_added'] }}</p> + </div> + + <div class="comment__text"> + <p>{{ comment['content'] | safe }}</p> + </div> + + <div class="comment__footer"> + + <div class="comment__author"> + <p class="comment__title">{{ comment['user_id']}}</p> + </div> + + <div class="tile__buttons"> + <a class="tile__btn" href="{{ url_for('frontend.delete_comment', post_id=post.id, comment_id=comment.id) }}">Delete Comment</a> + </div> + + </div> + + </div> + + </div> + + {% endfor %} + + {% endif %} + + <form class="comment__form" action="" method="POST"> + + {{ form.csrf_token }} + + {{ displayField(form.content, 'Type comment here...') }} + + <button class="btn btn--solid post-btn" type="submit">SUBMIT</button> + <a class="btn btn--link" href="{{ url_for('frontend.get_posts') }}">HOME</a> + + </form> + + {{ ckeditor.load(pkg_type="basic") }} + {{ ckeditor.config(name='content') }} + + <!-- END comments --> diff --git a/frontend/application/templates/macros.html b/frontend/application/templates/macros.html new file mode 100644 index 0000000000000000000000000000000000000000..3b27f63e9a2216182f46f935408263a5f5bcf150 --- /dev/null +++ b/frontend/application/templates/macros.html @@ -0,0 +1,12 @@ +{# application/templates/macros.html #} + + +{% macro displayField(fieldName, placeholderValue) %} + + {{ fieldName(class_='input__field', placeholder=placeholderValue, **kwargs) }} + + {% for error in fieldName.errors %} + <p class="form__error">{{ error }}</p> + {% endfor %} + +{% endmacro %} diff --git a/post-service/application/__pycache__/__init__.cpython-310.pyc b/post-service/application/__pycache__/__init__.cpython-310.pyc index 01f59a94537dc3b4f411c5680869877bd387dfbd..7b3e4c585f168d3fe3a6724ca740665e46c8cddc 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 6651d0197ba8e2b46e0f0bbe8010326a5dd19023..0652316d85c960e44d4ebabb0352c9537bf710fc 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/models.py b/post-service/application/models.py index bb46f062be623ec61bc14dd01c2d65ea34fd1461..9af9ad5ee24745ec1ee39bd4fd7560ceb3db0a0f 100644 --- a/post-service/application/models.py +++ b/post-service/application/models.py @@ -14,6 +14,7 @@ class Post(db.Model): def __repr__(self): return '<Post %r>' % (self.title + str(self.id)) + #this is going to change UserClient get user first_name, last_name by using user_id and return as 'user' def to_json(self): return { 'id': self.id, 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 1c46abf130a35003a70104be8e15244d557a2720..493b4f7de7b0050188b73b6aa70882525becf283 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/routes.py b/post-service/application/post_api/routes.py index 116dd30c67247856d1433e8a91b5a5f417f953bd..a0a34ca94026580706fa7c517ac7169619929c60 100644 --- a/post-service/application/post_api/routes.py +++ b/post-service/application/post_api/routes.py @@ -5,39 +5,7 @@ from .. import db from ..models import Post, Comment from .api.UserClient import UserClient -# for initial post -@post_api_blueprint.route('/api/initial-post') -def post_initialpost(): - post = Post() - post.user_id = int(1) - post.title = 'hello surrey' - post.category = 'social' - post.content = 'this is initial post, hello world' - - db.session.add(post) - db.session.commit() - - response = jsonify({'message': 'Post added', 'result': post.to_json()}) - - return response - -# for initial comment -@post_api_blueprint.route('/api/initial-comment') -def post_initialcomment(): - - comment = Comment() - comment.user_id = int(2) - comment.post_id = int(2) - comment.content = 'welcome to comment section' - - db.session.add(comment) - db.session.commit() - - response = jsonify({'message': 'Comment added', 'result': comment.to_json()}) - - return response - @post_api_blueprint.route('/api/posts', methods=['GET']) def get_posts(): data = [] @@ -99,9 +67,9 @@ def get_comment(post_id,comment_id): # @login_required def post_newpost(): - # user_id = request.form['user'] - user = request.form['current_user'] - u_id = int(user['id']) + # user = request.form['user'] + # u_id = int(user['id']) + u_id = 1 title = request.form['title'] category = request.form['category'] @@ -133,21 +101,21 @@ def delete_post(post_id): return response -# @post_api_blueprint.route('/api/<int:post_id>/new-comment', methods=['POST']) @post_api_blueprint.route('/api/new-comment', methods=['POST']) # @login_required def post_comment(): - user = request.form['current_user'] - u_id = int(user['id']) + # user = request.form['user'] + # u_id = int(user['id']) + u_id = 1 content = request.form['content'] - post_id = request.from_object['post_id'] + post_id = request.form['post_id'] comment = Comment() comment.user_id = u_id comment.post_id = post_id - comment.connect = content + comment.content = content db.session.add(comment) db.session.commit() diff --git a/post-service/instance/posts-service.db b/post-service/instance/posts-service.db index c0d65ae2fb58b795e3cce7e9b99df4007ff0b280..f6e18b37da6a48346349bc2e23f0587e609bd451 100755 Binary files a/post-service/instance/posts-service.db and b/post-service/instance/posts-service.db differ