Exemple #1
0
def view_powerstrip_switch_all_off(request):
    if request.method == 'GET':
        if request.user.is_staff:
            snippets = Snippet(classname='Powerstrip(device_room)',
                               code='switch_all_off()')
            serializer = SnippetSerializer(snippets)
            return JsonResponse(serializer.data, safe=False)
        else:
            return render(request, 'index_api.html', {})
    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = SnippetSerializer(data=data)
        if serializer.is_valid():
            parsed_data = encode_api_json(data)
            name = parsed_data[0]
            device_room = parsed_data[1]
            method = parsed_data[2]
            if name == "Powerstrip":
                if method == "switch_all_off":
                    Powerstrip(int(device_room)).switch_all_off()
                    return JsonResponse(data, status=201)
            return JsonResponse(serializer.errors, status=400)
        return JsonResponse(serializer.errors, status=400)
Exemple #2
0
def view_hdmi_switch_get_in_out_devices(request):
    if request.method == 'GET':
        if request.user.is_staff:
            snippets = Snippet(classname='HdmiSwitch()',
                               code='get_in_out_devices(status)')
            serializer = SnippetSerializer(snippets)
            return JsonResponse(serializer.data, safe=False)
        else:
            return render(request, 'index_api.html', {})
    elif request.method == 'POST':
        data = JSONParser().parse(request)
        serializer = SnippetSerializer(data=data)
        if serializer.is_valid():
            parsed_data = encode_api_json(data)
            name = parsed_data[0]
            device_id = parsed_data[1]
            method = parsed_data[2]
            status = parsed_data[3]
            if name == "HdmiSwitch":
                if method == "get_in_out_devices":
                    HdmiSwitch().get_in_out_devices(status)
                    return JsonResponse(data, status=201)
            return JsonResponse(serializer.errors, status=400)
        return JsonResponse(serializer.errors, status=400)