def setShortcutValue(self, shortcut: PreferencesShortcut, value):
     previous_val = shortcut.default_value
     shortcut.default_value = value
     shortcut.save()
     auditing_service.audit_update(self.container, shortcut.tableName(),
                                   shortcut.id, "default_value",
                                   previous_val, value)
def getShortcutContainerEnvs(container: Container):
    """
    Get all shortcut environment variables that link with the given container
    :return: Dict: {'<key>': '<value>'}
    """
    result = {}
    shortcuts = PreferencesShortcut.select().where(
        (PreferencesShortcut.container == container)
        & (PreferencesShortcut.shortcut == 'Environment')
        & PreferencesShortcut.enabled)
    for item in shortcuts:
        result[item.mapping_to] = item.default_value
    return result
def getShortcutPortMapping(container: Container):
    """
    Get all shortcut port mapping that link with the given container

    :return: Dict: {'<container port>/tcp': <host port>}
    :rtype:
    """
    result = {}
    shortcuts = PreferencesShortcut.select().where(
        (PreferencesShortcut.container == container)
        & (PreferencesShortcut.shortcut == 'Port Mapping')
        & PreferencesShortcut.enabled)
    for item in shortcuts:
        result[item.mapping_to + '/tcp'] = item.default_value
    return result
    def finish(self):
        if not self.ui.mapping_to.text() and self.ui.shortcut_type.currentText() != 'Constant':
            message = self._translate(self.template, 'The value of \'Mapping to\' can not be empty')
            QToolTip.showText(self.ui.mapping_to.mapToGlobal(QPoint()), message)
            return
        shortcut_type = self.ui.shortcut_type.currentText()
        mapping_to = self.ui.mapping_to.text()

        # Is create mode
        if self.is_creative_move and self.findShortcut(self.container, shortcut_type, mapping_to):
            msg = QMessageBox()
            msg.setIcon(QMessageBox.Critical)

            msg.setText(self._translate(self.template, "Preference shortcut already exists!!!"))
            msg.setInformativeText('Preference shortcut with type: %s, mapping to: %s is already exist!\n\n'
                                   'Please choose another one' % (shortcut_type, mapping_to))
            msg.setStandardButtons(QMessageBox.Ok)
            return msg.exec_()

        if self.is_creative_move:
            order = PreferencesShortcut.select().where(PreferencesShortcut.container == self.container).count() * 10000
            self.shortcut.order = order

        self.shortcut.container = self.container
        self.shortcut.label = self.ui.shortcut_label.text().strip()
        self.shortcut.default_value = self.ui.default_value.text().strip()
        self.shortcut.pref_type = self.ui.data_type.currentText().strip()
        self.shortcut.shortcut = self.ui.shortcut_type.currentText()
        self.shortcut.mapping_to = self.ui.mapping_to.text().strip()
        self.shortcut.description = self.ui.description.toPlainText().strip()

        if shortcut_service.hasChanged(self.shortcut):
            model = model_to_dict(self.shortcut)
            model_original = model_to_dict(shortcut_service.getShortcut(self.shortcut.id))
            changes = []
            for key in model:
                if model[key] != model_original[key] and key != 'id':
                    # Record changes, except label because it will not affect to the container it self
                    changes.append({'from': model_original[key], 'to': model[key], 'key': key})
            self.shortcut.save()
            if self.is_creative_move:
                auditing_service.audit_create(self.container, self.shortcut.tableName(), self.shortcut.id)
            else:
                for change in changes:
                    auditing_service.audit_update(self.container, self.shortcut.tableName(), self.shortcut.id,
                                                  change['key'], change['from'], change['to'])
        self.dialog.accept()
def importShortcuts(container: Container, shortcuts: List[dict]):
    for shortcut_dict in shortcuts:
        shortcut = PreferencesShortcut()
        update_model_from_dict(shortcut, shortcut_dict)
        shortcut.container = container
        if shortcut.pref_type in ['File', 'Folder']:
            if not os.path.exists(shortcut.default_value):
                shortcut.default_value = ''
        try:
            PreferencesShortcut.get(
                (PreferencesShortcut.container == container)
                & (PreferencesShortcut.shortcut == shortcut.shortcut)
                & (PreferencesShortcut.mapping_to == shortcut.mapping_to)
                & (PreferencesShortcut.pref_type == shortcut.pref_type))
            logger.info("Ignoring shortcut label %s cuz it is already exists" %
                        shortcut.label)
        except DoesNotExist:
            shortcut.save()
def getShortcutVolumeMounts(container: Container):
    """
    Get all shortcut volume mounts that linked with the given container

    :return: Dict: {'<host path>': {
                        'bind': '<container path>'
                        'mode': 'rw'}
                    }
    """
    result = {}
    shortcuts = PreferencesShortcut.select().where(
        (PreferencesShortcut.container == container)
        & (PreferencesShortcut.shortcut == 'Volume Mount')
        & PreferencesShortcut.enabled)
    for item in shortcuts:
        result[item.default_value] = {'bind': item.mapping_to, 'mode': 'rw'}
    return result
 def findShortcut(self, container, shortcut_type, mapping_to):
     condition = (PreferencesShortcut.container == container) & (PreferencesShortcut.shortcut == shortcut_type)
     condition = condition & (PreferencesShortcut.mapping_to == mapping_to)
     return PreferencesShortcut.select().where(condition)
def getEnabledShortcuts(container: Container) -> List[PreferencesShortcut]:
    return PreferencesShortcut.select()\
        .where((PreferencesShortcut.container == container) & PreferencesShortcut.enabled
               & (PreferencesShortcut.status == STATUS_ADDED)) \
        .order_by(PreferencesShortcut.order.asc())
def getShortcut(shortcut_id: int) -> PreferencesShortcut:
    return PreferencesShortcut.get(shortcut_id)
def deleteAll(container: Container):
    PreferencesShortcut.delete().where(
        PreferencesShortcut.container == container).execute()
 def onNewShortcutClicked(self):
     shortcut = PreferencesShortcut()
     shortcut_creator = ShortcutCreator(self.container, self.dialog,
                                        shortcut)
     if shortcut_creator.show():
         self.reloadData()