summaryrefslogtreecommitdiff
path: root/inventory/schema.py
diff options
context:
space:
mode:
Diffstat (limited to 'inventory/schema.py')
-rw-r--r--inventory/schema.py40
1 files changed, 40 insertions, 0 deletions
diff --git a/inventory/schema.py b/inventory/schema.py
new file mode 100644
index 0000000..4837d58
--- /dev/null
+++ b/inventory/schema.py
@@ -0,0 +1,40 @@
+import bson
+from flask import url_for
+from marshmallow import Schema, fields
+
+# Map ObjectId to String
+Schema.TYPE_MAPPING[bson.ObjectId] = fields.String
+
+
+class ObjectId(fields.Field):
+ def _serialize(self, value, attr, obj, **kwargs):
+ if value is None:
+ return value
+ return str(value)
+
+ def _deserialize(self, value, attr, data, **kwargs):
+ if value is None:
+ return value
+ return bson.ObjectId(value)
+
+
+class FieldSchema(Schema):
+ name = fields.String(required=True)
+ value = fields.String(required=True)
+ # type
+
+
+class NodeSchema(Schema):
+ _id = ObjectId(dump_only=True, data_key='id')
+ name = fields.String(required=True)
+ parent_id = ObjectId(default=None)
+ _fields = fields.List(fields.Nested(FieldSchema()), default=[], attribute='fields', data_key='fields')
+
+ # These are not set by the caller, but by the API endpoint
+ created_at = fields.AwareDateTime(dump_only=True)
+ updated_at = fields.AwareDateTime(dump_only=True)
+
+ # Not actual stored fields
+ parent = fields.Nested(lambda: NodeSchema(), dump_only=True)
+ children = fields.List(fields.Nested(lambda: NodeSchema()), dump_only=True)
+ url = fields.Function(lambda obj: url_for('.node', node_id=obj['_id']), dump_only=True)