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 FileFields in deeply nested inlines #127

Merged
merged 3 commits into from
Mar 24, 2019
Merged

Conversation

btknu
Copy link
Contributor

@btknu btknu commented Mar 10, 2019

Fixes #111 .

Some highlights:

  1. Django 2.1+ implemented has_file_field, this value no longer is hardcoded to True, but depends on is_multipart(). Django relies on has_file_field to set form's enctype to multipart.
    With false negatives in has_file_field, FileField changes are not saved. This commit adds support for nested_formsets to is_multipart().

  2. Django 2.1 and 2.2 use Form's is_multipart to calculate has_file_field. This could be improved, there's already a dedicated FormSet's is_multipart which handles a more general case. Django 3 should already use FormSet (see latest master). This PR includes code to handle both scenarios.

  3. The variable name _has_multipart_nested_formset feels a bit strange; I picked it because it's similar to has_file_field and is_multipart, I'm open to other suggestions and I can change it if needed.

Updated after forced push:

  1. In the early version of this PR the tests used to be in the tests.two_deep.tests.InlineAdminTestCaseMixin, this way they were run for both Stacked and Tabular inlines. I've moved them to TestStackedInlineAdmin only; TestTabularInlineAdmin test cases were failing due to elements becoming wider and overlapping UI buttons. However, the nesting style doesn't seem to matter in this case.

  2. I've added a minor cleanup to the DragAndDropAction, more details in commit messages.

@codecov
Copy link

codecov bot commented Mar 10, 2019

Codecov Report

Merging #127 into master will increase coverage by 0.08%.
The diff coverage is 95.45%.

@@            Coverage Diff             @@
##           master     #127      +/-   ##
==========================================
+ Coverage   78.48%   78.56%   +0.08%     
==========================================
  Files          17       17              
  Lines        2031     2053      +22     
==========================================
+ Hits         1594     1613      +19     
- Misses        437      440       +3
Flag Coverage Δ
#javascript 72.34% <ø> (-0.15%) ⬇️
#python 92% <95.45%> (+0.12%) ⬆️
Impacted Files Coverage Δ
nested_admin/nested.py 99.46% <100%> (+0.02%) ⬆️
nested_admin/formsets.py 91.74% <92.3%> (+0.03%) ⬆️
...ted_admin/src/nested-admin/jquery.djangoformset.js 87.28% <0%> (-0.35%) ⬇️
...admin/src/nested-admin/jquery.ui.nestedsortable.js 48.36% <0%> (-0.33%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2ad1143...3c60621. Read the comment docs.

@codecov
Copy link

codecov bot commented Mar 10, 2019

Codecov Report

Merging #127 into master will increase coverage by 0.08%.
The diff coverage is 88.88%.

@@            Coverage Diff             @@
##           master     #127      +/-   ##
==========================================
+ Coverage   78.48%   78.57%   +0.08%     
==========================================
  Files          17       17              
  Lines        2031     2058      +27     
==========================================
+ Hits         1594     1617      +23     
- Misses        437      441       +4
Flag Coverage Δ
#javascript 72.41% <ø> (-0.08%) ⬇️
#python 91.75% <88.88%> (-0.13%) ⬇️
Impacted Files Coverage Δ
nested_admin/formsets.py 91.36% <88.88%> (-0.35%) ⬇️
...admin/src/nested-admin/jquery.ui.nestedsortable.js 48.36% <0%> (-0.33%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2ad1143...1a0dc3c. Read the comment docs.

btknu added 3 commits March 12, 2019 00:47
The 'move_by_offset' method has been used in a single instance, it was
called with parameters: (0, -1), but the resulting move was: (0, -16).
This commit inlines 'move_by_offset' and deletes the method.
Updates offset of '_match_helper_with_target' from (0, -16) to (0, -15).
Without this, adding a new element to NestedStackedInline would fail
test suite.
Django 2.1+ implemented 'has_file_field', this value no longer is
hardcoded to 'True', but depends on 'is_multipart()'.

Django relies on 'has_file_field' to set form's enctype to multipart.
With false negatives in 'has_file_field', FileField changes are not
saved.

This commit adds support for nested_formsets to 'is_multipart()'.
@fdintino
Copy link
Member

This is really phenomenal!

I hope you'll excuse the fact that I made a few tweaks to your patch. In particular, I saw your commit for django/django@7c3a8b9 and it occurred to me that we could include the check for nested formsets in a custom is_multipart method on the formset, and then just patch on django 2.1 and 2.2 so that form.is_multipart returns the formset's is_multipart. This way, in the distant future we can drop the patches and have a clean, standard way of integrating django-nested-admin with is_multipart

@fdintino
Copy link
Member

fdintino commented Mar 12, 2019

Yours is the first pull request to ever include tests, and I can't express how much I appreciate that you spent the time writing that.

@fdintino fdintino merged commit ead0770 into theatlantic:master Mar 24, 2019
@alito
Copy link

alito commented Mar 24, 2019

Excellent. Thank you @btknu very much for that.

@btknu
Copy link
Contributor Author

btknu commented Mar 25, 2019

I'm happy to see that it was helpful, thanks for the review, tweaks and the merge!

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.

3 participants