def do_import_action(user, workflow, name, file_item): """ Receives a name and a file item (submitted through a form) and creates the structure of action with conditions and columns :param user: User record to use for the import (own all created items) :param workflow: Workflow object to attach the action :param name: Workflow name (it has been checked that it does not exist) :param file_item: File item obtained through a form :return: """ try: data_in = gzip.GzipFile(fileobj=file_item) data = JSONParser().parse(data_in) except IOError: return 'Incorrect file. Expecting a GZIP file (exported workflow).' # Serialize content action_data = ActionSelfcontainedSerializer(data=data, context={ 'user': user, 'name': name, 'workflow': workflow }) # If anything goes wrong, return a string to show in the page. action = None try: if not action_data.is_valid(): return 'Unable to import action:' + ' ' + action_data.errors # Save the new workflow action = action_data.save(user=user, name=name) except (TypeError, NotImplementedError) as e: return 'Unable to import action: ' + e.message except serializers.ValidationError as e: return 'Unable to import action due to a validation error:' + e.message except Exception as e: return 'Unable to import action: ' + e.message # Success # Log the event logs.ops.put(user, 'workflow_import', workflow, { 'id': workflow.id, 'name': workflow.name }) return None
def do_export_action(action): """ Proceed with the action export. :param action: Element to export. :return: Page that shows a confirmation message and starts the download """ # Context context = {'workflow': action.workflow} # Get the info to send from the serializer serializer = ActionSelfcontainedSerializer(action, context=context) to_send = JSONRenderer().render(serializer.data) # Get the in-memory file to compress zbuf = BytesIO() zfile = gzip.GzipFile(mode='wb', compresslevel=6, fileobj=zbuf) zfile.write(to_send) zfile.close() suffix = datetime.datetime.now().strftime('%y%m%d_%H%M%S') # Attach the compressed value to the response and send compressed_content = zbuf.getvalue() response = HttpResponse(compressed_content) response['Content-Type'] = 'application/octet-stream' response['Content-Transfer-Encoding'] = 'binary' response['Content-Disposition'] = \ 'attachment; filename="ontask_action_{0}.gz"'.format(suffix) response['Content-Length'] = str(len(compressed_content)) return response