Example #1
0
    def put(self, request, pk, format=None):
        user = self.get_object(pk=pk)
        serializer = UserUpdateSerializer(user, data=request.data)

        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data, status=status.HTTP_200_OK)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Example #2
0
 def patch(self, request, pk):
     """PATCH method for editing user's name and password"""
     instance = User.objects.get(pk=pk)
     serializer = UserUpdateSerializer(instance,
                                       data=request.data,
                                       partial=True)
     if not serializer.is_valid():
         return Response(data=serializer.errors,
                         status=status.HTTP_400_BAD_REQUEST)
     try:
         new_user = serializer.save()
     except:
         return Response(data={'error': sys.exc_info()[0]},
                         status=status.HTTP_400_BAD_REQUEST)
     serializer = UserSerializer(new_user)
     return Response(data=serializer.data, status=status.HTTP_202_ACCEPTED)
Example #3
0
    def test_user_put(self):
        data = {'email': '*****@*****.**', 'is_staff': True}
        response = self.client.put('/api/users/{}/'.format(self.user_1.id),
                                   json.dumps(data),
                                   content_type='application/json')
        self.assertEqual(response.status_code, 200)

        user = User.objects.get(pk=self.user_1.id)
        expected = UserUpdateSerializer(user).data
        self.assertEqual(response.data, expected)
        self.assertEqual(response.data['email'], data['email'])
        self.assertEqual(response.data['is_staff'], data['is_staff'])
Example #4
0
class ScheduleListSerializer(serializers.ModelSerializer):
    '''
    约运动序列化
    '''
    user = UserUpdateSerializer()
    sport = SportsSerializer()
    sport_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S',
                                           label='运动时间')
    add_time = serializers.DateTimeField(read_only=True,
                                         format='%Y-%m-%d %H:%M:%S')

    class Meta:
        model = Schedule
        fields = ('user', 'sport', 'address', 'sport_time', 'now_people',
                  'people_nums', 'add_time')
Example #5
0
class ProfileUpdateSerializer(serializers.ModelSerializer):
    user = UserUpdateSerializer()
    social_link = SocialLinkSerializer()

    class Meta:
        model = Profile
        lookup_field = 'uuid'
        fields = (
            'user',
            'occupation',
            'contact_email',
            'phone',
            'city',
            'country',
            'social_link',
        )

    def update(self, instance, validated_data):
        user_data = validated_data.pop('user')
        social_link_data = validated_data.pop('social_link')

        user = instance.user
        social_link = instance.social_link

        attributes = [
            'occupation', 'contact_email', 'phone', 'city', 'country'
        ]
        update_attr(instance, validated_data, attributes)
        instance.save()

        attributes = ['first_name', 'last_name']
        update_attr(user, user_data, attributes)
        user.save()

        attributes = ['github', 'linkedin', 'twitter', 'website']
        update_attr(social_link, social_link_data, attributes)
        social_link.save()

        return instance
Example #6
0
 def validate_breeder(value):
     return UserUpdateSerializer.validate_breeder(value)
Example #7
0
 def validate_veterinarian(value):
     return UserUpdateSerializer.validate_veterinarian(value)
Example #8
0
 def put(request):
     serializer = UserUpdateSerializer(request.user, data=request.data, partial=True)
     serializer.is_valid(raise_exception=True)
     serializer.save()
     return Response(status=status.HTTP_200_OK)
Example #9
0
    def put(self, request, pk, format=None):
        """
          API endpoint that update the users.
          ---
          Body example:
          ```
          {
            "username": "******",
            "email": "*****@*****.**",
            "password": "******"
          }
          ```
          Response example:
          ```
          {
            "id": 1,
            "username": "******",
            "email": "*****@*****.**",
            "profile": {
                "id": 1,
                "hours": 5,
                "abilities": [
                    {
                        "id": 1,
                        "name": "Gamificacao"
                    }
                ],
                "user": 1,
                "phone": 10,
                "photo": null,
                "coins": 0
            }
          }
          ```
        """
        user = self.get_object(pk=pk)
        serializer = UserUpdateSerializer(user, data=request.data)

        if serializer.is_valid():
            serializer.save()

            # profile_data = request.data['profile']

            # abilities_data = profile_data.pop('abilities')

            # profile = Profile.objects.filter(user=user).update(
            #     **profile_data
            # )

            # profile = Profile.objects.get(user=user)
            # profile.abilities.clear()

            # for abilitiest in abilities_data:
            #     abilitiest = Abilities.objects.filter(id=abilitiest['id'])
            #     profile.abilities.add(*abilitiest)
            #     profile.save()

            # profile_serializer = ProfileUpdateSerializer(profile)

            return Response(serializer.data, status=status.HTTP_200_OK)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)