django-oscar / django-oscar-api

RESTful JSON API for django-oscar
Other
363 stars 160 forks source link

_ in both ApiBasketMiddleWare and translations? #333

Open j2l opened 4 months ago

j2l commented 4 months ago

Hello, When I add the "oscarapi.middleware.ApiBasketMiddleWare", I get the error:


System check identified no issues (0 silenced).
Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/usr/lib/python3.10/threading.py", line 1016, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.10/threading.py", line 953, in run
    self._target(*self._args, **self._kwargs)
  File "django-oscar-api/.venv/lib/python3.10/site-packages/django/utils/autoreload.py", line 64, in wrapper
    fn(*args, **kwargs)
  File "django-oscar-api/.venv/lib/python3.10/site-packages/django/core/management/commands/runserver.py", line 139, in inner_run
    handler = self.get_handler(*args, **options)
  File "django-oscar-api/.venv/lib/python3.10/site-packages/django/contrib/staticfiles/management/commands/runserver.py", line 31, in get_handler
    handler = super().get_handler(*args, **options)
  File "django-oscar-api/.venv/lib/python3.10/site-packages/django/core/management/commands/runserver.py", line 78, in get_handler
    return get_internal_wsgi_application()
  File "django-oscar-api/.venv/lib/python3.10/site-packages/django/core/servers/basehttp.py", line 45, in get_internal_wsgi_application
    return get_wsgi_application()
  File "django-oscar-api/.venv/lib/python3.10/site-packages/django/core/wsgi.py", line 13, in get_wsgi_application
    return WSGIHandler()
  File "django-oscar-api/.venv/lib/python3.10/site-packages/django/core/handlers/wsgi.py", line 118, in __init__
    self.load_middleware()
  File "django-oscar-api/.venv/lib/python3.10/site-packages/django/core/handlers/base.py", line 40, in load_middleware
    middleware = import_string(middleware_path)
  File "django-oscar-api/.venv/lib/python3.10/site-packages/django/utils/module_loading.py", line 30, in import_string
    return cached_import(module_path, class_name)
  File "django-oscar-api/.venv/lib/python3.10/site-packages/django/utils/module_loading.py", line 15, in cached_import
    module = import_module(module_path)
  File "/usr/lib/python3.10/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "django-oscar-api/.venv/lib/python3.10/site-packages/oscarapi/middleware.py", line 8, in <module>
    from django.utils.translation import ugettext as _
ImportError: cannot import name 'ugettext' from 'django.utils.translation' (django-oscar-api/.venv/lib/python3.10/site-packages/django/utils/translation/__init__.py)

I'm using the sandbox with included settings.py:

from django.utils.translation import gettext_lazy as _
...
USE_I18N = True
LANGUAGE_CODE = "en-us"
LANGUAGES = (
    ("en-us", _("English")),
    ("fr", _("French")),
)

As soon as I comment the middleware, it works again.

For now, I replace _ with i_: from django.utils.translation import gettext_lazy as i_ and subsequent calls in languages ("en-us", i_("English")),.... It seems to work.

I'd love to know if there's a better fix.

Additionally, may I ask for an example of "fill your basket with the API and use plain Oscar views for checkout" as stated in the doc? https://django-oscar-api.readthedocs.io/en/stable/topics/outofthebox.html?highlight=basket#middleware-and-mixed-usage In my case, the API is called by a browser on a different port.

Thank you!

JulienPalard commented 1 month ago

This has been fixed in 1e10a5282d2bbad4265f1a0be783612c99b0b7b4 but not release yet.

Is it release time?