Merge branch 'onelab' of ssh://git.onelab.eu/git/myslice into onelab
authorCiro Scognamiglio <ciro.scognamiglio@cslash.net>
Fri, 28 Feb 2014 19:08:28 +0000 (20:08 +0100)
committerCiro Scognamiglio <ciro.scognamiglio@cslash.net>
Fri, 28 Feb 2014 19:08:28 +0000 (20:08 +0100)
myslice/urls.py
rest/__init__.py
rest/create.py [new file with mode: 0644]
rest/delete.py [new file with mode: 0644]

index bc8dad8..cc8b259 100644 (file)
@@ -67,6 +67,8 @@ urls = [
     (r'^table/(?P<object_type>[^/]+)/(?P<object_name>[^/]+)?/?$', 'rest.dispatch'),
     (r'^datatable/(?P<object_type>[^/]+)/(?P<object_name>[^/]+)?/?$', 'rest.dispatch'),
     (r'^update/(?P<object_type>[^/]+)/(?P<object_name>[^/]+)?/?$', 'rest.update.dispatch'),
+    (r'^create/(?P<object_type>[^/]+)/(?P<object_name>[^/]+)?/?$', 'rest.create.dispatch'),
+    (r'^delete/(?P<object_type>[^/]+)/(?P<object_name>[^/]+)?/?$', 'rest.delete.dispatch'),
     #
     #
     #(r'^view/?', include('view.urls')),
index d44d860..fe5e292 100644 (file)
@@ -39,6 +39,9 @@ class objectRequest(object):
         
         self.request = request
 
+    # XXX TODO: What about the local: objects? 
+    # Example: local:user (Manifold) is different from user (SFA GW)   
+
         if ((self.type == 'platform') or (self.type == 'testbed')) :
             self.type = 'local:platform'
             self.id = 'platform'
diff --git a/rest/create.py b/rest/create.py
new file mode 100644 (file)
index 0000000..eaad539
--- /dev/null
@@ -0,0 +1,68 @@
+from manifold.core.query            import Query
+
+from django.views.generic.base      import TemplateView
+from django.shortcuts               import render_to_response
+
+from unfold.loginrequired           import LoginRequiredView
+from django.http                    import HttpResponse
+
+from manifold.core.query            import Query, AnalyzedQuery
+from manifoldapi.manifoldapi        import execute_query
+
+from string import join
+import decimal
+import datetime
+import json
+
+# handles serialization of datetime in json
+DateEncoder = lambda obj: obj.strftime("%B %d, %Y %H:%M:%S") if isinstance(obj, datetime.datetime) else None
+
+# support converting decimal in json
+json.encoder.FLOAT_REPR = lambda o: format(o, '.2f')
+
+# handles decimal numbers serialization in json
+class DecimalEncoder(json.JSONEncoder):
+    def _iterencode(self, o, markers=None):
+        if isinstance(o, decimal.Decimal):
+            return (str(o) for o in [o])
+        return super(DecimalEncoder, self)._iterencode(o, markers)
+
+def dispatch(request, object_type, object_name):
+    
+    object_filters = {}
+    object_params = {}
+    result = {}
+    
+    if request.method == 'POST':
+        req_items = request.POST.items()
+    elif request.method == 'GET':
+        return HttpResponse(json.dumps({'error' : 'only post request is supported'}), content_type="application/json")
+
+    query = Query.create(object_type)
+
+# No filters for create    
+#    if object_filters :
+#        for k, f in object_filters.iteritems() :
+#            query.filter_by(k, '==', f)
+#    
+#    # DEBUG        
+#    print object_filters
+    
+    if object_params :
+        query.set(object_params.iteritems())
+    else :
+        return HttpResponse(json.dumps({'error' : 'params are required for create'}), content_type="application/json")
+    
+    # DEBUG
+    print object_params
+    
+    result = execute_query(request, query)
+    
+    # DEBUG
+    print result
+    
+    if result :
+        return HttpResponse(json.dumps({'error' : 'an error has occurred'}), content_type="application/json")
+    else :
+        return HttpResponse(json.dumps({'success' : 'record updated'}), content_type="application/json")
+    
diff --git a/rest/delete.py b/rest/delete.py
new file mode 100644 (file)
index 0000000..0ce5f38
--- /dev/null
@@ -0,0 +1,69 @@
+from manifold.core.query            import Query
+
+from django.views.generic.base      import TemplateView
+from django.shortcuts               import render_to_response
+
+from unfold.loginrequired           import LoginRequiredView
+from django.http                    import HttpResponse
+
+from manifold.core.query            import Query, AnalyzedQuery
+from manifoldapi.manifoldapi        import execute_query
+
+from string import join
+import decimal
+import datetime
+import json
+
+# handles serialization of datetime in json
+DateEncoder = lambda obj: obj.strftime("%B %d, %Y %H:%M:%S") if isinstance(obj, datetime.datetime) else None
+
+# support converting decimal in json
+json.encoder.FLOAT_REPR = lambda o: format(o, '.2f')
+
+# handles decimal numbers serialization in json
+class DecimalEncoder(json.JSONEncoder):
+    def _iterencode(self, o, markers=None):
+        if isinstance(o, decimal.Decimal):
+            return (str(o) for o in [o])
+        return super(DecimalEncoder, self)._iterencode(o, markers)
+
+def dispatch(request, object_type, object_name):
+    
+    object_filters = {}
+    object_params = {}
+    result = {}
+    
+    if request.method == 'POST':
+        req_items = request.POST.items()
+    elif request.method == 'GET':
+        return HttpResponse(json.dumps({'error' : 'only post request is supported'}), content_type="application/json")
+
+    query = Query.delete(object_type)
+    
+    if object_filters :
+        for k, f in object_filters.iteritems() :
+            query.filter_by(k, '==', f)
+    else:
+        return HttpResponse(json.dumps({'error' : 'Filters are required for delete'}), content_type="application/json")
+    # DEBUG        
+    print object_filters
+
+# No params in delete    
+#    if object_params :
+#        query.set(object_params.iteritems())
+#    else :
+#        return HttpResponse(json.dumps({'error' : 'an error has occurred'}), content_type="application/json")
+#    
+#    # DEBUG
+#    print object_params
+    
+    result = execute_query(request, query)
+    
+    # DEBUG
+    print result
+    
+    if result :
+        return HttpResponse(json.dumps({'error' : 'an error has occurred'}), content_type="application/json")
+    else :
+        return HttpResponse(json.dumps({'success' : 'record updated'}), content_type="application/json")
+