From 494dc98b65acc5fb80b76e91dd53cc43c6463e64 Mon Sep 17 00:00:00 2001 From: sushi-chaaaan Date: Wed, 2 Oct 2024 16:47:30 +0900 Subject: [PATCH] =?UTF-8?q?Post.link=E3=82=92computed=20field=E3=81=AB?= =?UTF-8?q?=E3=81=99=E3=82=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/birdxplorer_api/routers/data.py | 3 --- common/birdxplorer_common/models.py | 26 ++++++++++++++++++++++++-- common/tests/conftest.py | 3 --- 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/api/birdxplorer_api/routers/data.py b/api/birdxplorer_api/routers/data.py index e0ef426..e51e13d 100644 --- a/api/birdxplorer_api/routers/data.py +++ b/api/birdxplorer_api/routers/data.py @@ -132,9 +132,6 @@ def get_posts( search_url=search_url, ) - for post in posts: - post.link = HttpUrl(f"https://x.com/{post.x_user.name}/status/{post.post_id}") - base_url = str(request.url).split("?")[0] next_offset = offset + limit prev_offset = max(offset - limit, 0) diff --git a/common/birdxplorer_common/models.py b/common/birdxplorer_common/models.py index 908d52a..25b1e4b 100644 --- a/common/birdxplorer_common/models.py +++ b/common/birdxplorer_common/models.py @@ -19,7 +19,7 @@ from pydantic import BaseModel as PydanticBaseModel from pydantic import ConfigDict from pydantic import Field as PydanticField -from pydantic import GetCoreSchemaHandler, HttpUrl, TypeAdapter, model_validator +from pydantic import GetCoreSchemaHandler, HttpUrl, TypeAdapter, model_validator, computed_field from pydantic.alias_generators import to_camel from pydantic.main import IncEx from pydantic_core import core_schema @@ -770,7 +770,6 @@ def validate_link_id(cls, values: Dict[str, Any]) -> Dict[str, Any]: class Post(BaseModel): post_id: PostId - link: Optional[HttpUrl] = None x_user_id: UserId x_user: XUser text: str @@ -781,6 +780,29 @@ class Post(BaseModel): impression_count: NonNegativeInt links: List[Link] = [] + @property + @computed_field + def link(self) -> HttpUrl: + """ + PostのX上でのURLを返す。 + + Examples + -------- + >>> post = Post(post_id="1234567890123456789", + x_user_id="1234567890123456789", + x_user=XUser(user_id="1234567890123456789", + name="test", + profile_image="https://x.com/test"), + text="test", + created_at=1288834974657, + like_count=1, + repost_count=1, + impression_count=1) + >>> post.link + HttpUrl('https://x.com/test/status/1234567890123456789') + """ + return HttpUrl(f"https://x.com/{self.x_user.name}/status/{self.post_id}") + class PaginationMeta(BaseModel): next: Optional[HttpUrl] = None diff --git a/common/tests/conftest.py b/common/tests/conftest.py index 78865d7..c972acd 100644 --- a/common/tests/conftest.py +++ b/common/tests/conftest.py @@ -267,7 +267,6 @@ def post_samples( posts = [ post_factory.build( post_id="2234567890123456781", - link=None, x_user_id="1234567890123456781", x_user=x_user_samples[0], text="""\ @@ -283,7 +282,6 @@ def post_samples( ), post_factory.build( post_id="2234567890123456791", - link=None, x_user_id="1234567890123456781", x_user=x_user_samples[0], text="""\ @@ -299,7 +297,6 @@ def post_samples( ), post_factory.build( post_id="2234567890123456801", - link=None, x_user_id="1234567890123456782", x_user=x_user_samples[1], text="""\