From 51f527da28e5c8bc5c726a2335f60edddf2dd891 Mon Sep 17 00:00:00 2001 From: Eibriel Date: Fri, 27 Mar 2015 15:42:28 +0100 Subject: [PATCH] DB initialization, Nodes and NodeTypes Schemas --- attract/application/__init__.py | 5 +- attract/init_bd.py | 109 ++++++++++++++++++++++++++++++++ attract/settings.py | 52 +++++++-------- 3 files changed, 140 insertions(+), 26 deletions(-) create mode 100644 attract/init_bd.py diff --git a/attract/application/__init__.py b/attract/application/__init__.py index cdadfec2..77739731 100644 --- a/attract/application/__init__.py +++ b/attract/application/__init__.py @@ -31,6 +31,7 @@ class BasicsAuth(BasicAuth): class MyTokenAuth(BasicsAuth): + """Switch between Basic and Token auth""" def __init__(self): self.token_auth = TokensAuth() self.authorized_protected = BasicsAuth.authorized @@ -57,7 +58,9 @@ class ValidateCustomFields(Validator): if val: return True else: - self._error(field, "Must be hi") + print (val.errors) + self._error( + field, "Error validating properties") def add_token(documents): diff --git a/attract/init_bd.py b/attract/init_bd.py new file mode 100644 index 00000000..f69307f8 --- /dev/null +++ b/attract/init_bd.py @@ -0,0 +1,109 @@ +from pymongo import MongoClient +from pymongo.errors import DuplicateKeyError +from bson import ObjectId +from datetime import datetime + +client = MongoClient() +db = client.eve + +default_user = { + "_id": ObjectId("550171c8135d3248e477f288"), + "_updated": datetime.now(), + "firstname": "admin", + "lastname": "admin", + "role": "admin", + "email": "admin@admin.com", + "_created": datetime.now(), + "_etag": "302236e27f51d2e26041ae9de49505d77332b260" + } + +default_token = { + "_id": ObjectId("5502f289135d3274cb658ba7"), + "username": "admin", + "token": "ANLGNSIEZJ", + "_etag": "1e96ed46b133b7ede5ce6ef0d6d4fc53edd9f2ba" + } + +shot_node_type = { + "_id": ObjectId("55016a52135d32466fc800be"), + "_updated": datetime.now(), + "name": "shot", + "description": "Shot Node Type, for shots", + "dyn_schema": { + "url": { + "type": "string", + }, + "cut_in": { + "type": "integer" + }, + "cut_out": { + "type": "integer" + }, + "status": { + "type": "string", + "allowed": ["on_hold", + "todo", + "in_progress", + "review_required", + "final"], + }, + "notes": { + "type": "string", + "maxlength": 256, + }, + "order": { + "type": "integer", + }, + "shot_group": { + "type": "string", + #"data_relation": { + # "resource": "nodes", + # "field": "_id", + #}, + } + }, + "_created": datetime.now(), + "_etag": "0ea3c4f684a0cda85525184d5606c4f4ce6ac5f5" + } + +shot = { + "_id": ObjectId("55016a52135d32466fc800be"), + "_update": datetime.now(), + "_created": datetime.now(), + "_etag": "0ea3c4f684a0cda85525184d5606c4f4ce6ac5f5", + "name": "01", + "description": "A sheep tries to hang itself, but fails", + "thumbnail": "/tmp/attrackt-thumbnail.png", + "order": 0, + "parent": None, + "node_type": "55016a52135d32466fc800be", + "properties": { + "url": "shot01", + "cut_in": 100, + "cut_out": 900, + "status": "todo", + "notes": "I think the sheep should scream a bit more", + "order": 1, + "shot_group": "", + } +} + +try: + db.users.insert(default_user) +except DuplicateKeyError: + print ("default_user already exist") + +try: + db.node_types.insert(shot_node_type) +except DuplicateKeyError: + print ("shot_node_type already exist") + +try: + db.tokens.insert(default_token) +except DuplicateKeyError: + print ("default_token already exist") + +try: + db.nodes.insert(shot) +except DuplicateKeyError: + print ("shot already exist") diff --git a/attract/settings.py b/attract/settings.py index 038d560c..9fd04ac1 100644 --- a/attract/settings.py +++ b/attract/settings.py @@ -1,7 +1,5 @@ import os -from authentication import RolesAuth - # Enable reads (GET), inserts (POST) and DELETE for resources/collections # (if you omit this line, the API will default to ['GET'] and provide # read-only access to the endpoint). @@ -9,7 +7,7 @@ RESOURCE_METHODS = ['GET', 'POST', 'DELETE'] # Enable reads (GET), edits (PATCH), replacements (PUT) and deletes of # individual items (defaults to read-only item access). -ITEM_METHODS = ['GET', 'PATCH', 'PUT', 'DELETE'] +ITEM_METHODS = ['GET', 'PUT', 'DELETE'] users_schema = { @@ -24,8 +22,8 @@ users_schema = { 'maxlength': 15, }, 'role': { - 'type': 'string', - 'allowed': ["author", "contributor", "copy"], + 'type': 'list', + 'allowed': ["admin"], 'required': True, }, # An embedded 'strongly-typed' dictionary. @@ -50,34 +48,39 @@ nodes_schema = { }, 'description': { 'type': 'string', - 'minlength': 1, + 'minlength': 0, 'maxlength': 128, }, 'thumbnail': { 'type': 'string', - 'minlength': 1, + 'minlength': 0, 'maxlength': 128, }, + 'order': { + 'type': 'integer', + 'minlength': 0, + }, 'parent': { - 'type': 'objectid', - 'data_relation': { - 'resource': 'nodes', - 'field': '_id', - }, + 'type': 'string', + 'default': '', + #'data_relation': { + # 'resource': 'nodes', + # 'field': '_id', + #}, }, - 'node_type' : { - 'type' : 'string', + 'node_type': { + 'type': 'string', 'required': True, - 'data_relation': { - 'resource': 'node_types', - 'field': '_id', - }, + #'data_relation': { + # 'resource': 'node_types', + # 'field': '_id', + #}, }, - 'properties' : { + 'properties': { 'type' : 'dict', 'valid_properties' : True, 'required': True, - } + }, } @@ -88,6 +91,10 @@ node_types_schema = { 'maxlength': 128, 'required': True, }, + 'description': { + 'type': 'string', + 'maxlength': 256, + }, 'dyn_schema': { 'type': 'dict', 'required': True, @@ -111,11 +118,6 @@ nodes = { 'cache_control': 'max-age=10,must-revalidate', 'cache_expires': 10, - # most global settings can be overridden at resource level - 'resource_methods': ['GET', 'POST'], - - 'allowed_roles': ['author', 'contributor'], - 'schema': nodes_schema }