Beispiel #1
0
    def get_choices(data_source, filter, search_term=None, limit=20):
        table = get_indicator_table(data_source)
        sql_column = table.c[filter.name]
        query = Session.query(sql_column)
        if search_term:
            query = query.filter(sql_column.contains(search_term))

        return [v[0] for v in query.distinct().limit(limit)]
Beispiel #2
0
    def get_choices(data_source, filter, search_term=None, limit=20, page=0):
        table = get_indicator_table(data_source)
        sql_column = table.c[filter.field]
        query = Session.query(sql_column)
        if search_term:
            query = query.filter(sql_column.contains(search_term))

        offset = page * limit
        try:
            return [v[0] for v in query.distinct().order_by(sql_column).limit(limit).offset(offset)]
        except ProgrammingError:
            return []
Beispiel #3
0
def preview_data_source(request, domain, config_id):
    config = get_document_or_404(DataSourceConfiguration, domain, config_id)
    table = get_indicator_table(config)

    q = Session.query(table)
    context = _shared_context(domain)
    context.update({
        'data_source': config,
        'columns': q.column_descriptions,
        'data': q[:20],
    })
    return render(request, "userreports/preview_data.html", context)
Beispiel #4
0
def preview_data_source(request, domain, config_id):
    config, is_static = get_datasource_config_or_404(config_id, domain)
    table = get_indicator_table(config)

    q = Session.query(table)
    context = _shared_context(domain)
    context.update({
        'data_source': config,
        'columns': q.column_descriptions,
        'data': q[:20],
        'total_rows': q.count(),
    })
    return render(request, "userreports/preview_data.html", context)
Beispiel #5
0
    def get_choices(data_source, filter, search_term=None, limit=20, page=0):
        # todo: we may want to log this as soon as mobile UCR stops hitting this
        # for misconfigured filters
        if not isinstance(filter, DynamicChoiceListFilter):
            return []

        table = get_indicator_table(data_source)
        sql_column = table.c[filter.field]
        query = Session.query(sql_column)
        if search_term:
            query = query.filter(sql_column.contains(search_term))

        offset = page * limit
        try:
            return [v[0] for v in query.distinct().order_by(sql_column).limit(limit).offset(offset)]
        except ProgrammingError:
            return []
Beispiel #6
0
    def get_choices(data_source, filter, search_term=None, limit=20, page=0):
        # todo: we may want to log this as soon as mobile UCR stops hitting this
        # for misconfigured filters
        if not isinstance(filter, DynamicChoiceListFilter):
            return []

        table = get_indicator_table(data_source)
        sql_column = table.c[filter.field]
        query = Session.query(sql_column)
        if search_term:
            query = query.filter(sql_column.contains(search_term))

        offset = page * limit
        try:
            return [
                v[0] for v in query.distinct().order_by(sql_column).limit(
                    limit).offset(offset)
            ]
        except ProgrammingError:
            return []
Beispiel #7
0
def export_data_source(request, domain, config_id):
    format = request.GET.get('format', Format.UNZIPPED_CSV)
    config = get_document_or_404(DataSourceConfiguration, domain, config_id)
    table = get_indicator_table(config)
    q = Session.query(table)
    column_headers = [col['name'] for col in q.column_descriptions]

    # apply filtering if any
    filter_values = {key: value for key, value in request.GET.items() if key != 'format'}
    for key in filter_values:
        if key not in column_headers:
            return HttpResponse('Invalid filter parameter: {}'.format(key), status=400)
    q = q.filter_by(**filter_values)

    # build export
    def get_table(q):
        yield column_headers
        for row in q:
            yield row

    fd, path = tempfile.mkstemp()
    with os.fdopen(fd, 'wb') as temp:
        export_from_tables([[config.table_id, get_table(q)]], temp, format)
        return export_response(Temp(path), format, config.display_name)
Beispiel #8
0
 def tearDownClass(cls):
     cls._delete_everything()
     # todo: understand why this is necessary. the view call uses the session and the
     # signal doesn't fire to kill it.
     Session.remove()
Beispiel #9
0
 def tearDownClass(cls):
     cls._delete_everything()
     # todo: understand why this is necessary. the view call uses the session and the
     # signal doesn't fire to kill it.
     Session.remove()