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

Lines changed: 12 additions & 38 deletions
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

Lines changed: 1 addition & 1 deletion
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()
Lines changed: 19 additions & 0 deletions
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+
]
Lines changed: 27 additions & 0 deletions
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+
]
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-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+
]
Lines changed: 23 additions & 0 deletions
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+
]
Lines changed: 36 additions & 0 deletions
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+
]
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-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+
]
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-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+
]
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-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+
]

0 commit comments

Comments
 (0)