From ed9fece25919b1ff04f48a666a9d75124b86a304 Mon Sep 17 00:00:00 2001
From: Nicolas ROCHE <nroche@entrouvert.com>
Date: Mon, 21 Sep 2020 11:28:52 +0200
Subject: [PATCH] manager: load (ajax) assets size on export site popup
 (#45466)

---
 .../combo/manager_assets_export_size.html        |  1 +
 combo/apps/assets/urls.py                        |  2 ++
 combo/apps/assets/views.py                       | 16 ++++++++++++++++
 combo/manager/templates/combo/site_export.html   |  5 +++++
 tests/test_assets.py                             |  5 +++++
 5 files changed, 29 insertions(+)
 create mode 100644 combo/apps/assets/templates/combo/manager_assets_export_size.html

diff --git a/combo/apps/assets/templates/combo/manager_assets_export_size.html b/combo/apps/assets/templates/combo/manager_assets_export_size.html
new file mode 100644
index 00000000..845fde2f
--- /dev/null
+++ b/combo/apps/assets/templates/combo/manager_assets_export_size.html
@@ -0,0 +1 @@
+({{ size|filesizeformat }})
diff --git a/combo/apps/assets/urls.py b/combo/apps/assets/urls.py
index 1f947898..b3835718 100644
--- a/combo/apps/assets/urls.py
+++ b/combo/apps/assets/urls.py
@@ -35,9 +35,11 @@ assets_manager_urls = [
 
 urlpatterns = [
     url(r'^assets/(?P<key>[\w_:-]+)$', views.serve_asset),
     url(r'^manage/assets/', decorated_includes(manager_required,
         include(assets_manager_urls))),
 
     url(r'^api/assets/set/(?P<key>[\w_:-]+)/$', api_views.view_set,
         name='api-assets-set'),
+    url(r'^ajax/assets-export-size/$',
+        views.assets_export_size, name='combo-manager-assets-export-size'),
 ]
diff --git a/combo/apps/assets/views.py b/combo/apps/assets/views.py
index 74ae8935..2a14ebb1 100644
--- a/combo/apps/assets/views.py
+++ b/combo/apps/assets/views.py
@@ -372,8 +372,24 @@ def serve_asset(request, key):
         geometry_string += 'x%s' % height
 
     # no thumbnail whithout geometry_string or for a svg file
     if not geometry_string or asset.asset.name.endswith('svg'):
         return redirect(asset.asset.url)
 
     # get or create thumbnail and return url
     return redirect(get_thumbnail(asset.asset, geometry_string, **thumb_options).url)
+
+
+class AssetsExportSize(TemplateView):
+    template_name = 'combo/manager_assets_export_size.html'
+
+    def get_context_data(self, **kwargs):
+        context = super(AssetsExportSize, self).get_context_data(**kwargs)
+        media_prefix = default_storage.path('')
+        computed_size = 0
+        for basedir, dirnames, filenames in os.walk(media_prefix):
+            for filename in filenames:
+                computed_size += os.stat(os.path.join(basedir, filename)).st_size
+        context['size'] = computed_size
+        return context
+
+assets_export_size = AssetsExportSize.as_view()
diff --git a/combo/manager/templates/combo/site_export.html b/combo/manager/templates/combo/site_export.html
index 1eaa8fd6..1bca556d 100644
--- a/combo/manager/templates/combo/site_export.html
+++ b/combo/manager/templates/combo/site_export.html
@@ -8,10 +8,15 @@
 {% block content %}
 <form method="post">
   {% csrf_token %}
   {{ form.as_p }}
   <div class="buttons">
     <button>{% trans 'Export' %}</button>
     <a class="cancel" href="{% url 'combo-manager-homepage' %}">{% trans 'Cancel' %}</a>
   </div>
+  <script>
+    $.get("{% url 'combo-manager-assets-export-size' %}", function( data ) {
+      $("p").append("<span>" + data + "</span>")
+    }, "text" );
+  </script>
 </form>
 {% endblock %}
diff --git a/tests/test_assets.py b/tests/test_assets.py
index 87eb5922..41ad5659 100644
--- a/tests/test_assets.py
+++ b/tests/test_assets.py
@@ -133,8 +133,13 @@ def test_import_export_assets(some_assets, tmpdir):
     fd = open(filename, 'rb')
     import_assets(fd, overwrite=True)
     assert count_asset_files() == 2
     assert open('%s/assets/test.png' % path, 'r').read() == 'test'
     assert open('%s/assets/test2.png' % path, 'r').read() == 'test2'
     clean_assets_files()
     assert count_asset_files() == 0
     clean_assets_files()
+
+
+def test_assets_export_size_view(app, some_assets):
+    resp = app.get(reverse('combo-manager-assets-export-size'))
+    assert resp.text.split() == ['(9', 'bytes)']
-- 
2.28.0

