Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow overriding UidAndTokenSerializer's behaviour #692

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Ou7law007
Copy link
Contributor

Currently, UidAndTokenSerializer validates both fields uid and token at once and there's no way to reuse its code, because it doesn't allow overriding the validation of a single field without the other (either uid AND token or neither).

This change splits the validation into the respective fields to allow reusage. In my case, I needed to reuse UidAndTokenSerializer with a different token logic but with the same uid logic.

c74d590
@konradhalas left a comment 3 years ago that I do not quite understand:
# uid validation have to be here, because validate_<field_name>
# doesn't work with modelserializer

But UidAndTokenSerializer does not inherit from modelserializer!

For me the field validation works fine. It's also the same thing in the example provided by rest_framework, see: https://www.django-rest-framework.org/api-guide/serializers/#field-level-validation

I also left a comment at validate_token regarding the self.user. The reason I did not use any of the alternatives I mentioned is that if it broke in the future, then rest_framework must have had major changes that shouldn't go unspotted in that specific method.

Also, is it possible to update the pip repository? https://pypi.org/project/djoser/
Released: Oct 30, 2020
Plenty of updates have took place since then.


If this doesn't get accepted and someone is looking for a workaround, to "reuse" UidAndTokenSerializer, you just need to provide your own def validate_token and def validate_uid and you'd have to copy the code of one of them (the one you don't want to override) from UidAndTokenSerializer for one field and provide your own code for the second one (you one you want to override).

You also need to override 'def validate' to do nothing:

class My_UidAndTokenSerializer(UidAndTokenSerializer):

def validate_uid(self, value):
    # either your own code or copy the code from 'UidAndTokenSerializer'
    return value

def validate_token(self, value):
    # either your own code or copy the code from 'UidAndTokenSerializer'
    return value

def validate(self, attrs):
    return attrs

At this point, it's not even called 'reuse', so... 🍡

Ou7law007 and others added 3 commits September 1, 2022 13:30
Currently, UidAndTokenSerializer validates both fields 'uid' and 'token' at once and there's no way to reuse its code, because it doesn't allow overriding the validation of a single field without the other (either 'uid' AND 'token' or neither).

This change splits the validation into the respective fields to allow reusage. In my case, I needed to reuse 'UidAndTokenSerializer' with a different 'token' logic but with the same 'uid' logic.

sunscrapers@c74d590
@konradhalas left a comment 3 years ago that I do not quite understand:
        # uid validation have to be here, because validate_<field_name>
        # doesn't work with modelserializer

But UidAndTokenSerializer does not inherit from modelserializer!

For me the field validation works fine. It's also the same thing in the example provided by rest_framework, see: https://www.django-rest-framework.org/api-guide/serializers/#field-level-validation

I also left a comment at 'validate_token' regarding the 'self.user'

Also, is it possible to update the pip repository? https://pypi.org/project/djoser/
Released: Oct 30, 2020
Plenty of updates have took place since then.

---------------------

If this doesn't get accepted and someone is looking for a workaround, to "reuse" 'UidAndTokenSerializer', you just need to provide your own 'def validate_token' and 'def validate_uid' and you'd have to copy the code of one of them (the one you don't want to override) from UidAndTokenSerializer for one field and provide your own code for the second one (you one you want to override).

You also need to override 'def validate' to do nothing:

class My_UidAndTokenSerializer(UidAndTokenSerializer):

    def validate_uid(self, value):
        # either your own code or copy the code from 'UidAndTokenSerializer'
        return value

    def validate_token(self, value):
        # either your own code or copy the code from 'UidAndTokenSerializer'
        return value

    def validate(self, attrs):
        return attrs

At this point, it's not even called 'reuse', so... 🍡
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants