celiao / django-rest-authemail

A RESTful API for user signup and authentication using email addresses.
GNU General Public License v3.0
315 stars 92 forks source link

Add the request to the context for the serializer #55

Open pjrobertson opened 2 years ago

pjrobertson commented 2 years ago

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 commented 2 years ago

I just added one more commit that fixes #56