def lista_person_byGender(self, request, *args, **kwargs): try: gender = kwargs.get('gender') people = Person.objects.filter(gender=gender).all() serializer = PersonSerializer(people, many=True) return Response(serializer.data) except Person.DoesNotExist: message = {'error': 'No hay Personas registradas'} return Response(message, status.HTTP_404_NOT_FOUND)
def searching(self, request, *args, **kwargs): try: last_name = kwargs.get('last_name') person = Person.objects.get(last_name=last_name) serializer = PersonSerializer(person) return Response(serializer.data, status.HTTP_200_OK) except Person.DoesNotExist: message = {'error': 'Persona no encontrada'} return Response(message, status.HTTP_404_NOT_FOUND)
def deactivated(self, request, *args, **kwargs): try: id = kwargs.get('id') person = Person.objects.get(id=id) person.status = 0 person.save() serializer = PersonSerializer(person) return Response(serializer.data, status.HTTP_200_OK) except Person.DoesNotExist: message = {'error': 'Persona no encontrada'} return Response(message, status.HTTP_404_NOT_FOUND)
def get(self, request): # limit wasn't discuss in tech task so it's only mine initiative queryset = Person.objects.all().order_by( '-border_crosses__date_of_border_cross')[:10] serialized = PersonSerializer(queryset, many=True) return Response(serialized.data)
def post(self, request): serializer = PersonSerializer(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)