Beispiel #1
0
    def put(self, request):
        serializer = PeopleSerializer(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)
 def _update(self, request, people_id, partial=False):
     people = self._get_object(people_id)
     serializer = PeopleSerializer(data=request.data, partial=partial)
     if serializer.is_valid():
         for field in serializer.fields:
             if field in serializer.validated_data:
                 setattr(people, field, serializer.validated_data[field])
         people.save()
         return Response(status=status.HTTP_200_OK)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
 def post(self, request):
     serializer = PeopleSerializer(data=request.data)
     if serializer.is_valid():
         People.objects.create(
             name=serializer.validated_data['name'],
             homeworld=serializer.validated_data['homeworld'],
             height=serializer.validated_data['height'],
             mass=serializer.validated_data['mass'],
             hair_color=serializer.validated_data['hair_color'])
         return Response(status=status.HTTP_201_CREATED)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
Beispiel #4
0
 def persons(self, request, pk):
     """returns a list of persons which are curently on a specific train"""
     train_obj = ICE.objects.get(id=pk)
     sections_ids = train_obj.sections.values_list(flat=True)
     qs = People.objects.none()
     for elem in sections_ids:
         qs = qs.union(People.objects.filter(section=elem))
     serializer = PeopleSerializer(qs, many=True)
     return Response(serializer.data)
Beispiel #5
0
    def get(self, *args, **kwargs):
        username = self.kwargs.get('username')
        person = People.objects.get(username=username)
        serializer = PeopleSerializer(person)

        days_to_birthday = Helpers().birthday_due_in(person)

        message = Helpers().birthday_message(person.username, days_to_birthday)

        return Response(message, status=status.HTTP_200_OK)
Beispiel #6
0
    def post(self, request):
        serializer = PeopleSerializer(data=request.data)

        # Get date of Birth from Request
        date_of_birth = Helpers().get_date_from(request.data['date_of_birth'],
                                                0)

        # Convert Date of birth from String to Date
        is_user_born_today_or_in_future = Helpers().is_born_today_or_in_future(
            date_of_birth)

        # Check if data is valid
        # and Date of Birth is not today or in the Fuute
        if serializer.is_valid() and not is_user_born_today_or_in_future:
            serializer.save()  # Save the data
            return Response(
                status=status.HTTP_204_NO_CONTENT)  # Return No Content
        else:
            error_message = {
                "Error": "Date of Birth cannot be Today or in the Future"
            }
            return Response(error_message, status=status.HTTP_400_BAD_REQUEST)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
 def get(self, request, people_id):
     people = self._get_object(people_id)
     serializer = PeopleSerializer(people)
     return Response(serializer.data)
 def get(self, request):
     qs = People.objects.select_related('homeworld').all()
     serializer = PeopleSerializer(qs, many=True)
     return Response(serializer.data)
Beispiel #9
0
 def get(self, request, people_id):
     one_person = self._get_object(people_id)
     serial_person = PeopleSerializer(one_person)
     return Response(serial_person.data)
Beispiel #10
0
 def post(self, request):
     new_person = PeopleSerializer(data=request.data)
     if new_person.is_valid():
         People.objects.create(**new_person.validated_data)
         return Response(f"Thanks for posting {new_person.data}")
     return Response(new_person.errors, status=status.HTTP_400_BAD_REQUEST)
Beispiel #11
0
 def get(self, request):
     raw_people = People.objects.all()
     serial_people = PeopleSerializer(raw_people, many=True)
     return Response(serial_people.data)
Beispiel #12
0
 def get(self, request):
     people = self.get_all_queryset()
     serializer = PeopleSerializer(people, many=True)
     return Response(serializer.data, status=status.HTTP_200_OK)