rcrowther / django-streamfield-w

A StreamField for Django. Ported from Wagtail, with new DjangoAdmin-like API.
BSD 3-Clause "New" or "Revised" License
27 stars 4 forks source link

Cannot import django.utils.encoding.punycode in Django 2.2 #1

Closed denangeles closed 4 years ago

denangeles commented 4 years ago

Hello,

First of all, thanks for this port, im trying to integrate it in my projects!

My project uses django 2.2.x and ive encountered the following:

Exception in thread django-main-thread:
Traceback (most recent call last):
  File "/usr/local/Cellar/python@3.7/3.7.8_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 926, in _bootstrap_inner
    self.run()
  File "/usr/local/Cellar/python@3.7/3.7.8_1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "lib/python3.7/site-packages/django/utils/autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "lib/python3.7/site-packages/django/core/management/commands/runserver.py", line 109, in inner_run
    autoreload.raise_last_exception()
  File "lib/python3.7/site-packages/django/utils/autoreload.py", line 77, in raise_last_exception
    raise _exception[1]
  File "lib/python3.7/site-packages/django/core/management/__init__.py", line 337, in execute
    autoreload.check_errors(django.setup)()
  File "lib/python3.7/site-packages/django/utils/autoreload.py", line 54, in wrapper
    fn(*args, **kwargs)
  File "lib/python3.7/site-packages/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "lib/python3.7/site-packages/django/apps/registry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File "lib/python3.7/site-packages/django/apps/config.py", line 90, in create
    module = import_module(entry)
  File "lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "lib/python3.7/site-packages/streamfield/__init__.py", line 4, in <module>
    from streamfield.model_fields import StreamField, ListField, DefinitionListField
  File "lib/python3.7/site-packages/streamfield/model_fields.py", line 5, in <module>
    from streamfield.blocks import (
  File "lib/python3.7/site-packages/streamfield/blocks/__init__.py", line 3, in <module>
    from .field_block import *  # NOQA
  File "lib/python3.7/site-packages/streamfield/blocks/field_block.py", line 17, in <module>
    from streamfield.form_fields import RelURLField
  File "lib/python3.7/site-packages/streamfield/form_fields.py", line 7, in <module>
    from streamfield.validators import RelURLValidator
  File "lib/python3.7/site-packages/streamfield/validators.py", line 7, in <module>
    from django.utils.encoding import punycode
ImportError: cannot import name 'punycode' from 'django.utils.encoding' (lib/python3.7/site-packages/django/utils/encoding.py)

Digging in the django source code, i can see punycode is in django 3.0, but not 2.2.x

Is there anyway to maintain compatibility with older django versions?

rcrowther commented 4 years ago

First, my apology for slow response. I have work, and personal issues.

The app was ported/built in Python 3.8.2 and Django 3.1.1, in a homemade virtual environment.

I do not know about Punycode, but assume the general area is https://tools.ietf.org/html/rfc3490.html.

Source code from Django 3.1.1 tells me punycode method/function is this simple,

def punycode(domain): """Return the Punycode of the given domain if it's non-ASCII.""" return domain.encode('idna').decode('ascii')

';idna' is a custom Python codec, see bottom of this page https://docs.python.org/3/library/codecs.html

Where that places your project I am not sure. The shift from V2 to V3 of Python is large. It includes Unicode, which is relevant to the error you report. You could try implement a Punycode function, though how to do that within Python 2, if that is what you are using, I do not know. The reverse approach would be to eliminate Punycode from the app code. Python can do conditional imports, but how much would you/we need to change?

I'm leaving this issue open for a time, to allow time for respose.

denangeles commented 4 years ago

Thats OK, for now i have forked this repo and implemented my own changes, literally that code you put above.

Appreciate your response, and i might submit a PR soon!