diff --git a/pillar/application/__init__.py b/pillar/application/__init__.py index 298a9c17..1632da95 100644 --- a/pillar/application/__init__.py +++ b/pillar/application/__init__.py @@ -238,7 +238,7 @@ def setup_db_indices(): coll.create_index([('project', pymongo.ASCENDING), ('node_type', 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) diff --git a/pillar/application/modules/nodes.py b/pillar/application/modules/nodes.py index f225d390..c9a77d6e 100644 --- a/pillar/application/modules/nodes.py +++ b/pillar/application/modules/nodes.py @@ -31,25 +31,22 @@ def share_node(node_id): projection={ 'project': 1, 'node_type': 1, - 'short_codes': 1 + 'short_code': 1 }) check_permissions('nodes', node, request.method) log.info('Sharing node %s', node_id) - # We support storing multiple short links in the database, but - # for now we just always store one and the same. - short_codes = node.get('short_codes', []) - if not short_codes and request.method == 'POST': - short_code = generate_and_store_short_code(node) - status = 201 - else: - try: - short_code = short_codes[0] - except IndexError: + short_code = node.get('short_code') + status = 200 + + if not short_code: + if request.method == 'POST': + short_code = generate_and_store_short_code(node) + status = 201 + else: return '', 204 - status = 200 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) 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 try: result = nodes_coll.update_one({'_id': node_id}, - {'$set': {'short_codes': node['short_codes']}}) + {'$set': {'short_code': short_code}}) break except pymongo.errors.DuplicateKeyError: - node['short_codes'].remove(short_code) - log.info('Duplicate key while creating short code, retrying (attempt %i/%i)', attempt, max_attempts) 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. if result.matched_count != 1: - log.warning('Unable to update node %s with new short_links=%r', - node_id, node['short_codes']) + log.warning('Unable to update node %s with new short_links=%r', node_id, node['short_code']) raise InternalServerError('Unable to update node %s with new short links' % node_id) return short_code diff --git a/pillar/settings.py b/pillar/settings.py index e578d97a..5c553092 100644 --- a/pillar/settings.py +++ b/pillar/settings.py @@ -323,11 +323,8 @@ nodes_schema = { 'type': 'dict', 'schema': permissions_embedded_schema }, - 'short_codes': { - 'type': 'list', - 'schema': { - 'type': 'string', - }, + 'short_code': { + 'type': 'string', }, } diff --git a/tests/test_nodes.py b/tests/test_nodes.py index 02a6fe27..8c16315f 100644 --- a/tests/test_nodes.py +++ b/tests/test_nodes.py @@ -314,7 +314,7 @@ class NodeSharingTest(AbstractPillarTest): 'node_type': 'asset', 'name': 'collider', 'properties': {}, - 'short_codes': ['takenX'], + 'short_code': 'takenX', }) # Mock create_short_code so that it returns predictable short codes.