From ef4956e6d588f55b6869063831bf83607d24391c Mon Sep 17 00:00:00 2001 From: Viicos <65306057+Viicos@users.noreply.github.com> Date: Wed, 14 Feb 2024 11:35:52 +0100 Subject: [PATCH] Switch to `requires_context` for DRF --- src/objecttypes/api/validators.py | 34 +++++++++---------------------- 1 file changed, 10 insertions(+), 24 deletions(-) diff --git a/src/objecttypes/api/validators.py b/src/objecttypes/api/validators.py index 23ede440..7bd692be 100644 --- a/src/objecttypes/api/validators.py +++ b/src/objecttypes/api/validators.py @@ -10,21 +10,14 @@ class VersionUpdateValidator: message = _("Only draft versions can be changed") code = "non-draft-version-update" + requires_context = True - def set_context(self, serializer): - """ - This hook is called by the serializer instance, - prior to the validation call being made. - """ - # Determine the existing instance, if this is an update operation. - self.instance = getattr(serializer, "instance", None) - self.request = serializer.context["request"] - - def __call__(self, attrs): - if not self.instance: + def __call__(self, attrs, serializer): + instance = getattr(serializer, "instance", None) + if not instance: return - if self.instance.status != ObjectVersionStatus.draft: + if instance.status != ObjectVersionStatus.draft: raise serializers.ValidationError(self.message, code=self.code) @@ -45,22 +38,15 @@ class IsImmutableValidator: message = _("This field can't be changed") code = "immutable-field" + requires_context = True - def set_context(self, serializer_field): - """ - This hook is called by the serializer instance, - prior to the validation call being made. - """ - # Determine the existing instance, if this is an update operation. - self.serializer_field = serializer_field - self.instance = getattr(serializer_field.parent, "instance", None) - - def __call__(self, new_value): + def __call__(self, new_value, serializer_field): # no instance -> it's not an update - if not self.instance: + instance = getattr(serializer_field.parent, "instance", None) + if not instance: return - current_value = getattr(self.instance, self.serializer_field.source) + current_value = getattr(instance, serializer_field.source) if new_value != current_value: raise serializers.ValidationError(self.message, code=self.code)