Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Feb 6, 2024
1 parent fd540d0 commit 89f0b7f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 37 deletions.
2 changes: 2 additions & 0 deletions src/backend/app/db/db_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ class DbTaskHistory(Base):
{},
)


class TaskComment(Base):
"""Represents a comment associated with a task."""

Expand All @@ -374,6 +375,7 @@ class TaskComment(Base):
{},
)


class DbTask(Base):
"""Describes an individual mapping Task."""

Expand Down
57 changes: 37 additions & 20 deletions src/backend/app/tasks/tasks_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
from sqlalchemy.orm import Session
from sqlalchemy.sql import text

from app.auth.osm import AuthUser
from app.central import central_crud
from app.db import database, db_models
from app.models.enums import (
TaskStatus,
get_action_for_status_change,
verify_valid_status_update,
)
from app.auth.osm import AuthUser
from app.projects import project_crud
from app.tasks import tasks_schemas
from app.users import user_crud
Expand Down Expand Up @@ -316,44 +316,46 @@ async def edit_task_boundary(db: Session, task_id: int, boundary: str):
return True


async def get_task_comments(db: Session,project_id:int,task_id:int):
async def get_task_comments(db: Session, project_id: int, task_id: int):
"""Get a list of tasks id for a project."""
query = text(
f"""
SELECT task_comment.id,users.username,task_comment.comment_text,task_comment.created_at FROM task_comment
LEFT JOIN users ON task_comment.commented_by = users.id
SELECT task_comment.id,users.username,task_comment.comment_text,task_comment.created_at FROM task_comment
LEFT JOIN users ON task_comment.commented_by = users.id
where project_id = {project_id} AND task_id = {task_id}
"""
)
# Then execute the query with the desired parameter
result = db.execute(query)
print("result----")
print(result.__dict__)

# Convert the result to a list of dictionaries
result_dict_list = [{"id": row[0],"commented_by": row[1],"comment": row[2],"created_at": row[3]} for row in result.fetchall()]

# Convert the result to a list of dictionaries
result_dict_list = [
{"id": row[0], "commented_by": row[1], "comment": row[2], "created_at": row[3]}
for row in result.fetchall()
]

return result_dict_list


async def add_task_comments(db: Session, comment: tasks_schemas.TaskCommentBase, user_data: AuthUser):
"""
Add a comment to a task.
async def add_task_comments(
db: Session, comment: tasks_schemas.TaskCommentBase, user_data: AuthUser
):
"""Add a comment to a task.
Parameters:
- db: SQLAlchemy database session
- comment: TaskCommentBase instance containing the comment details
- user_data: AuthUser instance containing the user details
Returns:
- Dictionary with the details of the added comment
"""

# Construct the query to insert the comment and retrieve the details of the inserted comment
query = text(
f"""
WITH inserted_comment AS ( INSERT INTO task_comment(task_id,project_id,comment_text,commented_by)
WITH inserted_comment AS ( INSERT INTO task_comment(task_id,project_id,comment_text,commented_by)
VALUES({comment.task_id},{comment.project_id},'{comment.comment}',{user_data.id})
RETURNING task_comment.id, task_comment.comment_text, task_comment.created_at, task_comment.commented_by )
SELECT ic.id,username as commented_by,comment_text,created_at FROM inserted_comment ic
Expand All @@ -371,7 +373,13 @@ async def add_task_comments(db: Session, comment: tasks_schemas.TaskCommentBase,
row = result.fetchone()

# Return the details of the added comment as a dictionary
return {"id":row[0],"commented_by": row[1],"comment": row[2],"created_at": row[3]}
return {
"id": row[0],
"commented_by": row[1],
"comment": row[2],
"created_at": row[3],
}


async def get_task_comment_info_by_id(db: Session, comment_id: int):
"""Get the project info only by id."""
Expand All @@ -383,8 +391,10 @@ async def get_task_comment_info_by_id(db: Session, comment_id: int):
)
return db_project_info

async def delete_task_comment_by_id(db: Session, task_comment_id: int, user_data: AuthUser):

async def delete_task_comment_by_id(
db: Session, task_comment_id: int, user_data: AuthUser
):
# Query to get the comment by its ID
get_comment_query = text(
"""
Expand All @@ -395,12 +405,16 @@ async def delete_task_comment_by_id(db: Session, task_comment_id: int, user_data
)

# Execute the query and commit the transaction
comment = db.execute(get_comment_query, {"task_comment_id": task_comment_id}).fetchone()
comment = db.execute(
get_comment_query, {"task_comment_id": task_comment_id}
).fetchone()
if comment is None:
raise HTTPException(status_code=404, detail="Task Comment not found")
# check for user
# check for user
if comment.commented_by != user_data.id:
raise HTTPException(status_code=404, detail="Cannot delete Task Comment. You are not the owner.")
raise HTTPException(
status_code=404, detail="Cannot delete Task Comment. You are not the owner."
)

# Query to delete the comment by its ID and the authenticated user ID
delete_query = text(
Expand All @@ -410,12 +424,15 @@ async def delete_task_comment_by_id(db: Session, task_comment_id: int, user_data
"""
)
# Execute the query to delete the comment
result = db.execute(delete_query, {"task_comment_id": task_comment_id, "user_id": user_data.id})
result = db.execute(
delete_query, {"task_comment_id": task_comment_id, "user_id": user_data.id}
)
db.commit()

# Return the details of the added comment as a dictionary
return f"Task Comment {task_comment_id} deleted"


async def update_task_history(
tasks: List[tasks_schemas.Task], db: Session = Depends(database.get_db)
):
Expand Down
21 changes: 11 additions & 10 deletions src/backend/app/tasks/tasks_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
from app.tasks import tasks_crud, tasks_schemas
from app.users import user_schemas

from ..auth.osm import AuthUser, login_required
from . import tasks_crud, tasks_schemas
from ..auth.osm import AuthUser,login_required

router = APIRouter(
prefix="/tasks",
Expand Down Expand Up @@ -197,27 +197,25 @@ async def task_features_count(

return data


@router.get("/task-comments/", response_model=list[tasks_schemas.TaskCommentResponse])
async def task_comments(
project_id: int,
task_id: int,
db: Session = Depends(database.get_db),
):
task_comment_list = await tasks_crud.get_task_comments(db,project_id,task_id)

task_comment_list = await tasks_crud.get_task_comments(db, project_id, task_id)

return task_comment_list


@router.post("/task-comments/",response_model=tasks_schemas.TaskCommentResponse)
@router.post("/task-comments/", response_model=tasks_schemas.TaskCommentResponse)
async def task_comments(
comment: tasks_schemas.TaskCommentRequest,
db: Session = Depends(database.get_db),
user_data: AuthUser = Depends(login_required),

):
"""
Create a new task comment.
"""Create a new task comment.
Parameters:
comment (TaskCommentRequest): The task comment to be created.
Expand All @@ -227,7 +225,7 @@ async def task_comments(
Returns:
TaskCommentResponse: The created task comment.
"""
task_comment_list = await tasks_crud.add_task_comments(db,comment,user_data)
task_comment_list = await tasks_crud.add_task_comments(db, comment, user_data)
return task_comment_list


Expand All @@ -242,10 +240,13 @@ async def delete_task_comments(
if not task_comment:
raise HTTPException(status_code=404, detail="Task Comment not found")
else:
deleted_project = await tasks_crud.delete_task_comment_by_id(db, task_comment_id,user_data)

deleted_project = await tasks_crud.delete_task_comment_by_id(
db, task_comment_id, user_data
)

return deleted_project


@router.get("/activity/", response_model=List[tasks_schemas.TaskHistoryCount])
async def task_activity(
project_id: int, days: int = 10, db: Session = Depends(database.get_db)
Expand Down
17 changes: 10 additions & 7 deletions src/backend/app/tasks/tasks_schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
from app.config import decrypt_value
from app.db.postgis_utils import geometry_to_geojson, get_centroid
from app.models.enums import TaskStatus
from app.users.user_schemas import User



class TaskHistoryBase(BaseModel):
Expand Down Expand Up @@ -129,30 +127,35 @@ def decrypt_password(self, value: str) -> str:
"""Decrypt the ODK Token extracted from the db."""
if not value:
return ""

return decrypt_value(value)


class TaskCommentRequest(BaseModel):
"""Task mapping history."""

comment: str
project_id: int
task_id: int


class TaskCommentBase(BaseModel):
"""Task mapping history."""

comment: str
commented_by:str
commented_by: str
created_at: datetime


class TaskCommentResponse(BaseModel):
"""Task mapping history."""
id:int

id: int
commented_by: str
comment:str
comment: str
created_at: datetime



class ReadTask(Task):
"""Task details plus updated task history."""

Expand Down

0 comments on commit 89f0b7f

Please sign in to comment.