diff --git a/photo/queries.py b/photo/queries.py index 952b969..013a080 100644 --- a/photo/queries.py +++ b/photo/queries.py @@ -33,6 +33,10 @@ PictureCommentType, PictureType, UserType, + WinnerType, + WinnerSubmissionType, + WinnerPictureType, + WinnerContestType, ) from utils.enums import ContestInternalStates @@ -130,3 +134,35 @@ def set_order(element): else: query_results.sort(key=set_order) return query_results + + @strawberry.field + def winners(self) -> List[WinnerContestType]: + contests = Contest.objects.filter(winners__isnull=False).order_by('-voting_draw_end') + result = [] + for contest in contests: + winners = [] + for winner in contest.winners.all(): + submission = ContestSubmission.objects.filter(contest=contest, picture__user=winner).first() + winners.append( + WinnerType( + name_first=winner.name_first, + name_last=winner.name_last, + submission=WinnerSubmissionType( + picture=WinnerPictureType( + name=submission.picture.name, + file=submission.picture.file.url + ), + number_votes=submission.votes.count() + ) + ) + ) + result.append( + WinnerContestType( + title=contest.title, + description=contest.description, + prize=contest.prize, + voting_draw_end=contest.voting_draw_end, + winners=winners + ) + ) + return result diff --git a/photo/tests/test_queries/test_contest.py b/photo/tests/test_queries/test_contest.py index 62536a7..ee87489 100644 --- a/photo/tests/test_queries/test_contest.py +++ b/photo/tests/test_queries/test_contest.py @@ -2,6 +2,7 @@ from django.test import TestCase from django.utils import timezone +from photo.models import ContestSubmission, Picture from photo.models import Contest from photo.schema import schema @@ -109,6 +110,27 @@ def test_query_status(self): self.assertEqual(contest["status"], status[str(contest["id"])]) + def test_winners_query(self): + # Create a contest with winners + contest = ContestFactory.create(voting_draw_end=timezone.now()) + user = UserFactory.create() + contest.winners.add(user) + submission = ContestSubmission.objects.create(contest=contest, picture=Picture.objects.create(user=user, name='Test Picture', file='test.jpg')) + + # Execute the winners query + result = schema.execute_sync( + '{ winners { title description prize voting_draw_end winners { name_first name_last submission { picture { name file } number_votes } } } }' + ) + + # Check for errors + self.assertIsNone(result.errors) + + # Validate the response + self.assertEqual(len(result.data['winners']), 1) + self.assertEqual(result.data['winners'][0]['title'], contest.title) + self.assertEqual(result.data['winners'][0]['winners'][0]['name_first'], user.name_first) + self.assertEqual(result.data['winners'][0]['winners'][0]['submission']['picture']['name'], 'Test Picture') + class ContestFilterTest(TestCase): def test_filter_by_search(self): test_text = "This is a text with a weird word 1234Test1234." @@ -139,7 +161,7 @@ def test_filter_by_search(self): self.assertTrue(contest["id"] in contest_IDs) def test_filter_by_time(self): - time = timezone.now().replace(year=2020, month=4) + time = timezone.now().replace(year=2020, month=4, day=1) ContestFactory( upload_phase_start=time, diff --git a/photo/tests/test_winners_query.py b/photo/tests/test_winners_query.py new file mode 100644 index 0000000..e69de29 diff --git a/photo/tests/test_winners_view.py b/photo/tests/test_winners_view.py new file mode 100644 index 0000000..e69de29 diff --git a/photo/types.py b/photo/types.py index e63affc..889c764 100644 --- a/photo/types.py +++ b/photo/types.py @@ -51,6 +51,30 @@ class CollectionType: pictures: List[PictureType] +@strawberry.type +class WinnerPictureType: + name: str + file: str + +@strawberry.type +class WinnerSubmissionType: + picture: "WinnerPictureType" + number_votes: int + +@strawberry.type +class WinnerType: + name_first: str + name_last: str + submission: WinnerSubmissionType + +@strawberry.type +class WinnerContestType: + title: str + description: str + prize: str + voting_draw_end: str + winners: List[WinnerType] + @strawberry.django.type(Contest) class ContestType: id: int diff --git a/photo/views.py b/photo/views.py index 7f1ebff..52bc35e 100644 --- a/photo/views.py +++ b/photo/views.py @@ -1,7 +1,7 @@ from django.http import HttpResponse from strawberry.django.views import GraphQLView -from photo.queries import Context +from photo.queries import Context, Query class ReventGraphQLView(GraphQLView):