Skip to content

Commit 8716928

Browse files
authored
[Refactoring] Foreign key sponsorship --> package (#1852)
* Add and populape new fk Sponsorship --> SponsorshipPackage * Populate package fk when creating new sponsorship * Update admin and sponsorship approval process to use package FK instead * Refactor to replace level_name by the new FK; * Simplify package filtering * Update sponsorship accept process to reference package * Update form initial to set package * Store logo dimension info in sponsorship package object * Refactor agreed_fee property to check for fk instead of get query * Wrap level name field with properties to isolate the behavior.
1 parent 382741c commit 8716928

17 files changed

+227
-70
lines changed

sponsors/admin.py

+12-38
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,11 @@ class SponsorshipPackageAdmin(OrderedModelAdmin):
114114
ordering = ("order",)
115115
list_display = ["name", "move_up_down_links"]
116116

117+
def get_readonly_fields(self, request, obj=None):
118+
if request.user.is_superuser:
119+
return []
120+
return ["logo_dimension"]
121+
117122

118123
class SponsorContactInline(admin.TabularInline):
119124
model = SponsorContact
@@ -154,21 +159,6 @@ def has_delete_permission(self, request, obj=None):
154159
return obj.open_for_editing
155160

156161

157-
class LevelNameFilter(admin.SimpleListFilter):
158-
title = "level name"
159-
parameter_name = "level"
160-
161-
def lookups(self, request, model_admin):
162-
qs = SponsorshipPackage.objects.all()
163-
return list({(program.name, program.name) for program in qs})
164-
165-
def queryset(self, request, queryset):
166-
if self.value() == "all":
167-
return queryset
168-
if self.value():
169-
return queryset.filter(level_name=self.value())
170-
171-
172162
@admin.register(Sponsorship)
173163
class SponsorshipAdmin(admin.ModelAdmin):
174164
change_form_template = "sponsors/admin/sponsorship_change_form.html"
@@ -178,31 +168,13 @@ class SponsorshipAdmin(admin.ModelAdmin):
178168
list_display = [
179169
"sponsor",
180170
"status",
181-
"level_name",
171+
"package",
182172
"applied_on",
183173
"approved_on",
184174
"start_date",
185175
"end_date",
186176
]
187-
list_filter = ["status", LevelNameFilter]
188-
readonly_fields = [
189-
"for_modified_package",
190-
"sponsor",
191-
"status",
192-
"applied_on",
193-
"rejected_on",
194-
"approved_on",
195-
"finalized_on",
196-
"get_estimated_cost",
197-
"get_sponsor_name",
198-
"get_sponsor_description",
199-
"get_sponsor_landing_page_url",
200-
"get_sponsor_web_logo",
201-
"get_sponsor_print_logo",
202-
"get_sponsor_primary_phone",
203-
"get_sponsor_mailing_address",
204-
"get_sponsor_contacts",
205-
]
177+
list_filter = ["status", "package"]
206178

207179
fieldsets = [
208180
(
@@ -211,13 +183,14 @@ class SponsorshipAdmin(admin.ModelAdmin):
211183
"fields": (
212184
"sponsor",
213185
"status",
186+
"package",
214187
"for_modified_package",
215-
"level_name",
216188
"sponsorship_fee",
217189
"get_estimated_cost",
218190
"start_date",
219191
"end_date",
220-
"get_contract"
192+
"get_contract",
193+
"level_name",
221194
),
222195
},
223196
),
@@ -259,6 +232,7 @@ def get_readonly_fields(self, request, obj):
259232
"rejected_on",
260233
"approved_on",
261234
"finalized_on",
235+
"level_name",
262236
"get_estimated_cost",
263237
"get_sponsor_name",
264238
"get_sponsor_description",
@@ -272,7 +246,7 @@ def get_readonly_fields(self, request, obj):
272246
]
273247

274248
if obj and obj.status != Sponsorship.APPLIED:
275-
extra = ["start_date", "end_date", "level_name", "sponsorship_fee"]
249+
extra = ["start_date", "end_date", "package", "level_name", "sponsorship_fee"]
276250
readonly_fields.extend(extra)
277251

278252
return readonly_fields

sponsors/forms.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ def __init__(self, *args, **kwargs):
348348

349349
class Meta:
350350
model = Sponsorship
351-
fields = ["start_date", "end_date", "level_name", "sponsorship_fee"]
351+
fields = ["start_date", "end_date", "package", "sponsorship_fee"]
352352

353353
def clean(self):
354354
cleaned_data = super().clean()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Generated by Django 2.0.13 on 2021-08-27 12:23
2+
3+
from django.db import migrations, models
4+
import django.db.models.deletion
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
('sponsors', '0036_auto_20210826_1930'),
11+
]
12+
13+
operations = [
14+
migrations.AddField(
15+
model_name='sponsorship',
16+
name='package',
17+
field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.SET_NULL, to='sponsors.SponsorshipPackage'),
18+
),
19+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Generated by Django 2.0.13 on 2021-08-27 12:23
2+
3+
from django.db import migrations
4+
5+
6+
def populate_sponsorship_package_fk(apps, schema_editor):
7+
Sponsorship = apps.get_model('sponsors.Sponsorship')
8+
SponsorshipPackage = apps.get_model('sponsors.SponsorshipPackage')
9+
10+
for sponsorship in Sponsorship.objects.all().iterator():
11+
try:
12+
package = SponsorshipPackage.objects.get(name=sponsorship.level_name)
13+
sponsorship.package = package
14+
sponsorship.save()
15+
except SponsorshipPackage.DoesNotExist:
16+
continue
17+
18+
19+
class Migration(migrations.Migration):
20+
21+
dependencies = [
22+
('sponsors', '0037_sponsorship_package'),
23+
]
24+
25+
operations = [
26+
migrations.RunPython(populate_sponsorship_package_fk, migrations.RunPython.noop)
27+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 2.0.13 on 2021-08-27 12:48
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('sponsors', '0038_auto_20210827_1223'),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name='sponsorship',
15+
name='level_name',
16+
field=models.CharField(blank=True, default='', help_text='DEPRECATED: will be removed after manual data sanity check.', max_length=64),
17+
),
18+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Generated by Django 2.0.13 on 2021-08-27 13:13
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('sponsors', '0039_auto_20210827_1248'),
10+
]
11+
12+
operations = [
13+
migrations.AddField(
14+
model_name='sponsorshippackage',
15+
name='logo_dimension',
16+
field=models.PositiveIntegerField(default=175),
17+
),
18+
migrations.AlterField(
19+
model_name='sponsorship',
20+
name='level_name',
21+
field=models.CharField(blank=True, default='', help_text='DEPRECATED: shall be removed after manual data sanity check.', max_length=64),
22+
),
23+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# Generated by Django 2.0.13 on 2021-08-27 13:13
2+
3+
from django.db import migrations
4+
5+
6+
def populate_logo_dimensions(apps, schema_editor):
7+
SponsorshipPackage = apps.get_model("sponsors.SponsorshipPackage")
8+
logo_dimensions = {
9+
"Visionary": 350,
10+
"Sustainability": 300,
11+
"Maintaining": 300,
12+
"Contributing": 275,
13+
"Supporting": 250,
14+
"Partner": 225,
15+
"Participating": 225,
16+
"Associate": 175,
17+
}
18+
19+
for name, dimension in logo_dimensions.items():
20+
SponsorshipPackage.objects.filter(name=name).update(logo_dimension=dimension)
21+
22+
23+
def reset_logo_dimensions(apps, schema_editor):
24+
SponsorshipPackage = apps.get_model("sponsors.SponsorshipPackage")
25+
SponsorshipPackage.objects.all().update(logo_dimension=175)
26+
27+
28+
class Migration(migrations.Migration):
29+
30+
dependencies = [
31+
('sponsors', '0040_auto_20210827_1313'),
32+
]
33+
34+
operations = [
35+
migrations.RunPython(populate_logo_dimensions, reset_logo_dimensions)
36+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 2.0.13 on 2021-08-27 13:18
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('sponsors', '0041_auto_20210827_1313'),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name='sponsorshippackage',
15+
name='logo_dimension',
16+
field=models.PositiveIntegerField(blank=True, default=175, help_text='Internal value used to control logos dimensions at sponsors page'),
17+
),
18+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 2.0.13 on 2021-08-27 13:43
2+
3+
from django.db import migrations
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('sponsors', '0042_auto_20210827_1318'),
10+
]
11+
12+
operations = [
13+
migrations.RenameField(
14+
model_name='sponsorship',
15+
old_name='level_name',
16+
new_name='level_name_old',
17+
),
18+
]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Generated by Django 2.0.13 on 2021-08-27 13:44
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('sponsors', '0043_auto_20210827_1343'),
10+
]
11+
12+
operations = [
13+
migrations.AlterField(
14+
model_name='sponsorship',
15+
name='level_name_old',
16+
field=models.CharField(blank=True, default='', help_text='DEPRECATED: shall be removed after manual data sanity check.', max_length=64, verbose_name='Level name'),
17+
),
18+
]

sponsors/models.py

+14-4
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ class SponsorshipPackage(OrderedModel):
4141

4242
name = models.CharField(max_length=64)
4343
sponsorship_amount = models.PositiveIntegerField()
44+
logo_dimension = models.PositiveIntegerField(default=175, blank=True, help_text="Internal value used to control logos dimensions at sponsors page")
4445

4546
def __str__(self):
4647
return self.name
@@ -330,14 +331,23 @@ class Sponsorship(models.Model):
330331
default=False,
331332
help_text="If true, it means the user customized the package's benefits.",
332333
)
333-
level_name = models.CharField(max_length=64, default="")
334+
level_name_old = models.CharField(max_length=64, default="", blank=True, help_text="DEPRECATED: shall be removed after manual data sanity check.", verbose_name="Level name")
335+
package = models.ForeignKey(SponsorshipPackage, null=True, on_delete=models.SET_NULL)
334336
sponsorship_fee = models.PositiveIntegerField(null=True, blank=True)
335337

336338
class Meta:
337339
permissions = [
338340
("sponsor_publisher", "Can access sponsor placement API"),
339341
]
340342

343+
@property
344+
def level_name(self):
345+
return self.package.name if self.package else self.level_name_old
346+
347+
@level_name.setter
348+
def level_name(self, value):
349+
self.level_name_old = value
350+
341351
def __str__(self):
342352
repr = f"{self.level_name} ({self.get_status_display()}) for sponsor {self.sponsor.name}"
343353
if self.start_date and self.end_date:
@@ -370,6 +380,7 @@ def new(cls, sponsor, benefits, package=None, submited_by=None):
370380
submited_by=submited_by,
371381
sponsor=sponsor,
372382
level_name="" if not package else package.name,
383+
package=package,
373384
sponsorship_fee=None if not package else package.sponsorship_amount,
374385
for_modified_package=for_modified_package,
375386
)
@@ -403,9 +414,8 @@ def agreed_fee(self):
403414
if self.status in valid_status:
404415
return self.sponsorship_fee
405416
try:
406-
package = SponsorshipPackage.objects.get(name=self.level_name)
407417
benefits = [sb.sponsorship_benefit for sb in self.package_benefits.all().select_related('sponsorship_benefit')]
408-
if package and not package.has_user_customization(benefits):
418+
if self.package and not self.package.has_user_customization(benefits):
409419
return self.sponsorship_fee
410420
except SponsorshipPackage.DoesNotExist: # sponsorship level names can change over time
411421
return None
@@ -1028,7 +1038,7 @@ def benefit_feature_class(self):
10281038
return TieredQuantity
10291039

10301040
def get_benefit_feature_kwargs(self, **kwargs):
1031-
if kwargs["sponsor_benefit"].sponsorship.level_name == self.package.name:
1041+
if kwargs["sponsor_benefit"].sponsorship.package == self.package:
10321042
return super().get_benefit_feature_kwargs(**kwargs)
10331043
return None
10341044

sponsors/templatetags/sponsors.py

+3-14
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,14 @@ def list_sponsors(logo_place, publisher=PublisherChoices.FOUNDATION.value):
3535
# logos must be grouped by package and each package muse use
3636
# specific dimensions to control the logos' grid
3737
if logo_place == LogoPlacementChoices.SPONSORS_PAGE.value:
38-
logo_dimensions = {
39-
"Visionary": "350",
40-
"Sustainability": "300",
41-
"Maintaining": "300",
42-
"Contributing": "275",
43-
"Supporting": "250",
44-
"Partner": "225",
45-
"Participating": "225",
46-
"Associate": "175",
47-
}
4838
sponsorships_by_package = OrderedDict()
4939
for pkg in packages:
50-
key = pkg.name
51-
sponsorships_by_package[key] = {
52-
"logo_dimension": logo_dimensions.get(key, "175"),
40+
sponsorships_by_package[pkg] = {
41+
"logo_dimension": str(pkg.logo_dimension),
5342
"sponsorships": [
5443
sp
5544
for sp in sponsorships
56-
if sp.level_name == key
45+
if sp.package == pkg
5746
]
5847
}
5948

0 commit comments

Comments
 (0)