Skip to content

Commit

Permalink
feat: tarefa 4 finalizada
Browse files Browse the repository at this point in the history
  • Loading branch information
Karengrabarz committed Dec 4, 2023
1 parent b500418 commit b4c2e6f
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 9 deletions.
Binary file modified db.sqlite3
Binary file not shown.
Binary file modified pets/__pycache__/models.cpython-312.pyc
Binary file not shown.
15 changes: 7 additions & 8 deletions pets/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def get(self,request:Request,pet_id: int) -> Response:
except Pet.DoesNotExist:
return Response({
"detail": "Not found."
},status.HTTP_400_BAD_REQUEST)
},status.HTTP_404_NOT_FOUND)
serializer = PetSerializer(found_pet)
return Response(serializer.data,status.HTTP_200_OK)

Expand All @@ -61,7 +61,7 @@ def delete(self,request:Request,pet_id: int) -> Response:
except Pet.DoesNotExist:
return Response({
"detail": "Not found."
},status.HTTP_400_BAD_REQUEST)
},status.HTTP_404_NOT_FOUND)
found_pet.delete()
return Response(status=status.HTTP_204_NO_CONTENT)

Expand All @@ -70,7 +70,7 @@ def patch(self,request:Request,pet_id: int) -> Response:
found_pet = Pet.objects.get(pk=pet_id)
except Pet.DoesNotExist:
return Response(
{"detail": "Not found."}, status.HTTP_400_BAD_REQUEST
{"detail": "Not found."}, status.HTTP_404_NOT_FOUND
)
serializer = PetSerializer(data = request.data, partial=True)
if not serializer.is_valid():
Expand All @@ -85,9 +85,8 @@ def patch(self,request:Request,pet_id: int) -> Response:
group = Group.objects.get(**group_data)
except Group.DoesNotExist:
group = Group.objects.create(**group_data)

found_pet.group=group

found_pet.group=group

traits_data = serializer.validated_data.pop('traits',None)
if traits_data:
found_pet.traits.clear()
Expand All @@ -97,12 +96,12 @@ def patch(self,request:Request,pet_id: int) -> Response:
trait = Trait.objects.get(name__iexact=current_trait_data["name"])
except Trait.DoesNotExist:
trait = Trait.objects.create(**current_trait_data)

found_pet.traits.add(trait)
found_pet.traits.add(trait)


for key, value in serializer.validated_data.items():
setattr(found_pet, key, value)
found_pet.save()
serializer = PetSerializer(found_pet)

return Response(serializer.data, status.HTTP_200_OK)
Binary file modified traits/__pycache__/models.cpython-312.pyc
Binary file not shown.
2 changes: 1 addition & 1 deletion traits/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.db import models

class Trait (models.Model):
name = models.CharField(max_length=50, unique=True)
name = models.CharField(max_length=20, unique=True)
pets = models.ManyToManyField(
'pets.Pet',
related_name = 'traits'
Expand Down

0 comments on commit b4c2e6f

Please sign in to comment.