def test_column_filters():
    (app, db, admin) = setup()
    (Model1, Model2) = create_models(db)
    fill_db(Model1, Model2)
    view = CustomModelView(Model1, column_filters=['test1'])
    admin.add_view(view)
    eq_(len(view._filters), 7)
    eq_([(f['index'], f['operation']) for f in view._filter_groups[u'Test1']], [(0, 'contains'), (1, 'not contains'), (2, 'equals'), (3, 'not equal'), (4, 'empty'), (5, 'in list'), (6, 'not in list')])
    client = app.test_client()
    rv = client.get('/admin/model1/?flt0_0=test1_val_1')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_1' in data))
    ok_(('test1_val_2' not in data))
    rv = client.get('/admin/model1/?flt0_1=test1_val_1')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_1' not in data))
    ok_(('test1_val_2' in data))
    rv = client.get('/admin/model1/?flt0_2=test1_val_1')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_1' in data))
    ok_(('test1_val_2' not in data))
    rv = client.get('/admin/model1/?flt0_3=test1_val_1')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_1' not in data))
    ok_(('test1_val_2' in data))
    rv = client.get('/admin/model1/?flt0_4=1')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('empty_obj' in data))
    ok_(('test1_val_1' not in data))
    ok_(('test1_val_2' not in data))
    rv = client.get('/admin/model1/?flt0_4=0')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('empty_obj' not in data))
    ok_(('test1_val_1' in data))
    ok_(('test1_val_2' in data))
    rv = client.get('/admin/model1/?flt0_5=test1_val_1%2Ctest1_val_2')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_1' in data))
    ok_(('test2_val_2' in data))
    ok_(('test1_val_3' not in data))
    ok_(('test1_val_4' not in data))
    rv = client.get('/admin/model1/?flt0_6=test1_val_1%2Ctest1_val_2')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_1' not in data))
    ok_(('test2_val_2' not in data))
    ok_(('test1_val_3' in data))
    ok_(('test1_val_4' in data))
    view = CustomModelView(Model2, column_filters=['int_field'])
    admin.add_view(view)
    eq_([(f['index'], f['operation']) for f in view._filter_groups[u'Int Field']], [(0, 'equals'), (1, 'not equal'), (2, 'greater than'), (3, 'smaller than'), (4, 'empty'), (5, 'in list'), (6, 'not in list')])
    rv = client.get('/admin/model2/?flt0_0=5000')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('string_field_val_3' in data))
    ok_(('string_field_val_4' not in data))
    rv = client.get('/admin/model2/?flt0_0=badval')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('Invalid Filter Value' in data))
    rv = client.get('/admin/model2/?flt0_1=5000')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('string_field_val_3' not in data))
    ok_(('string_field_val_4' in data))
    rv = client.get('/admin/model2/?flt0_2=6000')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('string_field_val_3' not in data))
    ok_(('string_field_val_4' in data))
    rv = client.get('/admin/model2/?flt0_3=6000')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('string_field_val_3' in data))
    ok_(('string_field_val_4' not in data))
    rv = client.get('/admin/model2/?flt0_4=1')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('string_field_val_1' in data))
    ok_(('string_field_val_2' in data))
    ok_(('string_field_val_3' not in data))
    ok_(('string_field_val_4' not in data))
    rv = client.get('/admin/model2/?flt0_4=0')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('string_field_val_1' not in data))
    ok_(('string_field_val_2' not in data))
    ok_(('string_field_val_3' in data))
    ok_(('string_field_val_4' in data))
    rv = client.get('/admin/model2/?flt0_5=5000%2C9000')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('string_field_val_1' not in data))
    ok_(('string_field_val_2' not in data))
    ok_(('string_field_val_3' in data))
    ok_(('string_field_val_4' in data))
    rv = client.get('/admin/model2/?flt0_5=5000%2Cbadval')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('Invalid Filter Value' in data))
    rv = client.get('/admin/model2/?flt0_6=5000%2C9000')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('string_field_val_1' in data))
    ok_(('string_field_val_2' in data))
    ok_(('string_field_val_3' not in data))
    ok_(('string_field_val_4' not in data))
    view = CustomModelView(Model2, column_filters=['float_field'], endpoint='_float')
    admin.add_view(view)
    eq_([(f['index'], f['operation']) for f in view._filter_groups[u'Float Field']], [(0, 'equals'), (1, 'not equal'), (2, 'greater than'), (3, 'smaller than'), (4, 'empty'), (5, 'in list'), (6, 'not in list')])
    rv = client.get('/admin/_float/?flt0_0=25.9')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('string_field_val_3' in data))
    ok_(('string_field_val_4' not in data))
    rv = client.get('/admin/_float/?flt0_0=badval')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('Invalid Filter Value' in data))
    rv = client.get('/admin/_float/?flt0_1=25.9')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('string_field_val_3' not in data))
    ok_(('string_field_val_4' in data))
    rv = client.get('/admin/_float/?flt0_2=60.5')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('string_field_val_3' not in data))
    ok_(('string_field_val_4' in data))
    rv = client.get('/admin/_float/?flt0_3=60.5')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('string_field_val_3' in data))
    ok_(('string_field_val_4' not in data))
    rv = client.get('/admin/_float/?flt0_4=1')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('string_field_val_1' in data))
    ok_(('string_field_val_2' in data))
    ok_(('string_field_val_3' not in data))
    ok_(('string_field_val_4' not in data))
    rv = client.get('/admin/_float/?flt0_4=0')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('string_field_val_1' not in data))
    ok_(('string_field_val_2' not in data))
    ok_(('string_field_val_3' in data))
    ok_(('string_field_val_4' in data))
    rv = client.get('/admin/_float/?flt0_5=25.9%2C75.5')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('string_field_val_1' not in data))
    ok_(('string_field_val_2' not in data))
    ok_(('string_field_val_3' in data))
    ok_(('string_field_val_4' in data))
    rv = client.get('/admin/_float/?flt0_5=25.9%2Cbadval')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('Invalid Filter Value' in data))
    rv = client.get('/admin/_float/?flt0_6=25.9%2C75.5')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('string_field_val_1' in data))
    ok_(('string_field_val_2' in data))
    ok_(('string_field_val_3' not in data))
    ok_(('string_field_val_4' not in data))
    view = CustomModelView(Model1, column_filters=['datetime_field'], endpoint='_datetime')
    admin.add_view(view)
    eq_([(f['index'], f['operation']) for f in view._filter_groups[u'Datetime Field']], [(0, 'equals'), (1, 'not equal'), (2, 'greater than'), (3, 'smaller than'), (4, 'between'), (5, 'not between'), (6, 'empty')])
    rv = client.get('/admin/_datetime/?flt0_0=2014-04-03+01%3A09%3A00')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('datetime_obj1' in data))
    ok_(('datetime_obj2' not in data))
    rv = client.get('/admin/_datetime/?flt0_1=2014-04-03+01%3A09%3A00')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('datetime_obj1' not in data))
    ok_(('datetime_obj2' in data))
    rv = client.get('/admin/_datetime/?flt0_2=2014-04-03+01%3A08%3A00')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('datetime_obj1' in data))
    ok_(('datetime_obj2' not in data))
    rv = client.get('/admin/_datetime/?flt0_3=2014-04-03+01%3A08%3A00')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('datetime_obj1' not in data))
    ok_(('datetime_obj2' in data))
    rv = client.get('/admin/_datetime/?flt0_4=2014-04-02+00%3A00%3A00+to+2014-11-20+23%3A59%3A59')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('datetime_obj1' in data))
    ok_(('datetime_obj2' not in data))
    rv = client.get('/admin/_datetime/?flt0_5=2014-04-02+00%3A00%3A00+to+2014-11-20+23%3A59%3A59')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('datetime_obj1' not in data))
    ok_(('datetime_obj2' in data))
    rv = client.get('/admin/_datetime/?flt0_6=1')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test1_val_1' in data))
    ok_(('datetime_obj1' not in data))
    ok_(('datetime_obj2' not in data))
    rv = client.get('/admin/_datetime/?flt0_6=0')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test1_val_1' not in data))
    ok_(('datetime_obj1' in data))
    ok_(('datetime_obj2' in data))
def test_column_filters():
    (app, db, admin) = setup()
    (Model1, Model2) = create_models(db)
    fill_db(Model1, Model2)
    view = CustomModelView(Model1, column_filters=['test1'])
    admin.add_view(view)
    eq_(len(view._filters), 7)
    eq_([(f['index'], f['operation']) for f in view._filter_groups[u'Test1']], [(0, 'contains'), (1, 'not contains'), (2, 'equals'), (3, 'not equal'), (4, 'empty'), (5, 'in list'), (6, 'not in list')])
    client = app.test_client()
    rv = client.get('/admin/model1/?flt0_0=test1_val_1')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_1' in data))
    ok_(('test1_val_2' not in data))
    rv = client.get('/admin/model1/?flt0_1=test1_val_1')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_1' not in data))
    ok_(('test1_val_2' in data))
    rv = client.get('/admin/model1/?flt0_2=test1_val_1')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_1' in data))
    ok_(('test1_val_2' not in data))
    rv = client.get('/admin/model1/?flt0_3=test1_val_1')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_1' not in data))
    ok_(('test1_val_2' in data))
    rv = client.get('/admin/model1/?flt0_4=1')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('empty_obj' in data))
    ok_(('test1_val_1' not in data))
    ok_(('test1_val_2' not in data))
    rv = client.get('/admin/model1/?flt0_4=0')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('empty_obj' not in data))
    ok_(('test1_val_1' in data))
    ok_(('test1_val_2' in data))
    rv = client.get('/admin/model1/?flt0_5=test1_val_1%2Ctest1_val_2')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_1' in data))
    ok_(('test2_val_2' in data))
    ok_(('test1_val_3' not in data))
    ok_(('test1_val_4' not in data))
    rv = client.get('/admin/model1/?flt0_6=test1_val_1%2Ctest1_val_2')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test2_val_1' not in data))
    ok_(('test2_val_2' not in data))
    ok_(('test1_val_3' in data))
    ok_(('test1_val_4' in data))
    view = CustomModelView(Model2, column_filters=['int_field'])
    admin.add_view(view)
    eq_([(f['index'], f['operation']) for f in view._filter_groups[u'Int Field']], [(0, 'equals'), (1, 'not equal'), (2, 'greater than'), (3, 'smaller than'), (4, 'empty'), (5, 'in list'), (6, 'not in list')])
    rv = client.get('/admin/model2/?flt0_0=5000')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('string_field_val_3' in data))
    ok_(('string_field_val_4' not in data))
    rv = client.get('/admin/model2/?flt0_0=badval')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('Invalid Filter Value' in data))
    rv = client.get('/admin/model2/?flt0_1=5000')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('string_field_val_3' not in data))
    ok_(('string_field_val_4' in data))
    rv = client.get('/admin/model2/?flt0_2=6000')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('string_field_val_3' not in data))
    ok_(('string_field_val_4' in data))
    rv = client.get('/admin/model2/?flt0_3=6000')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('string_field_val_3' in data))
    ok_(('string_field_val_4' not in data))
    rv = client.get('/admin/model2/?flt0_4=1')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('string_field_val_1' in data))
    ok_(('string_field_val_2' in data))
    ok_(('string_field_val_3' not in data))
    ok_(('string_field_val_4' not in data))
    rv = client.get('/admin/model2/?flt0_4=0')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('string_field_val_1' not in data))
    ok_(('string_field_val_2' not in data))
    ok_(('string_field_val_3' in data))
    ok_(('string_field_val_4' in data))
    rv = client.get('/admin/model2/?flt0_5=5000%2C9000')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('string_field_val_1' not in data))
    ok_(('string_field_val_2' not in data))
    ok_(('string_field_val_3' in data))
    ok_(('string_field_val_4' in data))
    rv = client.get('/admin/model2/?flt0_5=5000%2Cbadval')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('Invalid Filter Value' in data))
    rv = client.get('/admin/model2/?flt0_6=5000%2C9000')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('string_field_val_1' in data))
    ok_(('string_field_val_2' in data))
    ok_(('string_field_val_3' not in data))
    ok_(('string_field_val_4' not in data))
    view = CustomModelView(Model2, column_filters=['float_field'], endpoint='_float')
    admin.add_view(view)
    eq_([(f['index'], f['operation']) for f in view._filter_groups[u'Float Field']], [(0, 'equals'), (1, 'not equal'), (2, 'greater than'), (3, 'smaller than'), (4, 'empty'), (5, 'in list'), (6, 'not in list')])
    rv = client.get('/admin/_float/?flt0_0=25.9')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('string_field_val_3' in data))
    ok_(('string_field_val_4' not in data))
    rv = client.get('/admin/_float/?flt0_0=badval')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('Invalid Filter Value' in data))
    rv = client.get('/admin/_float/?flt0_1=25.9')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('string_field_val_3' not in data))
    ok_(('string_field_val_4' in data))
    rv = client.get('/admin/_float/?flt0_2=60.5')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('string_field_val_3' not in data))
    ok_(('string_field_val_4' in data))
    rv = client.get('/admin/_float/?flt0_3=60.5')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('string_field_val_3' in data))
    ok_(('string_field_val_4' not in data))
    rv = client.get('/admin/_float/?flt0_4=1')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('string_field_val_1' in data))
    ok_(('string_field_val_2' in data))
    ok_(('string_field_val_3' not in data))
    ok_(('string_field_val_4' not in data))
    rv = client.get('/admin/_float/?flt0_4=0')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('string_field_val_1' not in data))
    ok_(('string_field_val_2' not in data))
    ok_(('string_field_val_3' in data))
    ok_(('string_field_val_4' in data))
    rv = client.get('/admin/_float/?flt0_5=25.9%2C75.5')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('string_field_val_1' not in data))
    ok_(('string_field_val_2' not in data))
    ok_(('string_field_val_3' in data))
    ok_(('string_field_val_4' in data))
    rv = client.get('/admin/_float/?flt0_5=25.9%2Cbadval')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('Invalid Filter Value' in data))
    rv = client.get('/admin/_float/?flt0_6=25.9%2C75.5')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('string_field_val_1' in data))
    ok_(('string_field_val_2' in data))
    ok_(('string_field_val_3' not in data))
    ok_(('string_field_val_4' not in data))
    view = CustomModelView(Model1, column_filters=['datetime_field'], endpoint='_datetime')
    admin.add_view(view)
    eq_([(f['index'], f['operation']) for f in view._filter_groups[u'Datetime Field']], [(0, 'equals'), (1, 'not equal'), (2, 'greater than'), (3, 'smaller than'), (4, 'between'), (5, 'not between'), (6, 'empty')])
    rv = client.get('/admin/_datetime/?flt0_0=2014-04-03+01%3A09%3A00')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('datetime_obj1' in data))
    ok_(('datetime_obj2' not in data))
    rv = client.get('/admin/_datetime/?flt0_1=2014-04-03+01%3A09%3A00')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('datetime_obj1' not in data))
    ok_(('datetime_obj2' in data))
    rv = client.get('/admin/_datetime/?flt0_2=2014-04-03+01%3A08%3A00')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('datetime_obj1' in data))
    ok_(('datetime_obj2' not in data))
    rv = client.get('/admin/_datetime/?flt0_3=2014-04-03+01%3A08%3A00')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('datetime_obj1' not in data))
    ok_(('datetime_obj2' in data))
    rv = client.get('/admin/_datetime/?flt0_4=2014-04-02+00%3A00%3A00+to+2014-11-20+23%3A59%3A59')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('datetime_obj1' in data))
    ok_(('datetime_obj2' not in data))
    rv = client.get('/admin/_datetime/?flt0_5=2014-04-02+00%3A00%3A00+to+2014-11-20+23%3A59%3A59')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('datetime_obj1' not in data))
    ok_(('datetime_obj2' in data))
    rv = client.get('/admin/_datetime/?flt0_6=1')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test1_val_1' in data))
    ok_(('datetime_obj1' not in data))
    ok_(('datetime_obj2' not in data))
    rv = client.get('/admin/_datetime/?flt0_6=0')
    eq_(rv.status_code, 200)
    data = rv.data.decode('utf-8')
    ok_(('test1_val_1' not in data))
    ok_(('datetime_obj1' in data))
    ok_(('datetime_obj2' in data))