Browse Source

Don't save svg, return rendering at api endpoint.

One less layer of needless indirection is nice, but I did this so that I could
use this app on heroku, which does not allow saving files to the file-system.
Tyler Hallada 9 years ago
parent
commit
0175764b17
3 changed files with 14 additions and 11 deletions
  1. 12 8
      laundry_app/laundry.py
  2. 1 1
      laundry_app/static/js/laundry.js
  3. 1 2
      laundry_app/views.py

+ 12 - 8
laundry_app/laundry.py

@@ -33,21 +33,26 @@ def get_num_machines_per_status(status, records):
33 33
             len(filter(lambda r: r.machine.type == DRYER and
34 34
                 r.availability == status, records))]
35 35
 
36
-def generate_current_chart(filepath, records, hall):
36
+def generate_current_chart(records, hall, filepath=None):
37 37
     """
38 38
     Generate stacked bar chart of current laundry usage for specified hall and
39 39
     save svg at filepath.
40 40
     """
41 41
     custom_style = Style(colors=('#B6E354', '#FF5995', '#FEED6C', '#E41B17'))
42
-    chart = pygal.StackedBar(style=custom_style, width=800, height=512, explicit_size=True)
42
+    chart = pygal.StackedBar(style=custom_style, width=800, height=512,
43
+                             explicit_size=True)
43 44
     chart.title = 'Current laundry machine usage in ' + hall.name
44 45
     chart.x_labels = ['Washers', 'Dryers']
46
+    print records
45 47
     chart.add('Available', get_num_machines_per_status(AVAILABLE, records))
46 48
     chart.add('In Use', get_num_machines_per_status(IN_USE, records))
47 49
     chart.add('Cycle Complete', get_num_machines_per_status(CYCLE_COMPLETE, records))
48 50
     chart.add('Unavailable', get_num_machines_per_status(UNAVAILABLE, records))
49 51
     chart.range = [0, 11]
50
-    chart.render_to_file(filepath)
52
+    if filepath:
53
+        chart.render_to_file(filepath)
54
+    else:
55
+        return chart.render()
51 56
 
52 57
 # NOTE: Abandoning generating the weekly chart via mysql and Django for now.
53 58
 # (cron script and csv file is just easier) Sorry if there are a lot of
@@ -112,9 +117,8 @@ def update(hall, filepath=None):
112 117
             machine = LaundryMachine.objects.get(number=number, hall=hall)
113 118
         record = LaundryRecord(machine=machine, availability=availability,
114 119
                 time_remaining=time_remaining)
115
-        if filepath:
116
-            records.append(record)
117
-        else:
118
-            record.save()
120
+        records.append(record)
119 121
     if filepath:
120
-        generate_current_chart(filepath, records, hall)
122
+        generate_current_chart(records, hall, filepath=filepath)
123
+    else:
124
+        return generate_current_chart(records, hall)

+ 1 - 1
laundry_app/static/js/laundry.js

@@ -27,7 +27,7 @@ function update_charts(selected) {
27 27
         request = $.ajax({
28 28
             url: '/ajax/current/' + halls[selected]
29 29
         }).done(function (result) {
30
-            svg.attr('src', result);
30
+            svg.attr('src', '/ajax/current/' + halls[selected]);
31 31
             $('#loading').remove();
32 32
             console.log(svg);
33 33
             $('.current-chart').append(svg);

+ 1 - 2
laundry_app/views.py

@@ -23,7 +23,6 @@ def ajax_get_current(request, hall):
23 23
     hall_obj = get_object_or_404(Hall, pk=hall)
24 24
     filename = str(hall_obj.id) + '_current.svg'
25 25
     try:
26
-        laundry.update(hall_obj, filepath=join(SVG_DIR, filename))
26
+        return HttpResponse(laundry.update(hall_obj), content_type='image/svg+xml')
27 27
     except ObjectDoesNotExist:
28 28
         return HttpResponse(status=500);
29
-    return HttpResponse(join(SVG_URL, filename))