Manager command to index nodes

This commit is contained in:
Francesco Siddi 2016-02-26 18:38:39 +01:00
parent ed7fd0de3b
commit d9b7310d2d
2 changed files with 40 additions and 8 deletions

View File

@ -54,12 +54,13 @@ def algolia_index_node_save(node):
files_collection = app.data.driver.db['files'] files_collection = app.data.driver.db['files']
lookup = {'_id': ObjectId(node['picture'])} lookup = {'_id': ObjectId(node['picture'])}
picture = files_collection.find_one(lookup) picture = files_collection.find_one(lookup)
variation_t = next((item for item in picture['variations'] \ if picture['backend'] == 'gcs':
if item['size'] == 't'), None) variation_t = next((item for item in picture['variations'] \
if variation_t: if item['size'] == 't'), None)
node_ob['picture'] = generate_link(picture['backend'], if variation_t:
variation_t['file_path'], project_id=str(picture['project']), node_ob['picture'] = generate_link(picture['backend'],
is_public=True) variation_t['file_path'], project_id=str(picture['project']),
is_public=True)
# If the node has world permissions, compute the Free permission # If the node has world permissions, compute the Free permission
if 'permissions' in node and 'world' in node['permissions']: if 'permissions' in node and 'world' in node['permissions']:
if 'GET' in node['permissions']: if 'GET' in node['permissions']:

View File

@ -310,7 +310,7 @@ def make_project_public(project_id):
nodes_collection = app.data.driver.db['nodes'] nodes_collection = app.data.driver.db['nodes']
for n in nodes_collection.find({'project': ObjectId(project_id)}): for n in nodes_collection.find({'project': ObjectId(project_id)}):
n['properties']['status'] = 'published' n['properties']['status'] = 'published'
print "Publishing {0} {1}".format(n['_id'], n['name']) print u"Publishing {0} {1}".format(n['_id'], n['name'].encode('ascii', 'ignore'))
if not DRY_RUN: if not DRY_RUN:
put_item('nodes', n) put_item('nodes', n)
@ -408,7 +408,8 @@ def convert_assets_to_textures(project_id):
processed_nodes.append(n) processed_nodes.append(n)
elif n_type['name'] == 'group': elif n_type['name'] == 'group':
# Change group type to texture group # Change group type to texture group
node_type_texture = node_types_collection.find_one({'name':'group_texture'}) node_type_texture = node_types_collection.find_one(
{'name':'group_texture'})
n['node_type'] = node_type_texture['_id'] n['node_type'] = node_type_texture['_id']
n['properties'].pop('notes', None) n['properties'].pop('notes', None)
print "Updating {0}".format(n['name']) print "Updating {0}".format(n['name'])
@ -539,5 +540,35 @@ def algolia_push_users():
print "Pushing {0}".format(user['username']) print "Pushing {0}".format(user['username'])
algolia_index_user_save(user) algolia_index_user_save(user)
@manager.command
def algolia_push_nodes():
"""Loop through all nodes and push them to Algolia"""
from application.utils.algolia import algolia_index_node_save
nodes_collection = app.data.driver.db['nodes']
for node in nodes_collection.find():
print u"Pushing {0}: {1}".format(node['_id'], node['name'].encode(
'ascii', 'ignore'))
algolia_index_node_save(node)
@manager.command
def files_make_public_t():
"""Loop through all files and if they are images on GCS, make the size t
public
"""
from application.utils.gcs import GoogleCloudStorageBucket
files_collection = app.data.driver.db['files']
for f in files_collection.find({'backend': 'gcs'}):
if 'variations' in f:
variation_t = next((item for item in f['variations'] \
if item['size'] == 't'), None)
if variation_t:
storage = GoogleCloudStorageBucket(str(f['project']))
blob = storage.Get(variation_t['file_path'], to_dict=False)
if blob:
blob.make_public()
if __name__ == '__main__': if __name__ == '__main__':
manager.run() manager.run()