-
Notifications
You must be signed in to change notification settings - Fork 0
Rework FolderSerializer to use native DRF validation #130
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we expand the scope of this PR to deleting FullCleanModelSerializer
altogether? Based on this PR and our discussion, it seems like we shouldn't be using it anywhere. I think the only other changes needed would be in rest/file.py.
FullCleanModelSerializer does not work properly with internally-set fields like "tree", since the value is not available at the time of initial validation. Currently, it only works because "tree" has "editable=False", so the Model-layer validator skips the field. This refactor also has the result of effectively removing the default of "parent=None" when creating a folder. This is probably a net benefit, as it forces API clients to be explicit when creating a new root folder.
0f2167b
to
1c29237
Compare
fields=['name'], | ||
message='Root folders must have a unique name.', | ||
), | ||
# folder_max_depth and unique_root_folder_per_tree are internal sanity constraints, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, folder_max_depth
is something we might want to check at the REST layer (rather than allowing the database to do it). I'm not sure how to do this, so I'll probably file a bug for a future fix if nobody else has suggestions.
|
||
def get_access(self, folder: Folder) -> Dict[str, bool]: | ||
return folder.tree.get_access(self.context.get('user')) | ||
|
||
|
||
class FolderUpdateSerializer(FolderSerializer): | ||
class Meta(FolderSerializer.Meta): | ||
fields = ['id', 'name', 'description'] | ||
read_only_fields = ['parent'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Specifying fields
had the result of also limiting the output of what this serializer produced in the response to a PUT
/PATCH
request. What we really want to do is ensure that 'parent' cannot be changed in an update request, which this now does.
@@ -51,7 +95,7 @@ def test_folder_rest_retrieve(admin_api_client, folder): | |||
|
|||
@pytest.mark.django_db | |||
def test_folder_rest_update(admin_api_client, folder): | |||
resp = admin_api_client.put( | |||
resp = admin_api_client.patch( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might want to outright disallow PUT
requests at some point, but since I think we typically expect clients to use PATCH
requests, it's better for this test to use that.
@zachmullen I intend to submit a second PR to remove |
Tests are failing due to kitware-resonant/django-composed-configuration#114 . They'll pass as soon as that's released. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After this change, the error response has the form:
{
"non_field_errors": [
"..."
]
}
I think we should associate these UniqueTogetherValidators with the name
field, as that will be more consumable downstream. (Is that difficult to do?)
For the case of sibling files, this is easy, as we just need to explicitly In the cases where |
Let's open that ticket for validating max depth. |
Pushed 2 more minor commits. |
As a note, following the advice to |
FullCleanModelSerializer
does not work properly with internally-set fields liketree
, since the value is not available at the time of initial validation. Currently, it only works becausetree
haseditable=False
, so the Model-layer validator skips the field.See this comment for more information on the problem.
This refactor also has the result of effectively removing the default of
parent=None
when creating a folder. This is probably a net benefit, as it forces API clients to be explicit when creating a new root folder.Also: