Skip to content

Commit

Permalink
working comment section get and post
Browse files Browse the repository at this point in the history
  • Loading branch information
GrGLeo committed Oct 30, 2024
1 parent 6a0abcd commit 17208b6
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 18 deletions.
2 changes: 1 addition & 1 deletion back/api_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class UserModel(BaseModel):
email: str


class CommentModel(BaseModel):
class PostCommentModel(BaseModel):
activity_id: int
comment_text: str

Expand Down
4 changes: 2 additions & 2 deletions back/data/etl/comment_feeder.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import pandas as pd
import bleach
from back.data.etl import Feeder
from back.api_model import CommentModel
from back.api_model import PostCommentModel


class CommentFeeder(Feeder):
def __init__(self, comment: CommentModel, user_id: int):
def __init__(self, comment: PostCommentModel, user_id: int):
self.user_id = user_id
self.text = comment.comment_text
self.schema = 'param'
Expand Down
17 changes: 12 additions & 5 deletions back/endpoints/comments.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
from fastapi import APIRouter, HTTPException, Header
from back.api_model import CommentModel
from sqlalchemy import text
from back.api_model import PostCommentModel
from back.endpoints.auth import retrieve_user_id
from back.utils.logger import logger
from back.endpoints.db_query import conn
from back.data.etl.comment_feeder import CommentFeeder


comment_router = APIRouter(prefix="/comments")


@comment_router.post("/post_comment/")
async def post_comment(comment: CommentModel, authorization: str = Header(None)):
async def post_comment(comment: PostCommentModel, authorization: str = Header(None)):
user_id = retrieve_user_id(authorization)
if len(comment.comment_text.strip()) == 0:
logger.warning("Empty comment")
Expand All @@ -22,6 +24,11 @@ async def post_comment(comment: CommentModel, authorization: str = Header(None))
pass


@comment_router.get("/get_comment")
async def get_comment():
pass
@comment_router.get("/get_comments/")
async def get_comment(activity_id):
query = text(f'select comment from param.activity_comments where activity_id = {activity_id} order by comment_id')
params = {'activity_id': activity_id}
with conn.connect() as connection:
result = connection.execute(query, params)
rows = [dict(zip(result.keys(), row)) for row in result.fetchall()]
return {"data": rows}
4 changes: 2 additions & 2 deletions back/endpoints/db_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async def simple_query(param: QueryModel, authorization: str = Header(None)):
token = authorization.split(" ")[1]
user_id = decode_jwt(token)

queryer = Query(user_id, conn)
query = Query(user_id, conn)
param = {k: v for k, v in param.model_dump().items() if v is not None}
data = queryer.get_query(**param)
data = query.get_query(**param)
return data
8 changes: 4 additions & 4 deletions back/tests/test_comment_feeder.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import pytest
import pandas as pd
from back.api_model import CommentModel
from back.api_model import PostCommentModel
from back.data.etl.comment_feeder import CommentFeeder


def test_sanitize_comment():
comment = CommentModel(user_id=1, activity_id=1, comment_text= "<script>alert('hack');</script>")
comment = PostCommentModel(user_id=1, activity_id=1, comment_text= "<script>alert('hack');</script>")
feeder = CommentFeeder(comment)

sanitized = feeder._sanitize_comment(comment.comment_text)

assert sanitized == "&lt;script&gt;alert('hack');&lt;/script&gt;"


def test_process_comment():
comment = CommentModel(user_id=1, activity_id=1, comment_text="This is a test comment!")
comment = PostCommentModel(user_id=1, activity_id=1, comment_text="This is a test comment!")
feeder = CommentFeeder(comment)

feeder.process()
Expand Down
12 changes: 8 additions & 4 deletions front/utilities/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ def add_comment(activity_id):


def write_comment(conn, activity_id):
query = "select * from param.activity_comments where activity_id = :activity_id"
comments = conn.query(query, params={'activity_id': activity_id})
response = requests.get(f"{API}/comments/get_comments/?activity_id={activity_id}")
if response.status_code == 200:
comments = response.json()['data']
else:
comments = [{"comment": "No comments yet"}]

comment_section = """
<div style='
min-height: 100px;
Expand All @@ -40,8 +44,8 @@ def write_comment(conn, activity_id):
'>
"""

for _, comment in comments.iterrows():
comment_section += f"<div style='padding: 5px; margin-bottom: 5px; border-bottom: 1px solid #ddd;'>{comment.comment}</div>"
for comment in comments:
comment_section += f"<div style='padding: 5px; margin-bottom: 5px; border-bottom: 1px solid #ddd;'>{comment['comment']}</div>"

# Close the scrollable container div
comment_section += "</div>"
Expand Down

0 comments on commit 17208b6

Please sign in to comment.