Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

search_placeholder fails with TypeError when using lazy_gettext for column_labels #1790

Open
fwiersENO opened this issue Jan 14, 2019 · 5 comments

Comments

@fwiersENO
Copy link

For translations I use lazy_gettext in column_labels in a ModelView. With version 1.5.3 this results in an error (was working for version 1.5.2):

TypeError: sequence item 0: expected str instance, _LazyString found
at: lib\site-packages\flask_admin\contrib\sqla\view.py", line 608, in search_placeholder
at: return 'Search: %s' % u', '.join(placeholders)

Maybe I am doing translations for column-names wrong and there is another (better) way to provide translations for column-names?

I monkey-patched the method locally to use the the previous statements (i.e. just return gettext('Search')) after I also tried to overwrite the method (which worked but the result is not to my liking, see related issue #1728 ):

def search_placeholder(self):
    """
    Bug in version 1.5.3: the join on placeholders fails with _LazyString
    """
    if not self.column_searchable_list:
        return 'Search'

    placeholders = []

    for searchable in self.column_searchable_list:
        if isinstance(searchable, InstrumentedAttribute):
            placeholders.append(
                str(self.column_labels.get(searchable.key, searchable.key)))
        else:
            placeholders.append(
                str(self.column_labels.get(searchable, searchable)))

    return gettext('Search') + ': %s' % u', '.join(placeholders)
@mrjoes
Copy link
Contributor

mrjoes commented Jan 22, 2019

I reviewed and merged #1728, see if the latest master fixes the issue for you.

@fwiersENO
Copy link
Author

I have not tried the latest master but I do no think it will work since the cause of the exception is not resolved. Below a small test-program I used to reproduce the issue:

from flask_babelex import lazy_gettext

column_searchable_list = ['first_name', 'last_name']
column_labels = {
	'first_name': lazy_gettext('First name'),
	'last_name': lazy_gettext('Last name')
}

placeholders = []
for searchable in column_searchable_list:
	placeholders.append(column_labels.get(searchable, searchable))
try:
	search_placeholder = u', '.join(placeholders)
	print("Yes: " + search_placeholder)
except Exception:
	print("No")

placeholders = []
for searchable in column_searchable_list:
	placeholders.append(str(column_labels.get(searchable, searchable)))

try:
	search_placeholder = u', '.join(placeholders)
	print("Yes: " + search_placeholder)
except Exception:
	print("No")

@lnlrbr
Copy link

lnlrbr commented Feb 4, 2019

Last master version installed and this bug is still present.
For some Views, removing the column name in a related parent-child table do the trick

# break in 1.5.3, fine in 1.5.2
column_list = ['parent_table.child_table.name']
column_labels = {'parent_table.child_table.name': lazy_gettext('Name')}

fix:

column_list = ['parent_table.child_table']
column_labels = {'parent_table.child_table': lazy_gettext('Name')}

For others views and as mentioned by @fwiersVANAD, a simple

def search_placeholder(self):
    return gettext('Search')

is a simple and clean solution

@selurvedu
Copy link

I can confirm that this bug is not fixed on latest release. I was able to work around it by using hints from previous comments and by looking at the current implementation in flask-admin. Just add this to your ModelView.

    def search_placeholder(self):
        if self.column_searchable_list:
            return ", ".join(
                str(self.column_labels.get(col, col))
                for col in self.column_searchable_list
            )

@macnewbold
Copy link

I was still running into this problem on current versions, and created a PR with a fix. If we evaluate the Lazy String to a string, then we no longer get the error, and we get properly translated column labels.
#2383

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

5 participants