Python rest_framework.parsers.JSONParser() Examples

The following are 30 code examples of rest_framework.parsers.JSONParser(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module rest_framework.parsers , or try the search function .
Example #1
Source File: utils.py    From django-rest-messaging with ISC License 7 votes vote down vote up
def parse_json_response(json):
    """ parse the json response """
    rendered = JSONRenderer().render(json)
    stream = BytesIO(rendered)
    return JSONParser().parse(stream) 
Example #2
Source File: views.py    From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License 6 votes vote down vote up
def flight_list(request):
    #Get all
    if request.method == 'GET':
        schedules = Schedule.objects.all()
        schedules_serializer = ScheduleSerializer(schedules,many=True)
        return JsonResponse(schedules_serializer.data, safe=False)
    
    #Add one
    if request.method == 'POST':
        schedule_data = JSONParser().parser(request)
        schedule_serializer = ScheduleSerializer(data=schedule_data)
        if schedule_serializer.is_valid():
            schedule_serializer.save()
            return JsonResponse(schedule_serializer.data, status=status.HTTP_201_CREATED)
        return JsonResponse(schedule_serializer.errors,status=status.HTTP_400_BAD_REQUEST)
    
    #Delete All
    if request.method == 'DELETE':
        Schedule.objects.all().delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT) 
Example #3
Source File: views.py    From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License 6 votes vote down vote up
def flight_list(request):
    #Get all
    if request.method == 'GET':
        schedules = Schedule.objects.all()
        schedules_serializer = ScheduleSerializer(schedules,many=True)
        return JsonResponse(schedules_serializer.data, safe=False)
    
    #Add one
    if request.method == 'POST':
        schedule_data = JSONParser().parser(request)
        schedule_serializer = ScheduleSerializer(data=schedule_data)
        if schedule_serializer.is_valid():
            schedule_serializer.save()
            return JsonResponse(schedule_serializer.data, status=status.HTTP_201_CREATED)
        return JsonResponse(schedule_serializer.errors,status=status.HTTP_400_BAD_REQUEST)
    
    #Delete All
    if request.method == 'DELETE':
        Schedule.objects.all().delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT) 
Example #4
Source File: views.py    From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License 6 votes vote down vote up
def flight_detail(request, pk):
    try:
        schedule = Schedule.objects.get(pk=pk)
    except Schedule.DoesNotExist:
        return HttpResponse(status=status.HTTP_404_NOT_FOUND)
    
    #Retrive one record
    if request.method == 'GET':
        schedule_serializer = ScheduleSerializer(schedule)
        return JsonResponse(schedule_serializer.data)

    #Update one record
    if request.method == 'PUT':
        schedule_data = JSONParser().parser(request)
        schedule_serializer = ScheduleSerializer(schedule, data=schedule_data)
        if schedule_serializer.is_valid():
            schedule_serializer.save()
            return JsonResponse(schedule_serializer.data)
        return JsonResponse(schedule_serializer.errors,status=status.HTTP_400_BAD_REQUEST)

    #Delete one record
    if request.method == 'DLETE':
        schedule.delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT) 
Example #5
Source File: views.py    From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License 6 votes vote down vote up
def flight_list(request):
    #Get all
    if request.method == 'GET':
        schedules = Schedule.objects.all()
        schedules_serializer = ScheduleSerializer(schedules,many=True)
        return JsonResponse(schedules_serializer.data, safe=False)
    
    #Add one
    if request.method == 'POST':
        schedule_data = JSONParser().parser(request)
        schedule_serializer = ScheduleSerializer(data=schedule_data)
        if schedule_serializer.is_valid():
            schedule_serializer.save()
            return JsonResponse(schedule_serializer.data, status=status.HTTP_201_CREATED)
        return JsonResponse(schedule_serializer.errors,status=status.HTTP_400_BAD_REQUEST)
    
    #Delete All
    if request.method == 'DELETE':
        Schedule.objects.all().delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT) 
Example #6
Source File: views.py    From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License 6 votes vote down vote up
def flight_list(request):
    #Get all
    if request.method == 'GET':
        schedules = Schedule.objects.all()
        schedules_serializer = ScheduleSerializer(schedules,many=True)
        return JsonResponse(schedules_serializer.data, safe=False)
    
    #Add one
    if request.method == 'POST':
        schedule_data = JSONParser().parser(request)
        schedule_serializer = ScheduleSerializer(data=schedule_data)
        if schedule_serializer.is_valid():
            schedule_serializer.save()
            return JsonResponse(schedule_serializer.data, status=status.HTTP_201_CREATED)
        return JsonResponse(schedule_serializer.errors,status=status.HTTP_400_BAD_REQUEST)
    
    #Delete All
    if request.method == 'DELETE':
        Schedule.objects.all().delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT) 
Example #7
Source File: views.py    From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License 6 votes vote down vote up
def flight_detail(request, pk):
    try:
        schedule = Schedule.objects.get(pk=pk)
    except Schedule.DoesNotExist:
        return HttpResponse(status=status.HTTP_404_NOT_FOUND)
    
    #Retrive one record
    if request.method == 'GET':
        schedule_serializer = ScheduleSerializer(schedule)
        return JsonResponse(schedule_serializer.data)

    #Update one record
    if request.method == 'PUT':
        schedule_data = JSONParser().parser(request)
        schedule_serializer = ScheduleSerializer(schedule, data=schedule_data)
        if schedule_serializer.is_valid():
            schedule_serializer.save()
            return JsonResponse(schedule_serializer.data)
        return JsonResponse(schedule_serializer.errors,status=status.HTTP_400_BAD_REQUEST)

    #Delete one record
    if request.method == 'DLETE':
        schedule.delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT) 
Example #8
Source File: views.py    From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License 6 votes vote down vote up
def flight_list(request):
    #Get all
    if request.method == 'GET':
        schedules = Schedule.objects.all()
        schedules_serializer = ScheduleSerializer(schedules,many=True)
        return JsonResponse(schedules_serializer.data, safe=False)
    
    #Add one
    if request.method == 'POST':
        schedule_data = JSONParser().parser(request)
        schedule_serializer = ScheduleSerializer(data=schedule_data)
        if schedule_serializer.is_valid():
            schedule_serializer.save()
            return JsonResponse(schedule_serializer.data, status=status.HTTP_201_CREATED)
        return JsonResponse(schedule_serializer.errors,status=status.HTTP_400_BAD_REQUEST)
    
    #Delete All
    if request.method == 'DELETE':
        Schedule.objects.all().delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT) 
Example #9
Source File: views.py    From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License 6 votes vote down vote up
def flight_detail(request, pk):
    try:
        schedule = Schedule.objects.get(pk=pk)
    except Schedule.DoesNotExist:
        return HttpResponse(status=status.HTTP_404_NOT_FOUND)
    
    #Retrive one record
    if request.method == 'GET':
        schedule_serializer = ScheduleSerializer(schedule)
        return JsonResponse(schedule_serializer.data)

    #Update one record
    if request.method == 'PUT':
        schedule_data = JSONParser().parser(request)
        schedule_serializer = ScheduleSerializer(schedule, data=schedule_data)
        if schedule_serializer.is_valid():
            schedule_serializer.save()
            return JsonResponse(schedule_serializer.data)
        return JsonResponse(schedule_serializer.errors,status=status.HTTP_400_BAD_REQUEST)

    #Delete one record
    if request.method == 'DLETE':
        schedule.delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT) 
Example #10
Source File: views.py    From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License 6 votes vote down vote up
def flight_list(request):
    #Get all
    if request.method == 'GET':
        schedules = Schedule.objects.all()
        schedules_serializer = ScheduleSerializer(schedules,many=True)
        return JsonResponse(schedules_serializer.data, safe=False)
    
    #Add one
    if request.method == 'POST':
        schedule_data = JSONParser().parser(request)
        schedule_serializer = ScheduleSerializer(data=schedule_data)
        if schedule_serializer.is_valid():
            schedule_serializer.save()
            return JsonResponse(schedule_serializer.data, status=status.HTTP_201_CREATED)
        return JsonResponse(schedule_serializer.errors,status=status.HTTP_400_BAD_REQUEST)
    
    #Delete All
    if request.method == 'DELETE':
        Schedule.objects.all().delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT) 
Example #11
Source File: views.py    From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License 6 votes vote down vote up
def flight_list(request):
    #Get all
    if request.method == 'GET':
        schedules = Schedule.objects.all()
        schedules_serializer = ScheduleSerializer(schedules,many=True)
        return JsonResponse(schedules_serializer.data, safe=False)
    
    #Add one
    if request.method == 'POST':
        schedule_data = JSONParser().parser(request)
        schedule_serializer = ScheduleSerializer(data=schedule_data)
        if schedule_serializer.is_valid():
            schedule_serializer.save()
            return JsonResponse(schedule_serializer.data, status=status.HTTP_201_CREATED)
        return JsonResponse(schedule_serializer.errors,status=status.HTTP_400_BAD_REQUEST)
    
    #Delete All
    if request.method == 'DELETE':
        Schedule.objects.all().delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT) 
Example #12
Source File: views.py    From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License 6 votes vote down vote up
def flight_detail(request, pk):
    try:
        schedule = Schedule.objects.get(pk=pk)
    except Schedule.DoesNotExist:
        return HttpResponse(status=status.HTTP_404_NOT_FOUND)
    
    #Retrive one record
    if request.method == 'GET':
        schedule_serializer = ScheduleSerializer(schedule)
        return JsonResponse(schedule_serializer.data)

    #Update one record
    if request.method == 'PUT':
        schedule_data = JSONParser().parser(request)
        schedule_serializer = ScheduleSerializer(schedule, data=schedule_data)
        if schedule_serializer.is_valid():
            schedule_serializer.save()
            return JsonResponse(schedule_serializer.data)
        return JsonResponse(schedule_serializer.errors,status=status.HTTP_400_BAD_REQUEST)

    #Delete one record
    if request.method == 'DLETE':
        schedule.delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT) 
Example #13
Source File: views.py    From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License 6 votes vote down vote up
def flight_list(request):
    #Get all
    if request.method == 'GET':
        schedules = Schedule.objects.all()
        schedules_serializer = ScheduleSerializer(schedules,many=True)
        return JsonResponse(schedules_serializer.data, safe=False)
    
    #Add one
    if request.method == 'POST':
        schedule_data = JSONParser().parser(request)
        schedule_serializer = ScheduleSerializer(data=schedule_data)
        if schedule_serializer.is_valid():
            schedule_serializer.save()
            return JsonResponse(schedule_serializer.data, status=status.HTTP_201_CREATED)
        return JsonResponse(schedule_serializer.errors,status=status.HTTP_400_BAD_REQUEST)
    
    #Delete All
    if request.method == 'DELETE':
        Schedule.objects.all().delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT) 
Example #14
Source File: views.py    From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License 6 votes vote down vote up
def flight_detail(request, pk):
    try:
        schedule = Schedule.objects.get(pk=pk)
    except Schedule.DoesNotExist:
        return HttpResponse(status=status.HTTP_404_NOT_FOUND)
    
    #Retrive one record
    if request.method == 'GET':
        schedule_serializer = ScheduleSerializer(schedule)
        return JsonResponse(schedule_serializer.data)

    #Update one record
    if request.method == 'PUT':
        schedule_data = JSONParser().parser(request)
        schedule_serializer = ScheduleSerializer(schedule, data=schedule_data)
        if schedule_serializer.is_valid():
            schedule_serializer.save()
            return JsonResponse(schedule_serializer.data)
        return JsonResponse(schedule_serializer.errors,status=status.HTTP_400_BAD_REQUEST)

    #Delete one record
    if request.method == 'DLETE':
        schedule.delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT) 
Example #15
Source File: views.py    From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License 6 votes vote down vote up
def flight_list(request):
    #Get all
    if request.method == 'GET':
        schedules = Schedule.objects.all()
        schedules_serializer = ScheduleSerializer(schedules,many=True)
        return JsonResponse(schedules_serializer.data, safe=False)
    
    #Add one
    if request.method == 'POST':
        schedule_data = JSONParser().parser(request)
        schedule_serializer = ScheduleSerializer(data=schedule_data)
        if schedule_serializer.is_valid():
            schedule_serializer.save()
            return JsonResponse(schedule_serializer.data, status=status.HTTP_201_CREATED)
        return JsonResponse(schedule_serializer.errors,status=status.HTTP_400_BAD_REQUEST)
    
    #Delete All
    if request.method == 'DELETE':
        Schedule.objects.all().delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT) 
Example #16
Source File: views.py    From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License 6 votes vote down vote up
def flight_list(request):
    #Get all
    if request.method == 'GET':
        schedules = Schedule.objects.all()
        schedules_serializer = ScheduleSerializer(schedules,many=True)
        return JsonResponse(schedules_serializer.data, safe=False)
    
    #Add one
    if request.method == 'POST':
        schedule_data = JSONParser().parser(request)
        schedule_serializer = ScheduleSerializer(data=schedule_data)
        if schedule_serializer.is_valid():
            schedule_serializer.save()
            return JsonResponse(schedule_serializer.data, status=status.HTTP_201_CREATED)
        return JsonResponse(schedule_serializer.errors,status=status.HTTP_400_BAD_REQUEST)
    
    #Delete All
    if request.method == 'DELETE':
        Schedule.objects.all().delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT) 
Example #17
Source File: views.py    From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License 6 votes vote down vote up
def flight_detail(request, pk):
    try:
        schedule = Schedule.objects.get(pk=pk)
    except Schedule.DoesNotExist:
        return HttpResponse(status=status.HTTP_404_NOT_FOUND)
    
    #Retrive one record
    if request.method == 'GET':
        schedule_serializer = ScheduleSerializer(schedule)
        return JsonResponse(schedule_serializer.data)

    #Update one record
    if request.method == 'PUT':
        schedule_data = JSONParser().parser(request)
        schedule_serializer = ScheduleSerializer(schedule, data=schedule_data)
        if schedule_serializer.is_valid():
            schedule_serializer.save()
            return JsonResponse(schedule_serializer.data)
        return JsonResponse(schedule_serializer.errors,status=status.HTTP_400_BAD_REQUEST)

    #Delete one record
    if request.method == 'DLETE':
        schedule.delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT) 
Example #18
Source File: views.py    From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License 6 votes vote down vote up
def flight_list(request):
    #Get all
    if request.method == 'GET':
        schedules = Schedule.objects.all()
        schedules_serializer = ScheduleSerializer(schedules,many=True)
        return JsonResponse(schedules_serializer.data, safe=False)
    
    #Add one
    if request.method == 'POST':
        schedule_data = JSONParser().parser(request)
        schedule_serializer = ScheduleSerializer(data=schedule_data)
        if schedule_serializer.is_valid():
            schedule_serializer.save()
            return JsonResponse(schedule_serializer.data, status=status.HTTP_201_CREATED)
        return JsonResponse(schedule_serializer.errors,status=status.HTTP_400_BAD_REQUEST)
    
    #Delete All
    if request.method == 'DELETE':
        Schedule.objects.all().delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT) 
Example #19
Source File: views.py    From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License 6 votes vote down vote up
def flight_detail(request, pk):
    try:
        schedule = Schedule.objects.get(pk=pk)
    except Schedule.DoesNotExist:
        return HttpResponse(status=status.HTTP_404_NOT_FOUND)
    
    #Retrive one record
    if request.method == 'GET':
        schedule_serializer = ScheduleSerializer(schedule)
        return JsonResponse(schedule_serializer.data)

    #Update one record
    if request.method == 'PUT':
        schedule_data = JSONParser().parser(request)
        schedule_serializer = ScheduleSerializer(schedule, data=schedule_data)
        if schedule_serializer.is_valid():
            schedule_serializer.save()
            return JsonResponse(schedule_serializer.data)
        return JsonResponse(schedule_serializer.errors,status=status.HTTP_400_BAD_REQUEST)

    #Delete one record
    if request.method == 'DLETE':
        schedule.delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT) 
Example #20
Source File: views.py    From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License 6 votes vote down vote up
def flight_list(request):
    #Get all
    if request.method == 'GET':
        schedules = Schedule.objects.all()
        schedules_serializer = ScheduleSerializer(schedules,many=True)
        return JsonResponse(schedules_serializer.data, safe=False)
    
    #Add one
    if request.method == 'POST':
        schedule_data = JSONParser().parser(request)
        schedule_serializer = ScheduleSerializer(data=schedule_data)
        if schedule_serializer.is_valid():
            schedule_serializer.save()
            return JsonResponse(schedule_serializer.data, status=status.HTTP_201_CREATED)
        return JsonResponse(schedule_serializer.errors,status=status.HTTP_400_BAD_REQUEST)
    
    #Delete All
    if request.method == 'DELETE':
        Schedule.objects.all().delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT) 
Example #21
Source File: views.py    From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License 6 votes vote down vote up
def flight_list(request):
    #Get all
    if request.method == 'GET':
        schedules = Schedule.objects.all()
        schedules_serializer = ScheduleSerializer(schedules,many=True)
        return JsonResponse(schedules_serializer.data, safe=False)
    
    #Add one
    if request.method == 'POST':
        schedule_data = JSONParser().parser(request)
        schedule_serializer = ScheduleSerializer(data=schedule_data)
        if schedule_serializer.is_valid():
            schedule_serializer.save()
            return JsonResponse(schedule_serializer.data, status=status.HTTP_201_CREATED)
        return JsonResponse(schedule_serializer.errors,status=status.HTTP_400_BAD_REQUEST)
    
    #Delete All
    if request.method == 'DELETE':
        Schedule.objects.all().delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT) 
Example #22
Source File: views.py    From Learning-Path-Learn-Web-Development-with-Python with MIT License 6 votes vote down vote up
def toy_detail(request, pk):
    try:
        toy = Toy.objects.get(pk=pk)
    except Toy.DoesNotExist:
        return HttpResponse(status=status.HTTP_404_NOT_FOUND)
    if request.method == 'GET':
        toy_serializer = ToySerializer(toy)
        return JSONResponse(toy_serializer.data)
    elif request.method == 'PUT':
        toy_data = JSONParser().parse(request)
        toy_serializer = ToySerializer(toy, data=toy_data)
        if toy_serializer.is_valid():
            toy_serializer.save()
            return JSONResponse(toy_serializer.data)
        return JSONResponse(toy_serializer.errors, \
            status=status.HTTP_400_BAD_REQUEST)
    elif request.method == 'DELETE':
        toy.delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT) 
Example #23
Source File: views.py    From ansible-mezzanine with MIT License 6 votes vote down vote up
def snippet_list(request):
    """
    List all code snippets, or create a new snippet.
    """
    if request.method == 'GET':
        snippets = Snippet.objects.all()
        serializer = SnippetSerializer(snippets, many=True)
        return JSONResponse(serializer.data)

    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = SnippetSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data, status=201)
        return JSONResponse(serializer.errors, status=400) 
Example #24
Source File: views.py    From ansible-mezzanine with MIT License 6 votes vote down vote up
def snippet_detail(request, pk):
    """
    Retrieve, update or delete a code snippet.
    """
    try:
        snippet = Snippet.objects.get(pk=pk)
    except Snippet.DoesNotExist:
        return HttpResponse(status=404)

    if request.method == 'GET':
        serializer = SnippetSerializer(snippet)
        return JSONResponse(serializer.data)

    elif request.method == 'PUT':
        data = JSONParser().parse(request)
        serializer = SnippetSerializer(snippet, data=data)
        if serializer.is_valid():
            serializer.save()
            return JSONResponse(serializer.data)
        return JSONResponse(serializer.errors, status=400)

    elif request.method == 'DELETE':
        snippet.delete()
        return HttpResponse(status=204) 
Example #25
Source File: views.py    From Learning-Path-Learn-Web-Development-with-Python with MIT License 6 votes vote down vote up
def toy_detail(request, pk):
    try:
        toy = Toy.objects.get(pk=pk)
    except Toy.DoesNotExist:
        return HttpResponse(status=status.HTTP_404_NOT_FOUND)
    if request.method == 'GET':
        toy_serializer = ToySerializer(toy)
        return JSONResponse(toy_serializer.data)
    elif request.method == 'PUT':
        toy_data = JSONParser().parse(request)
        toy_serializer = ToySerializer(toy, data=toy_data)
        if toy_serializer.is_valid():
            toy_serializer.save()
            return JSONResponse(toy_serializer.data)
        return JSONResponse(toy_serializer.errors, \
            status=status.HTTP_400_BAD_REQUEST)
    elif request.method == 'DELETE':
        toy.delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT) 
Example #26
Source File: views.py    From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License 6 votes vote down vote up
def flight_detail(request, pk):
    try:
        schedule = Schedule.objects.get(pk=pk)
    except Schedule.DoesNotExist:
        return HttpResponse(status=status.HTTP_404_NOT_FOUND)
    
    #Retrive one record
    if request.method == 'GET':
        schedule_serializer = ScheduleSerializer(schedule)
        return JsonResponse(schedule_serializer.data)

    #Update one record
    if request.method == 'PUT':
        schedule_data = JSONParser().parser(request)
        schedule_serializer = ScheduleSerializer(schedule, data=schedule_data)
        if schedule_serializer.is_valid():
            schedule_serializer.save()
            return JsonResponse(schedule_serializer.data)
        return JsonResponse(schedule_serializer.errors,status=status.HTTP_400_BAD_REQUEST)

    #Delete one record
    if request.method == 'DLETE':
        schedule.delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT) 
Example #27
Source File: views.py    From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License 6 votes vote down vote up
def flight_list(request):
    #Get all
    if request.method == 'GET':
        schedules = Schedule.objects.all()
        schedules_serializer = ScheduleSerializer(schedules,many=True)
        return JsonResponse(schedules_serializer.data, safe=False)
    
    #Add one
    if request.method == 'POST':
        schedule_data = JSONParser().parser(request)
        schedule_serializer = ScheduleSerializer(data=schedule_data)
        if schedule_serializer.is_valid():
            schedule_serializer.save()
            return JsonResponse(schedule_serializer.data, status=status.HTTP_201_CREATED)
        return JsonResponse(schedule_serializer.errors,status=status.HTTP_400_BAD_REQUEST)
    
    #Delete All
    if request.method == 'DELETE':
        Schedule.objects.all().delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT) 
Example #28
Source File: views.py    From Django-RESTful-Web-Services with MIT License 6 votes vote down vote up
def toy_detail(request, pk):
    try:
        toy = Toy.objects.get(pk=pk)
    except Toy.DoesNotExist:
        return HttpResponse(status=status.HTTP_404_NOT_FOUND)
    if request.method == 'GET':
        toy_serializer = ToySerializer(toy)
        return JSONResponse(toy_serializer.data)
    elif request.method == 'PUT':
        toy_data = JSONParser().parse(request)
        toy_serializer = ToySerializer(toy, data=toy_data)
        if toy_serializer.is_valid():
            toy_serializer.save()
            return JSONResponse(toy_serializer.data)
        return JSONResponse(toy_serializer.errors, \
            status=status.HTTP_400_BAD_REQUEST)
    elif request.method == 'DELETE':
        toy.delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT) 
Example #29
Source File: views.py    From -Full-Stack-Web-Development-with-Django-2.x-and-Angular-8 with MIT License 6 votes vote down vote up
def flight_detail(request, pk):
    try:
        schedule = Schedule.objects.get(pk=pk)
    except Schedule.DoesNotExist:
        return HttpResponse(status=status.HTTP_404_NOT_FOUND)
    
    #Retrive one record
    if request.method == 'GET':
        schedule_serializer = ScheduleSerializer(schedule)
        return JsonResponse(schedule_serializer.data)

    #Update one record
    if request.method == 'PUT':
        schedule_data = JSONParser().parser(request)
        schedule_serializer = ScheduleSerializer(schedule, data=schedule_data)
        if schedule_serializer.is_valid():
            schedule_serializer.save()
            return JsonResponse(schedule_serializer.data)
        return JsonResponse(schedule_serializer.errors,status=status.HTTP_400_BAD_REQUEST)

    #Delete one record
    if request.method == 'DLETE':
        schedule.delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT) 
Example #30
Source File: views.py    From Django-RESTful-Web-Services with MIT License 6 votes vote down vote up
def toy_detail(request, pk):
    try:
        toy = Toy.objects.get(pk=pk)
    except Toy.DoesNotExist:
        return HttpResponse(status=status.HTTP_404_NOT_FOUND)
    if request.method == 'GET':
        toy_serializer = ToySerializer(toy)
        return JSONResponse(toy_serializer.data)
    elif request.method == 'PUT':
        toy_data = JSONParser().parse(request)
        toy_serializer = ToySerializer(toy, data=toy_data)
        if toy_serializer.is_valid():
            toy_serializer.save()
            return JSONResponse(toy_serializer.data)
        return JSONResponse(toy_serializer.errors, \
            status=status.HTTP_400_BAD_REQUEST)
    elif request.method == 'DELETE':
        toy.delete()
        return HttpResponse(status=status.HTTP_204_NO_CONTENT)