Tweaks to upgrade_node_types

This commit is contained in:
2015-11-15 17:46:32 +01:00
parent 6282229488
commit a96526351b
4 changed files with 44 additions and 52 deletions

View File

@@ -62,23 +62,17 @@ def clear_db():
@manager.command
def upgrade_node_types():
"""Wipes node_types collection
and populates it again
"""
from pymongo import MongoClient
client = MongoClient(MONGO_HOST, 27017)
db = client.eve
node_types = db.node_types.find({})
"""Wipes node_types collection and populates it again"""
node_types_collection = app.data.driver.db['node_types']
node_types = node_types_collection.find({})
old_ids = {}
for nt in node_types:
old_ids[nt['name']] = nt['_id']
for node_type in node_types:
old_ids[node_type['name']] = node_type['_id']
populate_node_types(old_ids)
def get_id(collection, name):
"""Returns the _id of the given collection
and name."""
"""Returns the _id of the given collection and name"""
from pymongo import MongoClient
client = MongoClient(MONGO_HOST, 27017)
db = client.eve
@@ -164,37 +158,32 @@ def manage_groups():
def populate_node_types(old_ids={}):
from pymongo import MongoClient
client = MongoClient(MONGO_HOST, 27017)
db = client.eve
node_types_collection = app.data.driver.db['node_types']
def mix_node_type(old_id, node_type_dict):
# Take eve parameters
node_type = db.node_types.find({'_id':old_id})
node_type = node_type[0]
node_type = node_types_collection.find_one({'_id': old_id})
for attr in node_type:
if attr[0]=='_':
# Mix with node type attributes
node_type_dict[attr]=node_type[attr]
if attr[0] == '_':
# Mix with node eve attributes. This is really not needed since
# the attributes are stripped before doing a put_internal.
node_type_dict[attr] = node_type[attr]
elif attr == 'permissions':
node_type_dict['permissions'] = node_type['permissions']
return node_type_dict
def upgrade(node_type, old_ids):
print("Node {0}".format(node_type['name']))
node_name = node_type['name']
if node_name in old_ids:
node_type = mix_node_type(old_ids[node_name], node_type)
node_id = node_type['_id']
node_id = old_ids[node_name]
node_type = mix_node_type(node_id, node_type)
# Removed internal fields that would cause validation error
internal_fields = ['_id', '_etag', '_updated', '_created']
for field in internal_fields:
node_type.pop(field, None)
# Also remove permissions, since they are managed separately
node_type.pop('permissions', None)
p = put_internal('node_types', node_type, **{'_id': node_id})
else:
print("Making the node")
print(node_type)

View File

@@ -0,0 +1,8 @@
_file_embedded_schema = {
'type': 'objectid',
'data_relation': {
'resource': 'files',
'field': '_id',
'embeddable': True
}
}

View File

@@ -1,3 +1,5 @@
from manage.node_types import _file_embedded_schema
node_type_asset = {
'name': 'asset',
'description': 'Basic Asset Type',
@@ -23,19 +25,26 @@ node_type_asset = {
},
# We point to the original file (and use it to extract any relevant
# variation useful for our scope).
'file': {
'type': 'objectid',
'data_relation': {
'resource': 'files',
'field': '_id',
'embeddable': True
},
'file': _file_embedded_schema,
'description_attachments': {
'type': 'list',
'schema': {
'type': 'dict',
'schema': {
'file': _file_embedded_schema,
'url': {
'type': 'string',
'minlength': 1
}
}
}
}
},
'form_schema': {
'status': {},
'content_type': {'visible': False},
'file': {'visible': False},
'description_attachments': {'visible': False},
},
'permissions': {
# 'groups': [{

View File

@@ -1,3 +1,5 @@
from manage.node_types import _file_embedded_schema
node_type_project = {
'name': 'project',
'parent': {},
@@ -60,25 +62,9 @@ node_type_project = {
],
},
# Logo
'picture_square': {
'type': 'objectid',
'nullable': True,
'data_relation': {
'resource': 'files',
'field': '_id',
'embeddable': True
},
},
'picture_square': _file_embedded_schema,
# Header
'picture_header': {
'type': 'objectid',
'nullable': True,
'data_relation': {
'resource': 'files',
'field': '_id',
'embeddable': True
},
},
'picture_header': _file_embedded_schema,
# Short summary for the project
'summary': {
'type': 'string',