Exemple #1
0
def user_detail_api_view(request, pk=None):
    #queryset
    user = User.objects.filter(id=pk).first()

    #validation
    if user:

        #retrieve
        if request.method == 'GET':
            user_serializer = UserSerializer(user)
            return Response(user_serializer.data, status=status.HTTP_200_OK)

        #update
        elif request.method == 'PUT':
            user_serializer = UserSerializer(user, data=request.data)
            if user_serializer.is_valid():
                user_serializer.save()
                return Response({'message': 'se actualizaron los datos'},
                                status=status.HTTP_200_OK)

            return Response(user_serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)

        #delete
        elif request.method == 'DELETE':
            user.delete()
            return Response({'message': 'Usuario eliminado correctamente!'},
                            status=status.HTTP_200_OK)

    return Response(
        {'mesagge': 'no se ah encontrado a un usuario con esos datos'},
        status=status.HTTP_400_BAD_REQUEST)
Exemple #2
0
def api_users_create(request):
    if request.content_type == 'application/json':
        # create user
        if request.method == 'POST':
            if len(request.data) != 0:
                request.data['password'] = make_password(
                    request.data['password'])
                user_serializer = UserSerializer(data=request.data)
                if user_serializer.is_valid():
                    user_serializer.save()
                    response = user_serializer.data
                    response.pop('password', None)
                    response.pop('is_active', None)
                    response.pop('last_login', None)
                    response.pop('is_admin', None)
                    return Response(response, status=status.HTTP_201_CREATED)
                return Response(user_serializer.errors,
                                status=status.HTTP_400_BAD_REQUEST)
            else:
                return Response({'message': 'empty body'},
                                status=status.HTTP_400_BAD_REQUEST)
    else:
        return Response(
            {
                "error":
                "Request should have 'Content-Type' header with value 'application/json'"
            },
            status=status.HTTP_403_FORBIDDEN)
Exemple #3
0
def user_detail_api_view(request,pk):

    #queryset
    user = User.objects.filter(id=pk).first()

    #validation
    if user:

        #retrieve
        if request.method == 'GET':
            user_serializer = UserSerializer(user)
            return Response(user_serializer.data,status=status.HTTP_201_CREATED)

        #update
        elif request.method == 'PUT':
            
            user_serializer = UserSerializer(user,data = request.data) # en envia la instancia del objeto y el dato que se quiere actualizar
            if user_serializer.is_valid():
                user_serializer.save()
                return Response(user_serializer.data,status=status.HTTP_201_CREATED)
            return Response(user_serializer.errors,status=status.HTTP_400_BAD_REQUEST)

        #delete
        elif request.method == 'DELETE':
            user.delete()
            return response({'message':'User successfully remove'})
    return response({'message':'user not found'},status = status.HTTP_400_BAD_REQUEST)
Exemple #4
0
def edit_user(request, pk=None):
    if request.content_type == 'application/json':
        user = Users.objects.filter(id=pk).first()
        if user:
            if request.method == 'PUT':
                if len(request.data) != 0:
                    try:
                        if request.data['password']:
                            request.data['password'] = make_password(
                                request.data['password'])
                    except:
                        pass
                    user_serializer = UserSerializer(user, data=request.data)
                    if user_serializer.is_valid():
                        user_serializer.save()
                        response = user_serializer.data
                        response.pop('password', None)
                        response.pop('is_active', None)
                        response.pop('last_login', None)
                        response.pop('is_admin', None)
                        return Response(response, status=status.HTTP_200_OK)
                    return Response(user_serializer.errors,
                                    status=status.HTTP_400_BAD_REQUEST)
                else:
                    return Response({'message': 'empty body'},
                                    status=status.HTTP_400_BAD_REQUEST)
        return Response({'message': 'User not found'},
                        status=status.HTTP_400_BAD_REQUEST)
    else:
        return Response(
            {
                "error":
                "Request should have 'Content-Type' header with value 'application/json'"
            },
            status=status.HTTP_403_FORBIDDEN)
Exemple #5
0
def user_detail_api_view(request, pk=None):
    # consulta
    user = User.objects.filter(id=pk).first()
    if request.user.is_authenticated:
        # validacion
        if user:
            # retrieve
            if request.method == 'GET':

                users_serializer = UserSerializer(user)
                return Response(users_serializer.data, status=status.HTTP_200_OK)
            # update
            elif request.method == 'PUT':

                users_serializer = UserSerializer(user, data=request.data)
                # users_serializer = UserSerializer(user, data=request.data)
                if users_serializer.is_valid():
                    users_serializer.save()
                    return Response(users_serializer.data, status=status.HTTP_200_OK)
                return Response(users_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
            # delete
            elif request.method == 'DELETE':
                user.delete()
                return Response({'message': 'Usuario Eliminado correctamente!!'}, status=status.HTTP_200_OK)

        return Response({'message': 'No se ha encontrado un usuario con estos datos'},
                        status=status.HTTP_400_BAD_REQUEST)
    else:
        return Response({'message': 'Inicie Sesion!!'}, status=status.HTTP_400_BAD_REQUEST)
Exemple #6
0
def user_detail_api_view(request, pk=None):
    # Consulta
    user = User.objects.filter(id=pk).first()
    # Validacion
    if user:

        # Retrieve
        if request.method == 'GET':
            user_serializer = UserSerializer(user)
            return Response(user_serializer.data, status=status.HTTP_200_OK)

        # Update
        elif request.method == 'PUT':
            user_serializer = UserSerializer(user, data=request.data)

            if user_serializer.is_valid():
                user_serializer.save()
                return Response(user_serializer.data,
                                status=status.HTTP_200_OK)

            return Response(user_serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)

        # Delete
        elif request.method == 'DELETE':
            user.delete()
            return Response({'message': 'Usuario eliminado correctamente!'},
                            status=status.HTTP_200_OK)

    return Response({'message': 'Usuario no encontrado!'},
                    status=status.HTTP_400_BAD_REQUEST)
Exemple #7
0
 def put(self, request: Request, pk: int) -> Response:
     user = self.get_object(pk)
     serializer = UserSerializer(user, data=request.data)
     if serializer.is_valid():
         serializer.save()
         return Response(serializer.data)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemple #8
0
def user_detail_api_view(request, pk=None):

    user = User.objects.filter(id=pk).first()

    if user:
        if request.method == 'GET':
            user_serializer = UserSerializer(user)
            return Response(user_serializer.data, status=status.HTTP_200_OK)
        elif request.method == 'PUT':
            user_serializer = UserSerializer(user, data=request.data)
            if user_serializer.is_valid():
                user_serializer.save()
                return Response(user_serializer.data,
                                status=status.HTTP_200_OK)
            return Response(user_serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)

        elif request.method == 'DELETE':
            user.delete()
            return Response({'message': 'Usuario Eliminado correctamente!'},
                            status=status.HTTP_200_OK)

    return Response(
        {'message': 'No se ha encontrado un usuario con estos datos'},
        status=status.HTTP_400_BAD_REQUEST)
Exemple #9
0
 def post(self, request: Request) -> Response:
     serializer = UserSerializer(data=request.data)
     if serializer.is_valid():
         user = serializer.save()
         response = {
             "user": serializer.data,
         }
         return Response(response, status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemple #10
0
def user_api_view(request):

    if request.method == 'GET':
        users = User.objects.all()
        users_serializer = UserSerializer(users, many = True)
        return Response(users_serializer.data)

    elif request.method == 'POST':
        user_serializer = UserSerializer(data=request.data)
        if user_serializer.is_valid():
            user_serializer.save()
            return Response(user_serializer.data)
        return Response(user_serializer.errors)
Exemple #11
0
def user_api_view(request):

    #list
    if request.method == 'GET':
        users = User.objects.all().values('id','username','email','password')
        users_serializer = UserListSerializer(users,many=True) #se convietes a json a partir de la sonsulta (users)
        return Response(users_serializer.data,status=status.HTTP_200_OK)

    #create 
    elif request.method == 'POST':
        user_serializer = UserSerializer(data = request.data)
        if user_serializer.is_valid():
            user_serializer.save()
            return Response({'message':'user create'},status=status.HTTP_201_CREATED)
        return Response(user_serializer.errors,status=status.HTTP_400_BAD_REQUEST)
Exemple #12
0
def user_api_view(request):

    # List
    if request.method == 'GET':
        # Consulta
        users = User.objects.all().values('id', 'username', 'email',
                                          'password')
        users_serializer = UserListSerializer(users, many=True)
        return Response(users_serializer.data, status=status.HTTP_200_OK)

    elif request.method == 'POST':
        user_serializer = UserSerializer(data=request.data)
        if user_serializer.is_valid():
            user_serializer.save()
            return Response({'message': 'Usuario creado correctamente!'},
                            status=status.HTTP_201_CREATED)
        return Response(user_serializer.errors)
Exemple #13
0
def user_api_view(request):

    # list
    if request.method == 'GET':
        # queryset
        users = User.objects.all()
        users_serializer = UserSerializer(users, many=True)
        return Response(users_serializer.data, status=status.HTTP_200_OK)

    # create
    elif request.method == 'POST':
        user_serializer = UserSerializer(data=request.data)

        # validation
        if user_serializer.is_valid():
            user_serializer.save()
            return Response({'message': 'Usuario creado correctamente!'}, status=status.HTTP_201_CREATED)

        return Response(user_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Exemple #14
0
def user_api_view(request, ):

    # List
    if request.method == 'GET':

        # queryset
        users = User.objects.all()
        users_serializer = UserSerializer(users, many=True)

        # # hacemos un diccionario para pasarlo como data para hacer un usuario de prueba
        # test_data={
        #     'name': "seba",
        #     'email': '*****@*****.**',
        # }
        # # serializamos la info con la clase que creamos en serializers
        # test_user = UserSerializer(data=test_data)

        # # si pasa la validación imprimimos un mensaje.>> Se guarda la data en validated_data
        # # pero si no lo pasa, crea un diccionario llamado "errors" que luego lo podemos imprimir.
        # if test_user.is_valid():
        #     user_instance = test_user.save()
        #     print("Pasó las validaciones")
        # else :
        #     print(test_user.errors)
        return Response(users_serializer.data, status=status.HTTP_200_OK)

    # Create
    elif request.method == 'POST':

        # queryset
        user_serializer = UserSerializer(data=request.data)

        # validation
        if user_serializer.is_valid():
            user_serializer.save()
            return Response(
                {'message': 'El usuario ha sido creado con éxito.'},
                status=status.HTTP_201_CREATED)

        # Si hay un error se auto genera un atributo 'errors' en el serializer, como en los forms.
        return Response(user_serializer.errors)
def user_api_view(request):

    if request.method == "GET":
        users = User.objects.all()
        users_serializer = UserSerializer(users, many=True)

        testdata = {"name": "development", "email": "*****@*****.**"}
        testuser = TestUserSerializar(data=testdata)
        if testuser.is_valid():
            print("paso validaciones")

        return Response(users_serializer.data)

    elif request.method == "POST":
        user_serializer = UserSerializer(data=request.data)

        if user_serializer.is_valid():
            user_serializer.save()
            return Response(user_serializer.data)

        return Response(user_serializer.errors)
def user_detail_view(request, pk=None):

    if request.method == "GET":
        user = User.objects.filter(id=pk).first()
        user_serializer = UserSerializer(user)

        return Response(user_serializer.data)

    elif request.method == "PUT":
        user = User.objects.filter(id=pk).first()
        user_serializer = UserSerializer(user, data=request.data)

        if user_serializer.is_valid():
            user_serializer.save()
            return Response(user_serializer.data)

        return Response(user_serializer.errors)

    elif request.method == "DELETE":
        user = User.objects.filter(id=pk).first()
        user.delete()

        return Response("Eliminado")
Exemple #17
0
def user_detail_api_view(request, pk=None):

    # queryset
    user = User.objects.filter(id=pk).first()

    # valitation
    if user:
        # retrive
        if request.method == 'GET':
            user_serializer = UserSerializer(user)
            return Response(user_serializer.data, status=status.HTTP_200_OK)

        # update
        elif request.method == 'PUT':
            """
            en la siguiente línea: le pasamos el usuario en la db y la data que ingresa el usuario
            para que lo compare y actualize lo diferente
            """
            # EXPLICITO:    user_serializer = TestUserSerializer(instance = user, data=request.data) Es lo mismo que abajo:
            user_serializer = UserSerializer(user, data=request.data)
            if user_serializer.is_valid():
                user_serializer.save()
                return Response(user_serializer.data,
                                status=status.HTTP_200_OK)
            return Response(user_serializer.errors,
                            status=status.HTTP_400_BAD_REQUEST)

        # delete
        elif request.method == 'DELETE':
            user.delete()
            return Response(
                {'message': 'El usuario ha sido eliminado con éxito.'},
                status=status.HTTP_200_OK)

    return Response(
        {'message': 'No se ha encontrado un usuario con estos datos.'},
        status=status.HTTP_400_BAD_REQUEST)