django-cms / django-filer

File and Image Management Application for django
https://django-filer.readthedocs.io/
Other
1.73k stars 574 forks source link

fix: initialize dropzoneSelector inside inline formsets in a Django 4.1 compatible way #1431

Closed mbi closed 9 months ago

mbi commented 9 months ago

Description

Django 4.1 changed the JavaScript event type being fired when adding a new formset from a jQuery to a vanilla JavaScript event. https://docs.djangoproject.com/en/4.1/ref/contrib/admin/javascript/

This broke the drag-and-drop feature when adding dynamic inlines containing Filer fields in e.g. the Django admin, because the dropzone failed to initialize.

This patch checks for Django 4.1 and initializes the dropzone on newly added inline rows.

Related resources

This fixes #1400.

Checklist

codecov[bot] commented 9 months ago

Codecov Report

All modified lines are covered by tests :white_check_mark:

Comparison is base (421c86b) 76.43% compared to head (bfa7045) 76.43%.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #1431 +/- ## ======================================= Coverage 76.43% 76.43% ======================================= Files 75 75 Lines 3514 3514 Branches 562 562 ======================================= Hits 2686 2686 Misses 666 666 Partials 162 162 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

fsbraun commented 9 months ago

@mbi Is there any way to test this? How did you test?

mbi commented 9 months ago

@fsbraun I tested manually, i.e. with an Django admin inline containing a FilerImageField.

Without the patch, when you add a new row and drag a file onto the field, it doesn't react. With the PR applied, the field will react correctly and upload the file when dropped.

fsbraun commented 9 months ago

Did you check it runs on Django 3.2? Probably we can drop the support for Django < 1.9, too. I think the if statement in line 165 and the else block in lines 190+ can go away:

https://github.com/django-cms/django-filer/blob/797fd2157f719c7ab93da92bc0f8a712bfe111aa/filer/static/filer/js/addons/dropzone.init.js#L163-L165

https://github.com/django-cms/django-filer/blob/797fd2157f719c7ab93da92bc0f8a712bfe111aa/filer/static/filer/js/addons/dropzone.init.js#L190-L195

mbi commented 9 months ago

@fsbraun I just tested this on a 3.2 project, it works fine (as it was before: the new code line only triggers on 4.1+) I also remove the two blocks supporting Django < 1.9

fsbraun commented 9 months ago

Thanks, @mbi ! Great work!