You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
2.2 KiB
57 lines
2.2 KiB
from rest_framework import serializers
|
|
from .models import Club, Tournament, CustomUser
|
|
from django.contrib.auth import password_validation
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
class UserSerializer(serializers.HyperlinkedModelSerializer):
|
|
|
|
password = serializers.CharField(write_only=True)
|
|
|
|
def create(self, validated_data):
|
|
user = CustomUser.objects.create_user(
|
|
username=validated_data['username'],
|
|
password=validated_data['password'],
|
|
)
|
|
return user
|
|
|
|
class Meta:
|
|
club_id = serializers.PrimaryKeyRelatedField(queryset=Club.objects.all())
|
|
model = CustomUser
|
|
fields = ['id', 'username', 'password', 'club_id', 'umpire_code']
|
|
|
|
class ClubSerializer(serializers.HyperlinkedModelSerializer):
|
|
class Meta:
|
|
model = Club
|
|
fields = ['id', 'name', 'address']
|
|
|
|
class TournamentSerializer(serializers.HyperlinkedModelSerializer):
|
|
class Meta:
|
|
club_id = serializers.PrimaryKeyRelatedField(queryset=Club.objects.all())
|
|
model = Tournament
|
|
fields = ['id', 'name', 'club_id']
|
|
|
|
class ChangePasswordSerializer(serializers.Serializer):
|
|
old_password = serializers.CharField(max_length=128, write_only=True, required=True)
|
|
new_password1 = serializers.CharField(max_length=128, write_only=True, required=True)
|
|
new_password2 = serializers.CharField(max_length=128, write_only=True, required=True)
|
|
|
|
def validate_old_password(self, value):
|
|
user = self.context['request'].user
|
|
if not user.check_password(value):
|
|
raise serializers.ValidationError(
|
|
_('Your old password was entered incorrectly. Please enter it again.')
|
|
)
|
|
return value
|
|
|
|
def validate(self, data):
|
|
if data['new_password1'] != data['new_password2']:
|
|
raise serializers.ValidationError({'new_password2': _("The two password fields didn't match.")})
|
|
password_validation.validate_password(data['new_password1'], self.context['request'].user)
|
|
return data
|
|
|
|
def save(self, **kwargs):
|
|
password = self.validated_data['new_password1']
|
|
user = self.context['request'].user
|
|
user.set_password(password)
|
|
user.save()
|
|
return user
|
|
|