📄 editdb.py
字号:
nameSpace = {'entryList': None, 'editList': None, 'editForm': None, 'body': body} template = editdbTemplate(searchList=[nameSpace]) template.path = [('Home','/'), ('Tools','/toolbox'), ('Edit database',None)] return template.respond()########################## General functions########################### General function for handling editingdef editPage(req,page,request,unicodeError): ''' General handler function for all editpages. Whenever an action add, update, list or delete is performed, this function is called. ''' # Cancel button redirect if req.form.has_key(editForm.cnameCancel): nav.web.redirect(req,BASEPATH,seeOther=True) # Make a status object status = editdbStatus() # Get action from request (url) action = request[1] selected = [] addedId = None # Get editid from url if it is present (external links and list links) if len(request) > 1: if (len(request) == 3) or (len(request) == 4): if request[2]: selected = [request[2]] # Make a list of selected entries from a posted selectlist if req.form.has_key(selectList.cnameChk): if type(req.form[selectList.cnameChk]) is str: # only one selected selected = [req.form[selectList.cnameChk]] elif type(req.form[selectList.cnameChk]) is list: # more than one selected for s in req.form[selectList.cnameChk]: selected.append(s) # Remember entries which we are already editing # Used if editing is interrupted by an error if req.form.has_key(UPDATE_ENTRY): if type(req.form[UPDATE_ENTRY]) is str: # only one selected selected = [req.form[UPDATE_ENTRY]] elif type(req.form[UPDATE_ENTRY]) is list: # more than one selected for s in req.form[UPDATE_ENTRY]: selected.append(s) # Disallow adding (for pageVlan) if req.form.has_key(selectList.cnameAdd) and hasattr(page,'disallowAdd'): status.errors.append(page.disallowAddReason) action = 'list' # Check if any entries are selected when action is 'edit' or 'delete' if action == 'edit': if req.form.has_key(selectList.cnameEdit): if not selected: status.errors.append('No entries selected for editing') action = 'list' elif req.form.has_key(selectList.cnameDelete): action = 'delete' if not selected: status.errors.append('No entries selected') action = 'list' else: if not selected: action = 'add' # Check for unicode errors in input if unicodeError: status.errors.append('The data you input was sent in a ' +\ 'non-recognisible encoding. Make sure your ' 'browser uses automatic character encoding ' +\ 'or set it to \'' + str(DEFAULT_ENCODING) + '\'.') action = 'list' # Set 'current path' path = page.pathAdd templatebox = page.editbox(page) # Copy field defintions from the main templatebox (used by add/update) page.fields = templatebox.fields # Make form object for template outputForm = editForm() if hasattr(page,'action'): outputForm.action = page.action else: outputForm.action = page.basePath + 'edit' # List definition, get sorting parameter sort = None if req.form.has_key('sort'): sort = req.form['sort'] listView = page.listDef(req,page,sort) # Check if the confirm button has been pressed if req.form.has_key(outputForm.cnameConfirm): missing = templatebox.hasMissing(req) if not missing: status = templatebox.verifyFields(req,status) if not len(status.errors): if req.form.has_key(ADDNEW_ENTRY): # add new entry (status,action,outputForm,addedId) = page.add(req, outputForm,action) elif req.form.has_key(UPDATE_ENTRY): # update entry (status,action,outputForm,selected) = page.update(req, outputForm, selected) else: status.errors.append("Required field '" + missing + "' missing") # Confirm delete pressed? elif req.form.has_key(selectList.cnameDeleteConfirm): status = page.delete(selected,status) outputForm = None selected = None action = 'list' # Decide what to show if action == 'predefined': # Action is predefined by addNetbox() or updateNetbox() outputForm.textConfirm = 'Continue' outputForm.action = action outputForm.status = status listView = None elif action == 'edit': path = page.pathEdit title = 'Edit ' if len(selected) > 1: title += page.plural else: title += page.singular outputForm.title = title outputForm.action = action outputForm.status = status outputForm.textConfirm = 'Update' if page.editMultipleAllowed: # This page can edit multiple entries at a time for s in selected: outputForm.add(page.editbox(page,req,s,formData=req.form)) else: # This page can only edit one entry at a time (eg. netbox) outputForm.add(page.editbox(page,req,selected[0],formData=req.form)) # preserve path #outputForm.action = page.basePath + 'edit/' + selected[0] listView = None elif action == 'add': path = page.pathAdd outputForm.action = action outputForm.status = status outputForm.title = 'Add ' + page.singular outputForm.textConfirm = 'Add ' + page.singular outputForm.add(page.editbox(page,req,formData=req.form)) listView = None elif action == 'delete': path = page.pathDelete listView = page.listDef(req,page,sort,selected) listView.status = status listView.fill(req) outputForm = None elif action == 'list': if addedId: listView.selectedId = addedId if selected: listView.selectedId = selected[0] path = page.pathList listView.status=status listView.fill(req) outputForm = None elif action == 'redirect': # Redirect to main page (snmpoid add) return index(req,status=status) nameSpace = {'entryList': listView,'editList': None,'editForm': outputForm} template = editdbTemplate(searchList=[nameSpace]) template.path = path return template.respond()def insertNetbox(ip,sysname,catid,roomid,orgid, ro,rw,deviceid,serial, typeid,snmpversion,subcatlist=None, function=None): ''' Inserts a netbox into the database. Used by pageNetbox.add(). ''' if not deviceid: # Make new device first if len(serial): fields = {'serial': serial} else: # Don't insert an empty serialnumber (as serialnumbers must be # unique in the database) (ie. don't insert '' for serial) fields = {} deviceid = addEntryFields(fields, 'device', ('deviceid','device_deviceid_seq')) fields = {'ip': ip, 'roomid': roomid, 'deviceid': deviceid, 'sysname': sysname, 'catid': catid, 'orgid': orgid, 'ro': ro, 'rw': rw} #uptodate = false per default # Get prefixid query = "SELECT prefixid FROM prefix WHERE '%s'::inet << netaddr" \ % (fields['ip'],) try: result = executeSQLreturn(query) fields['prefixid'] = str(result[0][0]) except: pass if typeid: fields['typeid'] = typeid # Set uptyodate = false # This part is done in netbox now. And for a new box this # field defaults to 'f' #tifields = {'uptodate': 'f'} #updateEntryFields(tifields,'type','typeid',typeid) if snmpversion: # Only use the first char from initbox, can't insert eg. '2c' in # this field snmpversion = snmpversion[0] fields['snmp_version'] = snmpversion netboxid = addEntryFields(fields, 'netbox', ('netboxid','netbox_netboxid_seq')) # If subcatlist and function is given, insert them if subcatlist: if type(subcatlist) is list: for sc in subcatlist: fields = {'netboxid': netboxid, 'category': sc} addEntryFields(fields,'netboxcategory') else: fields = {'netboxid': netboxid, 'category': subcatlist} addEntryFields(fields,'netboxcategory') if function: fields = {'netboxid': netboxid, 'key': '', 'var': 'function', 'val': function} addEntryFields(fields,'netboxinfo')########################## General classes##########################class Table: ''' A general class for html tables used by index(). ''' def __init__(self,title,infotext,headings,rows): self.title = title self.infotext = infotext self.headings = headings self.rows = rowsclass editdbStatus: ''' Struct class which holds two lists (messages and errors). Every form object got an instance of this class and uses it to add messages and errors which is then displayed by the template. ''' # List of status messages, one line per message messages = [] # List of error messages, one line per message errors = [] def __init__(self): self.messages = [] self.errors = []class entryListCell: ''' Represents a cell (TD) in a selectlist object. ''' CHECKBOX = 'chk' RADIO = 'rad' HIDDEN = 'hid' def __init__(self,text=None,url=None,buttonType=None, image=None,tooltip=None): self.text = text self.url = url self.buttonType = buttonType self.image = image self.tooltip = tooltip## class entryList (rename to selectList)class entryList: ''' Flexible class for making lists of entries which can be selected. Used by all 'edit' pages. Uses the list definitions defined in every page class. descriptionFormat = [(text,forgetSQLfield),(text,...] ''' # Constants CNAME_SELECT = 'checkbox_id' CNAME_ADD = 'submit_add' CNAME_EDIT = 'submit_edit' CNAME_DELETE = 'submit_delete' CNAME_CONFIRM_DELETE = 'confirm_delete' CNAME_CANCEL = 'form_cancel' # Class variables used by the template title = None status = None body = None formMethod = 'post' formAction = None selectCname = CNAME_SELECT buttonsTop = [(CNAME_ADD,'Add new'), (CNAME_EDIT,'Edit selected'), (CNAME_DELETE,'Delete selected')] buttonsBottom = buttonsTop hideFirstHeading = False # Don't show first column heading (usually # the select heading) if True buttonTypeOverride = None # override the chosen select button type # used for bulk and delete lists where there # is no checkbox/radiobut., only hidden headings = [] # list of cell objects rows = [] # tuples of (sortstring,id,cell object) # Variables for filling the list tableName = None basePath = None sortBy = None # Sort by columnumber
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -