⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 statusprefs.py

📁 Network Administration Visualized 网络管理可视化源码
💻 PY
📖 第 1 页 / 共 2 页
字号:
        controlNumber = 0        newBaseName = typeId + '_' + repr(controlNumber)        while baseNameList.count(newBaseName):            controlNumber += 1            newBaseName = typeId + '_' + repr(controlNumber)        return controlNumber    def getPrefs(self):        " returns a StatusPrefs object with the current preferences "        prefs = StatusPrefs()        for section in self.editSectionBoxes:            if section.title == 'Mail':                raise('jepp'+repr(section.filterSettings))            newFilterSettings = {}            # Change filterSettings to use name instead of control name             # ie. org instead of netbox_0_org            for controlName,selected in section.filterSettings.items():                name = re.match('.*_([a-zA-Z]*)$',controlName)                 filterName = name.group(1)                newFilterSettings[filterName] = selected            prefs.addSection(section.controlBaseName,section.typeId,            section.title,newFilterSettings)        return prefs    def setPrefs(self, prefs):        """        Set current preferences for this instance (from loaded prefs)        """        for section in prefs.sections:            controlBaseName,typeId,title,filterSettings = section            # Must convert filterSettings used by the main status page to            # the format used by the prefs page (kludgy)            # the stored name is just the filtername, the full filtername            # should be controlBaseName + '_' + filtername            # (this is the reverse of what is done in getPrefs())            convertedFilterSettings = {}            for filterName,selected in filterSettings.items():                newFilterName = controlBaseName + '_' + filterName                convertedFilterSettings[newFilterName] = selected                         settings = []            settings.append(title)              settings.append(convertedFilterSettings)            self.addSectionBox(typeId,settings,controlBaseName)        return    def savePrefs(self):        " Pickles and saves the preferences "        prefs = self.getPrefs()                        connection = nav.db.getConnection('status', 'navprofile')        database = connection.cursor()        data = psycopg.QuotedString(cPickle.dumps(prefs))        sql = "SELECT property FROM accountproperty " + \              "WHERE accountid='%s' " % (self.req.session['user'].id,) + \              "AND property='%s'" % (self.STATUS_PROPERTY,)        database.execute(sql)        result = database.fetchall()        if result:            # Prefs exists, update            sql = "UPDATE accountproperty SET value=%s WHERE accountid=%s and \            property='%s'" % \            (data,self.req.session['user'].id,self.STATUS_PROPERTY)        else:            # No prefs previously saved            sql = "INSERT INTO accountproperty (accountid,property,value) \            VALUES (%s,'%s',%s)" % \            (self.req.session['user'].id,self.STATUS_PROPERTY,data)        database.execute(sql)        connection.commit()    def loadPrefs(cls,req):        accountid = req.session['user'].id        connection = nav.db.getConnection('status', 'navprofile')        database = connection.cursor()        sql = "SELECT value FROM accountproperty WHERE accountid=%s \        and property='%s'" % (req.session['user'].id,cls.STATUS_PROPERTY)        database.execute(sql)        data = database.fetchone()                if data:            (data,) = data            prefs = cPickle.loads(data)        else:            # No prefs for this user in the database            # Load default prefs from admin user            sql = "SELECT value FROM accountproperty WHERE accountid=%s \            and property='%s'" % (ADMIN_USER_ID,cls.STATUS_PROPERTY)            database.execute(sql)            data = database.fetchone()                    if data:                (data,) = data                prefs = cPickle.loads(data)            else:                # No system default prefs found (admin users prefs)                # load from StatusDefaultPrefs module                data = StatusDefaultPrefs.defaultPrefs                prefs = cPickle.loads(data)            # No prefs stored in the database for this user,            # load the default prefs from a file            #fh = file(DEFAULT_PREFS_FILENAME,'r')            #prefs = cPickle.load(fh)                return prefs    loadPrefs = classmethod(loadPrefs)    def saveDefaultPrefs(self):        " Saves current prefs as default preferences in a file "        prefs = self.getPrefs()        connection = nav.db.getConnection('status', 'navprofile')        database = connection.cursor()        data = psycopg.QuotedString(cPickle.dumps(prefs))        sql = "SELECT property FROM accountproperty " + \              "WHERE accountid='%s' " % (ADMIN_USER_ID,) + \              "AND property='%s'" % (self.STATUS_PROPERTY,)        database.execute(sql)        result = database.fetchall()        if result:            # Prefs exists, update            sql = "UPDATE accountproperty SET value=%s WHERE accountid=%s and \            property='%s'" % \            (data,ADMIN_USER_ID,self.STATUS_PROPERTY)        else:            # No prefs previously saved            sql = "INSERT INTO accountproperty (accountid,property,value) \            VALUES (%s,'%s',%s)" % \            (ADMIN_USER_ID,self.STATUS_PROPERTY,data)        database.execute(sql)        connection.commit()class EditSectionBox:    """    An editable section box on the prefs page.    """    name = None    title = None    typeId = None    controlBaseName = None    # dict of {'controlname': ['selected','entries',...]}    filterSettings = dict()    # list of tuples (controlname,list of (value,option,selected=True|False))    filterSelects = []    # list of strings ('org','category', etc.)    filterHeadings = []    def __init__(self,sectionType,controlNumber,settings,controlBaseName,orgList):        self.filterSettings = dict()        self.typeId = sectionType.typeId        self.orgList = orgList        # if this is a new section, use the (unique) controlNumber to make        # a new controlBaseName        self.controlBaseName = sectionType.typeId + '_' + repr(controlNumber)        # if this is an existing section, then preserve the controlBaseName        if controlBaseName:            self.controlBaseName = controlBaseName        self.filterHeadings, self.filterSelects = \        sectionType.getFilters(self.controlBaseName,self.orgList)                self.name = sectionType.name        self.title = sectionType.name         # if settings is present, then this isn't a new section box, so        # the settings (from the form or loaded prefs) must be preserved        if settings:            self.title = settings[0]            self.filterSettings = settings[1]            # set selected = True | False based on filterSettings            newFilterSelects = []            for controlName, optionList in self.filterSelects:                newOptionList = []                for value,option,selected in optionList:                    # MUST CHECK IF THE RESULT OF THE FORM CONTROL AS                    # PARSED BY HandleStatusPrefs.__init__ IS PRESENT                    # FieldStorage(keep_blank_values) SHOULD PREVENT                    # THE NEED FOR THIS, BUT SOMETHING IS WRONG                    if not self.filterSettings.has_key(controlName):                        # If the control name is missing, nothing is selected,                        # and the 'All' option should be auto selected                        self.filterSettings[controlName] = [FILTER_ALL_SELECTED]                    if self.filterSettings[controlName].count(value):                        selected = True                    else:                        selected = False                                            newOptionList.append((value,option,selected))                newFilterSelects.append((controlName,newOptionList))            self.filterSelects = newFilterSelectsclass StatusPrefs:    """     class holding a users/groups preference for the status page     """    sections = []    def __init__(self):        self.sections = []    def addSection(self,controlBaseName,typeId,title,filterSettings):        self.sections.append((controlBaseName,typeId,title,filterSettings))    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -