Skip to content

Commit

Permalink
Resolve merge conflits
Browse files Browse the repository at this point in the history
  • Loading branch information
dpgaspar committed Nov 29, 2016
1 parent 7cd0426 commit 08d9ea3
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
52 changes: 50 additions & 2 deletions flask_appbuilder/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import datetime
from sqlalchemy import Column, Integer, String, ForeignKey, Date, Float
from sqlalchemy.orm import relationship
from flask import request, session
from flask import redirect, request, session
from flask_appbuilder import Model, SQLA
from flask_appbuilder.models.sqla.filters import FilterStartsWith, FilterEqual
from flask_appbuilder.models.mixins import FileColumn, ImageColumn
Expand Down Expand Up @@ -36,6 +36,7 @@
NOTNULL_VALIDATION_STRING = 'This field is required'
DEFAULT_ADMIN_USER = 'admin'
DEFAULT_ADMIN_PASSWORD = 'general'
REDIRECT_OBJ_ID = 99999

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -110,7 +111,6 @@ class Model22View(ModelView):
edit_exclude_columns = ['excluded_string']
show_exclude_columns = ['excluded_string']


class Model1View(ModelView):
datamodel = SQLAInterface(Model1)
related_views = [Model2View]
Expand All @@ -119,6 +119,20 @@ class Model1View(ModelView):
class Model1CompactView(CompactCRUDMixin, ModelView):
datamodel = SQLAInterface(Model1)

class Model1ViewWithRedirects(ModelView):
datamodel = SQLAInterface(Model1)
obj_id = 1

def post_add_redirect(self):
return redirect('model1viewwithredirects/show/{0}'.format(REDIRECT_OBJ_ID))

def post_edit_redirect(self):
return redirect('model1viewwithredirects/show/{0}'.format(REDIRECT_OBJ_ID))

def post_delete_redirect(self):
return redirect('model1viewwithredirects/show/{0}'.format(REDIRECT_OBJ_ID))



class Model1Filtered1View(ModelView):
datamodel = SQLAInterface(Model1)
Expand Down Expand Up @@ -194,6 +208,7 @@ class Model1FormattedView(ModelView):


self.appbuilder.add_view(Model1View, "Model1", category='Model1')
self.appbuilder.add_view(Model1ViewWithRedirects, "Model1ViewWithRedirects", category='Model1')
self.appbuilder.add_view(Model1CompactView, "Model1Compact", category='Model1')
self.appbuilder.add_view(Model1MasterView, "Model1Master", category='Model1')
self.appbuilder.add_view(Model1MasterChartView, "Model1MasterChart", category='Model1')
Expand Down Expand Up @@ -432,6 +447,39 @@ def test_formatted_cols(self):
data = rv.data.decode('utf-8')
ok_('FORMATTED_STRING' in data)

def test_model_redirects(self):
"""
Test Model redirects after add, delete, edit
"""
client = self.app.test_client()
rv = self.login(client, DEFAULT_ADMIN_USER, DEFAULT_ADMIN_PASSWORD)

model1 = Model1(field_string='Test Redirects')
self.db.session.add(model1)
model1.id = REDIRECT_OBJ_ID
self.db.session.flush()

rv = client.post('/model1viewwithredirects/add',
data=dict(field_string='test_redirect', field_integer='1',
field_float='0.12',
field_date='2014-01-01'), follow_redirects=True)

eq_(rv.status_code, 200)
data = rv.data.decode('utf-8')
ok_('Test Redirects' in data)

model_id = self.db.session.query(Model1).filter_by(field_string='test_redirect').first().id
rv = client.post('/model1viewwithredirects/edit/{0}'.format(model_id),
data=dict(field_string='test_redirect_2', field_integer='2'),
follow_redirects=True)
eq_(rv.status_code, 200)
ok_('Test Redirects' in data)

rv = client.get('/model1viewwithredirects/delete/{0}'.format(model_id),
follow_redirects=True)
eq_(rv.status_code, 200)
ok_('Test Redirects' in data)

def test_excluded_cols(self):
"""
Test add_exclude_columns, edit_exclude_columns, show_exclude_columns
Expand Down
18 changes: 15 additions & 3 deletions flask_appbuilder/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,18 @@ class ModelView(RestCRUDView):
def __init__(self, **kwargs):
super(ModelView, self).__init__(**kwargs)

def post_add_redirect(self):
"""Override this function to control the redirect after add endpoint is called."""
return redirect(self.get_redirect())

def post_edit_redirect(self):
"""Override this function to control the redirect after edit endpoint is called."""
return redirect(self.get_redirect())

def post_delete_redirect(self):
"""Override this function to control the redirect after edit endpoint is called."""
return redirect(self.get_redirect())

"""
--------------------------------
LIST
Expand Down Expand Up @@ -479,7 +491,7 @@ def show(self, pk):
def add(self):
widget = self._add()
if not widget:
return redirect(self.get_redirect())
return self.post_add_redirect()
else:
return self.render_template(self.add_template,
title=self.add_title,
Expand All @@ -496,7 +508,7 @@ def add(self):
def edit(self, pk):
widgets = self._edit(pk)
if not widgets:
return redirect(self.get_redirect())
return self.post_edit_redirect()
else:
return self.render_template(self.edit_template,
title=self.edit_title,
Expand All @@ -513,7 +525,7 @@ def edit(self, pk):
@has_access
def delete(self, pk):
self._delete(pk)
return redirect(self.get_redirect())
return self.post_delete_redirect()

@expose('/download/<string:filename>')
@has_access
Expand Down

0 comments on commit 08d9ea3

Please sign in to comment.