<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def toggle_sensor(request, sensorname):
 """ This is used only if websocket fails """

    if service.read_only:
        service.logger.warning("Could not perform operation: read only mode enabled")
        raise Http404
    source = request.GET.get('source', 'main')
    sensor = service.system.namespace[sensorname]
    sensor.status = not sensor.status
    service.system.flush()
    return HttpResponseRedirect(reverse(source))