Skip to content

Commit

Permalink
Fix "actions" on ModelViews with composite primary keys
Browse files Browse the repository at this point in the history
The refactor in dpgaspar#1398 changes the `get()` function, and mistakenly
always applied the PK filter, even in the case of composite keys.

(The else block of `if self.is_pk_composite` applied it again, so it was
actually applying the same filter twice, which didn't break anything,
but was not needed)
  • Loading branch information
ashb committed Oct 22, 2020
1 parent 25b77d6 commit 414adeb
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
1 change: 0 additions & 1 deletion flask_appbuilder/models/sqla/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -917,7 +917,6 @@ def get(
_filters = filters.copy()
else:
_filters = Filters(self.filter_converter_class, self)
_filters.add_filter(pk, self.FilterEqual, id)

if self.is_pk_composite():
for _pk, _id in zip(pk, id):
Expand Down
23 changes: 23 additions & 0 deletions flask_appbuilder/tests/test_mvc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Set

from flask import Flask, redirect, request, session
from flask_appbuilder.actions import action
from flask_appbuilder import AppBuilder, SQLA
from flask_appbuilder.charts.views import (
ChartView,
Expand Down Expand Up @@ -389,6 +390,12 @@ class Model3View(ModelView):
add_columns = ["pk1", "pk2", "field_string"]
edit_columns = ["pk1", "pk2", "field_string"]

@action("muldelete", "Delete", "Delete all Really?", "fa-rocket", single=False)
def muldelete(self, items):
self.datamodel.delete_all(items)
self.update_redirect()
return redirect(self.get_redirect())

class Model1CompactView(CompactCRUDMixin, ModelView):
datamodel = SQLAInterface(Model1)

Expand Down Expand Up @@ -829,6 +836,22 @@ def test_model_crud_composite_pk(self):
model = self.db.session.query(Model3).filter_by(pk1=2).one_or_none()
self.assertEqual(model, None)

# Add it back, then delete via muldelete
self.appbuilder.get_session.add(Model3(pk1=1, pk2=datetime.datetime(2017, 1, 1), field_string="baz"))
self.appbuilder.get_session.commit()
__import__('pdb').set_trace()
rv = client.post(
"/model3view/action_post",
data=dict(
action="muldelete",
rowid=[json.dumps(["1", {"_type": "datetime", "value": "2017-01-01T00:00:00.000000"}])],
),
follow_redirects=True,
)
self.assertEqual(rv.status_code, 200)
model = self.db.session.query(Model3).filter_by(pk1=1).one_or_none()
self.assertEqual(model, None)

def test_model_crud_add_with_enum(self):
"""
Test Model add for Model with Enum Columns
Expand Down

0 comments on commit 414adeb

Please sign in to comment.