Ejemplo n.º 1
0
    def test_only_variations(self):
        from pillar.api.file_storage import moving, generate_link

        image_file_id, fdoc = self._create_image_file_doc()

        # Expect GETs on regenerated links.
        mock.add(mock.GET,
                 generate_link('unittest', fdoc['file_path']),
                 status=404)

        for variation in fdoc['variations']:
            mock.add(mock.GET,
                     generate_link('unittest', variation['file_path']),
                     body='file-content',
                     content_type='image/jpeg')

        with self.app.test_request_context():
            moving.change_file_storage_backend(image_file_id, 'gcs')

            # Check that the file document has been updated correctly
            files_coll = self.app.data.driver.db['files']
            fdoc = files_coll.find_one(image_file_id)

        self.assertEqual('gcs', fdoc['backend'])
        self.assertIn('/path/to/testing/gcs/', fdoc['link'])

        for variation in fdoc['variations']:
            self.assertIn('/path/to/testing/gcs/', variation['link'])
Ejemplo n.º 2
0
def _handle_picture(node: dict, to_index: dict):
    """Add picture URL in-place to the to-be-indexed node."""

    picture_id = node.get('picture')
    if not picture_id:
        return

    files_collection = current_app.data.driver.db['files']
    lookup = {'_id': ObjectId(picture_id)}
    picture = files_collection.find_one(lookup)

    for item in picture.get('variations', []):
        if item['size'] != 't':
            continue

        # Not all files have a project...
        pid = picture.get('project')
        if pid:
            link = generate_link(picture['backend'],
                                 item['file_path'],
                                 str(pid),
                                 is_public=True)
        else:
            link = item['link']
        to_index['picture'] = link
        break
Ejemplo n.º 3
0
def algolia_index_node_save(node):
    if not current_app.algolia_index_nodes:
        return
    if node['node_type'] not in INDEX_ALLOWED_NODE_TYPES:
        return
    # If a nodes does not have status published, do not index
    if node['properties'].get('status') != 'published':
        return

    projects_collection = current_app.data.driver.db['projects']
    project = projects_collection.find_one({'_id': ObjectId(node['project'])})

    users_collection = current_app.data.driver.db['users']
    user = users_collection.find_one({'_id': ObjectId(node['user'])})

    node_ob = {
        'objectID': node['_id'],
        'name': node['name'],
        'project': {
            '_id': project['_id'],
            'name': project['name']
        },
        'created': node['_created'],
        'updated': node['_updated'],
        'node_type': node['node_type'],
        'user': {
            '_id': user['_id'],
            'full_name': user['full_name']
        },
    }
    if 'description' in node and node['description']:
        node_ob['description'] = node['description']
    if 'picture' in node and node['picture']:
        files_collection = current_app.data.driver.db['files']
        lookup = {'_id': ObjectId(node['picture'])}
        picture = files_collection.find_one(lookup)
        if picture['backend'] == 'gcs':
            variation_t = next((item for item in picture['variations'] \
                                if item['size'] == 't'), None)
            if variation_t:
                node_ob['picture'] = generate_link(picture['backend'],
                                                   variation_t['file_path'],
                                                   project_id=str(picture['project']),
                                                   is_public=True)
    # If the node has world permissions, compute the Free permission
    if 'permissions' in node and 'world' in node['permissions']:
        if 'GET' in node['permissions']['world']:
            node_ob['is_free'] = True

    # Append the media key if the node is of node_type 'asset'
    if node['node_type'] == 'asset':
        node_ob['media'] = node['properties']['content_type']

    # Add extra properties
    for prop in ('tags', 'license_notes'):
        if prop in node['properties']:
            node_ob[prop] = node['properties'][prop]

    current_app.algolia_index_nodes.save_object(node_ob)
Ejemplo n.º 4
0
def algolia_index_post_save(node):

    projects_collection = current_app.data.driver.db['projects']
    project = projects_collection.find_one({'_id': ObjectId(node['project'])})

    users_collection = current_app.data.driver.db['users']
    user = users_collection.find_one({'_id': ObjectId(node['user'])})

    rating = node['properties']['rating_positive'] - node['properties'][
        'rating_negative']

    comments_count = post_count_comments(node['_id'])

    node_ob = {
        'objectID': node['_id'],
        'name': node['name'],
        'project': {
            '_id': project['_id'],
            'name': project['name'],
            'url': project['url'],
        },
        'created': node['_created'],
        'updated': node['_updated'],
        'node_type': node['node_type'],
        'user': {
            '_id': user['_id'],
            'full_name': user['full_name'],
            'username': user['username'],
        },
        'hot': node['properties']['hot'],
        'slug': node['properties']['slug'],
        'tags': node['properties']['tags'],
        'rating': rating,
        'shortcode': node['properties']['shortcode'],
        'comments_count': comments_count,
    }
    if 'content' in node['properties'] and node['properties']['content']:
        node_ob['content'] = node['properties']['content']
    # Hack for instantsearch.js. Because we can't do string comparison in Hogan,
    # we store this case in a boolean.
    if node['properties']['post_type'] == 'link':
        node_ob['is_link'] = True
    if 'picture' in node and node['picture']:
        files_collection = current_app.data.driver.db['files']
        lookup = {'_id': ObjectId(node['picture'])}
        picture = files_collection.find_one(lookup)

        variation_s = next(
            (item for item in picture['variations'] if item['size'] == 's'),
            None)
        if variation_s:
            node_ob['picture'] = generate_link(picture['backend'],
                                               variation_s['file_path'],
                                               project_id=str(
                                                   picture['project']),
                                               is_public=True)

    # Try to index additional props (see config.py for more info).
    for key in current_app.config['POST_ADDITIONAL_PROPERTIES']:
        if key in node['properties']:
            node_ob[key] = node['properties'][key]
    current_app.algolia_index_nodes.save_object(node_ob)