Exemplo n.º 1
0
    def __init__(self, hosts):
        super(HostConfigDialog, self).__init__()
        self.ui = Ui_HostConfig()
        self.ui.setupUi(self)
        self.ui.showPassword.clicked.connect(self.changePasswordVisibility)
        self.ui.group.lineEdit().setPlaceholderText("Group")  # not available from designer
        self.hosts = hosts

        self.optionalAttributes = ['group']
        self.attributes = ['name', 'address', 'user', 'password'] + self.optionalAttributes
Exemplo n.º 2
0
    def __init__(self, hosts):
        super(HostConfigDialog, self).__init__()
        self.ui = Ui_HostConfig()
        self.ui.setupUi(self)
        self.ui.showPassword.clicked.connect(self.changePasswordVisibility)
        self.ui.showPassword.setToolTip("Show password")  # don't why not generated from ui file
        self.hosts = hosts
        # label to use to show errors
        self.errorLabel = None

        self.attributes = ['name', 'address', 'user', 'password']
Exemplo n.º 3
0
class HostConfigDialog(QDialog):
    def __init__(self, hosts):
        super(HostConfigDialog, self).__init__()
        self.ui = Ui_HostConfig()
        self.ui.setupUi(self)
        self.ui.showPassword.clicked.connect(self.changePasswordVisibility)
        self.ui.showPassword.setToolTip("Show password")  # don't why not generated from ui file
        self.hosts = hosts
        # label to use to show errors
        self.errorLabel = None

        self.attributes = ['name', 'address', 'user', 'password']

    def changePasswordVisibility(self):
        if self.ui.showPassword.isChecked():
            self.ui.password.setEchoMode(QLineEdit.Normal)
        else:
            self.ui.password.setEchoMode(QLineEdit.Password)

    def getTextFieldValue(self, field):
        """ field value or None
        :param field: object id
        :return: value or None
        """
        field = getattr(self.ui, field)
        value = field.text()
        if value == '':
            return None
        return value

    def collectFieldsValues(self):
        attributesDict = {}
        for attr in self.attributes:
            attributesDict[attr] = unicode(self.getTextFieldValue(attr))
        return attributesDict

    def acceptAddHost(self):
        attributesDict = self.collectFieldsValues()
        try:
            self.hosts.create(**attributesDict)
        except Exception as e:
            self.setErrorLabel(e.message)
        else:
            self.accept()

    def acceptEditHost(self, host):
        attributesDict = self.collectFieldsValues()
        try:
            self.hosts.updateHostValues(host, attributesDict)
        except Exception as e:
            self.setErrorLabel(e.message)
        else:
            self.accept()

    def setErrorLabel(self, text):
        if self.errorLabel is None:
                self.errorLabel = QLabel()
        self.errorLabel.setText(text)
        self.ui.errorArea.addWidget(self.errorLabel)

    def add(self):
        """
        :return: dictionary {
            "code": return code,
            "name": host name if host should be connected
            }
        """
        response = dict()
        self.ui.acceptButton.clicked.connect(self.acceptAddHost)
        retCode = self.exec_()
        response["code"] = retCode
        self.ui.acceptButton.clicked.disconnect()

        if retCode and self.ui.connectCheckBox.isChecked():
            response["name"] = self.ui.name.text()

        return response

    def edit(self, hostName):
        """
        :param hostName:
        :type hostName: app.hosts.Hosts
        :return:
        """
        response = dict()
        host = self.hosts.get(hostName)
        for attribute in self.attributes:
            field = getattr(self.ui, attribute)
            field.setText(getattr(host, attribute))

        self.ui.acceptButton.clicked.connect(lambda: self.acceptEditHost(host))
        retCode = self.exec_()
        response["code"] = retCode
        self.ui.acceptButton.clicked.disconnect()

        return response
Exemplo n.º 4
0
class HostConfigDialog(QDialog):
    def __init__(self, hosts):
        super(HostConfigDialog, self).__init__()
        self.ui = Ui_HostConfig()
        self.ui.setupUi(self)
        self.ui.showPassword.clicked.connect(self.changePasswordVisibility)
        self.ui.group.lineEdit().setPlaceholderText("Group")  # not available from designer
        self.hosts = hosts

        self.optionalAttributes = ['group']
        self.attributes = ['name', 'address', 'user', 'password'] + self.optionalAttributes

    def changePasswordVisibility(self):
        if self.ui.showPassword.isChecked():
            self.ui.password.setEchoMode(QLineEdit.Normal)
        else:
            self.ui.password.setEchoMode(QLineEdit.Password)

    def getTextFieldValue(self, field):
        """ field value or None
        :param field: object id
        :return: value or None
        """
        fieldObject = getattr(self.ui, field)
        if not isinstance(fieldObject, QComboBox):
            value = fieldObject.text()
        else:
            value = fieldObject.lineEdit().text()
        if value == '':
            if field not in self.optionalAttributes:
                raise ValueError(u"Complete the required fields")
            return None
        return unicode(value)

    def collectFieldsValues(self):
        attributesDict = {}
        for attr in self.attributes:
            attributesDict[attr] = self.getTextFieldValue(attr)
        return attributesDict

    def _accept(self, action, host=None):
        try:
            attributesDict = self.collectFieldsValues()
            if action == "create":
                self.hosts.create(**attributesDict)
            elif action == "update":
                self.hosts.updateHostValues(host, attributesDict)
            else:
                raise NotImplementedError("Not supported action")
        except Exception as e:
            self.setErrorLabel(e.message)
        else:
            self.accept()

    def setErrorLabel(self, text):
        self.ui.informationLabel.setText(text)

    def setGroups(self, field):
        field.addItem(str())  # add empty element on list begin
        for group in self.hosts.getGroupsList():
            field.addItem(group)

    def _execDialog(self):
        """
        :return: dictionary {
            "code": return code,
            "name": host name if host should be connected
            }
        """
        response = dict()
        retCode = self.exec_()
        response["code"] = retCode

        if retCode and self.ui.connectCheckBox.isChecked():
            response["name"] = self.ui.name.text()
        return response

    def add(self):
        self.ui.buttonBox.accepted.connect(lambda: self._accept("create"))
        self.setGroups(self.ui.group)
        return self._execDialog()

    def edit(self, hostName):
        host = self.hosts.get(hostName)
        for attribute in self.attributes:
            field = getattr(self.ui, attribute)
            value = getattr(host, attribute, '')

            if value is None:
                value = ''

            if attribute == "group":
                self.setGroups(field)
                field.lineEdit().setText(value)
            else:
                field.setText(value)

        self.ui.buttonBox.accepted.connect(lambda: self._accept("update", host))
        return self._execDialog()