📄 editdb.py
字号:
if table: self.options = table.getOptions() class inputFile: """ Class representing a file upload input control. """ type = 'file' name = None value = '' disabled = False def __init__(self): passclass inputTextArea: """ Class representing a textarea input html control. """ type = 'textarea' name = None def __init__(self,rows=20,cols=80): self.rows = rows self.cols = cols self.value = '' self.disabled = Falseclass inputCheckbox: """ Class representing a checkbox input html control. """ type = 'checkbox' name = None def __init__(self,disabled=False): self.value = '0' self.disabled = disabledclass inputHidden: """ Class representing a hidden input html control. """ type = 'hidden' name = None disabled = False def __init__(self,value): self.value = valueclass inputServiceProperties: """ Contains a list of inputServiceProperty inputs. (used by pageService) """ type = 'serviceproperties' disabled = False def __init__(self,propertyList): self.propertyList = propertyListclass inputServiceProperty: """ Class representing a serviceproperty input box. (used by pageService) """ type = 'serviceproperty' disabled = False def __init__(self,title,id,args,optargs,display=True): self.title = title self.id = id self.display = display self.args = args self.optargs = optargsclass editbox: """ Parent class for all the different editboxes which are all added to an editform object. There are normally one editbox per page, but for some pages there are more (there are created three editboxes for the netbox page for example, editbox(main),editboxserial and editboxfunction (which also includes the subcat)). The editbox contains field defitions used by the template to render the forms and functions to fill the form with data from either the database or from a previous http post. """ boxName = ADDNEW_ENTRY boxId = 0 # Current box number keeps track of the box number when there are # more than one editbox in a form. Used by formFill to get correct data #currentBoxNumber = 0 def fill(self): """ Fill this form with data from the database (entry = editId). """ entry = self.table(self.editId) # Set the name of this box to reflect that we are # updating an entry self.boxName = UPDATE_ENTRY self.boxId = self.editId ## TEMPORARY: ## check if this is one of the new pages by checking ## for three instead of two entries in the desc list if len(self.fields[self.fields.keys()[0]]) > 2: # Uses psycopg to fill field values page = pageList[self.page] select = '' first = True keyNumber = {} i = 0 for key in self.fields.keys(): keyNumber[key] = i i+=1 if not first: select += ', ' select += key first = False tables = page.tableName where = page.tableIdKey + "='" + self.editId + "'" # For the benefit of pagePrefix (which must select from vlan too) if hasattr(self,'additionalSQL'): tables += ', vlan' where += self.additionalSQL sql = "SELECT %s FROM %s WHERE %s" % (select, tables, where) result = executeSQLreturn(sql) result = result[0] for key,desc in self.fields.items(): value = result[keyNumber[key]] if value: desc[0].value = str(value) else: # Old style filling with forgetsql for fieldname,desc in self.fields.items(): value = getattr(entry,fieldname) if value: desc[0].value = str(value) def setControlNames(self,controlList=None): """ Set controlnames for the inputs to the same as the fieldnames. """ if not controlList: controlList = self.fields for fieldname,desc in controlList.items(): desc[0].name = fieldname def verifyFields(self,req,status): """ Verify that data entered into fields are of correct type. Eg. integers in FIELD_INTEGER fields. """ for field,desc in self.fields.items(): if req.form.has_key(field): if type(req.form[field]) is list: # Editing several entries for each in req.form[field]: # Do tests here if desc[3] == FIELD_INTEGER: if len(each): try: int(each) except ValueError: error = "Invalid integer: '" +\ str(each) + "'" status.errors.append(error) else: # Editing only one field # Do tests here if desc[3] == FIELD_INTEGER: try: if len(req.form[field]): int(req.form[field]) except ValueError: error = "Invalid integer: '" + \ str(req.form[field]) + "'" status.errors.append(error) return status def hasMissing(self,req): """ Check if any of the required fields are missing in the req.form Returns the name the first missing field, or False Note: keep_blank_values (mod_python) must be True or empty fields won't be present in the form """ missing = False for field,desc in self.fields.items(): # Keep blank values must be switched on, or else the next line # will fail, could be more robust if req.form.has_key(field): if type(req.form[field]) is list: # the field is a list, several entries have been edited for each in req.form[field]: if desc[1] == REQ_TRUE: # this field is required if not len(each): if len(desc) > 2: # desc[2] is real fieldname missing = desc[2] else: # cryptic fieldname (remove this later) missing = field break else: if desc[1] == REQ_TRUE: # tihs field is required if not len(req.form[field]): if len(desc) > 2: # desc[2] is real fieldname missing = desc[2] else: # cryptic fieldname (remove this later) missing = field break return missing def addHidden(self,fieldname,value): """ Add hidden html input control to the editbox. """ self.hiddenFields[fieldname] = [inputHidden(value),False] self.hiddenFields[fieldname][0].name = fieldname def addDisabled(self): """ Since fields which are disabled, aren't posted (stupid HTML) we must add them as hidden fields. This only goes for textinputs (?!) so we must also change controlnames to avoid getting double values for selects, etc. """ for fieldname,definition in self.fields.items(): if definition[0].disabled and (not definition[0].type=='hidden'): self.addHidden(fieldname,definition[0].value) definition[0].name = definition[0].name + '_disabled' def formFill(self,formData): """ Fill this editbox with data from the form. This is used by intermediate steps (like register serial) to remember field values and for refilling a form if an error is encountered and the user has to resubmit a form. """ if not hasattr(editbox,'currentBoxNumber'): editbox.currentBoxNumber = 0 for field,definition in self.fields.items(): first = True numberOfBoxes = None if formData.has_key(field): if type(formData[field]) is list: # Remember how many editboxes this form has if first: # NB! ASSUMES THAT THE FIRST FIELDNAME IN A FORM # IS NEVER A CHECKBOX (SINCE UNCHECKED CHECCKBOXES # ARE NOT POSTED). IF IT IS, THEN numberOfBoxes # WILL BE ONE LESS THAN IT SHOULD BE FOR EACH # UNCHECKED CHECKBOX numberOfBoxes = len(formData[field]) first = False # We are editing more than one entry, pick the # right data from the form definition[0].value = formData[field][editbox.currentBoxNumber] else: definition[0].value = formData[field] # Update class variable currentFormNumber if numberOfBoxes: editbox.currentBoxNumber +=1 if editbox.currentBoxNumber == numberOfBoxes: # Reset the currentFormNumber class instance # Since it is a class instance, it will be common # to all editbox instances for all requests, that's # why it has to be reset editbox.currentBoxNumber = 0class editboxHiddenOrMessage(editbox): """ This editbox can display a message and contain hidden inputs. """ page = 'hiddenormessage' def __init__(self,message=None): self.hiddenFields = {} self.message = message # The editboxNetbox has UPDATE_ENTRY (which holds the id) or ADDNEW, # don't need to repeat it here (so setting boxname to IGNORE_BOX) self.boxName = IGNORE_BOXclass editdbPage: """ The main editing class. Every edit page inherits from this class. Contains functions for adding, updating and describing entries. Default functions can be overriden by children to do more specific handling of adding or updating. The children of this class contains all the information needed for handling the different tables. class editdbPage |+-- class listDef |+-- class editbox |+-- (optionally more editboxes) |+-- def add |+-- def update |+-- def delete |+-- def describe """ def add(self,req,outputForm,action): """ Called when 'Add' is clicked on an edit page. Takes the formdata from the request object and inserts an entry in the database. This is the general function used by almost all the edit pages. Some of the edit page classes overrides this function for more control over the adding (eg. the netbox page). req: request object containing a form outputForm: form with editboxes manipulated directly by eg. editNetbox.add() """ error = None status = editdbStatus() id = None nextId = None if self.sequence: # Get next id from sequence, will need this id to reload # entry when making a description of the inserted row # In the case that sequence=None, idfield is already # present in the field data sql = "SELECT nextval('%s')" % (self.sequence,) result = executeSQLreturn(sql) nextId = str(result[0][0]) sql = 'INSERT INTO ' + self.tableName + ' (' first = True for field,descr in self.fields.items(): if req.form.has_key(field): if len(req.form[field]): if not first: sql += ',' sql += field first = False # Add the idfield if we have queried the sequence if nextId: sql += "," + self.tableIdKey sql += ') VALUES (' first = True for field,descr in self.fields.items():
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -