Showing
1 changed file
with
44 additions
and
20 deletions
1 | -from django.contrib.auth.models import User, Group | 1 | +from rest_framework import serializers |
2 | -from rest_framework import serializers | 2 | +from api.models import User |
3 | -from api.models import Item, SharedItem | 3 | + |
4 | - | 4 | + |
5 | - | 5 | +class UserSerializer(serializers.ModelSerializer): |
6 | -class UserSerializer(serializers.HyperlinkedModelSerializer): | 6 | + class Meta: |
7 | - class Meta: | 7 | + model = User |
8 | - model = User | 8 | + fields = '__all__' |
9 | - fields = ['url', 'username', 'email', 'groups'] | 9 | + |
10 | - | 10 | + |
11 | -class GroupSerializer(serializers.HyperlinkedModelSerializer): | 11 | +class SignUpSerializer(serializers.ModelSerializer): |
12 | - class Meta: | 12 | + password2 = serializers.CharField(style={"input_type": "password"}) |
13 | - model = Group | 13 | + |
14 | - fields = ['url', 'name'] | 14 | + class Meta: |
15 | - | 15 | + model = User |
16 | -class ItemSerializer(serializers.ModelSerializer): | 16 | + fields = ['user_id', 'name', 'password', 'password2'] |
17 | - class Meta: | 17 | + |
18 | - model = Item | 18 | + def create(self, data): |
19 | - fields = '__all__' | 19 | + user_id = data['user_id'] |
20 | - | 20 | + name = data['name'] |
21 | + password = self.data['password'] | ||
22 | + password2 = self.data['password2'] | ||
23 | + | ||
24 | + if user_id and User.objects.filter(user_id=user_id).exclude(name=name).exists(): | ||
25 | + raise serializers.ValidationError({"user_id": "User_id must be unique."}) | ||
26 | + | ||
27 | + elif password != password2: | ||
28 | + raise serializers.ValidationError({'password': "Passwords must match."}) | ||
29 | + | ||
30 | + user = User.objects.create( | ||
31 | + user_id=data['user_id'], | ||
32 | + name=data['name'], | ||
33 | + password=data['password'], | ||
34 | + ) | ||
35 | + user.save() | ||
36 | + | ||
37 | + return user | ||
38 | + | ||
39 | + | ||
40 | +class UserInfoSerializer(serializers.ModelSerializer): | ||
41 | + class Meta: | ||
42 | + model = User | ||
43 | + fields = ['user_id', 'name', 'total_size', 'current_size'] | ||
44 | + | ... | ... |
-
Please register or login to post a comment