Skip to content

Commit

Permalink
Switch to requires_context for DRF
Browse files Browse the repository at this point in the history
  • Loading branch information
Viicos committed Feb 14, 2024
1 parent e7cf472 commit ef4956e
Showing 1 changed file with 10 additions and 24 deletions.
34 changes: 10 additions & 24 deletions src/objecttypes/api/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand All @@ -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)

0 comments on commit ef4956e

Please sign in to comment.