Esempio n. 1
0
def create_annotation(fields):
    """Create and store an annotation."""
    annotation = Annotation(fields)
    search_lib.prepare(annotation)
    annotation.save()

    return annotation
Esempio n. 2
0
def create_annotation(fields, userid):
    """Create and store an annotation."""
    # Create Annotation instance
    annotation = Annotation(fields)
    annotation['user'] = userid

    # Save it in the database
    search_lib.prepare(annotation)
    annotation.save()

    log.debug('Created annotation; user: %s', annotation['user'])

    return annotation
Esempio n. 3
0
def _add_any_field_params_into_query(search_params):
    """Add any_field parameters to ES query."""
    any_terms = search_params['query'].getall('any')
    del search_params['query']['any']

    query = search_params.get('query', None)
    offset = search_params.get('offset', None)
    limit = search_params.get('limit', None)
    sort = search_params.get('sort', None)
    order = search_params.get('order', None)
    query = Annotation._build_query(query, offset, limit, sort, order)

    multi_match_query = {
        'multi_match': {
            'query': any_terms,
            'type': 'cross_fields',
            'fields': ['quote', 'tags', 'text', 'uri.parts', 'user']
        }
    }

    # Remove match_all if we add the multi-match part
    if 'match_all' in query['query']['bool']['must'][0]:
        query['query']['bool']['must'] = []
    query['query']['bool']['must'].append(multi_match_query)

    return query
Esempio n. 4
0
def create_annotation(fields, userid):
    """Create and store an annotation."""
    # Some fields are not to be set by the user, ignore them
    for field in PROTECTED_FIELDS:
        fields.pop(field, None)

    # Create Annotation instance
    annotation = Annotation(fields)
    annotation['user'] = userid

    # Save it in the database
    search_lib.prepare(annotation)
    annotation.save()

    log.debug('Created annotation; user: %s', annotation['user'])

    return annotation
Esempio n. 5
0
def annotations_index(request):
    """Do a search for all annotations on anything and return results.

    This will use the default limit, 20 at time of writing, and results
    are ordered most recent first.
    """
    user = get_user(request)
    return Annotation.search(user=user)
Esempio n. 6
0
def _create_annotation(fields, user):
    """Create and store an annotation."""

    # Some fields are not to be set by the user, ignore them
    for field in PROTECTED_FIELDS:
        fields.pop(field, None)

    # Create Annotation instance
    annotation = Annotation(fields)

    annotation['user'] = user.id
    annotation['consumer'] = user.consumer.key

    # Save it in the database
    annotation.save()

    log.debug('Created annotation; user: %s, consumer key: %s',
              annotation['user'], annotation['consumer'])

    return annotation
Esempio n. 7
0
def _create_annotation(fields, user):
    """Create and store an annotation."""

    # Some fields are not to be set by the user, ignore them
    for field in PROTECTED_FIELDS:
        fields.pop(field, None)

    # Create Annotation instance
    annotation = Annotation(fields)

    annotation['user'] = user.id
    annotation['consumer'] = user.consumer.key

    # Save it in the database
    annotation.save()

    log.debug('Created annotation; user: %s, consumer key: %s',
              annotation['user'], annotation['consumer'])

    return annotation
Esempio n. 8
0
def create_annotation(fields, user):
    """Create and store an annotation."""
    # Some fields are not to be set by the user, ignore them
    for field in PROTECTED_FIELDS:
        fields.pop(field, None)

    # Create Annotation instance
    annotation = Annotation(fields)

    annotation['user'] = user.id
    annotation['consumer'] = user.consumer.key

    if nipsa.has_nipsa(user.id):
        annotation["nipsa"] = True

    # Save it in the database
    search_lib.prepare(annotation)
    annotation.save()

    log.debug('Created annotation; user: %s, consumer key: %s',
              annotation['user'], annotation['consumer'])

    return annotation
Esempio n. 9
0
File: search.py Progetto: juli-so/h
def search(request_params, user=None):
    """Search with the given params and return the matching annotations.

    :param request_params: the HTTP request params that were posted to the
        h search API
    :type request_params: webob.multidict.NestedMultiDict

    :param user: the authorized user, or None
    :type user: h.accounts.models.User or None

    :returns: a dict with keys "rows" (the list of matching annotations, as
        dicts) and "total" (the number of matching annotations, an int)
    :rtype: dict

    """
    log.debug("Searching with user=%s, for uri=%s",
              user.id if user else 'None',
              request_params.get('uri'))

    query = build_query(request_params)
    results = Annotation.search_raw(query, user=user)
    count = Annotation.search_raw(query, {'search_type': 'count'},
                                  raw_result=True)
    return {"rows": results, "total": count["hits"]["total"]}
Esempio n. 10
0
def _search(request_params, user=None):
    # Compile search parameters
    search_params = _search_params(request_params, user=user)

    log.debug("Searching with user=%s, for uri=%s",
              user.id if user else 'None',
              request_params.get('uri'))

    if 'any' in search_params['query']:
        # Handle any field parameters
        query = _add_any_field_params_into_query(search_params)
        results = Annotation.search_raw(query)

        params = {'search_type': 'count'}
        count = Annotation.search_raw(query, params, raw_result=True)
        total = count['hits']['total']
    else:
        results = Annotation.search(**search_params)
        total = Annotation.count(**search_params)

    return {
        'rows': results,
        'total': total,
    }