Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post.link を computed fieldにする #114

Merged
merged 4 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading