Exemplo n.º 1
0
    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)
Exemplo n.º 2
0
    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)
Exemplo n.º 3
0
 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)