Skip to content

Commit

Permalink
fixup! implement allow_frozen_fields and excluded_frozen_fields
Browse files Browse the repository at this point in the history
  • Loading branch information
falko17 committed Oct 22, 2022
1 parent 84b7d46 commit 798af52
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions flask_admin/model/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -870,7 +870,7 @@ def _refresh_forms_cache(self):

# Initialize frozen fields
if not hasattr(self, "_saved_frozen_fields"):
self._saved_frozen_fields = None
self._frozen_id = None

def _refresh_filters_cache(self):
self._filters = self.get_filters()
Expand Down Expand Up @@ -2119,15 +2119,21 @@ def create_view(self):
if not self.can_create:
return redirect(return_url)

form = self.create_form()
# handle frozen fields, coming from an edit form
existing_model = None
if self._frozen_id:
existing_model = self.get_one(self._frozen_id)
self._frozen_id = None

form = self.create_form(obj=existing_model)
if not hasattr(form, '_validated_ruleset') or not form._validated_ruleset:
self._validate_form_instance(ruleset=self._form_create_rules, form=form)

# handle frozen fields, coming from an edit form
if self._saved_frozen_fields:
for (field, data) in self._saved_frozen_fields.items():
getattr(form, field).data = data
self._saved_frozen_fields = None
if existing_model is not None:
# we need to reset excluded frozen fields here.
for field in self.excluded_frozen_fields:
if field in form.data:
getattr(form, field).data = ""

if self.validate_form(form):
# in versions 1.1.0 and before, this returns a boolean
Expand All @@ -2143,7 +2149,7 @@ def create_view(self):
for field in self.excluded_frozen_fields:
if field in form.data:
getattr(form, field).data = ""
self._saved_frozen_fields = form.data
self._frozen_id = self.get_pk_value(model)
return redirect(request.url)
elif '_continue_editing' in request.form:
# if we have a valid model, try to go to the edit view
Expand Down Expand Up @@ -2199,12 +2205,7 @@ def edit_view(self):
if '_add_another' in request.form:
return redirect(self.get_url('.create_view', url=return_url))
elif '_freeze_fields' in request.form and self.allow_frozen_fields:
# we simply don't redirect here, which will cause the fields to
# stay as they are. All we need to do is reset the excluded fields.
for field in self.excluded_frozen_fields:
if field in form.data:
getattr(form, field).data = ""
self._saved_frozen_fields = form.data
self._frozen_id = self.get_pk_value(model)
return redirect(self.get_url('.create_view', url=return_url))
elif '_continue_editing' in request.form:
return redirect(self.get_url('.edit_view', id=self.get_pk_value(model)))
Expand Down

0 comments on commit 798af52

Please sign in to comment.