xoslib second prototype
diff --git a/planetstack/core/xoslib/__init__.py b/planetstack/core/xoslib/__init__.py
new file mode 100644
index 0000000..f71ac5d
--- /dev/null
+++ b/planetstack/core/xoslib/__init__.py
@@ -0,0 +1 @@
+from .xoslib import *
diff --git a/planetstack/core/xoslib/objects/__init__.py b/planetstack/core/xoslib/objects/__init__.py
new file mode 100644
index 0000000..5387bfe
--- /dev/null
+++ b/planetstack/core/xoslib/objects/__init__.py
@@ -0,0 +1,12 @@
+from .slivers import XOSSlivers
+from .sites import XOSSites
+from .nodes import XOSNodes
+from .slices import XOSSlices
+
+XOSLIB_OBJECTS = {}
+
+XOSLIB_OBJECTS[XOSSlivers.name] = XOSSlivers
+XOSLIB_OBJECTS[XOSSites.name] = XOSSites
+XOSLIB_OBJECTS[XOSNodes.name] = XOSNodes
+XOSLIB_OBJECTS[XOSSlices.name] = XOSSlices
+
diff --git a/planetstack/core/xoslib/objects/nodes.py b/planetstack/core/xoslib/objects/nodes.py
new file mode 100644
index 0000000..fedd0b0
--- /dev/null
+++ b/planetstack/core/xoslib/objects/nodes.py
@@ -0,0 +1,19 @@
+from core.models import Node
+from xosbase import XOSBase
+from django.forms.models import model_to_dict
+
+class XOSNodes(XOSBase):
+ name = "nodes"
+
+ def __init__(self):
+ super(XOSNodes, self).__init__()
+
+ def get(self):
+ allNodes = list(Node.objects.all())
+
+ result = []
+ for nocd in allNodes:
+ d=model_to_dict(nocd)
+ result.append(self.ensure_serializable(d))
+
+ return result
diff --git a/planetstack/core/xoslib/objects/sites.py b/planetstack/core/xoslib/objects/sites.py
new file mode 100644
index 0000000..7146320
--- /dev/null
+++ b/planetstack/core/xoslib/objects/sites.py
@@ -0,0 +1,20 @@
+from core.models import Site
+from xosbase import XOSBase
+from django.forms.models import model_to_dict
+
+class XOSSites(XOSBase):
+ name = "sites"
+
+ def __init__(self):
+ super(XOSSites, self).__init__()
+
+ def get(self):
+ allSites = list(Site.objects.all())
+
+ result = []
+ for site in allSites:
+ d=model_to_dict(site)
+ result.append(self.ensure_serializable(d))
+
+ return result
+
diff --git a/planetstack/core/xoslib/objects/slices.py b/planetstack/core/xoslib/objects/slices.py
index 139597f..04a04f2 100644
--- a/planetstack/core/xoslib/objects/slices.py
+++ b/planetstack/core/xoslib/objects/slices.py
@@ -1,2 +1,21 @@
+from core.models import Slice
+from xosbase import XOSBase
+from django.forms.models import model_to_dict
+
+class XOSSlices(XOSBase):
+ name = "slices"
+
+ def __init__(self):
+ super(XOSSlices, self).__init__()
+
+ def get(self):
+ allSlices = list(Slice.objects.all())
+
+ result = []
+ for slice in allSlices:
+ d = model_to_dict(slice)
+ result.append(self.ensure_serializable(d))
+
+ return result
diff --git a/planetstack/core/xoslib/objects/slivers.py b/planetstack/core/xoslib/objects/slivers.py
index 139597f..1428908 100644
--- a/planetstack/core/xoslib/objects/slivers.py
+++ b/planetstack/core/xoslib/objects/slivers.py
@@ -1,2 +1,20 @@
+from core.models import Sliver
+from xosbase import XOSBase
+from django.forms.models import model_to_dict
+class XOSSlivers(XOSBase):
+ name = "slivers"
+
+ def __init__(self):
+ super(XOSSlivers, self).__init__()
+
+ def get(self):
+ allSlivers = list(Sliver.objects.all())
+
+ result = []
+ for sliver in allSlivers:
+ d=model_to_dict(sliver)
+ result.append(self.ensure_serializable(d))
+
+ return result
diff --git a/planetstack/core/xoslib/objects/xosbase.py b/planetstack/core/xoslib/objects/xosbase.py
new file mode 100644
index 0000000..881c7b5
--- /dev/null
+++ b/planetstack/core/xoslib/objects/xosbase.py
@@ -0,0 +1,21 @@
+import datetime
+import time
+
+class XOSBase(object):
+ name = "XOSBase"
+
+ def __init__(self):
+ pass
+
+ def ensure_serializable(self, d):
+ d2={}
+ for (k,v) in d.items():
+ # datetime is not json serializable
+ if isinstance(v, datetime.datetime):
+ d2[k] = time.mktime(v.timetuple())
+ elif v.__class__.__name__ == "Geoposition":
+ pass
+ else:
+ d2[k] = v
+ return d2
+
diff --git a/planetstack/core/xoslib/xoslib.py b/planetstack/core/xoslib/xoslib.py
index 53d8b28..b8866c4 100644
--- a/planetstack/core/xoslib/xoslib.py
+++ b/planetstack/core/xoslib/xoslib.py
@@ -6,85 +6,14 @@
import json
from django.http import HttpResponse, HttpResponseServerError, HttpResponseForbidden
from django.views.generic import TemplateView, View
-from core.models import *
from django.forms.models import model_to_dict
+from objects import XOSLIB_OBJECTS
-def ensure_serializable(d):
- d2={}
- for (k,v) in d.items():
- # datetime is not json serializable
- if isinstance(v, datetime.datetime):
- d2[k] = time.mktime(v.timetuple())
- elif v.__class__.__name__ == "Geoposition":
- pass
+class XOSLibDataView(View):
+ def get(self, request, name="hello_world", **kwargs):
+ if name in XOSLIB_OBJECTS:
+ result = XOSLIB_OBJECTS[name]().get()
else:
- d2[k] = v
- return d2
-
-def sliver_to_dict(sliver):
- d = model_to_dict(sliver)
- d["slice_id"] = sliver.slice.id
- d["node_id"] = sliver.node.id
- return d
-
-def slice_to_dict(slice):
- d = model_to_dict(slice)
- d["slivers"] = [sliver_to_dict(x) for x in slice.slivers]
- return d
-
-def node_to_dict(node):
- d = model_to_dict(node)
- d["slivers"] = []
-
-
-class OpenCloudData:
- def __init__(self, user):
- self.loadAll()
-
- def loadAll(self):
- self.allNodes = list(Node.objects.all())
- self.allSlices = list(Slice.objects.all())
- self.allSlivers = list(Sliver.objects.all())
- self.allSites = list(Site.objects.all())
-
- self.site_id = {}
- for site in self.allSites:
- d = model_to_dict(site)
- d["node_ids"] = []
- d["slice_ids"] = []
- self.site_id[site.id] = ensure_serializable(d)
-
- self.node_id = {}
- for node in self.allNodes:
- d = model_to_dict(node)
- d["sliver_ids"] = []
- self.node_id[node.id] = ensure_serializable(d)
- self.site_id[node.site_id]["node_ids"].append(node.id)
-
- self.slice_id = {}
- for slice in self.allSlices:
- d = model_to_dict(slice)
- d["sliver_ids"] = []
- self.slice_id[slice.id] = ensure_serializable(d)
- self.site_id[slice.site_id]["slice_ids"].append(site.id)
-
- self.sliver_id = {}
- for sliver in self.allSlivers:
- self.sliver_id[sliver.id] = model_to_dict(sliver)
-
- self.slice_id[sliver.slice_id]["sliver_ids"].append(sliver.id)
- self.node_id[sliver.node_id]["sliver_ids"].append(sliver.id)
-
- def get_opencloud_data(self):
- return {"slices": self.slice_id.values(),
- "slivers": self.sliver_id.values(),
- "nodes": self.node_id.values(),
- "sites": self.site_id.values()}
-
-class ShellDataView(View):
- url = r'^shelldata/'
-
- def get(self, request, **kwargs):
- result = OpenCloudData(request.user).get_opencloud_data()
+ raise ValueError("Unknown object %s" % name)
return HttpResponse(json.dumps(result), mimetype='application/json')