Created
October 15, 2019 22:17
-
-
Save mitchtabian/5dce50c510e9eff8ecf531c8a4f63bf5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from rest_framework import serializers | |
from account.models import Account | |
class RegistrationSerializer(serializers.ModelSerializer): | |
password2 = serializers.CharField(style={'input_type': 'password'}, write_only=True) | |
class Meta: | |
model = Account | |
fields = ['email', 'username', 'password', 'password2'] | |
extra_kwargs = { | |
'password': {'write_only': True}, | |
} | |
def save(self): | |
account = Account( | |
email=self.validated_data['email'], | |
username=self.validated_data['username'] | |
) | |
password = self.validated_data['password'] | |
password2 = self.validated_data['password2'] | |
if password != password2: | |
raise serializers.ValidationError({'password': 'Passwords must match.'}) | |
account.set_password(password) | |
account.save() | |
return account | |
class AccountPropertiesSerializer(serializers.ModelSerializer): | |
class Meta: | |
model = Account | |
fields = ['pk', 'email', 'username', ] | |
class ChangePasswordSerializer(serializers.Serializer): | |
old_password = serializers.CharField(required=True) | |
new_password = serializers.CharField(required=True) | |
confirm_new_password = serializers.CharField(required=True) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment