Raise a ValidationError for incorrect related slug when importing resources#631
Raise a ValidationError for incorrect related slug when importing resources#631AdrianDAlessandro merged 4 commits intomainfrom
Conversation
d36dc6a to
03095e6
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
4dc2975 to
141f9d0
Compare
dalonsoa
left a comment
There was a problem hiding this comment.
The functionality looks OK and makes sense, but importing a function as an underscore really does not look good.
|
|
||
| from django.core.exceptions import ObjectDoesNotExist, ValidationError | ||
| from django.utils.encoding import force_str | ||
| from django.utils.translation import gettext_lazy as _ |
There was a problem hiding this comment.
Importing something as an underscore is pretty odd. It can be confusing and it is not informative, when used. Just import and use it normally, which is way cleaner.
There was a problem hiding this comment.
It's a standard Django pattern for translation. I don't love it either, but it's convention, so I'm happy to go with it https://docs.djangoproject.com/en/6.0/topics/i18n/translation/#internationalization-in-python-code
There was a problem hiding this comment.
Ok. It looks horrible, but if that's the standard, so be it...
| try: | ||
| return super().clean(value, row=row, **kwargs) | ||
| except ObjectDoesNotExist as e: | ||
| raise ValidationError({self.column_name: _(force_str(e))}) |
There was a problem hiding this comment.
Here. I barely noticed the underscore was being used, which is not good.
There was a problem hiding this comment.
That's kind of the point, I think. I believe it's intended as an essentially invisible translation. If the website is in English, then the function does nothing, otherwise it will translate it.
| ) | ||
| raise ValidationError( | ||
| { | ||
| self.column_name: _( |
Description
This PR prevents incorrect slugs from being ignored when importing to the DB using the Resource classes in
io_resources.pyIt achieves this by adding a check to the
cleanmethod of the ForeignKey and M2M Widget classes. If any of the slugs provided in the import data do not exist, it raises a ValidationError. This error provides a dictionary with the offending field as keys, which renders nicely in the Admin backend.M2M Example with the following import data
ForeignKey Example with the following import data
Fixes #630
Type of change
Key checklist
python -m pytest)mkdocs serve)pre-commit run --all-files)Further checks