예제 #1
0
def get_all_reports():
    """ Get abuse reports

        Filtering is possible through "filters" query string, JSON double encoded format
    """
    if 'filters' in request.args:
        code, resp, nb_reps = ReportsController.index(filters=request.args['filters'], user=g.user)
    else:
        code, resp, nb_reps = ReportsController.index(user=g.user)
    return code, resp
예제 #2
0
def search(**kwargs):
    """ Global Search
    """
    filters = {}
    user = kwargs['user']

    if 'filters' in kwargs:
        try:
            filters = json.loads(unquote(unquote(kwargs['filters'])))
        except (ValueError, SyntaxError):
            return 400, {'status': 'Bad Request', 'code': 400, 'message': 'Unable to decode JSON'}

    custom_filters = {
        'ticket': {
            'fields': SEARCH_TICKET_FIELDS,
            'filters': deepcopy(filters),
        },
        'report': {
            'fields': SEARCH_REPORT_FIELDS,
            'filters': deepcopy(filters),
        },
    }

    try:
        for _, values in custom_filters.iteritems():
            for key, val in filters.iteritems():
                if key == 'queryFields':
                    values['filters'][key] = [q for q in val if q in values['fields']]
                if key == 'sortBy':
                    values['filters'][key] = {k: v for k, v in filters[key].iteritems() if k in values['fields']}
                if key == 'where':
                    for key2 in filters[key].keys():
                        values['filters'][key][key2] = [a for a in filters[key][key2] if a.keys()[0] in values['fields']]
    except AttributeError:
        return 400, {'status': 'Bad Request', 'code': 400, 'message': 'Invalid fields in body'}

    for _, values in custom_filters.iteritems():
        if 'where' in values['filters']:
            new_where = deepcopy(values['filters']['where'])
            for key, val in values['filters']['where'].iteritems():
                for field in values['filters']['where'][key]:
                    if field.keys()[0] in SEARCH_MAPPING:
                        for new_field in SEARCH_MAPPING[field.keys()[0]]:
                            new_where[key].append({new_field: field[field.keys()[0]]})
                        new_where[key].remove(field)
                    elif 'ticketTag' in field:
                        if Tag.objects.filter(name__in=field['ticketTag'], tagType='Report').count():
                            new_where[key].append({'reportsTag': field['ticketTag']})
                        else:
                            new_where[key].append({'ticketsTag': field['ticketTag']})
                        new_where[key].remove(field)
            values['filters']['where'] = new_where

    from api.controllers import ReportsController, TicketsController
    code1, reps, nb_reps = ReportsController.index(filters=json.dumps(custom_filters['report']['filters']), user=user)
    code2, ticks, nb_ticks = TicketsController.index(filters=json.dumps(custom_filters['ticket']['filters']), user=user)

    code = code1 if code1 > code2 else code2
    return code, {'tickets': ticks, 'reports': reps, 'ticketsCount': nb_ticks, 'reportsCount': nb_reps}