Skip to content

Commit 4bf3b75

Browse files
committed
Refactor signal handlers in downloads/models.py
1 parent 8683cdd commit 4bf3b75

File tree

1 file changed

+50
-43
lines changed

1 file changed

+50
-43
lines changed

downloads/models.py

Lines changed: 50 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -126,35 +126,33 @@ def get_version(self):
126126

127127

128128
def update_supernav():
129-
try:
130-
latest_python2 = Release.objects.latest_python2()
131-
except Release.DoesNotExist:
132-
latest_python2 = None
133-
134-
try:
135-
latest_python3 = Release.objects.latest_python3()
136-
except Release.DoesNotExist:
137-
latest_python3 = None
129+
latest_python3 = Release.objects.latest_python3()
130+
if not latest_python3:
131+
return
138132

139133
python_files = []
140134
for o in OS.objects.all():
141135
data = {
142136
'os': o,
143-
'python2': None,
144137
'python3': None,
145138
}
146139

147-
if latest_python2:
148-
data['python2'] = latest_python2.download_file_for_os(o.slug)
149-
150-
if latest_python3:
151-
data['python3'] = latest_python3.download_file_for_os(o.slug)
140+
release_file = latest_python3.download_file_for_os(o.slug)
141+
if not release_file:
142+
continue
143+
data['python3'] = release_file
152144

153145
python_files.append(data)
154146

147+
if not python_files:
148+
return
149+
150+
if not all(f['python3'] for f in python_files):
151+
# We have a latest Python release, different OSes, but don't have release
152+
# files for the release, so return early.
153+
return
154+
155155
content = render_to_string('downloads/supernav.html', {
156-
'latest_python2': latest_python2,
157-
'latest_python3': latest_python3,
158156
'python_files': python_files,
159157
})
160158

@@ -166,21 +164,27 @@ def update_supernav():
166164
}
167165
)
168166

169-
# Update latest Sources box on Download landing page
167+
168+
def update_download_landing_sources_box():
169+
latest_python2 = Release.objects.latest_python2()
170+
latest_python3 = Release.objects.latest_python3()
171+
172+
context = {}
173+
170174
if latest_python2:
171175
latest_python2_source = latest_python2.download_file_for_os('source')
172-
else:
173-
latest_python2_source = None
176+
if latest_python2_source:
177+
context['latest_python2_source'] = latest_python2_source
174178

175179
if latest_python3:
176180
latest_python3_source = latest_python3.download_file_for_os('source')
177-
else:
178-
latest_python3_source = None
181+
if latest_python3_source:
182+
context['latest_python3_source'] = latest_python3_source
179183

180-
source_content = render_to_string('downloads/download-sources-box.html', {
181-
'latest_python2_source': latest_python2_source,
182-
'latest_python3_source': latest_python3_source,
183-
})
184+
if 'latest_python2_source' not in context or 'latest_python3_source' not in context:
185+
return
186+
187+
source_content = render_to_string('downloads/download-sources-box.html', context)
184188
source_box, _ = Box.objects.update_or_create(
185189
label='download-sources',
186190
defaults={
@@ -191,20 +195,21 @@ def update_supernav():
191195

192196

193197
def update_homepage_download_box():
194-
try:
195-
latest_python2 = Release.objects.latest_python2()
196-
except Release.DoesNotExist:
197-
latest_python2 = None
198-
199-
try:
200-
latest_python3 = Release.objects.latest_python3()
201-
except Release.DoesNotExist:
202-
latest_python3 = None
203-
204-
content = render_to_string('downloads/homepage-downloads-box.html', {
205-
'latest_python2': latest_python2,
206-
'latest_python3': latest_python3,
207-
})
198+
latest_python2 = Release.objects.latest_python2()
199+
latest_python3 = Release.objects.latest_python3()
200+
201+
context = {}
202+
203+
if latest_python2:
204+
context['latest_python2'] = latest_python2
205+
206+
if latest_python3:
207+
context['latest_python3'] = latest_python3
208+
209+
if 'latest_python2' not in context or 'latest_python3' not in context:
210+
return
211+
212+
content = render_to_string('downloads/homepage-downloads-box.html', context)
208213

209214
box, _ = Box.objects.update_or_create(
210215
label='homepage-downloads',
@@ -260,14 +265,16 @@ def purge_fastly_download_pages(sender, instance, **kwargs):
260265

261266

262267
@receiver(post_save, sender=Release)
263-
def update_download_supernav(sender, instance, **kwargs):
264-
""" Update download supernav """
268+
def update_download_supernav_and_boxes(sender, instance, **kwargs):
265269
# Skip in fixtures
266270
if kwargs.get('raw', False):
267271
return
268272

269273
if instance.is_published:
270-
update_supernav()
274+
# Supernav only has download buttons for Python 3.
275+
if instance.version == instance.PYTHON3:
276+
update_supernav()
277+
update_download_landing_sources_box()
271278
update_homepage_download_box()
272279

273280

0 commit comments

Comments
 (0)