Repository: flask-admin/flask-admin

Issue:
sqla-custom-filter example throws Internal error while selecting "is Brown" filter
## Observation:

Custom filter "is Brown" throws error: TypeError: apply() takes exactly 3 arguments (4 given)
## Expected Result:

User list should be filter based on value(Yes/No) for "is Brown" filter
## Steps to reproduce:
- Go to user page and add filter "Last Name"
- Select "is Brown" operation and any value (Yes or No) for the operation
- Click on "Apply" button and it should throw an error
## Root cause:
- app.py -> class FilterLastNameBrown(BaseSQLAFilter) -> method apply(self, query, value)
  apply method takes only two parameter while custom filter call in UserAdmin model view passes three parameter.
- **work around:** Just adding third argument to apply works fine. [apply(self, query, value, third_arg)] - This signature works file. Though third argument in UserAdmin view is option (Yes/No), it gets mapped to second argument "value" in the apply method.
### I don't know much internal of the Flask-Admin and its custom sql filter, but somebody can look into the issue.



Discussion:

Resolving patch:
diff --git a/examples/sqla-custom-filter/app.py b/examples/sqla-custom-filter/app.py
--- a/examples/sqla-custom-filter/app.py
+++ b/examples/sqla-custom-filter/app.py
@@ -2,7 +2,7 @@
 from flask_sqlalchemy import SQLAlchemy
 
 from flask_admin.contrib import sqla
-from flask_admin import expose, Admin
+from flask_admin import Admin
 
 # required for creating custom filters
 from flask_admin.contrib.sqla.filters import BaseSQLAFilter, FilterEqual
@@ -19,6 +19,13 @@
 app.config['SQLALCHEMY_ECHO'] = True
 db = SQLAlchemy(app)
 
+
+# Flask views
+@app.route('/')
+def index():
+    return '<a href="/admin/">Click me to get to Admin!</a>'
+
+
 # Create model
 class User(db.Model):
     def __init__(self, first_name, last_name, username, email):
@@ -33,13 +40,14 @@ def __init__(self, first_name, last_name, username, email):
     username = db.Column(db.String(80), unique=True)
     email = db.Column(db.String(120), unique=True)
 
-    # Required for administrative interface. For python 3 please use __str__ instead.
+    # Required for admin interface. For python 3 please use __str__ instead.
     def __unicode__(self):
         return self.username
 
+
 # Create custom filter class
 class FilterLastNameBrown(BaseSQLAFilter):
-    def apply(self, query, value):
+    def apply(self, query, value, alias=None):
         if value == '1':
             return query.filter(self.column == "Brown")
         else:
@@ -48,18 +56,23 @@ def apply(self, query, value):
     def operation(self):
         return 'is Brown'
 
+
 # Add custom filter and standard FilterEqual to ModelView
 class UserAdmin(sqla.ModelView):
     # each filter in the list is a filter operation (equals, not equals, etc)
     # filters with the same name will appear as operations under the same filter
     column_filters = [
         FilterEqual(User.last_name, 'Last Name'),
-        FilterLastNameBrown(User.last_name, 'Last Name', options=(('1', 'Yes'),('0', 'No')))
+        FilterLastNameBrown(
+            User.last_name, 'Last Name', options=(('1', 'Yes'), ('0', 'No'))
+        )
     ]
 
+
 admin = Admin(app, template_mode="bootstrap3")
 admin.add_view(UserAdmin(User, db.session))
 
+
 def build_sample_db():
     db.drop_all()
     db.create_all()
@@ -70,6 +83,7 @@ def build_sample_db():
     db.session.add_all([user_obj1, user_obj2, user_obj3])
     db.session.commit()
 
+
 if __name__ == '__main__':
     build_sample_db()
-    app.run(port=5000)
+    app.run(port=5000, debug=True)


Test patch: