def create(self, request): serializer = CreateUserSerializer(data=request.data) if serializer.is_valid(): email = serializer.data.get('email', None) username = serializer.data['username'] password = serializer.data['password'] try: existing_user = PushUser.objects.get(username=username) except PushUser.DoesNotExist: existing_user = None if existing_user is not None: # The 'Login' request auth_user = authenticate(username=username, password=password) if auth_user is not None: return Response(create_user_response_data(existing_user)) else: return Response({'error': 'Invalid credentials'}, status=status.HTTP_401_UNAUTHORIZED) if email is not None and len(email) > 0: existing_users = PushUser.objects.filter(email=email) if len(existing_users) > 0: return Response({'error': 'An account with that email address already exists.'}, status=status.HTTP_400_BAD_REQUEST) user = PushUser.objects.create_user(email=email, username=username, password=password) user.save() # A token is created on user.save() return Response(create_user_response_data(user)) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, *args, **kwargs): """ Create new user. """ serializer = CreateUserSerializer(data=request.data) if serializer.is_valid(): user, created = User.objects.get_or_create(username=serializer.data['username']) # Validate if user exists, return bad request. HTTP Code 409 Conflict if created: user.set_password(serializer.data['password']) user.save() response_data = { 'id': user.id, 'username': user.username } return Response(response_data, status=status.HTTP_200_OK) else: return Response({'error': 'USER_ALREADY_EXISTS'}, status=status.HTTP_409_CONFLICT) # Validate errors in POST data else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request, *args, **kwargs): serializer = CreateUserSerializer(data=request.data) serializer.is_valid(raise_exception=True) data = serializer.data user = AccountData.objects.filter(username=data['MOBILE']).first() message = "user already registered" status_code = "200" if not user: user = AccountData.objects.create_user(username=data['MOBILE'], password=data['PASSWORD'], email=data['EMAIL'], date_of_birth=data['DOB'], address=data['ADDRESS'], gender=data['GENDER'], idnumber=data['IDNUMBER']) message = "user registered successfully" status_code = "201" data.update({"id": user.id, "message": message, "code": status_code}) return Response(data)
def create(self, request): serializer = CreateUserSerializer(data=request.DATA) if serializer.is_valid(): email = serializer.data.get('email', None) username = serializer.data['username'] password = serializer.data['password'] try: existing_user = PushUser.objects.get(username=username) except PushUser.DoesNotExist: existing_user = None if existing_user is not None: return Response(create_user_response_data(existing_user)) if email is not None and len(email) > 0: existing_users = PushUser.objects.filter(email=email) if len(existing_users) > 0: return Response({'error': 'An account with that email address already exists.'}, status=status.HTTP_400_BAD_REQUEST) user = PushUser.objects.create_user(email=email, username=username, password=password) user.save() token = Token.objects.create(user=user) return Response(create_user_response_data(user, token)) else: return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): serialized_data = CreateUserSerializer(data=request.data) if serialized_data.is_valid(): serialized_data.save() return Response(data=serialized_data.data, status=status.HTTP_200_OK) return Response(data=serialized_data.errors, status=status.HTTP_400_BAD_REQUEST)