Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.51 KB | None | 0 0
  1. from rest_framework import serializers
  2. from django.db import transaction
  3. from .models import Profile
  4. from django.contrib.auth.models import User
  5. from django.contrib.auth.hashers import make_password
  6.  
  7.  
  8. class UserSerializer(serializers.HyperlinkedModelSerializer):
  9.  
  10.     class Meta:
  11.         model = User
  12.         fields = ('email', 'password')
  13.         extra_kwargs = {
  14.             'username': {'read_only': True},
  15.             # 'password': {'write_only': True}
  16.         }
  17.  
  18.     def validate_email(self, value):
  19.         usr = User.objects.filter(email__icontains=value)
  20.  
  21.         if not value:
  22.             raise serializers.ValidationError("Este campo não pode ser em branco.")
  23.  
  24.         if self.parent:
  25.             if self.parent.instance:
  26.                 usr = usr.exclude(pk=self.parent.instance.user.id)
  27.  
  28.         if usr.exists():
  29.             raise serializers.ValidationError("E-mail já está cadastrado")
  30.  
  31.         return value
  32.  
  33.     def validate_password(self, value):
  34.  
  35.         if not value:
  36.             raise serializers.ValidationError("Este campo não pode ser em branco.")
  37.  
  38.         return value
  39.  
  40.  
  41. class ProfileSerializer(serializers.HyperlinkedModelSerializer):
  42.     user = UserSerializer()
  43.  
  44.     class Meta:
  45.         model = Profile
  46.         fields = ('id', 'url', 'user', 'bio', 'location')
  47.  
  48.     @transaction.atomic
  49.     def create(self, validated_data):
  50.         track_data = validated_data.pop('user')
  51.  
  52.         if 'email' not in track_data:
  53.             raise serializers.ValidationError({"user": {"email": ["Este campo precisa ser informado"]}})
  54.  
  55.         if 'password' not in track_data:
  56.             raise serializers.ValidationError({"user": {"password": ["Este campo precisa ser informado"]}})
  57.  
  58.  
  59.         track_data['username'] = track_data['email']
  60.         user = User.objects.create(**track_data)
  61.         user.set_password(track_data['password'])
  62.         user.save()
  63.  
  64.         validated_data['user'] = user
  65.         profile = super(ProfileSerializer, self).create(validated_data)
  66.  
  67.         return profile
  68.  
  69.     @transaction.atomic
  70.     def update(self, instance, validated_data):
  71.         track_data = validated_data.pop('user')
  72.  
  73.         profile = super(ProfileSerializer, self).update(instance, validated_data)
  74.  
  75.         user = UserSerializer(profile.user, track_data)
  76.  
  77.         if 'email' in track_data:
  78.             track_data['username'] = track_data['email']
  79.  
  80.         if user.is_valid():
  81.             User.objects.filter(pk=profile.user.id).update(**track_data)
  82.  
  83.         return profile
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement