Patchwork [bitbake-devel,30/94] bitbake: webhob: Setup API for build model

login
register
mail settings
Submitter Alexandru DAMIAN
Date Sept. 24, 2013, 4:51 p.m.
Message ID <7e90fce7d6081702591ebca0be119371a3514937.1380041477.git.alexandru.damian@intel.com>
Download mbox | patch
Permalink /patch/58877/
State New
Headers show

Comments

Alexandru DAMIAN - Sept. 24, 2013, 4:51 p.m.
From: Calin Dragomir <calinx.l.dragomir@intel.com>

Adding REST API version 1.0 interface of WebHob.

Build call API is implemented in this patch
Documentation is here:
https://wiki.yoctoproject.org/wiki/Webhob_REST_API_Builds

Signed-off-by: Calin Dragomir <calinx.l.dragomir@intel.com>
Signed-off-by: Alexandru DAMIAN <alexandru.damian@intel.com>
---
 bitbake/lib/webhob/bldviewer/api.py   |  6 ++++
 bitbake/lib/webhob/bldviewer/views.py | 56 +++++++++++++++++++++++++++++++++++
 bitbake/lib/webhob/whbmain/urls.py    |  1 +
 3 files changed, 63 insertions(+)
 create mode 100644 bitbake/lib/webhob/bldviewer/api.py

Patch

diff --git a/bitbake/lib/webhob/bldviewer/api.py b/bitbake/lib/webhob/bldviewer/api.py
new file mode 100644
index 0000000..d945861
--- /dev/null
+++ b/bitbake/lib/webhob/bldviewer/api.py
@@ -0,0 +1,6 @@ 
+from django.conf.urls import patterns, include, url
+
+
+urlpatterns = patterns('bldviewer.views',
+        url(r'^builds/$', 'builds', name='builds'),
+)
diff --git a/bitbake/lib/webhob/bldviewer/views.py b/bitbake/lib/webhob/bldviewer/views.py
index eb901a4..58d9444 100644
--- a/bitbake/lib/webhob/bldviewer/views.py
+++ b/bitbake/lib/webhob/bldviewer/views.py
@@ -33,3 +33,59 @@  def layer(request):
     context = {'layers': layer_info}
 
     return render(request, template, context)
+
+
+#### API
+
+import json
+from django.core import serializers
+from django.http import HttpResponse
+
+
+def builds(request):
+    response_data = {}
+
+    try:
+        limit = int(request.GET.get('limit', 0))
+    except ValueError:
+        limit = 0
+
+    try:
+        offset = int(request.GET.get('offset', 0))
+    except ValueError:
+        offset = 0
+
+    filter_string = request.GET.get('filter', '')
+
+    if filter_string:
+        filter_terms = _get_filtering_terms(filter_string)
+        try:
+            queryset = Build.objects.filter(**filter_terms)
+        except ValueError:
+            queryset = []
+    else:
+        queryset = Build.objects.all()
+
+    if offset and limit:
+        queryset = queryset[offset:(offset+limit)]
+    elif offset:
+        queryset = queryset[offset:]
+    elif limit:
+        queryset = queryset[:limit]
+
+    if queryset:
+        response_data['count'] = queryset.count()
+    else:
+        response_data['count'] = 0
+
+    response_data['list'] = serializers.serialize('json', queryset)
+
+    return HttpResponse(json.dumps(response_data), content_type='application/json')
+
+def _get_filtering_terms(filter_string):
+
+    search_terms = filter_string.split(":")
+    keys = search_terms[0].split(',')
+    values = search_terms[1].split(',')
+
+    return dict(zip(keys, values))
diff --git a/bitbake/lib/webhob/whbmain/urls.py b/bitbake/lib/webhob/whbmain/urls.py
index af536aa..14345f6 100644
--- a/bitbake/lib/webhob/whbmain/urls.py
+++ b/bitbake/lib/webhob/whbmain/urls.py
@@ -6,6 +6,7 @@  from django.conf.urls import patterns, include, url
 
 urlpatterns = patterns('',
     url(r'^build/', include('bldviewer.urls')),
+    url(r'^api/1.0/', include('bldviewer.api')),
     url(r'^gui/', include('whbgui.urls')),
     # Examples:
     # url(r'^webhob/', include('webhob.foo.urls')),