def event_detail(request, pk): """ Retrieve, update or delete a code event. """ try: event = Event.objects.get(pk=pk) except Event.DoesNotExist: return HttpResponse(status=404) if request.method == 'GET': serializer = EventSerializer(event) return JSONResponse(serializer.data) elif request.method == 'PUT': # slugify the name before saving request.POST['slug'] = slugify(request.POST['name']) data = JSONParser().parse(request) serializer = EventSerializer(event, data=data) if serializer.is_valid(): serializer.save() return JSONResponse(serializer.data) return JSONResponse(serializer.errors, status=400) elif request.method == 'DELETE': event.delete() return HttpResponse(status=204)
def event_list(request, id=None, slug=None, category_slug=None): if request.method == 'GET': if category_slug: events = Event.objects.filter(category__slug=category_slug) elif id and slug: events = Event.objects.filter(id=id, slug=slug) else: events = Event.objects.all() serializer = EventSerializer(events, many=True) return JSONResponse(serializer.data) elif request.method == 'POST': data = JSONParser().parse(request) serializer = EventSerializer(data=data) if serializer.is_valid(): serializer.save() return JSONResponse(serializer.data, status=201) else: return JSONResponse(serializer.errors, status=400)