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.
This commit is contained in:
Tyler Hallada 2014-07-28 22:32:38 -04:00
parent 09cb0a311c
commit 0175764b17
3 changed files with 14 additions and 11 deletions

View File

@ -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]
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()
if filepath:
generate_current_chart(filepath, records, hall)
generate_current_chart(records, hall, filepath=filepath)
else:
return generate_current_chart(records, hall)

View File

@ -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);

View File

@ -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))