Ver código fonte

copy files from fixtures dir to MEDIA_ROOT (or remote storage) during load_initial_data (#255)

Note: This change will require any existing deployments to manually copy
bakerydemo/base/fixtures/media/original_images/ into bakerydemo/media/
Tobias McNulty 5 anos atrás
pai
commit
d06f1d0f97
46 arquivos alterados com 32 adições e 8 exclusões
  1. 0 1
      .gitignore
  2. 3 0
      .travis.yml
  3. 0 0
      bakerydemo/base/fixtures/media/original_images/Akranes.jpg
  4. 0 0
      bakerydemo/base/fixtures/media/original_images/Anadama_bread_1.jpg
  5. 0 0
      bakerydemo/base/fixtures/media/original_images/Appam_served_with_Coconut_Milk_in_Tamil_Nadu.JPG
  6. 0 0
      bakerydemo/base/fixtures/media/original_images/Arepa_asada.JPG
  7. 0 0
      bakerydemo/base/fixtures/media/original_images/Bagel.jpg
  8. 0 0
      bakerydemo/base/fixtures/media/original_images/Baguette_de_pain_WikiCheese_Lausanne.jpg
  9. 0 0
      bakerydemo/base/fixtures/media/original_images/Bammies.jpg
  10. 0 0
      bakerydemo/base/fixtures/media/original_images/Bazin.jpg
  11. 0 0
      bakerydemo/base/fixtures/media/original_images/Bean-jam-bunanpankatori-cityjapan.JPG
  12. 0 0
      bakerydemo/base/fixtures/media/original_images/Bedouins_making_bread.jpg
  13. 0 0
      bakerydemo/base/fixtures/media/original_images/Belgische_waffeln.jpg
  14. 0 0
      bakerydemo/base/fixtures/media/original_images/Bhakri_1.jpg
  15. 0 0
      bakerydemo/base/fixtures/media/original_images/Bolani_Afghan_bread_01.jpg
  16. 0 0
      bakerydemo/base/fixtures/media/original_images/Breads_1.jpg
  17. 0 0
      bakerydemo/base/fixtures/media/original_images/Capture_decran_2017-03-23_a_15.16.46.png
  18. 0 0
      bakerydemo/base/fixtures/media/original_images/Hofn.jpg
  19. 0 0
      bakerydemo/base/fixtures/media/original_images/Olandshvedebrod_6082070226.jpg
  20. 0 0
      bakerydemo/base/fixtures/media/original_images/Sourdough_rye_with_walnuts.jpg
  21. 0 0
      bakerydemo/base/fixtures/media/original_images/aevar-gudmundsson-selfoss.jpg
  22. 0 0
      bakerydemo/base/fixtures/media/original_images/bakingsoda.jpg
  23. 0 0
      bakerydemo/base/fixtures/media/original_images/bostoncream.jpg
  24. 0 0
      bakerydemo/base/fixtures/media/original_images/bread5.jpg
  25. 0 0
      bakerydemo/base/fixtures/media/original_images/bread6.jpg
  26. 0 0
      bakerydemo/base/fixtures/media/original_images/breads1.jpg
  27. 0 0
      bakerydemo/base/fixtures/media/original_images/breads2.jpg
  28. 0 0
      bakerydemo/base/fixtures/media/original_images/breads3.jpg
  29. 0 0
      bakerydemo/base/fixtures/media/original_images/breads4.jpg
  30. 0 0
      bakerydemo/base/fixtures/media/original_images/hof-cornell-university-filter.jpg
  31. 0 0
      bakerydemo/base/fixtures/media/original_images/howlin.jpg
  32. 0 0
      bakerydemo/base/fixtures/media/original_images/lightnin_hopkins.jpg
  33. 0 0
      bakerydemo/base/fixtures/media/original_images/muddy_waters.jpg
  34. 0 0
      bakerydemo/base/fixtures/media/original_images/muddy_waters_hUPkmSW.jpg
  35. 0 0
      bakerydemo/base/fixtures/media/original_images/reykjavik-sverrir-thorolfsson.jpg
  36. 0 0
      bakerydemo/base/fixtures/media/original_images/robert_johnson2.jpg
  37. 0 0
      bakerydemo/base/fixtures/media/original_images/sliced.jpg
  38. 0 0
      bakerydemo/base/fixtures/media/original_images/soda_bread.jpg
  39. 0 0
      bakerydemo/base/fixtures/media/original_images/sprint_crew.jpg
  40. 0 0
      bakerydemo/base/fixtures/media/original_images/vik.jpg
  41. 0 0
      bakerydemo/base/fixtures/media/original_images/yeast.jpg
  42. 19 0
      bakerydemo/base/management/commands/load_initial_data.py
  43. 4 0
      bakerydemo/media/.gitignore
  44. 4 0
      docker-compose.yml
  45. 2 4
      readme.md
  46. 0 3
      requirements/production.txt

+ 0 - 1
.gitignore

@@ -9,7 +9,6 @@
 /data
 /data
 .~*
 .~*
 common/CACHE
 common/CACHE
-bakerydemo/media/images/*
 bakerydemo/settings/local.py
 bakerydemo/settings/local.py
 bakerydemodb
 bakerydemodb
 __pycache__
 __pycache__

+ 3 - 0
.travis.yml

@@ -22,6 +22,9 @@ script:
   - docker-compose run app /bin/sh -c "/venv/bin/pip install flake8 && /venv/bin/flake8 ."
   - docker-compose run app /bin/sh -c "/venv/bin/pip install flake8 && /venv/bin/flake8 ."
   - docker-compose run app /venv/bin/python /code/manage.py check
   - docker-compose run app /venv/bin/python /code/manage.py check
   - docker-compose run app /venv/bin/python /code/manage.py makemigrations --check
   - docker-compose run app /venv/bin/python /code/manage.py makemigrations --check
+  - docker-compose run app /venv/bin/python /code/manage.py load_initial_data
+  # Check for broken image links (wget will have a non-zero exit code if it encounters a 404)
+  - wget -m http://localhost:8000
 
 
 after_script:
 after_script:
   - docker-compose logs
   - docker-compose logs

+ 0 - 0
bakerydemo/media/original_images/Akranes.jpg → bakerydemo/base/fixtures/media/original_images/Akranes.jpg


+ 0 - 0
bakerydemo/media/original_images/Anadama_bread_1.jpg → bakerydemo/base/fixtures/media/original_images/Anadama_bread_1.jpg


+ 0 - 0
bakerydemo/media/original_images/Appam_served_with_Coconut_Milk_in_Tamil_Nadu.JPG → bakerydemo/base/fixtures/media/original_images/Appam_served_with_Coconut_Milk_in_Tamil_Nadu.JPG


+ 0 - 0
bakerydemo/media/original_images/Arepa_asada.JPG → bakerydemo/base/fixtures/media/original_images/Arepa_asada.JPG


+ 0 - 0
bakerydemo/media/original_images/Bagel.jpg → bakerydemo/base/fixtures/media/original_images/Bagel.jpg


+ 0 - 0
bakerydemo/media/original_images/Baguette_de_pain_WikiCheese_Lausanne.jpg → bakerydemo/base/fixtures/media/original_images/Baguette_de_pain_WikiCheese_Lausanne.jpg


+ 0 - 0
bakerydemo/media/original_images/Bammies.jpg → bakerydemo/base/fixtures/media/original_images/Bammies.jpg


+ 0 - 0
bakerydemo/media/original_images/Bazin.jpg → bakerydemo/base/fixtures/media/original_images/Bazin.jpg


+ 0 - 0
bakerydemo/media/original_images/Bean-jam-bunanpankatori-cityjapan.JPG → bakerydemo/base/fixtures/media/original_images/Bean-jam-bunanpankatori-cityjapan.JPG


+ 0 - 0
bakerydemo/media/original_images/Bedouins_making_bread.jpg → bakerydemo/base/fixtures/media/original_images/Bedouins_making_bread.jpg


+ 0 - 0
bakerydemo/media/original_images/Belgische_waffeln.jpg → bakerydemo/base/fixtures/media/original_images/Belgische_waffeln.jpg


+ 0 - 0
bakerydemo/media/original_images/Bhakri_1.jpg → bakerydemo/base/fixtures/media/original_images/Bhakri_1.jpg


+ 0 - 0
bakerydemo/media/original_images/Bolani_Afghan_bread_01.jpg → bakerydemo/base/fixtures/media/original_images/Bolani_Afghan_bread_01.jpg


+ 0 - 0
bakerydemo/media/original_images/Breads_1.jpg → bakerydemo/base/fixtures/media/original_images/Breads_1.jpg


+ 0 - 0
bakerydemo/media/original_images/Capture_decran_2017-03-23_a_15.16.46.png → bakerydemo/base/fixtures/media/original_images/Capture_decran_2017-03-23_a_15.16.46.png


+ 0 - 0
bakerydemo/media/original_images/Hofn.jpg → bakerydemo/base/fixtures/media/original_images/Hofn.jpg


+ 0 - 0
bakerydemo/media/original_images/Olandshvedebrod_6082070226.jpg → bakerydemo/base/fixtures/media/original_images/Olandshvedebrod_6082070226.jpg


+ 0 - 0
bakerydemo/media/original_images/Sourdough_rye_with_walnuts.jpg → bakerydemo/base/fixtures/media/original_images/Sourdough_rye_with_walnuts.jpg


+ 0 - 0
bakerydemo/media/original_images/aevar-gudmundsson-selfoss.jpg → bakerydemo/base/fixtures/media/original_images/aevar-gudmundsson-selfoss.jpg


+ 0 - 0
bakerydemo/media/original_images/bakingsoda.jpg → bakerydemo/base/fixtures/media/original_images/bakingsoda.jpg


+ 0 - 0
bakerydemo/media/original_images/bostoncream.jpg → bakerydemo/base/fixtures/media/original_images/bostoncream.jpg


+ 0 - 0
bakerydemo/media/original_images/bread5.jpg → bakerydemo/base/fixtures/media/original_images/bread5.jpg


+ 0 - 0
bakerydemo/media/original_images/bread6.jpg → bakerydemo/base/fixtures/media/original_images/bread6.jpg


+ 0 - 0
bakerydemo/media/original_images/breads1.jpg → bakerydemo/base/fixtures/media/original_images/breads1.jpg


+ 0 - 0
bakerydemo/media/original_images/breads2.jpg → bakerydemo/base/fixtures/media/original_images/breads2.jpg


+ 0 - 0
bakerydemo/media/original_images/breads3.jpg → bakerydemo/base/fixtures/media/original_images/breads3.jpg


+ 0 - 0
bakerydemo/media/original_images/breads4.jpg → bakerydemo/base/fixtures/media/original_images/breads4.jpg


+ 0 - 0
bakerydemo/media/original_images/hof-cornell-university-filter.jpg → bakerydemo/base/fixtures/media/original_images/hof-cornell-university-filter.jpg


+ 0 - 0
bakerydemo/media/original_images/howlin.jpg → bakerydemo/base/fixtures/media/original_images/howlin.jpg


+ 0 - 0
bakerydemo/media/original_images/lightnin_hopkins.jpg → bakerydemo/base/fixtures/media/original_images/lightnin_hopkins.jpg


+ 0 - 0
bakerydemo/media/original_images/muddy_waters.jpg → bakerydemo/base/fixtures/media/original_images/muddy_waters.jpg


+ 0 - 0
bakerydemo/media/original_images/muddy_waters_hUPkmSW.jpg → bakerydemo/base/fixtures/media/original_images/muddy_waters_hUPkmSW.jpg


+ 0 - 0
bakerydemo/media/original_images/reykjavik-sverrir-thorolfsson.jpg → bakerydemo/base/fixtures/media/original_images/reykjavik-sverrir-thorolfsson.jpg


+ 0 - 0
bakerydemo/media/original_images/robert_johnson2.jpg → bakerydemo/base/fixtures/media/original_images/robert_johnson2.jpg


+ 0 - 0
bakerydemo/media/original_images/sliced.jpg → bakerydemo/base/fixtures/media/original_images/sliced.jpg


+ 0 - 0
bakerydemo/media/original_images/soda_bread.jpg → bakerydemo/base/fixtures/media/original_images/soda_bread.jpg


+ 0 - 0
bakerydemo/media/original_images/sprint_crew.jpg → bakerydemo/base/fixtures/media/original_images/sprint_crew.jpg


+ 0 - 0
bakerydemo/media/original_images/vik.jpg → bakerydemo/base/fixtures/media/original_images/vik.jpg


+ 0 - 0
bakerydemo/media/original_images/yeast.jpg → bakerydemo/base/fixtures/media/original_images/yeast.jpg


+ 19 - 0
bakerydemo/base/management/commands/load_initial_data.py

@@ -1,6 +1,7 @@
 import os
 import os
 
 
 from django.conf import settings
 from django.conf import settings
+from django.core.files.storage import default_storage, FileSystemStorage
 from django.core.management.base import BaseCommand
 from django.core.management.base import BaseCommand
 from django.core.management import call_command
 from django.core.management import call_command
 
 
@@ -8,10 +9,28 @@ from wagtail.core.models import Site, Page
 
 
 
 
 class Command(BaseCommand):
 class Command(BaseCommand):
+    def _copy_files(self, local_storage, path):
+        """
+        Recursively copy files from local_storage to default_storage. Used
+        to automatically bootstrap the media directory (both locally and on
+        cloud providers) with the images linked from the initial data (and
+        included in MEDIA_ROOT).
+        """
+        directories, file_names = local_storage.listdir(path)
+        for directory in directories:
+            self._copy_files(local_storage, path + directory + '/')
+        for file_name in file_names:
+            with local_storage.open(path + file_name) as file_:
+                default_storage.save(path + file_name, file_)
+
     def handle(self, **options):
     def handle(self, **options):
         fixtures_dir = os.path.join(settings.PROJECT_DIR, 'base', 'fixtures')
         fixtures_dir = os.path.join(settings.PROJECT_DIR, 'base', 'fixtures')
         fixture_file = os.path.join(fixtures_dir, 'bakerydemo.json')
         fixture_file = os.path.join(fixtures_dir, 'bakerydemo.json')
 
 
+        print("Copying media files to configured storage...")
+        local_storage = FileSystemStorage(os.path.join(fixtures_dir, 'media'))
+        self._copy_files(local_storage, '')  # file storage paths are relative
+
         # Wagtail creates default Site and Page instances during install, but we already have
         # Wagtail creates default Site and Page instances during install, but we already have
         # them in the data load. Remove the auto-generated ones.
         # them in the data load. Remove the auto-generated ones.
         if Site.objects.filter(hostname='localhost').exists():
         if Site.objects.filter(hostname='localhost').exists():

+ 4 - 0
bakerydemo/media/.gitignore

@@ -0,0 +1,4 @@
+# Ignore everything in this directory
+*
+# Except this file
+!.gitignore

+ 4 - 0
docker-compose.yml

@@ -29,6 +29,8 @@ services:
     build:
     build:
       context: .
       context: .
       dockerfile: ./Dockerfile
       dockerfile: ./Dockerfile
+    volumes:
+      - media-root:/code/bakerydemo/media
     links:
     links:
       - db:db
       - db:db
       - redis:redis
       - redis:redis
@@ -39,3 +41,5 @@ services:
       - db
       - db
       - redis
       - redis
       - elasticsearch
       - elasticsearch
+volumes:
+  media-root:

+ 2 - 4
readme.md

@@ -188,10 +188,8 @@ environment, set the same environment variables there using the method appropria
 
 
 Once Heroku restarts your application or your Docker container is refreshed, you should have persistent media storage!
 Once Heroku restarts your application or your Docker container is refreshed, you should have persistent media storage!
 
 
-To copy the initial data included with this demo to the S3 bucket (assuming you ran `./manage.py load_initial_data` per
-the above), you can use the AWS CLI included with the requirements:
-
-    heroku run aws s3 sync bakerydemo/media/original_images/ s3://<bucket-name>/original_images/
+Running `./manage.py load_initial_data` will copy local images to S3, but if you set up S3 after you ran it the first
+time you might need to run it again.
 
 
 # Next steps
 # Next steps
 
 

+ 0 - 3
requirements/production.txt

@@ -15,6 +15,3 @@ botocore==1.12.33
 aws-requests-auth==0.4.0
 aws-requests-auth==0.4.0
 django-redis==4.8.0
 django-redis==4.8.0
 django_cache_url==2.0.0
 django_cache_url==2.0.0
-# For copying initial media to S3 bucket
-awscli==1.16.43
-urllib3>=1.21.1,<1.26