theriverman / django-minio-backend

Minio Backend for Django
https://pypi.org/project/django-minio-backend/
MIT License
112 stars 22 forks source link

TypeError: can't compare offset-naive and offset-aware datetimes when running collectstatic #34

Closed Uninen closed 2 years ago

Uninen commented 2 years ago

I think this might be user error but I'm having hard time figuring out how to debug this.

I have latest django-minio (3.2.1) running on Django 3.2 and connected to minio (version 2021-11-09T03:21:45Z) that is running behind Traefik proxy.

The connection, bucket creation and initial collectstatic works ok, but when I run collectstatic again I get following error:


Traceback (most recent call last):
  File "/project/manage.py", line 25, in <module>
    main()
  File "/project/manage.py", line 22, in main
    execute_from_command_line(sys.argv)
  File "/project/venv39/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
    utility.execute()
  File "/project/venv39/lib/python3.9/site-packages/django/core/management/__init__.py", line 413, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/project/venv39/lib/python3.9/site-packages/django/core/management/base.py", line 354, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/project/venv39/lib/python3.9/site-packages/django/core/management/base.py", line 398, in execute
    output = self.handle(*args, **options)
  File "/project/venv39/lib/python3.9/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 187, in handle
    collected = self.collect()
  File "/project/venv39/lib/python3.9/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 114, in collect
    handler(path, prefixed_path, storage)
  File "/project/venv39/lib/python3.9/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 338, in copy_file
    if not self.delete_file(path, prefixed_path, source_storage):
  File "/project/venv39/lib/python3.9/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 278, in delete_file
    target_last_modified.replace(microsecond=0) >=
TypeError: can't compare offset-naive and offset-aware datetimes

Using following relevant settings:

MINIO_PUBLIC_BUCKETS = [
    "my-dev-staticfiles",
]
MINIO_STATIC_FILES_BUCKET = "my-dev-staticfiles"
STATICFILES_STORAGE = "django_minio_backend.models.MinioBackendStatic"

When I delete the uploaded staticfiles + the bucket and re-run collectatic the command works again. (But only for the first time.)

theriverman commented 2 years ago

hi, @Uninen

I could not reproduce your case, but I think it's got to do something with the following parameters in settings.py:

TIME_ZONE = 'UTC'
USE_TZ = True

Could you tell me what values are you using here, please?

While trying to figure out root cause I've fixed another bug in the Storage backend's .stat() implementation so please upgrade to 3.3.0:

pip install django-minio-backend==3.3.0
Uninen commented 2 years ago

I'm afraid I don't have access to the environment anymore bc we needed to ditch Minio altogether, but I think you're right, the issue might have been in the timezone settings. The settings at the time were:

TIME_ZONE = "Europe/Helsinki"
USE_TZ = False

Feel free to close this issue if you can't easily repro it as it doesn't affect our project anymore and I feel bad as well for not being able to help debugging!

Thank You for your work 🙏

theriverman commented 2 years ago

It was indeed related to USE_TZ. The official Minio Client always returns TZ-aware datetime objects and if you turn off TZ in Django, then you run into this problem, b/c TZ-aware and TZ-unaware objects cannot be compared.

I've pushed v3.3.2 which resolves this issue. Thanks, @Uninen !