Skip to content

Commit

Permalink
Task and project card last contributions fixed and list dto for proje…
Browse files Browse the repository at this point in the history
…ct messages.
  • Loading branch information
prabinoid committed Nov 15, 2024
1 parent 654d9d4 commit c525ba2
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 40 deletions.
1 change: 0 additions & 1 deletion backend/api/comments/resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 4 additions & 6 deletions backend/models/dtos/mapping_dto.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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):
Expand All @@ -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"""
Expand Down
48 changes: 36 additions & 12 deletions backend/models/dtos/message_dto.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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
10 changes: 3 additions & 7 deletions backend/models/dtos/project_dto.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
23 changes: 9 additions & 14 deletions backend/models/postgis/project_chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c525ba2

Please sign in to comment.