def clean_file(self): file = self.cleaned_data['file'] action_code = file.read() file.seek(0) parser = ActionParser() action = [] try: action = parser.parse(action_code) except SyntaxError, e: raise forms.ValidationError('File %s contains syntax errors (line %s, col %s): %s' % (file.name, e.lineno, e.offset, e.text))
def save(self): action_code = self.cleaned_data['file'].read() parser = ActionParser() action = parser.parse(action_code) with transaction.commit_on_success(): a = Action(name=action['action_name'], precondition_expression=action['precondition_expression'], action_file=self.cleaned_data['file']) a.save() for parameter in action['parameters']: ap = ActionParameter(action=a, name=parameter['name'], parameter_position=parameter['position']) ap.save() action_devices = {} for key, device in action['devices'].items(): ad = ActionDevice(action=a, name=key, parameter_position=device['parameter_position']) ad.save() action_devices[key] = ad for interface_name in device['interfaces']: i = Interface.objects.get(name=interface_name) adi = ActionDeviceInterface(action_device=ad, interface=i) adi.save() for position, replacement in enumerate(action['precondition_replacements']): apm = ActionPreconditionMethod(expression_position=position, action=a, action_device=action_devices[replacement['device']]) m = Method.objects.get(name=replacement['method'], interface__name=replacement['interface']) apm.method = m apm.save() # Send file to Mirri event_handler = EventHandler() try: client = MirriClient() client.upload_action_file(self.cleaned_data['file']) except (MirriConnectionError, MirriTimeoutError, MirriNotFoundError), e: logger.error('Action file %s could not be posted to Mirri: %s' % (self.cleaned_data['file'].name, e)) event_handler.add_event('Action file %s could not be posted to Mirri: %s' % (self.cleaned_data['file'].name, e)) else: