-
Notifications
You must be signed in to change notification settings - Fork 5
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
Add a 'prefix' feature along with documentation and tests for this fe… #1
Conversation
…ature and DRF serialization.
Thank you for this PR and contribution, Jack! I had a look at your proposed changes and would like to merge this after some points for discussion:
Side note: this PR sparked an interesting thought in my head: It would be possible to use regular Sqids for all possible object instances while maintaining full separation: by also encoding the content type ID as the first number in the sqid! I can see this as a possible addition to this project. def every_detail(r, sqid):
content_type, object_id = sqids_instance.decode(sqid)
...
urlpatterns = path("detail/<sqid>/", every_detail) |
@julianwachholz thanks for your input. I just submitted a separate PR for the DRF portion. I will rework this PR as requested, and implement some tests for urls once I resolve that issue. As for use-case, I really like the prefixes as a simple identifier. The main application I work on provides a suite of tools for utilities districts. Each service request starts with "SR-" Each Invoice starts with "I-", Each Serial Number starts with "S-". The prefix just provides some quick context about what the identifier we're looking at belongs to. I'm not sure if anyone else will find it useful, but I really like being able to provide a short pseudo-random value with a bit of added context for each record my customers see. That content-type idea is pretty cool! |
Please let me know if this meets the intent. I added a set of tests for urls functionality with and without prefix. Here's the current behavior - let me know if it still needs rework:
Note: I did leave the DRF dev requirement in place, since the tests in this PR check that prefixes serialize correctly, but I moved all DRF-only tests to #2. Edit: If the user is using sqids without a prefix, everything works 100% like normal. A correct sqid returns the view, an incorrect sqid returns 404. In the case of using sqids with prefix, any incorrect sqid currently returns IncorrectPrefixError. There are 3 ways I can think of the deal with this: 1: Modify
|
I believe option 1 to be the least surprising and most in line with how Django's path converters work. |
That makes sense. Updated the PR to behave more in-line with the expected results, as requested.
|
Thank you for your contribution, @jacklinke! |
django-sqids builds off of django-hashids, but one of the nice features of its sister package, django-hashid-field was the ability to provide a per-field prefix that was prepended to the resulting Hashids for that model field.
This pull request adds such a feature to django-sqids - an optional prefix that can be provided as an argument in the model field's implementation. For instance:
will result in model instances that have
sqid
values like "P-J3TAC2V" or "P-M2A87BX", andother_sqid
values like "user_3N9CAW12V" and "user_J90MGN4RD".Included in the pull request are:
IncorrectPrefixError
to help identify when the prefix is used incorrectlyPlease let me know if you find this useful. I hope you will consider merging it.