-
Notifications
You must be signed in to change notification settings - Fork 0
feat: 공개 API에 이벤트별 필터링 추가 및 최신 이벤트 기본값 적용 #35
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
Open
JaeHyuckSa
wants to merge
2
commits into
main
Choose a base branch
from
feat/event-filter-public-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+172
−37
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| from django.db.models import Q | ||
| from django_filters import rest_framework as filters | ||
| from django_filters.constants import EMPTY_VALUES | ||
| from event.models import Event | ||
|
|
||
|
|
||
| class EventFilterMixin(filters.FilterSet): | ||
| event = filters.CharFilter(method="filter_by_event_name") | ||
| event_field_prefix = "event" | ||
|
|
||
| def filter_by_event_name(self, queryset, name, value): | ||
| if value in EMPTY_VALUES: | ||
| return queryset | ||
|
|
||
| prefix = self.event_field_prefix | ||
| return queryset.filter(Q(**{f"{prefix}__name_ko": value}) | Q(**{f"{prefix}__name_en": value})) | ||
|
|
||
| def filter_queryset(self, queryset): | ||
| queryset = super().filter_queryset(queryset) | ||
|
|
||
| if self.data.get("event") in EMPTY_VALUES: | ||
| latest = Event.objects.filter_active().first() | ||
| if latest: | ||
| queryset = queryset.filter(**{self.event_field_prefix: latest}) | ||
|
|
||
| return queryset |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| from core.models import BaseAbstractModelQuerySet | ||
| from django.db.models import Q | ||
| from django_filters import rest_framework as filters | ||
| from django_filters.constants import EMPTY_VALUES | ||
| from event.filters import EventFilterMixin | ||
|
|
||
|
|
||
| class PresentationFilterSet(EventFilterMixin): | ||
| event_field_prefix = "type__event" | ||
| types = filters.BaseCSVFilter(method="filter_by_type_names") | ||
|
|
||
| def filter_by_type_names(self, queryset: BaseAbstractModelQuerySet, name: str, values: list[str]) -> Q: | ||
| if values in EMPTY_VALUES: | ||
| return queryset | ||
|
|
||
| return queryset.filter(Q(type__name_ko__in=values) | Q(type__name_en__in=values)) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| import http | ||
| import urllib.parse | ||
| from datetime import datetime | ||
|
|
||
| import pytest | ||
| from django.urls import reverse | ||
|
|
@@ -22,18 +23,20 @@ def test_presentation_api(api_client: APIClient, create_presentation_set: Presen | |
| def test_presentation_event_type_filter_api(api_client: APIClient): | ||
| # Given: 행사 2개에 각각 2 종류의 발표 유형이 있고, 각 발표 유형마다 1개의 발표가 있음. | ||
| organization = Organization.objects.create(name="Test Organization") | ||
| event_1: Event = Event.objects.create(organization=organization, name="Test Event 1") | ||
| event_2: Event = Event.objects.create(organization=organization, name="Test Event 2") | ||
| event_1: Event = Event.objects.create( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 현재 해당하는 사항은 아니지만, 3일동안 열리는 행사가 아닌 경우에 대비해서 (파이콘이 2일만 열리거나, |
||
| organization=organization, name="Test Event 1", event_start_at=datetime(2025, 8, 1) | ||
| ) | ||
| event_2: Event = Event.objects.create( | ||
| organization=organization, name="Test Event 2", event_start_at=datetime(2026, 8, 1) | ||
| ) | ||
|
|
||
| event_1_prst_type_1 = PresentationType.objects.create(event=event_1, name="Type 1") | ||
| event_1_prst_type_2 = PresentationType.objects.create(event=event_1, name="Type 2") | ||
| event_2_prst_type_1 = PresentationType.objects.create(event=event_2, name="Type 1") | ||
| event_2_prst_type_2 = PresentationType.objects.create(event=event_2, name="Type 2") | ||
| PresentationType.objects.create(event=event_2, name="Type 1") | ||
| PresentationType.objects.create(event=event_2, name="Type 2") | ||
|
|
||
| event_1_prst_type_1_prst = Presentation.objects.create(type=event_1_prst_type_1, title="Presentation 1") | ||
| event_1_prst_type_2_prst = Presentation.objects.create(type=event_1_prst_type_2, title="Presentation 2") | ||
| event_2_prst_type_1_prst = Presentation.objects.create(type=event_2_prst_type_1, title="Presentation 3") | ||
| Presentation.objects.create(type=event_2_prst_type_2, title="Presentation 4") | ||
|
|
||
| # When: API 요청을 통해 행사 1의 발표 유형 1과 2에 해당하는 발표를 요청할 시 | ||
| qs = urllib.parse.urlencode( | ||
|
|
@@ -51,16 +54,29 @@ def test_presentation_event_type_filter_api(api_client: APIClient): | |
| str(event_1_prst_type_2_prst.id), | ||
| } | ||
|
|
||
| # When: API 요청을 통해 행사 유형은 지정하지 않고 유형 1에 해당하는 발표를 요청할 시 | ||
| qs = urllib.parse.urlencode({"types": event_1_prst_type_1.name}) | ||
| response = api_client.get(f"{reverse('v1:presentation-list')}?{qs}") | ||
|
|
||
| # Then: 행사 1의 발표 유형 1과 행사 2의 발표 유형 1에 해당하는 발표가 반환되어야 함. | ||
| assert response.status_code == http.HTTPStatus.OK | ||
| @pytest.mark.django_db | ||
| def test_presentation_defaults_to_latest_event(api_client: APIClient): | ||
| # Given: 2개의 행사가 있고, 각각 발표가 있음. | ||
| organization = Organization.objects.create(name="Test Organization") | ||
| old_event = Event.objects.create( | ||
| organization=organization, name="PyCon Korea 2025", event_start_at=datetime(2025, 8, 1) | ||
| ) | ||
| new_event = Event.objects.create( | ||
| organization=organization, name="PyCon Korea 2026", event_start_at=datetime(2026, 8, 1) | ||
| ) | ||
|
|
||
| old_type = PresentationType.objects.create(event=old_event, name="Talk") | ||
| new_type = PresentationType.objects.create(event=new_event, name="Talk") | ||
|
|
||
| Presentation.objects.create(type=old_type, title="Old Presentation") | ||
| new_prst = Presentation.objects.create(type=new_type, title="New Presentation") | ||
|
|
||
| # When: event 파라미터 없이 요청 | ||
| response = api_client.get(reverse("v1:presentation-list")) | ||
|
|
||
| # Then: 최신 행사(2026)의 발표만 반환 | ||
| assert response.status_code == http.HTTPStatus.OK | ||
| response_data = response.json() | ||
| assert len(response_data) == 2, "Should return presentations for type 1 across all events" | ||
| assert {datum["id"] for datum in response_data} == { | ||
| str(event_1_prst_type_1_prst.id), | ||
| str(event_2_prst_type_1_prst.id), | ||
| } | ||
| assert len(response_data) == 1 | ||
| assert response_data[0]["id"] == str(new_prst.id) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| from event.filters import EventFilterMixin | ||
|
|
||
|
|
||
| class SponsorTierFilterSet(EventFilterMixin): | ||
| event_field_prefix = "event" |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import http | ||
| from datetime import datetime | ||
|
|
||
| import pytest | ||
| from django.urls import reverse | ||
| from event.models import Event | ||
| from event.sponsor.models import Sponsor, SponsorTier, SponsorTierSponsorRelation | ||
| from file.models import PublicFile | ||
| from rest_framework.test import APIClient | ||
| from user.models.organization import Organization | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def api_client(): | ||
| return APIClient() | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def two_events(): | ||
| organization = Organization.objects.create(name="Test Organization") | ||
| old_event = Event.objects.create( | ||
| organization=organization, name="PyCon Korea 2025", event_start_at=datetime(2025, 8, 1) | ||
| ) | ||
| new_event = Event.objects.create( | ||
| organization=organization, name="PyCon Korea 2026", event_start_at=datetime(2026, 8, 1) | ||
| ) | ||
| return old_event, new_event | ||
|
|
||
|
|
||
| def _make_sponsor(event, name, tier): | ||
| logo = PublicFile.objects.create( | ||
| file=f"public/{name}.png", | ||
| mimetype="image/png", | ||
| hash=name, | ||
| size=0, | ||
| ) | ||
| sponsor = Sponsor.objects.create(event=event, name=name, logo=logo) | ||
| SponsorTierSponsorRelation.objects.create(tier=tier, sponsor=sponsor) | ||
| return sponsor | ||
|
|
||
|
|
||
| @pytest.mark.django_db | ||
| def test_sponsor_defaults_to_latest_event(api_client: APIClient, two_events): | ||
| old_event, new_event = two_events | ||
|
|
||
| # Given: 각 행사에 후원 등급과 후원사가 있음 | ||
| old_tier = SponsorTier.objects.create(event=old_event, name="Gold", order=0) | ||
| new_tier = SponsorTier.objects.create(event=new_event, name="Gold", order=0) | ||
|
|
||
| _make_sponsor(old_event, "Old Sponsor", old_tier) | ||
| _make_sponsor(new_event, "New Sponsor", new_tier) | ||
|
|
||
| # When: event 파라미터 없이 요청 | ||
| response = api_client.get(reverse("v1:sponsor-list")) | ||
|
|
||
| # Then: 최신 행사(2026)의 후원 등급만 반환 | ||
| assert response.status_code == http.HTTPStatus.OK | ||
| response_data = response.json() | ||
| assert len(response_data) == 1 | ||
| assert response_data[0]["id"] == str(new_tier.id) | ||
|
|
||
|
|
||
| @pytest.mark.django_db | ||
| def test_sponsor_filter_by_event_name(api_client: APIClient, two_events): | ||
| old_event, new_event = two_events | ||
|
|
||
| old_tier = SponsorTier.objects.create(event=old_event, name="Gold", order=0) | ||
| new_tier = SponsorTier.objects.create(event=new_event, name="Gold", order=0) | ||
|
|
||
| _make_sponsor(old_event, "Old Sponsor", old_tier) | ||
| _make_sponsor(new_event, "New Sponsor", new_tier) | ||
|
|
||
| # When: 2025 행사를 명시적으로 지정 | ||
| response = api_client.get(reverse("v1:sponsor-list"), {"event": "PyCon Korea 2025"}) | ||
|
|
||
| # Then: 2025 행사의 후원 등급만 반환 | ||
| assert response.status_code == http.HTTPStatus.OK | ||
| response_data = response.json() | ||
| assert len(response_data) == 1 | ||
| assert response_data[0]["id"] == str(old_tier.id) | ||
|
|
||
|
|
||
| @pytest.mark.django_db | ||
| def test_sponsor_no_events_returns_empty(api_client: APIClient): | ||
| # When: 이벤트가 없을 때 요청 | ||
| response = api_client.get(reverse("v1:sponsor-list")) | ||
|
|
||
| # Then: 빈 응답 | ||
| assert response.status_code == http.HTTPStatus.OK | ||
| assert response.json() == [] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
별도 Filter로 구분한 부분 좋은 것 같습니다!