# Buggy code
def page_pid():
    """Display PID settings"""
    pids = PID.query.all()
    relay = Relay.query.all()
    sensor = Sensor.query.all()

    display_order = csv_to_list_of_int(DisplayOrder.query.first().pid)

    form_add_pid = flaskforms.PIDAdd()
    form_mod_pid_base = flaskforms.PIDModBase()
    form_mod_pid_relay = flaskforms.PIDModRelay()
    form_mod_pid_pwm = flaskforms.PIDModPWM()

    method = Method.query.all()

    # Create list of file names from the pid_options directory
    # Used in generating the correct options for each PID
    pid_templates = []
    pid_path = os.path.join(
        INSTALL_DIRECTORY, "mycodo/mycodo_flask/templates/pages/pid_options"
    )
    for _, _, file_names in os.walk(pid_path):
        pid_templates.extend(file_names)
        break

    if request.method == "POST":
        if not flaskutils.user_has_permission("edit_controllers"):
            return redirect(url_for("general_routes.home"))

        form_name = request.form["form-name"]
        if form_name == "addPID":
            flaskutils.pid_add(form_add_pid)
        elif form_name == "modPID":
            if form_mod_pid_base.save.data:
                flaskutils.pid_mod(
                    form_mod_pid_base, form_mod_pid_pwm, form_mod_pid_relay
                )
            elif form_mod_pid_base.delete.data:
                flaskutils.pid_del(form_mod_pid_base.pid_id.data)
            elif form_mod_pid_base.reorder_up.data:
                flaskutils.pid_reorder(
                    form_mod_pid_base.pid_id.data, display_order, "up"
                )
            elif form_mod_pid_base.reorder_down.data:
                flaskutils.pid_reorder(
                    form_mod_pid_base.pid_id.data, display_order, "down"
                )
            elif form_mod_pid_base.activate.data:
                flaskutils.pid_activate(form_mod_pid_base.pid_id.data)
            elif form_mod_pid_base.deactivate.data:
                flaskutils.pid_deactivate(form_mod_pid_base.pid_id.data)
            elif form_mod_pid_base.hold.data:
                flaskutils.pid_manipulate(form_mod_pid_base.pid_id.data, "Hold")
            elif form_mod_pid_base.pause.data:
                flaskutils.pid_manipulate(form_mod_pid_base.pid_id.data, "Pause")
            elif form_mod_pid_base.resume.data:
                flaskutils.pid_manipulate(form_mod_pid_base.pid_id.data, "Resume")

        return redirect("/pid")

    return render_template(
        "pages/pid.html",
        method=method,
        pids=pids,
        pid_templates=pid_templates,
        relay=relay,
        sensor=sensor,
        displayOrder=display_order,
        form_add_pid=form_add_pid,
        form_mod_pid_base=form_mod_pid_base,
        form_mod_pid_pwm=form_mod_pid_pwm,
        form_mod_pid_relay=form_mod_pid_relay,
    )


# Traceback (IOError)
2017-06-07 11:03:18,941 - mycodo.sensors.raspi - ERROR - RaspberryPiCPUTemp.get_measurement() method raised IOError: [Errno 24] Too many open files: '/sys/class/thermal/thermal_zone0/temp'
2017-06-07 11:03:20,138 - mycodo.pid_1 - ERROR - Run Error: (sqlite3.OperationalError) unable to open database file

========= Remote Traceback (1) =========
Traceback (most recent call last):
File "/var/www/mycodo/env/local/lib/python2.7/site-packages/rpyc/core/protocol.py", line 329, in _dispatch_request
File "/var/www/mycodo/env/local/lib/python2.7/site-packages/rpyc/core/protocol.py", line 583, in _handle_call
File "/var/www/mycodo/mycodo/mycodo_daemon.py", line 201, in exposed_relay_off
File "/var/www/mycodo/mycodo/mycodo_daemon.py", line 553, in relay_off
File "/var/www/mycodo/mycodo/controller_relay.py", line 348, in relay_on_off
self.check_conditionals(relay_id, duration)
File "/var/www/mycodo/mycodo/controller_relay.py", line 397, in check_conditionals
for each_conditional in conditionals.all():

# Fixed code
def page_pid():
    """Display PID settings"""
    method = Method.query.all()
    pids = PID.query.all()
    relay = Relay.query.all()
    sensor = Sensor.query.all()

    sensor_choices = flaskutils.choices_sensors(sensor)

    display_order = csv_to_list_of_int(DisplayOrder.query.first().pid)

    form_add_pid = flaskforms.PIDAdd()
    form_mod_pid_base = flaskforms.PIDModBase()
    form_mod_pid_relay = flaskforms.PIDModRelay()
    form_mod_pid_pwm = flaskforms.PIDModPWM()

    # Create list of file names from the pid_options directory
    # Used in generating the correct options for each PID
    pid_templates = []
    pid_path = os.path.join(
        INSTALL_DIRECTORY, "mycodo/mycodo_flask/templates/pages/pid_options"
    )
    for _, _, file_names in os.walk(pid_path):
        pid_templates.extend(file_names)
        break

    if request.method == "POST":
        if not flaskutils.user_has_permission("edit_controllers"):
            return redirect(url_for("general_routes.home"))

        form_name = request.form["form-name"]
        if form_name == "addPID":
            flaskutils.pid_add(form_add_pid)
        elif form_name == "modPID":
            if form_mod_pid_base.save.data:
                flaskutils.pid_mod(
                    form_mod_pid_base, form_mod_pid_pwm, form_mod_pid_relay
                )
            elif form_mod_pid_base.delete.data:
                flaskutils.pid_del(form_mod_pid_base.pid_id.data)
            elif form_mod_pid_base.reorder_up.data:
                flaskutils.pid_reorder(
                    form_mod_pid_base.pid_id.data, display_order, "up"
                )
            elif form_mod_pid_base.reorder_down.data:
                flaskutils.pid_reorder(
                    form_mod_pid_base.pid_id.data, display_order, "down"
                )
            elif form_mod_pid_base.activate.data:
                flaskutils.pid_activate(form_mod_pid_base.pid_id.data)
            elif form_mod_pid_base.deactivate.data:
                flaskutils.pid_deactivate(form_mod_pid_base.pid_id.data)
            elif form_mod_pid_base.hold.data:
                flaskutils.pid_manipulate(form_mod_pid_base.pid_id.data, "Hold")
            elif form_mod_pid_base.pause.data:
                flaskutils.pid_manipulate(form_mod_pid_base.pid_id.data, "Pause")
            elif form_mod_pid_base.resume.data:
                flaskutils.pid_manipulate(form_mod_pid_base.pid_id.data, "Resume")

        return redirect("/pid")

    return render_template(
        "pages/pid.html",
        method=method,
        pids=pids,
        pid_templates=pid_templates,
        relay=relay,
        sensor=sensor,
        sensor_choices=sensor_choices,
        displayOrder=display_order,
        form_add_pid=form_add_pid,
        form_mod_pid_base=form_mod_pid_base,
        form_mod_pid_pwm=form_mod_pid_pwm,
        form_mod_pid_relay=form_mod_pid_relay,
    )