def create(self, request): logout(request) serializer = UserRegistrationSerializer(data=request.data) if serializer.is_valid(): validated_data = serializer.validated_data user = user_model.objects.create( username=validated_data['username'], email=validated_data['email'], first_name=validated_data['first_name'], last_name=validated_data['last_name']) user.set_password(validated_data['password']) user.save() print('--------------------- User created') # send confirmation mail try: # TODO: replace hardcoded url send_mail( "Registration successful", "You have successfully registered at ws18sdf-a.f4.htw-berlin.de", "*****@*****.**", [validated_data['email']]) except ConnectionRefusedError as err: print("Mail could not be send, you might be on localhost!", err) login(request, user) return Response(UserInfoSerializer(user).data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def user_registration(self, request): serializer = UserRegistrationSerializer(data=request.data) if serializer.is_valid(): user = serializer.save() user.is_active = True user.save() if user: json = serializer.data return Response(json, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, *args, **kwargs): serializer = UserRegistrationSerializer(data=request.data, context={'request': request}) if serializer.is_valid(raise_exception=True): user = serializer.save() if user: token = Token.objects.create(user=user) details = serializer.data details['token'] = token.key return Response(details, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def registration(request): serializer = UserRegistrationSerializer(data=request.data) if not serializer.is_valid(): return response.Response(serializer.errors, status.HTTP_400_BAD_REQUEST) user = serializer.save() refresh = RefreshToken.for_user(user) res = { 'refresh': str(refresh), 'access': str(refresh.access_token), } return response.Response(res, status.HTTP_201_CREATED)
def test_fields(self): serializer = UserRegistrationSerializer() self.assert_fields_required(True, serializer, self.required_fields) self.assert_fields_required(False, serializer, self.not_required_fields) self.assertEqual( len(serializer.fields), len(self.required_fields) + len(self.not_required_fields))
def post(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) if serializer.is_valid(): user = serializer.user userObject = User.objects.filter(username=user) serializer = UserRegistrationSerializer(userObject, many=True) token, _ = Token.objects.get_or_create(user=user) return Response( { 'auth_token': token.key, "date": token.created, "users": serializer.data }, status=status.HTTP_200_OK, ) else: return Response( data=serializer.errors, status=status.HTTP_400_BAD_REQUEST, )
def post(self, request): """ :param request: { "email":"*****@*****.**", "password":"******", "first_name":"Test", "last_name":"Test" } :return: 'Failure' for invalid request params { "error": true, "message": "Bad Request", "data": { "email": [ "This field is required." ], "password": [ "This field is required." ] } } { "error": true, "message": "Bad Request", "data": { "email": [ "Enter a valid email address." ] } } 'Success' { "error": false, "message": "Registered Successfully", "data": { "user": { "id": 2, "last_login": "******", "first_name": "dharati", "last_name": "dekavadiya", "email": "*****@*****.**", "username": null, "is_email_verified": false, "created_at": "2020-07-10T06:22:11.659724Z", "updated_at": "2020-07-10T06:22:11.842947Z" }, "token": "e27bd8dfc7e36c14d38cd90a22ec4fa43cd3a768" } } """ serializer = UserRegistrationSerializer(data=request.data) if not serializer.is_valid(): return Response( { 'error': True, 'message': "Bad Request", 'data': serializer.errors }, status=status.HTTP_400_BAD_REQUEST) extra_fields = { 'first_name': request.data['first_name'], 'last_name': request.data['last_name'], 'last_login': timezone.now(), 'username': request.data['username'] if 'username' in request.data else None } user = User.objects.create_user(email=request.data['email'], password=request.data['password'], **extra_fields) # Creating auth token for the user token = Token.objects.create(user=user) user_serializer = UserSerializer(user) tasks.send_verification_mail.delay(user_serializer.data) return Response( { 'error': False, 'message': "Registered Successfully", 'data': { 'user': user_serializer.data, 'token': token.key } }, status=status.HTTP_201_CREATED)
def get(self, request): data = Users.objects.all() s1 = UserRegistrationSerializer(data, many=True) print(type(s1)) return HttpResponse(json.dumps(s1.data), content_type="application/json")