From 6d68753b1be791c4eae69c2746ef4516655902a8 Mon Sep 17 00:00:00 2001 From: Francesco Siddi Date: Wed, 20 Jul 2016 15:15:13 +0200 Subject: [PATCH] Introducing support for removing indexed nodes Nodes are deindexed if they have a 'status' property that is different from 'published'. Nodes are deindexed also when they are deleted. --- pillar/application/modules/nodes.py | 32 +++++++++++++++++++++++------ pillar/application/utils/algolia.py | 23 +++++++++++++++------ 2 files changed, 43 insertions(+), 12 deletions(-) diff --git a/pillar/application/modules/nodes.py b/pillar/application/modules/nodes.py index 065fa307..a347fc84 100644 --- a/pillar/application/modules/nodes.py +++ b/pillar/application/modules/nodes.py @@ -13,6 +13,8 @@ from application.utils import str2id, jsonify from application.utils.authorization import check_permissions, require_login from application.utils.gcs import update_file_name from application.utils.activities import activity_subscribe, activity_object_add +from application.utils.algolia import algolia_index_node_delete +from application.utils.algolia import algolia_index_node_save log = logging.getLogger(__name__) blueprint = Blueprint('nodes', __name__) @@ -200,13 +202,20 @@ def after_replacing_node(item, original): return from algoliasearch.client import AlgoliaException - from application.utils.algolia import algolia_index_node_save + status = item['properties'].get('status', 'unpublished') - try: - algolia_index_node_save(item) - except AlgoliaException as ex: - log.warning('Unable to push node info to Algolia for node %s; %s', - item.get('_id'), ex) + if status == 'published': + try: + algolia_index_node_save(item) + except AlgoliaException as ex: + log.warning('Unable to push node info to Algolia for node %s; %s', + item.get('_id'), ex) + else: + try: + algolia_index_node_delete(item) + except AlgoliaException as ex: + log.warning('Unable to delete node info to Algolia for node %s; %s', + item.get('_id'), ex) def before_inserting_nodes(items): @@ -369,6 +378,15 @@ def nodes_set_default_picture(nodes): node_set_default_picture(node) +def after_deleting_node(item): + from algoliasearch.client import AlgoliaException + try: + algolia_index_node_delete(item) + except AlgoliaException as ex: + log.warning('Unable to delete node info to Algolia for node %s; %s', + item.get('_id'), ex) + + def setup_app(app, url_prefix): app.on_fetched_item_nodes += before_returning_node app.on_fetched_resource_nodes += before_returning_nodes @@ -386,4 +404,6 @@ def setup_app(app, url_prefix): app.on_insert_nodes += nodes_set_default_picture app.on_inserted_nodes += after_inserting_nodes + app.on_deleted_item_nodes += after_deleting_node + app.register_blueprint(blueprint, url_prefix=url_prefix) diff --git a/pillar/application/utils/algolia.py b/pillar/application/utils/algolia.py index 004fce3f..1632f257 100644 --- a/pillar/application/utils/algolia.py +++ b/pillar/application/utils/algolia.py @@ -10,13 +10,17 @@ from . import skip_when_testing log = logging.getLogger(__name__) +INDEX_ALLOWED_USER_ROLES = {'admin', 'subscriber', 'demo'} +INDEX_ALLOWED_NODE_TYPES = {'asset', 'texture', 'group', 'hdri'} + + @skip_when_testing def algolia_index_user_save(user): - # Define accepted roles - accepted_roles = ['admin', 'subscriber', 'demo'] + if algolia_index_users is None: + return # Strip unneeded roles if 'roles' in user: - roles = [r for r in user['roles'] if r in accepted_roles] + roles = set(user['roles']).intersection(INDEX_ALLOWED_USER_ROLES) else: roles = None if algolia_index_users: @@ -25,15 +29,15 @@ def algolia_index_user_save(user): 'objectID': user['_id'], 'full_name': user['full_name'], 'username': user['username'], - 'roles': roles, + 'roles': list(roles), 'groups': user['groups'], 'email': user['email'] }) + @skip_when_testing def algolia_index_node_save(node): - accepted_node_types = ['asset', 'texture', 'group'] - if node['node_type'] in accepted_node_types and algolia_index_nodes: + if node['node_type'] in INDEX_ALLOWED_NODE_TYPES and algolia_index_nodes: # If a nodes does not have status published, do not index if 'status' in node['properties'] \ and node['properties']['status'] != 'published': @@ -85,3 +89,10 @@ def algolia_index_node_save(node): node_ob['tags'] = node['properties']['tags'] algolia_index_nodes.save_object(node_ob) + + +@skip_when_testing +def algolia_index_node_delete(node): + if algolia_index_nodes is None: + return + algolia_index_nodes.delete_object(node['_id'])