DB initialization, Nodes and NodeTypes Schemas

This commit is contained in:
Eibriel 2015-03-27 15:42:28 +01:00
parent a45a4b491e
commit 51f527da28
3 changed files with 140 additions and 26 deletions

View File

@ -31,6 +31,7 @@ class BasicsAuth(BasicAuth):
class MyTokenAuth(BasicsAuth): class MyTokenAuth(BasicsAuth):
"""Switch between Basic and Token auth"""
def __init__(self): def __init__(self):
self.token_auth = TokensAuth() self.token_auth = TokensAuth()
self.authorized_protected = BasicsAuth.authorized self.authorized_protected = BasicsAuth.authorized
@ -57,7 +58,9 @@ class ValidateCustomFields(Validator):
if val: if val:
return True return True
else: else:
self._error(field, "Must be hi") print (val.errors)
self._error(
field, "Error validating properties")
def add_token(documents): def add_token(documents):

109
attract/init_bd.py Normal file
View File

@ -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")

View File

@ -1,7 +1,5 @@
import os import os
from authentication import RolesAuth
# Enable reads (GET), inserts (POST) and DELETE for resources/collections # Enable reads (GET), inserts (POST) and DELETE for resources/collections
# (if you omit this line, the API will default to ['GET'] and provide # (if you omit this line, the API will default to ['GET'] and provide
# read-only access to the endpoint). # 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 # Enable reads (GET), edits (PATCH), replacements (PUT) and deletes of
# individual items (defaults to read-only item access). # individual items (defaults to read-only item access).
ITEM_METHODS = ['GET', 'PATCH', 'PUT', 'DELETE'] ITEM_METHODS = ['GET', 'PUT', 'DELETE']
users_schema = { users_schema = {
@ -24,8 +22,8 @@ users_schema = {
'maxlength': 15, 'maxlength': 15,
}, },
'role': { 'role': {
'type': 'string', 'type': 'list',
'allowed': ["author", "contributor", "copy"], 'allowed': ["admin"],
'required': True, 'required': True,
}, },
# An embedded 'strongly-typed' dictionary. # An embedded 'strongly-typed' dictionary.
@ -50,34 +48,39 @@ nodes_schema = {
}, },
'description': { 'description': {
'type': 'string', 'type': 'string',
'minlength': 1, 'minlength': 0,
'maxlength': 128, 'maxlength': 128,
}, },
'thumbnail': { 'thumbnail': {
'type': 'string', 'type': 'string',
'minlength': 1, 'minlength': 0,
'maxlength': 128, 'maxlength': 128,
}, },
'order': {
'type': 'integer',
'minlength': 0,
},
'parent': { 'parent': {
'type': 'objectid', 'type': 'string',
'data_relation': { 'default': '',
'resource': 'nodes', #'data_relation': {
'field': '_id', # 'resource': 'nodes',
}, # 'field': '_id',
#},
}, },
'node_type' : { 'node_type': {
'type' : 'string', 'type': 'string',
'required': True, 'required': True,
'data_relation': { #'data_relation': {
'resource': 'node_types', # 'resource': 'node_types',
'field': '_id', # 'field': '_id',
}, #},
}, },
'properties' : { 'properties': {
'type' : 'dict', 'type' : 'dict',
'valid_properties' : True, 'valid_properties' : True,
'required': True, 'required': True,
} },
} }
@ -88,6 +91,10 @@ node_types_schema = {
'maxlength': 128, 'maxlength': 128,
'required': True, 'required': True,
}, },
'description': {
'type': 'string',
'maxlength': 256,
},
'dyn_schema': { 'dyn_schema': {
'type': 'dict', 'type': 'dict',
'required': True, 'required': True,
@ -111,11 +118,6 @@ nodes = {
'cache_control': 'max-age=10,must-revalidate', 'cache_control': 'max-age=10,must-revalidate',
'cache_expires': 10, 'cache_expires': 10,
# most global settings can be overridden at resource level
'resource_methods': ['GET', 'POST'],
'allowed_roles': ['author', 'contributor'],
'schema': nodes_schema 'schema': nodes_schema
} }