Exemple #1
0
 def wrapper(*args, **kwargs):
     # validate size
     if kwargs["size"] > app.config["PAPI_MAX_SIZE"]:
         return problem( 400, "Bad Request",
             "Value of parameter size= must not be greater than %d"
             % app.config["PAPI_MAX_SIZE"]
         )
     # validate value of sortBy    
     sort_by, _ = split_sortby(kwargs['sortBy'])
     if sort_by.lower() not in [v.lower() for v in ALLOWED_SORT_BY_VALUES]:
         return problem( 400, "Bad Request",
             "Value '{}' of parameter sortBy= is not allowed. Use one of these values: {}".format(
                 kwargs['sortBy'], ", ".join(ALLOWED_SORT_BY_VALUES)))
     # validate filter names (depending on compliance level)
     non_filters = ('size', 'page', 'sortBy', 'body')
     if app.config['PAPI_COMPLIANCE_LEVEL'] == 0:
         for kw in kwargs:
             if kw not in non_filters and not kw.lower() in ALLOWED_FILTERS_CL0:
                 return problem(400, "Bad Request", "'{}' is not a valid filter for compliance level 0".format(kw))
     else: # compliance level > 0
         allowed_filters = ALLOWED_FILTERS_CL0 + ALLOWED_EXTRA_FILTERS
         for kw in kwargs:
             if kw not in non_filters and not kw.lower() in allowed_filters:
                 return problem(400, "Bad Request", 
                     "'{}' is not a valid filter for compliance level {}".format(kw, app.config['PAPI_COMPLIANCE_LEVEL']))
     return func(*args, **kwargs)
Exemple #2
0
def get_sources(size, page, sortBy='createdWhen', body=None, **filters):
    """Return a (filtered) list of sources.
    """
    connector = get_connector()
    # we assume all other kwargs are filters
    sort_by, sort_order = split_sortby(sortBy)

    # from is a reserved word, so we replace it with 'from_'
    from_ = filters.pop('from', '')
    if from_: filters['from_'] = from_
    
    sources = connector.search(size, page, sort_by, sort_order, **filters)
    if not sources:
        return problem(404, "Not found", "No (more) results found.")
    total_hits = connector.count(**filters)
    return {
        "protocol": {"page": page, "size": size, "totalHits": total_hits},
        "sources": sources
    }