def create(self, validated_data): user = AccountUser(email=validated_data["email"], first_name=validated_data["first_name"], last_name=validated_data["last_name"], password=pwd_hash(validated_data["password"]), role=validated_data["role"]) user.save() return user
def update(self, instance, validated_data): instance.email = validated_data.get("email", instance.email) instance.first_name = validated_data.get("first_name", instance.first_name) instance.last_name = validated_data.get("last_name", instance.last_name) instance.role = validated_data.get("role", instance.role) if validated_data.get('password', None) is not None: instance.password = pwd_hash(validated_data["password"]) instance.save() return instance
def post(self, request, *args, **kwargs): first_name = request.data.get("first_name", "") last_name = request.data.get("last_name", "") email = request.data.get("email", "") password = request.data.get("password", "") user = AccountUser(email=email, first_name=first_name, last_name=last_name, password=pwd_hash(password)) user.save() return Response(status=status.HTTP_201_CREATED)
def authenticate(self, request, username=None, password=None, **kwargs): UserModel = get_user_model() try: user = UserModel.objects.get(Q(email__iexact=username)) except UserModel.DoesNotExist: UserModel().set_password(password) else: pwd_valid = (user.password == pwd_hash(password)) if pwd_valid: return user return None
def post(self, request, *args, **kwargs): user = self.request.user first_name = request.data.get("first_name", None) last_name = request.data.get("last_name", None) password = request.data.get("password", None) user.first_name = first_name user.last_name = last_name if password is not None: user.password = pwd_hash(password) user.save() return Response( data={ "token": jwt_encode_handler(jwt_payload_handler(user)), "user_id": user.id, "email": user.email, "first_name": user.first_name, "last_name": user.last_name, "role": user.role, "working_hour": user.working_hour })