Skip to content

Commit

Permalink
Post.linkをcomputed fieldにする
Browse files Browse the repository at this point in the history
  • Loading branch information
sushichan044 committed Oct 8, 2024
1 parent 0c58393 commit 494dc98
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
3 changes: 0 additions & 3 deletions api/birdxplorer_api/routers/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
26 changes: 24 additions & 2 deletions common/birdxplorer_common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 0 additions & 3 deletions common/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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="""\
Expand All @@ -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="""\
Expand All @@ -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="""\
Expand Down

0 comments on commit 494dc98

Please sign in to comment.