Commit 87bd923e authored by Serge S. Koval's avatar Serge S. Koval

Merge pull request #1026 from pawl/fix_inline_editable_order

Editable list view fixes (order of choices & relations bug)
parents b929fc50 5f01c3ae
...@@ -2,7 +2,7 @@ from flask import json ...@@ -2,7 +2,7 @@ from flask import json
from jinja2 import escape from jinja2 import escape
from wtforms.widgets import HTMLString, html_params from wtforms.widgets import HTMLString, html_params
from flask_admin._compat import as_unicode from flask_admin._compat import as_unicode, text_type
from flask_admin.babel import gettext from flask_admin.babel import gettext
from flask_admin.helpers import get_url from flask_admin.helpers import get_url
from flask_admin.form import RenderTemplateWidget from flask_admin.form import RenderTemplateWidget
...@@ -113,7 +113,14 @@ class XEditableWidget(object): ...@@ -113,7 +113,14 @@ class XEditableWidget(object):
kwargs['data-role'] = 'x-editable-boolean' kwargs['data-role'] = 'x-editable-boolean'
elif subfield.type == 'Select2Field': elif subfield.type == 'Select2Field':
kwargs['data-type'] = 'select' kwargs['data-type'] = 'select'
kwargs['data-source'] = dict(subfield.choices) choices = [{'value': x, 'text': y} for x, y in subfield.choices]
# prepend a blank field to choices if allow_blank = True
if getattr(subfield, 'allow_blank', False):
choices.insert(0, {'value': '__None', 'text': ''})
# json.dumps fixes issue with unicode strings not loading correctly
kwargs['data-source'] = json.dumps(choices)
elif subfield.type == 'DateField': elif subfield.type == 'DateField':
kwargs['data-type'] = 'combodate' kwargs['data-type'] = 'combodate'
kwargs['data-format'] = 'YYYY-MM-DD' kwargs['data-format'] = 'YYYY-MM-DD'
...@@ -135,20 +142,22 @@ class XEditableWidget(object): ...@@ -135,20 +142,22 @@ class XEditableWidget(object):
kwargs['data-type'] = 'number' kwargs['data-type'] = 'number'
kwargs['data-step'] = 'any' kwargs['data-step'] = 'any'
elif subfield.type in ['QuerySelectField', 'ModelSelectField']: elif subfield.type in ['QuerySelectField', 'ModelSelectField']:
# QuerySelectField and ModelSelectField are for relations
kwargs['data-type'] = 'select' kwargs['data-type'] = 'select'
choices = {} choices = []
for choice in subfield: for choice in subfield:
try: try:
choices[str(choice._value())] = str(choice.label.text) choices.append({'value': text_type(choice._value()),
'text': text_type(choice.label.text)})
except TypeError: except TypeError:
choices[str(choice._value())] = "" # unable to display text value
kwargs['data-source'] = choices choices.append({'value': text_type(choice._value()),
'text': ''})
# blank field is already included if allow_blank
kwargs['data-source'] = json.dumps(choices)
else: else:
raise Exception('Unsupported field type: %s' % (type(subfield),)) raise Exception('Unsupported field type: %s' % (type(subfield),))
# for Select2, QuerySelectField, and ModelSelectField
if getattr(subfield, 'allow_blank', False):
kwargs['data-source']['__None'] = ""
return kwargs return kwargs
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment