Skip to content

Commit 6704290

Browse files
berinhardewdurbin
andauthored
New boolean to SponsorshipPackage (#1865)
* Add boolean flag to toggle package in application form * Data migration to update existing sponsorship packages * Only list advertisable packages in the application form * reorder migrations * minor rename and text updates Co-authored-by: Ee Durbin <[email protected]>
1 parent 8a02c10 commit 6704290

File tree

8 files changed

+70
-13
lines changed

8 files changed

+70
-13
lines changed

sponsors/admin.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ def update_related_sponsorships(self, *args, **kwargs):
112112
@admin.register(SponsorshipPackage)
113113
class SponsorshipPackageAdmin(OrderedModelAdmin):
114114
ordering = ("order",)
115-
list_display = ["name", "move_up_down_links"]
115+
list_display = ["name", "advertise", "move_up_down_links"]
116+
list_filter = ["advertise"]
117+
search_fields = ["name"]
116118

117119
def get_readonly_fields(self, request, obj=None):
118120
if request.user.is_superuser:

sponsors/forms.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class Meta:
4646

4747
class SponsorshiptBenefitsForm(forms.Form):
4848
package = forms.ModelChoiceField(
49-
queryset=SponsorshipPackage.objects.all(),
49+
queryset=SponsorshipPackage.objects.list_advertisables(),
5050
widget=forms.RadioSelect(),
5151
required=False,
5252
empty_label=None,

sponsors/managers.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,8 @@ def with_packages(self):
6767
.exclude(num_packages=0)
6868
.order_by("-num_packages", "order")
6969
)
70+
71+
72+
class SponsorshipPackageManager(OrderedModelManager):
73+
def list_advertisables(self):
74+
return self.filter(advertise=True)
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 2.0.13 on 2021-09-08 13:52
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('sponsors', '0045_add_added_by_user_sponsorbenefit'),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name='sponsorshippackage',
15+
name='advertise',
16+
field=models.BooleanField(default=False, help_text='If checked, this package will be advertised in the sponsosrhip application'),
17+
),
18+
]
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Generated by Django 2.0.13 on 2021-09-08 13:57
2+
3+
from django.db import migrations
4+
5+
6+
def update_package_as_advertisable(apps, schema_editor):
7+
SponsorshipPackage = apps.get_model("sponsors.SponsorshipPackage")
8+
# initial sponsorship packages should remaing visible in the form
9+
SponsorshipPackage.objects.all().update(advertise=True)
10+
11+
12+
class Migration(migrations.Migration):
13+
14+
dependencies = [
15+
('sponsors', '0046_sponsorshippackage_advertise'),
16+
]
17+
18+
operations = [
19+
migrations.RunPython(update_package_as_advertisable, migrations.RunPython.noop)
20+
]

sponsors/models.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
SponsorContactQuerySet,
2424
SponsorshipQuerySet,
2525
SponsorshipBenefitManager,
26+
SponsorshipPackageManager
2627
)
2728
from .exceptions import (
2829
SponsorWithExistingApplicationException,
@@ -38,9 +39,11 @@ class SponsorshipPackage(OrderedModel):
3839
"""
3940
Represent default packages of benefits (visionary, sustainability etc)
4041
"""
42+
objects = SponsorshipPackageManager()
4143

4244
name = models.CharField(max_length=64)
4345
sponsorship_amount = models.PositiveIntegerField()
46+
advertise = models.BooleanField(default=False, blank=True, help_text="If checked, this package will be advertised in the sponsosrhip application")
4447
logo_dimension = models.PositiveIntegerField(default=175, blank=True, help_text="Internal value used to control logos dimensions at sponsors page")
4548

4649
def __str__(self):

sponsors/tests/test_forms.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def setUp(self):
2828
self.program_2_benefits = baker.make(
2929
SponsorshipBenefit, program=self.wk, _quantity=5
3030
)
31-
self.package = baker.make("sponsors.SponsorshipPackage")
31+
self.package = baker.make("sponsors.SponsorshipPackage", advertise=True)
3232
self.package.benefits.add(*self.program_1_benefits)
3333
self.package.benefits.add(*self.program_2_benefits)
3434

@@ -63,6 +63,15 @@ def test_specific_field_to_select_add_ons(self):
6363
for benefit in self.program_2_benefits:
6464
self.assertIn(benefit.id, [c[0] for c in choices])
6565

66+
def test_package_list_only_advertisable_ones(self):
67+
ads_pkgs = baker.make('SponsorshipPackage', advertise=True, _quantity=2)
68+
baker.make('SponsorshipPackage', advertise=False)
69+
70+
form = SponsorshiptBenefitsForm()
71+
field = form.fields.get("package")
72+
73+
self.assertEqual(3, field.queryset.count())
74+
6675
def test_invalidate_form_without_benefits(self):
6776
form = SponsorshiptBenefitsForm(data={})
6877
self.assertFalse(form.is_valid())
@@ -127,19 +136,19 @@ def test_package_only_benefit_without_package_should_not_validate(self):
127136

128137
def test_package_only_benefit_with_wrong_package_should_not_validate(self):
129138
SponsorshipBenefit.objects.all().update(package_only=True)
130-
package = baker.make("sponsors.SponsorshipPackage")
139+
package = baker.make("sponsors.SponsorshipPackage", advertise=True)
131140
package.benefits.add(*SponsorshipBenefit.objects.all())
132141

133142
data = {
134143
"benefits_psf": [self.program_1_benefits[0]],
135-
"package": baker.make("sponsors.SponsorshipPackage").id, # other package
144+
"package": baker.make("sponsors.SponsorshipPackage", advertise=True).id, # other package
136145
}
137146

138147
form = SponsorshiptBenefitsForm(data=data)
139148
self.assertFalse(form.is_valid())
140149
self.assertIn(
141150
"The application has 1 or more package only benefits but wrong sponsor package.",
142-
form.errors["__all__"],
151+
form.errors["__all__"][0],
143152
)
144153

145154
data = {

sponsors/tests/test_views.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ def setUp(self):
3535
self.program_2_benefits = baker.make(
3636
SponsorshipBenefit, program=self.wk, _quantity=5
3737
)
38-
self.package = baker.make("sponsors.SponsorshipPackage")
38+
self.package = baker.make("sponsors.SponsorshipPackage", advertise=True)
3939
self.package.benefits.add(*self.program_1_benefits)
40-
package_2 = baker.make("sponsors.SponsorshipPackage")
40+
package_2 = baker.make("sponsors.SponsorshipPackage", advertise=True)
4141
package_2.benefits.add(*self.program_2_benefits)
4242
self.add_on_benefits = baker.make(
4343
SponsorshipBenefit, program=self.psf, _quantity=2
@@ -61,8 +61,8 @@ def populate_test_cookie(self):
6161
session.save()
6262

6363
def test_display_template_with_form_and_context(self):
64-
psf_package = baker.make("sponsors.SponsorshipPackage")
65-
extra_package = baker.make("sponsors.SponsorshipPackage")
64+
psf_package = baker.make("sponsors.SponsorshipPackage", advertise=True)
65+
extra_package = baker.make("sponsors.SponsorshipPackage", advertise=True)
6666

6767
r = self.client.get(self.url)
6868
packages = r.context["sponsorship_packages"]
@@ -103,15 +103,15 @@ def test_populate_form_initial_with_values_from_cookie(self):
103103
self.assertEqual(self.data, r.context["form"].initial)
104104

105105
def test_capacity_flag(self):
106-
psf_package = baker.make("sponsors.SponsorshipPackage")
106+
psf_package = baker.make("sponsors.SponsorshipPackage", advertise=True)
107107
r = self.client.get(self.url)
108108
self.assertEqual(False, r.context["capacities_met"])
109109

110110
def test_capacity_flag_when_needed(self):
111111
at_capacity_benefit = baker.make(
112112
SponsorshipBenefit, program=self.psf, capacity=0, soft_capacity=False
113113
)
114-
psf_package = baker.make("sponsors.SponsorshipPackage")
114+
psf_package = baker.make("sponsors.SponsorshipPackage", advertise=True)
115115

116116
r = self.client.get(self.url)
117117
self.assertEqual(True, r.context["capacities_met"])
@@ -149,7 +149,7 @@ def setUp(self):
149149
self.program_1_benefits = baker.make(
150150
SponsorshipBenefit, program=self.psf, _quantity=3
151151
)
152-
self.package = baker.make("sponsors.SponsorshipPackage")
152+
self.package = baker.make("sponsors.SponsorshipPackage", advertise=True)
153153
for benefit in self.program_1_benefits:
154154
benefit.packages.add(self.package)
155155
self.client.cookies["sponsorship_selected_benefits"] = json.dumps(

0 commit comments

Comments
 (0)