def new_amenity(request, format=None): """ Create new amenity """ if request.method == "POST": serializer = AmenitySerializer(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 new_amenity(request, format=None): """ Create new amenity """ if request.method == 'POST': serializer = AmenitySerializer(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_amenity(request, pk): """ Update existing amenity """ try: amenity = Amenity.objects.get(pk=pk) except Amenity.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == "PUT": serializer = AmenitySerializer(amenity, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def update_amenity(request, pk): """ Update existing amenity """ try: amenity = Amenity.objects.get(pk=pk) except Amenity.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'PUT': serializer = AmenitySerializer(amenity, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def amenity_list(request, format=None): """ List all Amenities """ if request.method == 'GET': amenities = Amenity.objects.all() serializer = AmenitySerializer(amenities, many=True) return Response(serializer.data)
def get_amenity(request, pk): """ Retrieve an existing amenity """ try: amenity = Amenity.objects.get(pk=pk) except Amenity.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = AmenitySerializer(amenity) return Response(serializer.data)