From 551aa594eec5d4fc226868eae72ea3932bedf917 Mon Sep 17 00:00:00 2001 From: jessiebelle Date: Tue, 2 Jan 2024 20:06:05 +0200 Subject: [PATCH 1/7] WIP --- sponsors/admin.py | 9 ++++++++- sponsors/tests/test_admin.py | 7 +++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/sponsors/admin.py b/sponsors/admin.py index d6601140f..1deff2fc2 100644 --- a/sponsors/admin.py +++ b/sponsors/admin.py @@ -247,8 +247,15 @@ def has_delete_permission(self, request, obj=None): return obj.open_for_editing def get_queryset(self, *args, **kwargs): + match = request.resolver_match + sponsorship = self.parent_model.objects.get(pk=match.kwargs["object_id"]) + + year = sponsorship.year + # available_packages = SponsorshipPackage.objects.from_year(year) qs = super().get_queryset(*args, **kwargs) - return qs.select_related("sponsorship_benefit__program", "program") + filtered = qs.filter(sponsorship_benefit__packages__year=year) + + return filtered.select_related("sponsorship_benefit__program", "program") class TargetableEmailBenefitsFilter(admin.SimpleListFilter): diff --git a/sponsors/tests/test_admin.py b/sponsors/tests/test_admin.py index 1e94fa6df..42bef91fc 100644 --- a/sponsors/tests/test_admin.py +++ b/sponsors/tests/test_admin.py @@ -8,6 +8,13 @@ from sponsors.admin import SponsorshipStatusListFilter, SponsorshipAdmin from sponsors.models import Sponsorship +class TestSponsorshipAdmin(TestCase): + def setUp(self): + self.request = RequestFactory().get("/") + self.model_admin = SponsorshipAdmin + def test_get_benefits_for_current_year(self): + + class TestCustomSponsorshipStatusListFilter(TestCase): def setUp(self): From 6b517a086783a872d1ed98c8af25840178b4a6eb Mon Sep 17 00:00:00 2001 From: jessiebelle Date: Sat, 6 Jan 2024 18:39:56 +0200 Subject: [PATCH 2/7] WIP --- sponsors/admin.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sponsors/admin.py b/sponsors/admin.py index 1deff2fc2..22326b55c 100644 --- a/sponsors/admin.py +++ b/sponsors/admin.py @@ -253,7 +253,7 @@ def get_queryset(self, *args, **kwargs): year = sponsorship.year # available_packages = SponsorshipPackage.objects.from_year(year) qs = super().get_queryset(*args, **kwargs) - filtered = qs.filter(sponsorship_benefit__packages__year=year) + filtered = qs.filter(sponsorship_benefit__year=year) return filtered.select_related("sponsorship_benefit__program", "program") From 3e0bdfe38382d6974a8fcd29e953e02f375a9cf8 Mon Sep 17 00:00:00 2001 From: jessiebelle Date: Sun, 7 Jan 2024 10:39:52 +0200 Subject: [PATCH 3/7] WIP --- sponsors/admin.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/sponsors/admin.py b/sponsors/admin.py index 22326b55c..13dce2eb0 100644 --- a/sponsors/admin.py +++ b/sponsors/admin.py @@ -246,16 +246,15 @@ def has_delete_permission(self, request, obj=None): return True return obj.open_for_editing - def get_queryset(self, *args, **kwargs): + def get_queryset(self, request): + qs = super().get_queryset(request) match = request.resolver_match - sponsorship = self.parent_model.objects.get(pk=match.kwargs["object_id"]) + sponsorship = self.parent_model.objects.get(pk=match.kwargs["object_id"]) year = sponsorship.year - # available_packages = SponsorshipPackage.objects.from_year(year) - qs = super().get_queryset(*args, **kwargs) filtered = qs.filter(sponsorship_benefit__year=year) - return filtered.select_related("sponsorship_benefit__program", "program") + return filtered class TargetableEmailBenefitsFilter(admin.SimpleListFilter): From a2ea849dfeb62e18a088c15d172bb6cb1ac4f4ff Mon Sep 17 00:00:00 2001 From: jessiebelle Date: Sun, 7 Jan 2024 15:51:16 +0200 Subject: [PATCH 4/7] we finally got there with the qs filter --- sponsors/admin.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/sponsors/admin.py b/sponsors/admin.py index 13dce2eb0..88aff8c57 100644 --- a/sponsors/admin.py +++ b/sponsors/admin.py @@ -247,14 +247,12 @@ def has_delete_permission(self, request, obj=None): return obj.open_for_editing def get_queryset(self, request): - qs = super().get_queryset(request) + #filters the available benefits by the benefits for the year of the sponsorship match = request.resolver_match - sponsorship = self.parent_model.objects.get(pk=match.kwargs["object_id"]) year = sponsorship.year - filtered = qs.filter(sponsorship_benefit__year=year) - return filtered + return super().get_queryset(request).filter(sponsorship_benefit__year=year) class TargetableEmailBenefitsFilter(admin.SimpleListFilter): From 1ea75aabb6e0d27e696b2e15bf857f7c5d0fac6c Mon Sep 17 00:00:00 2001 From: jessiebelle Date: Sun, 7 Jan 2024 20:25:43 +0200 Subject: [PATCH 5/7] removing redundant tests --- sponsors/tests/test_admin.py | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/sponsors/tests/test_admin.py b/sponsors/tests/test_admin.py index 42bef91fc..938faac75 100644 --- a/sponsors/tests/test_admin.py +++ b/sponsors/tests/test_admin.py @@ -5,14 +5,9 @@ from django.test import TestCase, RequestFactory -from sponsors.admin import SponsorshipStatusListFilter, SponsorshipAdmin -from sponsors.models import Sponsorship - -class TestSponsorshipAdmin(TestCase): - def setUp(self): - self.request = RequestFactory().get("/") - self.model_admin = SponsorshipAdmin - def test_get_benefits_for_current_year(self): +from sponsors.admin import SponsorshipStatusListFilter, SponsorshipAdmin, SponsorBenefitInline +from sponsors.forms import SponsorBenefitAdminInlineForm +from sponsors.models import Sponsorship, SponsorBenefit class TestCustomSponsorshipStatusListFilter(TestCase): From 447708b1914cc7c0a25605de66413357df4c8ddb Mon Sep 17 00:00:00 2001 From: jessiebelle Date: Sat, 13 Jan 2024 13:34:30 +0200 Subject: [PATCH 6/7] removing unused imports --- sponsors/tests/test_admin.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/sponsors/tests/test_admin.py b/sponsors/tests/test_admin.py index 938faac75..26a15e2bb 100644 --- a/sponsors/tests/test_admin.py +++ b/sponsors/tests/test_admin.py @@ -5,10 +5,9 @@ from django.test import TestCase, RequestFactory -from sponsors.admin import SponsorshipStatusListFilter, SponsorshipAdmin, SponsorBenefitInline +from sponsors.admin import SponsorshipStatusListFilter, SponsorshipAdmin from sponsors.forms import SponsorBenefitAdminInlineForm -from sponsors.models import Sponsorship, SponsorBenefit - +from sponsors.models import Sponsorship class TestCustomSponsorshipStatusListFilter(TestCase): From d904feaad41390ed7050e9bfcbba4c33f9941925 Mon Sep 17 00:00:00 2001 From: jessiebelle Date: Sat, 13 Jan 2024 13:34:56 +0200 Subject: [PATCH 7/7] missed one --- sponsors/tests/test_admin.py | 1 - 1 file changed, 1 deletion(-) diff --git a/sponsors/tests/test_admin.py b/sponsors/tests/test_admin.py index 26a15e2bb..1e94fa6df 100644 --- a/sponsors/tests/test_admin.py +++ b/sponsors/tests/test_admin.py @@ -6,7 +6,6 @@ from django.test import TestCase, RequestFactory from sponsors.admin import SponsorshipStatusListFilter, SponsorshipAdmin -from sponsors.forms import SponsorBenefitAdminInlineForm from sponsors.models import Sponsorship class TestCustomSponsorshipStatusListFilter(TestCase):