Skip to content

Commit

Permalink
func: Adiciona rota para deletar usuario
Browse files Browse the repository at this point in the history
  • Loading branch information
WellingtonLFaria committed Sep 27, 2024
1 parent f1af1e4 commit 54c104d
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/tupan/usuarios/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class UsuarioSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Usuario
fields = ['email', 'password', 'ativo', 'criacao', 'alterado']
fields = ['id', 'email', 'password', 'ativo', 'criacao', 'alterado']
extra_kwargs = {'password': {'write_only': True}}

def create(self, validated_data):
Expand Down
9 changes: 8 additions & 1 deletion src/tupan/usuarios/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,11 @@ def test_listagem_usuarios(self):
usuarios = Usuario.objects.all()
assert len(usuarios) == 2
assert user1 in usuarios
assert user2 in usuarios
assert user2 in usuarios

@pytest.mark.django_db
def test_inativar_usuario(self):
user1 = Usuario.objects.create_user(email="wellingtonll.faria@gmail.com", password="123123")
user1.ativo = False
user1.save()
assert user1.ativo == False
8 changes: 5 additions & 3 deletions src/tupan/usuarios/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class UsuarioList(APIView):
Lista, cria, atualiza e deleta os usuários.
"""
def get(self, request, format=None):
usuarios = Usuario.objects.all()
usuarios = Usuario.objects.all().filter(ativo=True)
serializer = UsuarioSerializer(usuarios, many=True)
return Response(serializer.data)

Expand All @@ -20,14 +20,16 @@ def delete(self, request, format=None):
usuario = Usuario.objects.get(id=id_usuario)
usuario.ativo = False
usuario.save()
return Response(usuario, status=status.HTTP_200_OK)
serializer = UsuarioSerializer(usuario)
return Response(serializer.data, status=status.HTTP_200_OK)
except Usuario.DoesNotExist:
return Response(status=status.HTTP_404_NOT_FOUND)
except Exception as e:
return Response({"message": str(e)}, status=status.HTTP_400_BAD_REQUEST)

def post(self, request, format=None):
serializer = UsuarioSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

0 comments on commit 54c104d

Please sign in to comment.