From 12d7155d571b9076880c070f50270a65451c812d Mon Sep 17 00:00:00 2001 From: onstabb Date: Mon, 11 Dec 2023 10:16:02 +0100 Subject: [PATCH] -Fixes --- src/contacts/routers.py | 4 ++-- src/contacts/schemas.py | 15 +++++++++------ src/contacts/service.py | 6 ++++-- src/likes/routers.py | 7 +++---- src/likes/service.py | 7 ++++++- src/models.py | 4 ++-- 6 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/contacts/routers.py b/src/contacts/routers.py index 2214797..dbfed06 100644 --- a/src/contacts/routers.py +++ b/src/contacts/routers.py @@ -84,7 +84,7 @@ def send_message( current_user: CurrentActiveCompletedUser, target_user: TargetActiveCompletedUser, ): - contact = service.get_contact_by_users_pair(current_user.id, target_user.id) + contact = service.get_contact_by_users_pair(current_user, target_user) if not contact or not contact.established: raise HTTPException(status_code=status.HTTP_401_UNAUTHORIZED, detail="Contact must be established") @@ -92,6 +92,6 @@ def send_message( raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="Limit of the messages has been exceeded") message = service.create_message(contact, sender=current_user, message_in=message_in) - message_out = MessageOut.model_validate(message) + message_out = MessageOut.model_validate(message, from_attributes=True) notification_manager.put_notification(message_out, recipient_id=target_user.id, notification_type=NotificationType.MESSAGE) return message_out diff --git a/src/contacts/schemas.py b/src/contacts/schemas.py index 9058d27..f9805ec 100644 --- a/src/contacts/schemas.py +++ b/src/contacts/schemas.py @@ -1,11 +1,11 @@ from datetime import datetime from functools import cached_property -from pydantic import BaseModel, Field, constr, computed_field +from pydantic import AliasChoices, BaseModel, Field, constr, computed_field from contacts import config from contacts.enums import ContactState -from models import PydanticObjectId +from models import PydanticObjectId, DateTimeFromObjectId from users.schemas import UserPublicOut @@ -22,13 +22,11 @@ class MessageOut(MessageBase): date: datetime = Field(validation_alias="created_at") -class ContactOut(BaseModel): +class ContactBaseOut(BaseModel): initiator: UserPublicOut | PydanticObjectId = Field(exclude=True) respondent: UserPublicOut | PydanticObjectId = Field(exclude=True) opposite_user: UserPublicOut | None = Field(exclude=True, default=None) - - status: ContactState | None - messages: list[MessageOut] + created_at: DateTimeFromObjectId = Field(validation_alias=AliasChoices("_id", "id"), ) @computed_field @cached_property @@ -44,6 +42,11 @@ def user(self) -> UserPublicOut | None: return None +class ContactOut(ContactBaseOut): + status: ContactState | None + messages: list[MessageOut] + + class ContactStateIn(BaseModel): action: ContactState diff --git a/src/contacts/service.py b/src/contacts/service.py index 4f799cb..54dd0b6 100644 --- a/src/contacts/service.py +++ b/src/contacts/service.py @@ -103,11 +103,13 @@ def update_contact_status(contact_state_data: ContactStateIn, user: User, contac def create_message(contact: Contact, sender: User, message_in: MessageIn) -> Message: - return contact.messages.create(sender=sender, text=message_in.text) + message = contact.messages.create(sender=sender, text=message_in.text) + contact.save() + return message def get_messages_count_from_sender(contact: Contact, sender: User) -> int: - if sender not in (contact.initiator, contact.respondent): + if sender.id not in (contact.initiator, contact.respondent): raise ValueError(f"Contact does not contain user with {sender.id}") return len(contact.messages.filter(sender=sender)) diff --git a/src/likes/routers.py b/src/likes/routers.py index 4865f1a..6fb5e38 100644 --- a/src/likes/routers.py +++ b/src/likes/routers.py @@ -1,14 +1,13 @@ from fastapi import APIRouter +from contacts.schemas import ContactBaseOut from likes import service from users.dependencies import CurrentActiveCompletedUser -from users.schemas import UserPublicOut router = APIRouter() -@router.get("", response_model=list[UserPublicOut]) +@router.get("", response_model=list[ContactBaseOut]) def get_who_liked(current_user: CurrentActiveCompletedUser): - return [contact.initiator for contact in service.get_likes_for_user(current_user)] - + return service.get_likes_for_user(current_user) diff --git a/src/likes/service.py b/src/likes/service.py index 254d134..7002096 100644 --- a/src/likes/service.py +++ b/src/likes/service.py @@ -16,4 +16,9 @@ def get_likes_for_user(user: User) -> list[Contact]: result = Contact.objects( respondent=user, initiator_state=ContactState.ESTABLISHED, status=None ).exclude("respondent") - return list(result) + + def set_respondent_id(contact: Contact) -> Contact: + contact.respondent = user.id + return contact + + return list(contact for contact in map(set_respondent_id, result)) diff --git a/src/models.py b/src/models.py index f7fb424..8c45165 100644 --- a/src/models.py +++ b/src/models.py @@ -8,7 +8,7 @@ DoesNotExist, ) from mongoengine.base import BaseField, LazyReference -from pydantic import GetCoreSchemaHandler, GetJsonSchemaHandler, PlainSerializer +from pydantic import GetCoreSchemaHandler, GetJsonSchemaHandler, PlainSerializer, BeforeValidator from pydantic.json_schema import JsonSchemaValue from pydantic_core import core_schema @@ -82,4 +82,4 @@ def created_at(self) -> datetime: SerializeDocToId = PlainSerializer(lambda doc: doc.id, return_type=str, when_used="json") - +DateTimeFromObjectId = typing.Annotated[datetime, BeforeValidator(lambda value: value.generation_time)]