Introducing Algolia search for users
With the new dependency, algoliasearch, we can push and store on the Algolia service a limited set of properties from the Users collection.
This commit is contained in:
@@ -4,6 +4,7 @@ from bson import ObjectId
|
||||
from datetime import datetime
|
||||
import bugsnag
|
||||
from bugsnag.flask import handle_exceptions
|
||||
from algoliasearch import algoliasearch
|
||||
from flask import g
|
||||
from flask import request
|
||||
from flask import url_for
|
||||
@@ -95,11 +96,21 @@ bugsnag.configure(
|
||||
)
|
||||
handle_exceptions(app)
|
||||
|
||||
# Algolia search
|
||||
if 'ALGOLIA_USER' in app.config:
|
||||
client = algoliasearch.Client(
|
||||
app.config['ALGOLIA_USER'],
|
||||
app.config['ALGOLIA_API_KEY'])
|
||||
algolia_index_users = client.init_index(app.config['ALGOLIA_INDEX_USERS'])
|
||||
else:
|
||||
algolia_index_users = None
|
||||
|
||||
from application.utils.authentication import validate_token
|
||||
from application.utils.authorization import check_permissions
|
||||
from application.utils.cdn import hash_file_path
|
||||
from application.utils.gcs import GoogleCloudStorageBucket
|
||||
from application.utils.gcs import update_file_name
|
||||
from application.utils.algolia import algolia_index_user_save
|
||||
|
||||
|
||||
def before_returning_item_permissions(response):
|
||||
@@ -226,7 +237,12 @@ def post_GET_user(request, payload):
|
||||
# compute_permissions(json_data['_id'], app.data.driver)
|
||||
payload.data = json.dumps(json_data)
|
||||
|
||||
def after_replacing_user(item, original):
|
||||
"""Push an update to the Algolia index when a user item is updated"""
|
||||
algolia_index_user_save(item)
|
||||
|
||||
app.on_post_GET_users += post_GET_user
|
||||
app.on_replace_users += after_replacing_user
|
||||
|
||||
from modules.file_storage import process_file
|
||||
from modules.file_storage import delete_file
|
||||
|
20
pillar/application/utils/algolia.py
Normal file
20
pillar/application/utils/algolia.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from application import algolia_index_users
|
||||
|
||||
def algolia_index_user_save(user):
|
||||
# Define accepted roles
|
||||
accepted_roles = ['admin', 'subscriber', 'demo']
|
||||
# Strip unneeded roles
|
||||
if 'roles' in user:
|
||||
roles = [r for r in user['roles'] if r in accepted_roles]
|
||||
else:
|
||||
roles = None
|
||||
if algolia_index_users:
|
||||
# Create or update Algolia index for the user
|
||||
algolia_index_users.save_object({
|
||||
'objectID': user['_id'],
|
||||
'full_name': user['full_name'],
|
||||
'username': user['username'],
|
||||
'roles': roles,
|
||||
'groups': user['groups'],
|
||||
'email': user['email']
|
||||
})
|
@@ -550,5 +550,14 @@ def test_post_internal(node_id):
|
||||
print post_internal('nodes', node)
|
||||
|
||||
|
||||
@manager.command
|
||||
def algolia_push_users():
|
||||
"""Loop through all users and push them to Algolia"""
|
||||
from application.utils.algolia import algolia_index_user_save
|
||||
users_collection = app.data.driver.db['users']
|
||||
for user in users_collection.find():
|
||||
print "Pushing {0}".format(user['username'])
|
||||
algolia_index_user_save(user)
|
||||
|
||||
if __name__ == '__main__':
|
||||
manager.run()
|
||||
|
@@ -42,7 +42,7 @@ users_schema = {
|
||||
},
|
||||
'roles': {
|
||||
'type': 'list',
|
||||
'allowed': ["admin", "subscriber"],
|
||||
'allowed': ["admin", "subscriber", "demo"],
|
||||
},
|
||||
'groups': {
|
||||
'type': 'list',
|
||||
|
Reference in New Issue
Block a user