class IMigrator(Interface): """Remote source interface. """ config = Choice( title=_(u"Select configuration"), description=_(u"Registered configurations to choose from."), vocabulary=u"collective-migrator-configurations", )
class Migrator(form.Form): label = _(u"Synchronize and migrate") fields = field.Fields(IMigrator) ignoreContext = True @button.buttonAndHandler(_(u'Select')) def handleSelect(self, action): data, errors = self.extractData() if errors: return False self.request.RESPONSE.redirect( '%s/@@mr.migrator-run?form.widgets.%s' % (self.context.absolute_url(), urllib.urlencode(data)))
class MigratorRun(group.GroupForm, form.Form): label = _(u"Synchronize and migrate") fields = field.Fields(IMigratorRun) ignoreContext = True allow_prefill_from_GET_request = True def update(self): configname = self.request.get('form.widgets.config') self.groups = formfactory(configname) return super(MigratorRun, self).update() def updateWidgets(self): super(MigratorRun, self).updateWidgets() self.widgets['config'].mode = interfaces.HIDDEN_MODE @button.buttonAndHandler(_(u'Run')) def handleRun(self, action): data, errors = self.extractData() if errors: return False overrides = {} for key, value in data.items(): if '-' not in key: continue elif value is None: continue elif isinstance(value, list): value = '\n'.join(value) elif isinstance(value, bool): value = value and 'True' or 'False' else: value = str(value) section, key = key.split('-') overrides.setdefault(section, {})[key] = value logger.info("Start importing profile: " + data['config']) Transmogrifier(self.context)(data['config'], **overrides) logger.info("Stop importing profile: " + data['config'])