]> git.0d.be Git - django-panik-emissions.git/commitdiff
load-from-drupal-json: handle errors when loading remote images
authorFrédéric Péters <fpeters@0d.be>
Thu, 26 Dec 2019 13:12:53 +0000 (14:12 +0100)
committerFrédéric Péters <fpeters@0d.be>
Thu, 26 Dec 2019 13:12:53 +0000 (14:12 +0100)
emissions/management/commands/load-from-drupal-json.py

index 86d95e2cbd60f4ed953353832ca5456c200d8111..de82ecfb69d54fa7a701bb3c8be3cddd88525e82 100644 (file)
@@ -60,12 +60,16 @@ class Command(BaseCommand):
                 if default_storage.exists(orig_path):
                     path = orig_path
                 else:
-                    path = default_storage.save(
-                            'images/%s/%s' % (
-                                emission.slug,
-                                episode_data.get('image').split('/')[-1]),
-                            ContentFile(requests.get(episode_data.get('image')).content))
-                episode.image = default_storage.open(path)
+                    episode.image = None
+                    episode.save()
+                    response = requests.get(episode_data.get('image'))
+                    if response.status_code == 200:
+                        path = default_storage.save(
+                                'images/%s/%s' % (
+                                    emission.slug,
+                                    episode_data.get('image').split('/')[-1]),
+                                ContentFile(response.content))
+                        episode.image = default_storage.open(path)
                 try:
                     episode.save()
                 except OSError:  # OSError: cannot identify image file