From 401f0f6c1ea1ca563c0e8c98615eedfe27572851 Mon Sep 17 00:00:00 2001
From: Owen Graham <ohgraham1@madisoncollege.edu>
Date: Mon, 27 Jun 2022 14:57:19 -0500
Subject: [PATCH] Give plots even better filenames using headers

---
 visualizer/plotting.py | 24 +++++++++++++++++++-----
 1 file changed, 19 insertions(+), 5 deletions(-)

diff --git a/visualizer/plotting.py b/visualizer/plotting.py
index b10fe07..28998e6 100644
--- a/visualizer/plotting.py
+++ b/visualizer/plotting.py
@@ -61,7 +61,10 @@ def plot_time_series():
     name = station['name']
     plt.suptitle(f'{meas.title} measurements, {name} Station, '
                  f'{data[0][0].year}')
-    return savefig_response(fig)
+    return savefig_response(
+        fig,
+        filename=f'time-series.{station_id}.{year}.{meas.url_name}.png',
+    )
 
 
 @add_plotter('overlay', 'Overlay')
@@ -119,7 +122,12 @@ def plot_overlay():
     title_dsets = ' / '.join(f'{dset.name} Station, {dset.data[0][0].year}'
                              for dset in datasets)
     plt.suptitle(f'{meas.title} measurements, {title_dsets}')
-    return savefig_response(fig)
+    filename_dsets = '.'.join(f'{dset.station_id}.{dset.year}'
+                              for dset in datasets)
+    return savefig_response(
+        fig,
+        filename=f'overlay.{filename_dsets}.{meas.url_name}.png',
+    )
 
 
 @add_plotter('boxplot', 'Boxplot')
@@ -169,11 +177,17 @@ def plot_boxplot():
     name = station['name']
     plt.suptitle(f'{meas.title} measurements, {name} Station, '
                  f'{start_year} - {end_year}.')
-    return savefig_response(fig)
+    return savefig_response(
+        fig,
+        filename=(f'boxplot.{station_id}.{year1}.{year2}.{meas.url_name}.png'),
+    )
 
 
-def savefig_response(fig):
+def savefig_response(fig, filename=None):
     """Make an image response with `fig.savefig()`."""
     buf = BytesIO()
     fig.savefig(buf, format='png')
-    return Response(buf.getvalue(), mimetype='image/png')
+    res = Response(buf.getvalue(), mimetype='image/png')
+    if filename is not None:
+        res.headers['Content-Disposition'] = f'inline; filename="{filename}"'
+    return res
-- 
GitLab