django-rest-authemail icon indicating copy to clipboard operation
django-rest-authemail copied to clipboard

Add the request to the context for the serializer

Open pjrobertson opened this issue 3 years ago • 1 comments

This is useful if subclassing the views and using custom serializers.

Use case: User Signups is restricted to administrator users only. I set up a custom SignupSerializer that needs to validate the email address to make sure the domain matches the current user - the current user needs to be found from request.user

E.g.:

def email_domain(email):
    # return email domain of 'email' - example.com

class EmailSignupSerializer(SignupSerializer):
    def validate_email(self, value):
        if email_domain(value) != email_domain(self.context['request'].user.email):
                raise serializers.ValidationError("You may only add users with from the same email domain as yours')
        return value

pjrobertson avatar Jul 24 '22 01:07 pjrobertson

I just added one more commit that fixes #56

pjrobertson avatar Jul 26 '22 00:07 pjrobertson