fix: Failing invenio-admin due to lazy string#531
fix: Failing invenio-admin due to lazy string#531mesemus wants to merge 1 commit intoinveniosoftware:masterfrom
Conversation
* Flask-admin can not cope with column_labels returning lazy string * As they are used from instances, not from class, this solution changes them to method that use string-returning gettext
3f7616e to
ee7e814
Compare
|
Thanks for fixing this! Using Otherwise LGTM! |
|
You are right, if someone was inheriting from the class their code would be broken. As it is in session activity view and user identity view i guess it is unlikely that someone would do so - but ofc I can't be sure. An alternative (apart from patching flask admin) would be not to have translations for this view (it is more or less deprecated so may make sense).
|
| "sid_s": lazy_gettext("Session ID"), | ||
| } | ||
| @property | ||
| def column_labels(self): |
There was a problem hiding this comment.
this change does only affects the admin page or?
this would mean that InvenioRDM is not affected by this change?
There was a problem hiding this comment.
Yes, this is the old flask-admin implementation which is not used in InvenioRDM anymore, so RDM is not affected.
The RDM implementation of user administration is directly in invenio_app_rdm and does not use this code.
There was a problem hiding this comment.
Given that context (and since these views are more or less deprecated), maybe the simplest/least risky fix is just to drop the translations here instead of adding the property override logic?
That way we avoid breaking overrides and keep the code minimal. What do you think @mesemus @utnapischtim ?
There was a problem hiding this comment.
We still use this module in some services at CERN e.g. https://github.com/CERNDocumentServer/cds-videos . Checking quickly our code we do override the column labels dict but not for the SessionActivityView or UserIdentityView so it is fine from our side... Can we make the change backwards compatible e.g. implement a dict like class?
There was a problem hiding this comment.
"LazyTranslationDict" would not raise Exception in the Child class, but would not provide working translation as well. Let's have:
class LazyTranslationDict(dict):
def __getitem__(): str(super().__getitem__())
def values(): (str(x) for x in super().values())
def items(): ...
class Parent:
column_labels = LazyTranslationDict({"key": lazy_gettext("...")})The common way of extending is with **:
from invenio_i18n import gettext as _
class Child(Parent):
column_labels = {**Parent.column_labels, "mykey": _("incorrect translation")}If we do this, the values would get converted to plain string at the time of initialization, not at the time of request. That means that the UI would get the translation for the locale of the server, not the locale of the actual request.
With property, the same child code will fail at the time of initialization:
class Parent:
@property
def column_labels(self): ...and would force fixing the "Child" class. The fix would be to convert it to property as well, and then the translation for "mykey" would start to work.
@jma @zzacharo do you plan to have translations in your own admin classes? If you plan to migrate to RDM & new administration and do not need to have functioning translation of /admin, then I would say let's just remove the lazy_gettext from here and keep it untranslated.
There was a problem hiding this comment.
In our case, we have only english translation so it is not a problem... That said, we are also installing invenio-accounts==5.1.7 so maybe it is also ok the "breaking change" as part of the v6.0.0 major release.... given the fact that invenio-admin is somewhat deprecated for most of the instances... But indeed @jma how it is impacting you?
Description
Lazy strings are not handled correctly in
column_labelspart of flask-admin'sModelAdminclass (and seem to be ok elsewhere). This PR fixes that by converting column_labels to computed properties and performing simple gettext there.This bug was reported in #529
After the fix the UI looks like:
Checklist
Ticks in all boxes and 🟢 on all GitHub actions status checks are required to merge:
Frontend
Reminder
By using GitHub, you have already agreed to the GitHub’s Terms of Service including that: