Esempio n. 1
0
def gravity_add_board(request):
    # TODO - Add user permissioning
    # if not request.user.has_perm('app.add_device'):
    #     messages.error(request, 'Your account is not permissioned to add devices. Please contact an admin')
    #     return redirect("/")

    manual_form = forms.ManualForm()
    tilt_form = forms.TiltCreateForm()

    if request.POST:
        if request.POST['sensor_family'] == "manual":
            manual_form = forms.ManualForm(request.POST)
            if manual_form.is_valid():
                sensor = manual_form.save()
                messages.success(request, 'New manual sensor added')

                return redirect('gravity_list')

        elif request.POST['sensor_family'] == "tilt":
            tilt_form = forms.TiltCreateForm(request.POST)
            if tilt_form.is_valid():
                sensor = GravitySensor(
                    name=tilt_form.cleaned_data['name'],
                    temp_format=tilt_form.cleaned_data['temp_format'],
                    sensor_type=GravitySensor.SENSOR_TILT,
                )
                sensor.save()

                tilt_config = TiltConfiguration(
                    sensor=sensor,
                    color=tilt_form.cleaned_data['color'],
                )
                tilt_config.save()
                messages.success(request, 'New tilt sensor added')

                return redirect('gravity_list')

        messages.error(request, 'Error adding sensor')

    # Basically, if we don't get redirected, in every case we're just outputting the same template
    return render_with_devices(request,
                               template_name='gravity/gravity_family.html',
                               context={
                                   'manual_form': manual_form,
                                   'tilt_form': tilt_form
                               })
Esempio n. 2
0
def gravity_add_board(request):
    # TODO - Add user permissioning
    # if not request.user.has_perm('app.add_device'):
    #     messages.error(request, 'Your account is not permissioned to add devices. Please contact an admin')
    #     return redirect("/")

    manual_form = forms.ManualForm()
    tilt_form = forms.TiltCreateForm()
    ispindel_form = forms.IspindelCreateForm()

    if request.POST:
        if request.POST['sensor_family'] == "manual":
            manual_form = forms.ManualForm(request.POST)
            if manual_form.is_valid():
                sensor = manual_form.save()
                messages.success(request, 'New manual sensor added')

                return redirect('gravity_list')

        elif request.POST['sensor_family'] == "tilt":
            tilt_form = forms.TiltCreateForm(request.POST)
            if tilt_form.is_valid():

                if tilt_form.cleaned_data[
                        'connection_type'] == TiltConfiguration.CONNECTION_BRIDGE:
                    if tilt_form.cleaned_data['tiltbridge'] == '+':
                        tilt_bridge = TiltBridge(
                            mdns_id=tilt_form.
                            cleaned_data['tiltbridge_mdns_id'],
                            name=tilt_form.cleaned_data['tiltbridge_name'])
                        tilt_bridge.save()
                        messages.success(request, 'New TiltBridge created')
                    else:
                        tilt_bridge = TiltBridge.objects.get(
                            mdns_id=tilt_form.cleaned_data['tiltbridge'])
                else:
                    # We don't need to link to a bridge, so leave it null
                    tilt_bridge = None

                sensor = GravitySensor(
                    name=tilt_form.cleaned_data['name'],
                    temp_format=tilt_form.cleaned_data['temp_format'],
                    sensor_type=GravitySensor.SENSOR_TILT,
                )
                sensor.save()

                tilt_config = TiltConfiguration(
                    sensor=sensor,
                    color=tilt_form.cleaned_data['color'],
                    connection_type=tilt_form.cleaned_data['connection_type'],
                    tiltbridge=tilt_bridge,
                )
                tilt_config.save()
                messages.success(request, 'New tilt sensor added')

                return redirect('gravity_list')

        elif request.POST['sensor_family'] == "ispindel":
            ispindel_form = forms.IspindelCreateForm(request.POST)
            if ispindel_form.is_valid():
                sensor = GravitySensor(
                    name=ispindel_form.cleaned_data['name'],
                    temp_format=ispindel_form.cleaned_data['temp_format'],
                    sensor_type=GravitySensor.SENSOR_ISPINDEL,
                )
                sensor.save()

                ispindel_config = IspindelConfiguration(
                    sensor=sensor,
                    name_on_device=ispindel_form.
                    cleaned_data['name_on_device'],
                )
                ispindel_config.save()

                messages.success(request, 'New iSpindel sensor added')

                return redirect('gravity_ispindel_setup', sensor_id=sensor.id)

        # TODO - Display form validation error message
        messages.error(request, 'Error adding sensor - See below for details')

    # Basically, if we don't get redirected, in every case we're just outputting the same template
    return render(request,
                  template_name='gravity/gravity_add_sensor.html',
                  context={
                      'manual_form': manual_form,
                      'tilt_form': tilt_form,
                      'ispindel_form': ispindel_form
                  })