diff options
Diffstat (limited to 'bitbake/lib/toaster/toastergui/views.py')
-rwxr-xr-x | bitbake/lib/toaster/toastergui/views.py | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/bitbake/lib/toaster/toastergui/views.py b/bitbake/lib/toaster/toastergui/views.py index 91c4fa2543..280159ad2c 100755 --- a/bitbake/lib/toaster/toastergui/views.py +++ b/bitbake/lib/toaster/toastergui/views.py | |||
@@ -87,6 +87,35 @@ def _project_recent_build_list(prj): | |||
87 | list(prj.buildrequest_set.filter(state__in=[BuildRequest.REQ_COMPLETED, BuildRequest.REQ_FAILED]).order_by("-pk")[:3])) | 87 | list(prj.buildrequest_set.filter(state__in=[BuildRequest.REQ_COMPLETED, BuildRequest.REQ_FAILED]).order_by("-pk")[:3])) |
88 | 88 | ||
89 | 89 | ||
90 | |||
91 | def objtojson(obj): | ||
92 | from django.db.models.query import QuerySet | ||
93 | from django.db.models import Model, IntegerField | ||
94 | from django.db.models.fields.related import ForeignKey | ||
95 | |||
96 | if isinstance(obj, datetime): | ||
97 | return obj.isoformat() | ||
98 | elif isinstance(obj, timedelta): | ||
99 | return obj.total_seconds() | ||
100 | elif isinstance(obj, QuerySet) or isinstance(obj, set): | ||
101 | return list(obj) | ||
102 | elif type(obj).__name__ == "RelatedManager": | ||
103 | return [x.pk for x in obj.all()] | ||
104 | elif hasattr( obj, '__dict__') and isinstance(obj, Model): | ||
105 | d = obj.__dict__ | ||
106 | nd = dict(d) | ||
107 | for di in d.keys(): | ||
108 | if di.startswith("_"): | ||
109 | del nd[di] | ||
110 | elif isinstance(d[di], Model): | ||
111 | nd[di] = d[di].pk | ||
112 | elif isinstance(d[di], int) and hasattr(obj, "get_%s_display" % di): | ||
113 | nd[di] = getattr(obj, "get_%s_display" % di)() | ||
114 | return nd | ||
115 | else: | ||
116 | raise TypeError("Unserializable object %s (%s) of type %s" % ( obj, dir(obj), type(obj))) | ||
117 | |||
118 | |||
90 | def _template_renderer(template): | 119 | def _template_renderer(template): |
91 | def func_wrapper(view): | 120 | def func_wrapper(view): |
92 | def returned_wrapper(request, *args, **kwargs): | 121 | def returned_wrapper(request, *args, **kwargs): |
@@ -127,7 +156,7 @@ def _template_renderer(template): | |||
127 | else: | 156 | else: |
128 | raise TypeError("Unserializable object %s of type %s" % ( obj, type(obj))) | 157 | raise TypeError("Unserializable object %s of type %s" % ( obj, type(obj))) |
129 | 158 | ||
130 | return HttpResponse(jsonfilter(context, default=_objtojson ), | 159 | return HttpResponse(jsonfilter(context, default=objtojson ), |
131 | content_type = "application/json; charset=utf-8") | 160 | content_type = "application/json; charset=utf-8") |
132 | else: | 161 | else: |
133 | return render(request, template, context) | 162 | return render(request, template, context) |