Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Oct 2, 2023

Conversation

mbi
Copy link
Contributor

@mbi mbi commented Oct 2, 2023

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

  • I have opened this pull request against master
  • I have added or modified the tests when changing logic
  • I have followed the conventional commits guidelines to add meaningful information into the changelog
  • I have read the contribution guidelines and I have joined #workgroup-pr-review on
    Slack to find a “pr review buddy” who is going to review my pull request.

@codecov
Copy link

codecov bot commented Oct 2, 2023

Codecov Report

All modified lines are covered by tests ✅

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

Additional details and impacted files
@@           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           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@fsbraun
Copy link
Sponsor Member

fsbraun commented Oct 2, 2023

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

@mbi
Copy link
Contributor Author

mbi commented Oct 2, 2023

@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
Copy link
Sponsor Member

fsbraun commented Oct 2, 2023

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:

// window.__admin_utc_offset__ is used as canary to detect Django 1.8
// There is no way to feature detect the new behavior implemented in Django 1.9
if (!window.__admin_utc_offset__) {

} else {
$('.add-row a').on('click', function () {
var dropzones = $(dropzoneSelector);
dropzones.each(createDropzone);
});
}

@mbi
Copy link
Contributor Author

mbi commented Oct 2, 2023

@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

Copy link
Sponsor Member

@marksweb marksweb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds straightforward enough and it's good to see the explanation of what's happening.

@fsbraun I'm always in favour of dropping support for unsupported Django. If we maintain support we're effectively encouraging people to use unsupported versions.

@fsbraun fsbraun merged commit 68c3a5c into django-cms:master Oct 2, 2023
25 of 26 checks passed
@fsbraun
Copy link
Sponsor Member

fsbraun commented Oct 2, 2023

Thanks, @mbi ! Great work!

@mbi mbi mentioned this pull request Nov 10, 2023
4 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

FilerImageField drag'n'drop not working on inlines
3 participants