Scott Baker | bf1610d | 2014-06-18 18:22:03 -0700 | [diff] [blame] | 1 | # /opt/planetstack/core/dashboard/views/helloworld.py |
Scott Baker | 0b48b4b | 2014-06-19 15:24:58 -0700 | [diff] [blame] | 2 | import datetime |
Scott Baker | bf1610d | 2014-06-18 18:22:03 -0700 | [diff] [blame] | 3 | import os |
| 4 | import sys |
Scott Baker | 0b48b4b | 2014-06-19 15:24:58 -0700 | [diff] [blame] | 5 | import time |
Scott Baker | bf1610d | 2014-06-18 18:22:03 -0700 | [diff] [blame] | 6 | import json |
| 7 | from django.http import HttpResponse, HttpResponseServerError, HttpResponseForbidden |
| 8 | from django.views.generic import TemplateView, View |
| 9 | from core.models import * |
| 10 | from django.forms.models import model_to_dict |
| 11 | |
Scott Baker | 0b48b4b | 2014-06-19 15:24:58 -0700 | [diff] [blame] | 12 | def ensure_serializable(d): |
| 13 | d2={} |
| 14 | for (k,v) in d.items(): |
| 15 | # datetime is not json serializable |
| 16 | if isinstance(v, datetime.datetime): |
| 17 | d2[k] = time.mktime(v.timetuple()) |
| 18 | else: |
| 19 | d2[k] = v |
| 20 | return d2 |
| 21 | |
Scott Baker | bf1610d | 2014-06-18 18:22:03 -0700 | [diff] [blame] | 22 | class ShellDataView(View): |
| 23 | url = r'^shelldata/' |
| 24 | |
| 25 | def get(self, request, **kwargs): |
| 26 | allSlices = [] |
| 27 | for slice in Slice.objects.all(): |
Scott Baker | 0b48b4b | 2014-06-19 15:24:58 -0700 | [diff] [blame] | 28 | allSlices.append(ensure_serializable(model_to_dict(slice))) |
Scott Baker | bf1610d | 2014-06-18 18:22:03 -0700 | [diff] [blame] | 29 | |
| 30 | result = {"slices": allSlices} |
| 31 | |
| 32 | return HttpResponse(json.dumps(result), mimetype='application/json') |