From d369693f9ff77de33ada4719a628a0e9b4c5145c Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Sat, 15 Dec 2018 22:29:28 -0600 Subject: [PATCH] Add `oneoff_import()` helper method to MasterView class --- tailbone/views/master.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tailbone/views/master.py b/tailbone/views/master.py index 58b283ce..74a18f05 100644 --- a/tailbone/views/master.py +++ b/tailbone/views/master.py @@ -1622,6 +1622,30 @@ class MasterView(View): progress.session['success_url'] = self.get_index_url() progress.session.save() + def oneoff_import(self, importer, host_object=None): + """ + Basic helper method, to do a one-off import (or export, depending on + perspective) of the "current instance" object. Where the data "goes" + depends on the importer you provide. + """ + if not host_object: + host_object = self.get_instance() + + host_data = importer.normalize_host_object(host_object) + if not host_data: + return + + key = importer.get_key(host_data) + local_object = importer.get_local_object(key) + if local_object: + if importer.allow_update: + local_data = importer.normalize_local_object(local_object) + if importer.data_diffs(local_data, host_data) and importer.allow_update: + local_object = importer.update_object(local_object, host_data, local_data) + return local_object + elif importer.allow_create: + return importer.create_object(key, host_data) + def execute(self): """ Execute an object.