core/services/common_result.py
Untone 160f02e67f
All checks were successful
Deploy on push / deploy (push) Successful in 1m49s
0.4.5-api-update
2024-10-21 10:52:23 +03:00

55 lines
1.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

from dataclasses import dataclass
from typing import Any, Dict, List, Optional
from orm.author import Author
from orm.community import Community
from orm.reaction import Reaction
from orm.shout import Shout
from orm.topic import Topic
@dataclass
class CommonResult:
error: Optional[str] = None
slugs: Optional[List[str]] = None
shout: Optional[Shout] = None
shouts: Optional[List[Shout]] = None
author: Optional[Author] = None
authors: Optional[List[Author]] = None
reaction: Optional[Reaction] = None
reactions: Optional[List[Reaction]] = None
topic: Optional[Topic] = None
topics: Optional[List[Topic]] = None
community: Optional[Community] = None
communities: Optional[List[Community]] = None
@classmethod
def ok(cls, data: Dict[str, Any]) -> "CommonResult":
"""
Создает успешный результат.
Args:
data: Словарь с данными для включения в результат.
Returns:
CommonResult: Экземпляр с предоставленными данными.
"""
result = cls()
for key, value in data.items():
if hasattr(result, key):
setattr(result, key, value)
return result
@classmethod
def error(cls, message: str):
"""
Create an error result.
Args:
message: The error message.
Returns:
CommonResult: An instance with the error message.
"""
return cls(error=message)