From 0175764b172c631d4e7ead0956242eb6d9627ef5 Mon Sep 17 00:00:00 2001 From: Tyler Hallada Date: Mon, 28 Jul 2014 22:32:38 -0400 Subject: [PATCH] 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. --- laundry_app/laundry.py | 20 ++++++++++++-------- laundry_app/static/js/laundry.js | 2 +- laundry_app/views.py | 3 +-- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/laundry_app/laundry.py b/laundry_app/laundry.py index 771b9cb..5bd159b 100644 --- a/laundry_app/laundry.py +++ b/laundry_app/laundry.py @@ -33,21 +33,26 @@ def get_num_machines_per_status(status, records): len(filter(lambda r: r.machine.type == DRYER and r.availability == status, records))] -def generate_current_chart(filepath, records, hall): +def generate_current_chart(records, hall, filepath=None): """ Generate stacked bar chart of current laundry usage for specified hall and save svg at filepath. """ custom_style = Style(colors=('#B6E354', '#FF5995', '#FEED6C', '#E41B17')) - chart = pygal.StackedBar(style=custom_style, width=800, height=512, explicit_size=True) + chart = pygal.StackedBar(style=custom_style, width=800, height=512, + explicit_size=True) chart.title = 'Current laundry machine usage in ' + hall.name chart.x_labels = ['Washers', 'Dryers'] + print records chart.add('Available', get_num_machines_per_status(AVAILABLE, records)) chart.add('In Use', get_num_machines_per_status(IN_USE, records)) chart.add('Cycle Complete', get_num_machines_per_status(CYCLE_COMPLETE, records)) chart.add('Unavailable', get_num_machines_per_status(UNAVAILABLE, records)) chart.range = [0, 11] - chart.render_to_file(filepath) + if filepath: + chart.render_to_file(filepath) + else: + return chart.render() # NOTE: Abandoning generating the weekly chart via mysql and Django for now. # (cron script and csv file is just easier) Sorry if there are a lot of @@ -112,9 +117,8 @@ def update(hall, filepath=None): machine = LaundryMachine.objects.get(number=number, hall=hall) record = LaundryRecord(machine=machine, availability=availability, time_remaining=time_remaining) - if filepath: - records.append(record) - else: - record.save() + records.append(record) if filepath: - generate_current_chart(filepath, records, hall) + generate_current_chart(records, hall, filepath=filepath) + else: + return generate_current_chart(records, hall) diff --git a/laundry_app/static/js/laundry.js b/laundry_app/static/js/laundry.js index 4c09e12..321f4ec 100644 --- a/laundry_app/static/js/laundry.js +++ b/laundry_app/static/js/laundry.js @@ -27,7 +27,7 @@ function update_charts(selected) { request = $.ajax({ url: '/ajax/current/' + halls[selected] }).done(function (result) { - svg.attr('src', result); + svg.attr('src', '/ajax/current/' + halls[selected]); $('#loading').remove(); console.log(svg); $('.current-chart').append(svg); diff --git a/laundry_app/views.py b/laundry_app/views.py index 010eb7d..78d6cb4 100644 --- a/laundry_app/views.py +++ b/laundry_app/views.py @@ -23,7 +23,6 @@ def ajax_get_current(request, hall): hall_obj = get_object_or_404(Hall, pk=hall) filename = str(hall_obj.id) + '_current.svg' try: - laundry.update(hall_obj, filepath=join(SVG_DIR, filename)) + return HttpResponse(laundry.update(hall_obj), content_type='image/svg+xml') except ObjectDoesNotExist: return HttpResponse(status=500); - return HttpResponse(join(SVG_URL, filename))