From c525ba2a5281255212fcfd1605736ea596945c24 Mon Sep 17 00:00:00 2001 From: prabinoid <38830224+prabinoid@users.noreply.github.com> Date: Fri, 15 Nov 2024 14:18:33 +0545 Subject: [PATCH] Task and project card last contributions fixed and list dto for project messages. --- backend/api/comments/resources.py | 1 - backend/models/dtos/mapping_dto.py | 10 +++--- backend/models/dtos/message_dto.py | 48 +++++++++++++++++++------- backend/models/dtos/project_dto.py | 10 ++---- backend/models/postgis/project_chat.py | 23 +++++------- 5 files changed, 52 insertions(+), 40 deletions(-) diff --git a/backend/api/comments/resources.py b/backend/api/comments/resources.py index 5325b28876..fe406e7618 100644 --- a/backend/api/comments/resources.py +++ b/backend/api/comments/resources.py @@ -3,7 +3,6 @@ from fastapi import APIRouter, Depends, Request from loguru import logger from fastapi.responses import JSONResponse - from backend.db import get_db, get_session from backend.models.dtos.mapping_dto import TaskCommentDTO from backend.models.dtos.message_dto import ChatMessageDTO diff --git a/backend/models/dtos/mapping_dto.py b/backend/models/dtos/mapping_dto.py index 042d868a13..5b4b919ab3 100644 --- a/backend/models/dtos/mapping_dto.py +++ b/backend/models/dtos/mapping_dto.py @@ -2,7 +2,7 @@ from backend.models.postgis.statuses import TaskStatus from backend.models.dtos.mapping_issues_dto import TaskMappingIssueDTO from backend.models.dtos.task_annotation_dto import TaskAnnotationDTO -from pydantic import BaseModel, Field, ValidationError, validator, root_validator +from pydantic import BaseModel, Field, ValidationError, validator from typing import List, Optional @@ -66,11 +66,7 @@ class TaskHistoryDTO(BaseModel): class Config: populate_by_name = True - @root_validator(pre=True) - def format_sent_date(cls, values): - if "action_date" in values and values["action_date"]: - values["action_date"] = values["action_date"].isoformat() + "Z" - return values + json_encoders = {datetime: lambda v: v.isoformat() + "Z" if v else None} class TaskStatusDTO(BaseModel): @@ -84,6 +80,8 @@ class TaskStatusDTO(BaseModel): class Config: populate_by_name = True + json_encoders = {datetime: lambda v: v.isoformat() + "Z" if v else None} + class TaskDTO(BaseModel): """Describes a Task DTO""" diff --git a/backend/models/dtos/message_dto.py b/backend/models/dtos/message_dto.py index 479b68704d..f6534c032c 100644 --- a/backend/models/dtos/message_dto.py +++ b/backend/models/dtos/message_dto.py @@ -1,7 +1,7 @@ from datetime import datetime from typing import List, Optional -from pydantic import BaseModel, Field, root_validator +from pydantic import BaseModel, Field from backend.models.dtos.stats_dto import Pagination @@ -25,11 +25,7 @@ class MessageDTO(BaseModel): class Config: populate_by_name = True - @root_validator(pre=True) - def format_sent_date(cls, values): - if "sent_date" in values and values["sent_date"]: - values["sent_date"] = values["sent_date"].isoformat() + "Z" - return values + json_encoders = {datetime: lambda v: v.isoformat() + "Z" if v else None} class MessagesDTO(BaseModel): @@ -58,14 +54,42 @@ class ChatMessageDTO(BaseModel): timestamp: datetime username: str + # class Config: + # populate_by_name = True + + # json_encoders = { + # datetime: lambda v: v.isoformat() + "Z" if v else None + # } + + +class ListChatMessageDTO(BaseModel): + """DTO describing an individual project chat message""" + + id: Optional[int] = Field(None, alias="id") + message: str = Field(required=True) + picture_url: str = Field(default=None, alias="pictureUrl") + timestamp: datetime + username: str + class Config: populate_by_name = True - def dict(self, **kwargs): - data = super().dict(**kwargs) - if self.timestamp: - data["timestamp"] = self.timestamp.isoformat() + "Z" - return data + json_encoders = {datetime: lambda v: v.isoformat() + "Z" if v else None} + + # def dict(self, *args, **kwargs): + # """ + # Override the dict method to exclude `user_id` and `project_id` + # from the dictionary representation. + # """ + # exclude_fields = {"user_id", "project_id"} + # # Generate the dict as usual, excluding the fields + # return super().dict(*args, **kwargs, exclude=exclude_fields) + + # def dict(self, **kwargs): + # data = super().dict(**kwargs) + # if self.timestamp: + # data["timestamp"] = self.timestamp.isoformat() + "Z" + # return data class ProjectChatDTO(BaseModel): @@ -76,5 +100,5 @@ def __init__(self): super().__init__() self.chat = [] - chat: Optional[List[ChatMessageDTO]] = None + chat: Optional[List[ListChatMessageDTO]] = None pagination: Optional[Pagination] = None diff --git a/backend/models/dtos/project_dto.py b/backend/models/dtos/project_dto.py index e558a9beb4..1bd2654100 100644 --- a/backend/models/dtos/project_dto.py +++ b/backend/models/dtos/project_dto.py @@ -438,19 +438,15 @@ class ListSearchResultDTO(BaseModel): percent_validated: Optional[int] = Field(alias="percentValidated", default=None) status: Optional[str] = None active_mappers: Optional[int] = Field(alias="activeMappers", default=None) - last_updated: Optional[str] = Field(alias="lastUpdated", default=None) - due_date: Optional[str] = Field(alias="dueDate", default=None) + last_updated: Optional[datetime] = Field(alias="lastUpdated", default=None) + due_date: Optional[datetime] = Field(alias="dueDate", default=None) total_contributors: Optional[int] = Field(alias="totalContributors", default=None) country: Optional[str] = Field(default="", serialize=False) class Config: populate_by_name = True - -# class ProjectSearchResultsDTO(BaseModel): -# map_results: Optional[List] = [] -# results: Optional[List[ListSearchResultDTO]] = [] -# pagination: Optional[Pagination] = {} + json_encoders = {datetime: lambda v: v.isoformat() + "Z" if v else None} class ProjectSearchResultsDTO(BaseModel): diff --git a/backend/models/postgis/project_chat.py b/backend/models/postgis/project_chat.py index 917f83529e..015b03c658 100644 --- a/backend/models/postgis/project_chat.py +++ b/backend/models/postgis/project_chat.py @@ -6,7 +6,12 @@ from sqlalchemy.orm import relationship from backend.db import Base, get_session -from backend.models.dtos.message_dto import ChatMessageDTO, Pagination, ProjectChatDTO +from backend.models.dtos.message_dto import ( + ChatMessageDTO, + ListChatMessageDTO, + Pagination, + ProjectChatDTO, +) from backend.models.postgis.user import User from backend.models.postgis.utils import timestamp @@ -82,14 +87,12 @@ async def create_from_dto(cls, dto: ChatMessageDTO, db: Database): """, {"message_id": new_message_id}, ) - return ChatMessageDTO( + return ListChatMessageDTO( id=new_message["id"], message=new_message["message"], picture_url=new_message["picture_url"], timestamp=new_message["time_stamp"], username=new_message["username"], - project_id=new_message["project_id"], - user_id=new_message["user_id"], ) @staticmethod @@ -126,22 +129,14 @@ async def get_messages( dto = ProjectChatDTO() for message in messages: - chat_dto = ChatMessageDTO( + chat_dto = ListChatMessageDTO( id=message["id"], message=message["message"], picture_url=message["picture_url"], timestamp=message["time_stamp"], username=message["username"], - project_id=message["project_id"], - user_id=message["user_id"], ) - chat_dto_dict = chat_dto.dict() - filtered_chat_dto_dict = { - k: v - for k, v in chat_dto_dict.items() - if k not in ["project_id", "user_id"] - } - dto.chat.append(filtered_chat_dto_dict) + dto.chat.append(chat_dto) dto.pagination = Pagination.from_total_count( page=page, per_page=per_page, total=total_count