Nodes now have only one short code

This commit is contained in:
Sybren A. Stüvel 2016-07-07 15:41:42 +02:00
parent 3f3e9ac7db
commit 0f8cfc89b3
4 changed files with 16 additions and 25 deletions

View File

@ -238,7 +238,7 @@ def setup_db_indices():
coll.create_index([('project', pymongo.ASCENDING), coll.create_index([('project', pymongo.ASCENDING),
('node_type', pymongo.ASCENDING)]) ('node_type', pymongo.ASCENDING)])
coll.create_index([('parent', pymongo.ASCENDING)]) coll.create_index([('parent', pymongo.ASCENDING)])
coll.create_index([('short_codes', pymongo.ASCENDING)], coll.create_index([('short_code', pymongo.ASCENDING)],
sparse=True, unique=True) sparse=True, unique=True)

View File

@ -31,25 +31,22 @@ def share_node(node_id):
projection={ projection={
'project': 1, 'project': 1,
'node_type': 1, 'node_type': 1,
'short_codes': 1 'short_code': 1
}) })
check_permissions('nodes', node, request.method) check_permissions('nodes', node, request.method)
log.info('Sharing node %s', node_id) log.info('Sharing node %s', node_id)
# We support storing multiple short links in the database, but short_code = node.get('short_code')
# for now we just always store one and the same. status = 200
short_codes = node.get('short_codes', [])
if not short_codes and request.method == 'POST': if not short_code:
if request.method == 'POST':
short_code = generate_and_store_short_code(node) short_code = generate_and_store_short_code(node)
status = 201 status = 201
else: else:
try:
short_code = short_codes[0]
except IndexError:
return '', 204 return '', 204
status = 200
return jsonify(short_link_info(short_code), status=status) return jsonify(short_link_info(short_code), status=status)
@ -67,16 +64,14 @@ def generate_and_store_short_code(node):
short_code = create_short_code(node) short_code = create_short_code(node)
log.debug('Created short code for node %s: %s', node_id, short_code) log.debug('Created short code for node %s: %s', node_id, short_code)
node.setdefault('short_codes', []).append(short_code) node['short_code'] = short_code
# Store it in MongoDB # Store it in MongoDB
try: try:
result = nodes_coll.update_one({'_id': node_id}, result = nodes_coll.update_one({'_id': node_id},
{'$set': {'short_codes': node['short_codes']}}) {'$set': {'short_code': short_code}})
break break
except pymongo.errors.DuplicateKeyError: except pymongo.errors.DuplicateKeyError:
node['short_codes'].remove(short_code)
log.info('Duplicate key while creating short code, retrying (attempt %i/%i)', log.info('Duplicate key while creating short code, retrying (attempt %i/%i)',
attempt, max_attempts) attempt, max_attempts)
pass pass
@ -87,8 +82,7 @@ def generate_and_store_short_code(node):
# We were able to store a short code, now let's verify the result. # We were able to store a short code, now let's verify the result.
if result.matched_count != 1: if result.matched_count != 1:
log.warning('Unable to update node %s with new short_links=%r', log.warning('Unable to update node %s with new short_links=%r', node_id, node['short_code'])
node_id, node['short_codes'])
raise InternalServerError('Unable to update node %s with new short links' % node_id) raise InternalServerError('Unable to update node %s with new short links' % node_id)
return short_code return short_code

View File

@ -323,12 +323,9 @@ nodes_schema = {
'type': 'dict', 'type': 'dict',
'schema': permissions_embedded_schema 'schema': permissions_embedded_schema
}, },
'short_codes': { 'short_code': {
'type': 'list',
'schema': {
'type': 'string', 'type': 'string',
}, },
},
} }
tokens_schema = { tokens_schema = {

View File

@ -314,7 +314,7 @@ class NodeSharingTest(AbstractPillarTest):
'node_type': 'asset', 'node_type': 'asset',
'name': 'collider', 'name': 'collider',
'properties': {}, 'properties': {},
'short_codes': ['takenX'], 'short_code': 'takenX',
}) })
# Mock create_short_code so that it returns predictable short codes. # Mock create_short_code so that it returns predictable short codes.