Skip to content

Commit

Permalink
feat(serializer): add read-only status
Browse files Browse the repository at this point in the history
  • Loading branch information
Xenepix committed Feb 29, 2024
1 parent 22c12b4 commit 96466eb
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions django_napse/utils/serializers/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def __new__(cls: MetaSerializer, name: str, bases: tuple, attrs: dict) -> Serial

class Serializer(BaseSerializer, Field, metaclass=MetaSerializer): # noqa
Model = None
is_read_only_serializer = False

def __init__(self, instance=None, data=None, many=False, **kwargs): # noqa
self._instance = instance
Expand Down Expand Up @@ -173,12 +174,20 @@ def validate(self, data: dict | str | int | uuid.UUID) -> any:
Please to not use this method.
"""
if self.is_read_only_serializer:
error_msg: str = "This serializer is read only."
raise ValueError(error_msg)

if isinstance(data, (str, int, uuid.UUID)):
return self.get(uuid_or_id=data)

return self.validate_data(data)

def _model_checks(self, validated_data): # noqa
if self.is_read_only_serializer:
error_msg: str = "This serializer is read only."
raise ValueError(error_msg)

if not self.Model:
error_msg: str = "Model is not defined."
raise ValueError(error_msg)
Expand Down

0 comments on commit 96466eb

Please sign in to comment.