Coverage for users/serializers.py: 82%
85 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-04-18 14:05 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-04-18 14:05 -0600
1from django.conf import settings
2from django.contrib.auth.password_validation import validate_password
3from django.contrib.auth.tokens import PasswordResetTokenGenerator
4from rest_framework import serializers
5from rest_framework.validators import ValidationError
6from users.models import User, Functionary, Employee
9class UserSerializer(serializers.ModelSerializer):
10 """
11 Serializer for Django User Model
12 """
14 password = serializers.CharField(write_only=True, required=False)
16 class Meta:
17 model = User
18 read_only_fields = ["is_active"]
19 fields = ["email", "password"]
21 def create(self, validated_data):
22 """Override create method to use create_user manager method"""
23 return User.objects.create_user(**validated_data)
26class UserDetailSerializer(serializers.ModelSerializer):
27 class Meta:
28 model = User
29 read_only_fields = ["email", "name", "photo"]
30 fields = read_only_fields + []
33class FunctionarySerializer(serializers.ModelSerializer):
34 email = serializers.EmailField(write_only=True)
36 class Meta:
37 model = Functionary
38 read_only_fields = ["random_slug", "user"]
39 fields = ["email"] + read_only_fields
41 def create(self, validated_data):
42 """
43 Override to get or create user for Functionary
44 """
45 email = validated_data.pop("email")
46 user, _ = User.objects.get_or_create(email=email)
47 functionary, _ = Functionary.objects.get_or_create(user=user)
48 return functionary
51class PanelUserSerializer(serializers.ModelSerializer):
52 """Serializer for user use"""
54 is_employee = serializers.SerializerMethodField()
55 is_member = serializers.SerializerMethodField()
57 class Meta:
58 model = User
59 fields = [
60 "email",
61 "name",
62 "photo",
63 "phone",
64 "address",
65 "state",
66 "city",
67 "country",
68 "zip_code",
69 "date_joined",
70 "is_employee",
71 "is_member",
72 ]
74 def get_is_employee(self, instance):
75 return hasattr(instance, "employee")
77 def get_is_member(self, instance):
78 return instance.membership_set.exists()
81class EmployeeSerializer(serializers.ModelSerializer):
82 """Serializer for employee"""
84 user = UserDetailSerializer(read_only=True)
86 class Meta:
87 model = Employee
88 read_only_fields = ["random_slug", "user"]
89 fields = read_only_fields + []
92class ResetPasswordSerializer(serializers.Serializer):
93 email = serializers.EmailField()
94 panel = serializers.ChoiceField([("A", "Admin"), ("M", "Member")])
96 def validate_email(self, value):
97 try:
98 return User.objects.get(email=value)
99 except User.DoesNotExist:
100 raise serializers.ValidationError("No existe un usuario con ese email")
102 def save(self, validated_data):
103 email = validated_data.get("email")
104 user = User.objects.get(email=email)
105 token = PasswordResetTokenGenerator().make_token(user)
106 panel = validated_data.get("panel")
107 if panel == "A": 107 ↛ 108line 107 didn't jump to line 108, because the condition on line 107 was never true
108 domain = settings.FRONTEND_DOMAIN_ADMIN
109 elif panel == "M": 109 ↛ 111line 109 didn't jump to line 111, because the condition on line 109 was never false
110 domain = settings.FRONTEND_DOMAIN_MEMBER
111 return f"{domain}{settings.FRONTEND_RESET_PASSWORD_PATH}/{token}?u={user.pk}", user
114class SetPasswordSerializer(serializers.Serializer):
115 """Serializer to set password"""
117 user_id = serializers.CharField()
118 token = serializers.CharField()
119 password = serializers.CharField()
121 def validate_password(self, value):
122 validate_password(value)
123 return value
125 def validate(self, attrs):
126 token = attrs.get("token")
127 user_id = attrs.get("user_id")
128 user = User.objects.get(pk=user_id)
129 verified_token = PasswordResetTokenGenerator().check_token(user, token)
130 if verified_token:
131 return attrs
132 else:
133 raise ValidationError({"token": "The token is not valid"})
135 def save(self, validated_data):
136 data = self.validated_data
137 user_id = data.get("user_id")
138 user = User.objects.get(pk=user_id)
139 user.set_password(data.get("password"))
140 user.save()