-
Notifications
You must be signed in to change notification settings - Fork 649
Add downloads factories #1317
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add downloads factories #1317
Conversation
99310a4
to
ad1fac6
Compare
Tested this with the new API and made some corrections to support it. While I was at it I also fixed some other issues. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is great! I needed this when I was working on #1370 and I noticed that I didn't expose the is_latest
field.
django_get_or_create = ('slug',) | ||
|
||
creator = factory.SubFactory(UserFactory) | ||
is_published = True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add is_latest
as well? We need it to make update_supernav()
work. 5ad3ebf should expose it in both APIs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's no need to add is_latest
to the factory, if it's on the model then it's also on the factory. So in this case is_latest
will default to False
. In initial_data
the release is created like this ReleaseFactory(**obj)
, so all fields that are available in the API are set on the newly created instance. So this should already set is_latest
correctly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, yes, I missed you've added is_published = True
because its default value is False
. Good point!
downloads/factories.py
Outdated
""" | ||
Create the data for the downloads section by importing | ||
it from the python.org API. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Style nit: Newline can be dropped.
downloads/factories.py
Outdated
|
||
# Create all the releases | ||
for key, obj in objects['releases'].items(): | ||
obj.pop('release_page') # Ignore release pages |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you skip adding release pages for now or did you get blocked by something? I think we can add a # TODO
comment if it takes too long to implement it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I got blocked on something, but I cannot remember what. I'll add a TODO
for now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great! Feel free to file an issue if you remember what was the blocker.
I think there's a bug here. After I ran I created a temporary from django.core.management import BaseCommand, call_command
class Command(BaseCommand):
def handle(self, **options):
from downloads.models import update_supernav
update_supernav() After I ran it: I also applied this patch: diff --git a/downloads/models.py b/downloads/models.py
index 305143b..72f40e4 100644
--- a/downloads/models.py
+++ b/downloads/models.py
@@ -126,35 +126,34 @@ class Release(ContentManageable, NameSlugModel):
def update_supernav():
- try:
- latest_python2 = Release.objects.latest_python2()
- except Release.DoesNotExist:
- latest_python2 = None
-
try:
latest_python3 = Release.objects.latest_python3()
except Release.DoesNotExist:
- latest_python3 = None
+ # If we don't have a Python 3 release, we don't need to bother updating supernav.
+ return
python_files = []
for o in OS.objects.all():
data = {
'os': o,
- 'python2': None,
'python3': None,
}
- if latest_python2:
- data['python2'] = latest_python2.download_file_for_os(o.slug)
-
if latest_python3:
- data['python3'] = latest_python3.download_file_for_os(o.slug)
+ release_file = latest_python3.download_file_for_os(o.slug)
+ if not release_file:
+ return
+ data['python3'] = release_file
python_files.append(data)
+ if not python_files:
+ return
+
+ if not all(f['python3'] for f in python_files):
+ return
+
content = render_to_string('downloads/supernav.html', {
- 'latest_python2': latest_python2,
- 'latest_python3': latest_python3,
'python_files': python_files,
})
@@ -166,7 +165,20 @@ def update_supernav():
}
)
+
+def update_download_sources_box():
# Update latest Sources box on Download landing page
+
+ try:
+ latest_python2 = Release.objects.latest_python2()
+ except Release.DoesNotExist:
+ latest_python2 = None
+
+ try:
+ latest_python3 = Release.objects.latest_python3()
+ except Release.DoesNotExist:
+ latest_python3 = None
+
if latest_python2:
latest_python2_source = latest_python2.download_file_for_os('source')
else:
@@ -177,6 +189,9 @@ def update_supernav():
else:
latest_python3_source = None
+ if not latest_python2_source or not latest_python3_source:
+ return
+
source_content = render_to_string('downloads/download-sources-box.html', {
'latest_python2_source': latest_python2_source,
'latest_python3_source': latest_python3_source,
@@ -260,14 +275,14 @@ def purge_fastly_download_pages(sender, instance, **kwargs):
@receiver(post_save, sender=Release)
-def update_download_supernav(sender, instance, **kwargs):
- """ Update download supernav """
+def update_download_supernav_and_boxes(sender, instance, **kwargs):
# Skip in fixtures
if kwargs.get('raw', False):
return
if instance.is_published:
update_supernav()
+ update_download_sources_box()
update_homepage_download_box() |
I think I figured it out. Due to how I will work on my inline patch to make signal handlers more robust. |
Recreate the downloads section by importing data from the python.org API
* Responses don't have the 'objects' key * Responses are not paginated * Endpoints don't support the 'limit' argument
It only works on a clean database or `create_initial_data` is called with the `--flush` flag
ad1fac6
to
247713d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! I already tested this last week and will merge once Travis is happy.
Thanks for merging, glad you find it useful :) I just did a run where I added a |
It could be a bug I introduced while I was porting API v1 to DRF. Could you check https://github.com/python/pythondotorg/blob/master/downloads/tests/test_views.py whether there's a test for the |
There's However, I just did a run against API V1 and the To use API v1 replace these bits:
and
|
We don't use the release_page field much these days. You may need to create a release with a release page locally to verify it. |
Yeah, seems like release_page is a legacy field, the fix to #956 actually removes a lot of release pages. |
Recreate the downloads section by importing data from the python.org API