[690] | 1 | from zope.app.annotation.interfaces import IAnnotations |
---|
[679] | 2 | |
---|
| 3 | from Products.Five.browser import BrowserView |
---|
| 4 | |
---|
| 5 | from collective.transmogrifier.transmogrifier import configuration_registry |
---|
| 6 | |
---|
| 7 | ANNOKEY = 'quintagroup.transmogrifier.config' |
---|
| 8 | |
---|
| 9 | class PipelineConfigView(BrowserView): |
---|
| 10 | """ View for setting persistent pipeline config. |
---|
| 11 | """ |
---|
| 12 | |
---|
| 13 | def __init__(self, context, request): |
---|
| 14 | super(PipelineConfigView, self).__init__(context, request) |
---|
| 15 | self.anno = IAnnotations(context) |
---|
| 16 | self.status = None |
---|
| 17 | |
---|
| 18 | def __call__(self): |
---|
| 19 | action = self.request.form.get('action') |
---|
| 20 | if action is not None: |
---|
| 21 | stat = [] |
---|
| 22 | # handle export config |
---|
| 23 | export_config = self.request.form['export'].strip() |
---|
| 24 | expkey = ANNOKEY+'.export' |
---|
| 25 | oldconfig = self.getConfig('export') |
---|
[1237] | 26 | if export_config and self._configChanged(oldconfig, export_config): |
---|
[679] | 27 | self.anno[expkey] = export_config |
---|
| 28 | stat.append('updated export') |
---|
| 29 | elif not export_config and expkey in self.anno: |
---|
| 30 | del self.anno[expkey] |
---|
| 31 | stat.append('removed export') |
---|
| 32 | # handle import config |
---|
| 33 | import_config = self.request.form['import'].strip() |
---|
| 34 | impkey = ANNOKEY+'.import' |
---|
| 35 | oldconfig = self.getConfig('import') |
---|
[1237] | 36 | if import_config and self._configChanged(oldconfig, import_config): |
---|
[679] | 37 | self.anno[impkey] = import_config |
---|
| 38 | stat.append('updated import') |
---|
| 39 | elif not import_config and impkey in self.anno: |
---|
| 40 | del self.anno[impkey] |
---|
| 41 | stat.append('removed import') |
---|
| 42 | if stat: |
---|
| 43 | self.status = 'Changes: %s configuration.' % ' and '.join(stat) |
---|
| 44 | else: |
---|
| 45 | self.status = 'No changes' |
---|
| 46 | |
---|
| 47 | return self.index() |
---|
| 48 | |
---|
| 49 | def getConfig(self, type_): |
---|
| 50 | key = '%s.%s' % (ANNOKEY, type_) |
---|
| 51 | if key in self.anno: |
---|
| 52 | return self.anno[key] |
---|
| 53 | else: |
---|
| 54 | fname = configuration_registry.getConfiguration(type_)['configuration'] |
---|
| 55 | return file(fname).read() |
---|
| 56 | |
---|
[1237] | 57 | def _configChanged(self, old, new): |
---|
| 58 | """ Compare configs with normalization of line endings. |
---|
| 59 | """ |
---|
| 60 | if old == new: |
---|
| 61 | return False |
---|
| 62 | if old == new.replace('\r\n', '\n'): |
---|
| 63 | return False |
---|
| 64 | if old.strip() == new.replace('\r\n', '\n'): |
---|
| 65 | return False |
---|
| 66 | return True |
---|
| 67 | |
---|
[679] | 68 | def isDefault(self, type_): |
---|
| 69 | key = '%s.%s' % (ANNOKEY, type_) |
---|
| 70 | return key not in self.anno |
---|