django-simple-history
django-simple-history copied to clipboard
Fix: Error in history view of object with forward slash in primary key
- Used quote method to convert a primary key value into a string that can be used in a URL for a history view.
- Used unquote method to convert quoted primary key back into its original form for history form view.
Description
This error occurs while rendering object URL with primary key in Change History view. This line causes the issue.
I was able to fix this issue using admin_urlquote filter on object.pk. However, it breaks history form view as it receives encoded value in object id and object does not exist with this value. To fix this, I used unquote method to to convert quoted primary key back into its original form for history form view.
I also used quote method in [revert_url method] (https://github.com/jazzband/django-simple-history/blob/master/simple_history/models.py#L481) to encode objects primary key for constructing URL for history form view.
Related Issue
#1295
Motivation and Context
History view of the object with "/" in primary key is broken in admin portal.
How Has This Been Tested?
- I wrote unit tests that render a history view for an object with "/" in the primary key.
- After making these changes some tests related to history form view were broken, because these tests was passing integer to history_form_view method and unquote method does not work with integers. I fixed those tests, by converting these ids to strings for assertion. In history_form_view method, I explicitly converted object id to string for unquote method.
- I manually tested these changes via the admin portal after importing the django-simple-history package in a sample django application.
- These changes affects areas of code that uses simple history urls in admin.
Screenshots (if appropriate):
Error Before this fix
Types of changes
- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)
Checklist:
- [x] I have run the
pre-commit run
command to format and lint. - [ ] My change requires a change to the documentation.
- [ ] I have updated the documentation accordingly.
- [x] I have read the CONTRIBUTING document.
- [x] I have added tests to cover my changes.
- [x] I have added my name and/or github handle to
AUTHORS.rst
- [x] I have added my change to
CHANGES.rst
- [x] All new and existing tests passed.
Codecov Report
All modified and coverable lines are covered by tests :white_check_mark:
Comparison is base (
f48b53d
) 96.87% compared to head (e8b2887
) 96.87%.
Additional details and impacted files
@@ Coverage Diff @@
## master #1297 +/- ##
=======================================
Coverage 96.87% 96.87%
=======================================
Files 23 23
Lines 1278 1280 +2
Branches 211 211
=======================================
+ Hits 1238 1240 +2
Misses 21 21
Partials 19 19
:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.