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 2, 2024
1 parent 8b72330 commit 4eddd34
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
4 changes: 0 additions & 4 deletions api/birdxplorer_api/routers/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from dateutil.parser import parse as dateutil_parse
from fastapi import APIRouter, HTTPException, Query, Request
from pydantic import HttpUrl

from birdxplorer_common.models import (
BaseModel,
Expand Down Expand Up @@ -127,9 +126,6 @@ def get_posts(
search_text=search_text,
)

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 @@ -17,7 +17,7 @@
from pydantic import BaseModel as PydanticBaseModel
from pydantic import ConfigDict
from pydantic import Field as PydanticField
from pydantic import GetCoreSchemaHandler, HttpUrl, TypeAdapter
from pydantic import GetCoreSchemaHandler, HttpUrl, TypeAdapter, computed_field
from pydantic.alias_generators import to_camel
from pydantic_core import core_schema

Expand Down Expand Up @@ -705,7 +705,6 @@ class Media(BaseModel):

class Post(BaseModel):
post_id: PostId
link: Optional[HttpUrl] = None
x_user_id: UserId
x_user: XUser
text: str
Expand All @@ -715,6 +714,29 @@ class Post(BaseModel):
repost_count: NonNegativeInt
impression_count: NonNegativeInt

@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 @@ -242,7 +242,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 @@ -257,7 +256,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 @@ -272,7 +270,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 4eddd34

Please sign in to comment.