def edit(vendor):
    settings = Server.query.filter_by(vendor=vendor).first()
    if settings is None:
        abort(404)                        

    if settings.rrd_file != '':
        initial_has_rrd_file = True
        header = rrdfetch.header(settings.rrd_file)
        columns = [row.columns for row in settings.columns]
    else:
        initial_has_rrd_file = False

    class EditForm(baseForm):
        # Override the validator for the basefrom
        # Want to make sure that we can accept the data by 
        # relaxing the fact that the vendor is unique.
        def validate_vendor(self, field):
            if field.data == '':
                raise ValidationError('Required Field')

    if initial_has_rrd_file is True:
        # Dynamically create the fields based on column names
        # And check if they are in the database
        for column_name in header:
            checkbox_name = column_name
            if column_name in columns:
                setattr(EditForm, checkbox_name, 
                        BooleanField(label=column_name, default=True))
            else:
                setattr(EditForm, checkbox_name, 
                        BooleanField(label=column_name))
    
    setattr(EditForm, 'submit', SubmitField('Update'))

    form = EditForm(port=settings.port, vendor=settings.vendor, 
                    server=settings.server, 
                    software_feature=settings.software, 
                    rrd_file=settings.rrd_file)

    if form.validate_on_submit():
        record = Server.query.filter_by(vendor=vendor).first()
        # Deal with form that has the column names.
        if initial_has_rrd_file is True:
            # Clear out the current options then add the new ones.
            for column_name in header:
                column_record = Columns.query.filter_by(
                    columns=column_name, 
                    server_id=settings.id).first()
                checkbox_state = getattr(form, column_name).data
                # Test the checkbox
                if checkbox_state is True and column_record is None:
                    record.columns.append(Columns(columns=column_name))
                    db.session.add(record)
                elif checkbox_state is False and column_record is not None:
                    db.session.delete(column_record)

        # Deal with the rest of the data to be updated
        record.vendor = form.vendor.data
        record.port = form.port.data
        record.server = form.server.data
        record.software_feature = form.software_feature.data
        
        # Check to see if the rrd file has been changed 
        # if so:
        # Delete all of the column names in the columns table
        # then return this page with the new headers if any
        # headers 
        if record.rrd_file != form.rrd_file.data:
            Columns.query.filter_by(server_id=record.id).delete()
            record.rrd_file = form.rrd_file.data
            db.session.commit()
            return redirect(url_for('edit', vendor=vendor))
        else:
            db.session.commit()
            return redirect(url_for('index'))

    return render_template('config.html', form=form, 
                           vendor=vendor)
def edit(vendor):
    settings = Server.query.filter_by(vendor=vendor).first()
    if settings is None:
        abort(404)

    if settings.rrd_file != '':
        initial_has_rrd_file = True
        header = rrdfetch.header(settings.rrd_file)
        columns = [row.columns for row in settings.columns]
    else:
        initial_has_rrd_file = False

    class EditForm(baseForm):
        # Override the validator for the basefrom
        # Want to make sure that we can accept the data by
        # relaxing the fact that the vendor is unique.
        def validate_vendor(self, field):
            if field.data == '':
                raise ValidationError('Required Field')

    if initial_has_rrd_file is True:
        # Dynamically create the fields based on column names
        # And check if they are in the database
        for column_name in header:
            checkbox_name = column_name
            if column_name in columns:
                setattr(EditForm, checkbox_name,
                        BooleanField(label=column_name, default=True))
            else:
                setattr(EditForm, checkbox_name,
                        BooleanField(label=column_name))

    setattr(EditForm, 'submit', SubmitField('Update'))

    form = EditForm(port=settings.port,
                    vendor=settings.vendor,
                    server=settings.server,
                    software_feature=settings.software,
                    rrd_file=settings.rrd_file)

    if form.validate_on_submit():
        record = Server.query.filter_by(vendor=vendor).first()
        # Deal with form that has the column names.
        if initial_has_rrd_file is True:
            # Clear out the current options then add the new ones.
            for column_name in header:
                column_record = Columns.query.filter_by(
                    columns=column_name, server_id=settings.id).first()
                checkbox_state = getattr(form, column_name).data
                # Test the checkbox
                if checkbox_state is True and column_record is None:
                    record.columns.append(Columns(columns=column_name))
                    db.session.add(record)
                elif checkbox_state is False and column_record is not None:
                    db.session.delete(column_record)

        # Deal with the rest of the data to be updated
        record.vendor = form.vendor.data
        record.port = form.port.data
        record.server = form.server.data
        record.software_feature = form.software_feature.data

        # Check to see if the rrd file has been changed
        # if so:
        # Delete all of the column names in the columns table
        # then return this page with the new headers if any
        # headers
        if record.rrd_file != form.rrd_file.data:
            Columns.query.filter_by(server_id=record.id).delete()
            record.rrd_file = form.rrd_file.data
            db.session.commit()
            return redirect(url_for('edit', vendor=vendor))
        else:
            db.session.commit()
            return redirect(url_for('index'))

    return render_template('config.html', form=form, vendor=vendor)
 def validate_columns(self, field):
     for column_name in field.data.split(','):
         if column_name not in rrdfetch.header(self.rrd_file.data):
             raise ValueError (rrdfetch.header(self.rrd_file.data))
 def validate_columns(self, field):
     for column_name in field.data.split(','):
         if column_name not in rrdfetch.header(self.rrd_file.data):
             raise ValueError(rrdfetch.header(self.rrd_file.data))