You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I've been using this module to add pbkdf2_sha256 support for my Django project.
However, when I upgraded Django from 3.2 to 4.2, I encountered a problem due to the deprecation of ugettext_noop in Django 4.2, which caused errors in the project. Aside from this, Passlib worked well without any other issues.
Since both this project and its dependency, passlib, invoked ugettext_noop, it seemed that simply updating the reference to ugettext_noop (actually just replacing ugettext_noop with gettext_noop) might solve the problem.
But I found that both libraries haven't been updated for a while, and just submitting a PR here wouldn't fix the issues in both libraries. So, I tried using a patching approach to address this issue and am sharing it for others who might encounter the same problem.
My Approach
You'll need to create a new app, for example, passlib_patch, and then add a patches.py file.
fromdjango.utils.translationimportgettext_noopas_# This file is a patch for django-hashers-passlib==0.4 and passlib==1.7.4 with Django 4.2. # These packages have not been updated for a long time and do not support Django 4.2.defapply_patches():
# Patch for passlib.ext.django.utilstry:
frompasslib.ext.django.utilsimport_PasslibHasherWrapper# Patched safe_summary method to use the patched _ function instead of the original ugettext_noopdefpatched_safe_summary(self, encoded):
fromcollectionsimportOrderedDictfromdjango.contrib.auth.hashersimportmask_hashhandler=self.passlib_handleritems= [
(_('algorithm'), handler.name),
]
ifhasattr(handler, "parsehash"):
kwds=handler.parsehash(encoded, sanitize=mask_hash)
forkey, valueinkwds.items():
key=self._translate_kwds.get(key, key)
items.append((_(key), value))
returnOrderedDict(items)
_PasslibHasherWrapper.safe_summary=patched_safe_summaryexceptImportErrorase:
# Handle the case where passlib is not installedprint(f"Failed to apply patch for passlib: {e}")
# Patch for hashers_passlibtry:
# Check if ugettext_noop already exists, if not, add itfromdjango.utils.translationimportugettext_noopexceptImportError:
# Dynamically add ugettext_noop pointing to gettext_noop in the django.utils.translation moduleimportdjango.utils.translationdjango.utils.translation.ugettext_noop=_# Now safely import hashers_passlib since ugettext_noop has been addedimporthashers_passlib# Since we've successfully imported hashers_passlib, this indicates the patch has taken effect,# If necessary, original state can be restored here# However, typically, if ugettext_noop is not directly used elsewhere in the Django project, restoration may not be neededexceptExceptionase:
print(f"Unexpected error while applying patch for hashers_passlib: {e}")
Then add the following code in apps.py:
fromdjango.appsimportAppConfigclassPasslibPatchConfig(AppConfig):
default_auto_field='django.db.models.BigAutoField'name='passlib_patch'defready(self):
from .patchesimportapply_patchesapply_patches()
And add in __init__.py:
from . importpatchesdefault_app_config='passlib_patch.apps.PasslibPatchConfig'
I've been using this module to add pbkdf2_sha256 support for my Django project.
However, when I upgraded Django from 3.2 to 4.2, I encountered a problem due to the deprecation of
ugettext_noop
in Django 4.2, which caused errors in the project. Aside from this, Passlib worked well without any other issues.Since both this project and its dependency, passlib, invoked
ugettext_noop
, it seemed that simply updating the reference tougettext_noop
(actually just replacingugettext_noop
withgettext_noop
) might solve the problem.But I found that both libraries haven't been updated for a while, and just submitting a PR here wouldn't fix the issues in both libraries. So, I tried using a patching approach to address this issue and am sharing it for others who might encounter the same problem.
My Approach
You'll need to create a new app, for example,
passlib_patch
, and then add apatches.py
file.Then add the following code in
apps.py
:And add in
__init__.py
:I wrote a
tests.py
file and passed the tests.I can't guarantee that this code will work for everyone, but this patch did fix the error for me.
If there are any questions, feel free to discuss.
The text was updated successfully, but these errors were encountered: