def providers_list(request): """ List all providers, or create a new provider. """ if request.method == 'GET': snippets = MozioProvider.objects.all() serializer = MozioProviderSerializer(snippets, many=True) return JSONResponse(serializer.data) elif request.method == 'POST': data = JSONParser().parse(request) serializer = MozioProviderSerializer(data=data) if serializer.is_valid(): serializer.save() return JSONResponse(serializer.data, status=201) return JSONResponse(serializer.errors, status=400)
def provider_detail(request, pk): """ Retrieve, update or delete a provider. """ try: snippet = MozioProvider.objects.get(pk=pk) except MozioProvider.DoesNotExist: return HttpResponse(status=404) if request.method == 'GET': serializer = MozioProviderSerializer(snippet) return JSONResponse(serializer.data) elif request.method == 'PUT': data = JSONParser().parse(request) serializer = MozioProviderSerializer(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)