def run_web_hook(request, command, run):
    """
    The web hook lets the message queue tell us how the command got on. It 
    accepts a JSON document with the status and the output from the command.
    When we recieve it we also change the status.
    """

    # we're dealing with a post request
    if request.POST:
        try:
            # we try and get the run based on the url parameters
            existing_run = Run.objects.get(id=run, command__slug=command)

            # the web hook will only run against in progress tasks
            # so in theory should only be run once.
            if existing_run.status == "in_progress":

                # sample json input
                # json = """{
                #     "status": 0,
                #     "output": "bob2"
                # }"""
                
                # get json document from post body in request.POST
                json = request.raw_post_data

                # not try parse the JSON
                try:
                    obj = simplejson.loads(json)
                except __HOLE__, e:
                    # invalid input
                    return HttpResponseBadRequest()

                # get the status code and convert it to our values
                if obj['status'] == 0:
                    code = "succeeded"
                else:
                    code = "failed"

                # set the attributes on our run and save it
                existing_run.status = code # succeeded failed in_progress
                existing_run.output = obj['output']
                existing_run.save()

                # return a 200 code as everything went OK
                return HttpResponse(
                    content_type = 'application/javascript; charset=utf8'
                )

            else:
                # this run is not in progress, only the first response is recorded
                # should be client error
                return HttpResponseBadRequest()

        except Run.DoesNotExist:
            # we didn't find a run, so throw a 404
            return HttpResponseNotFound()

    else:
        # should be method not allowed as we only respond to post
        return HttpResponseNotAllowed(['POST'])
dataset/ETHPy150Open garethr/Asteroid/asteroid/apps/runner/views.py/run_web_hook