diff --git a/tailbone/views/master.py b/tailbone/views/master.py index eee2884d..c4abff36 100644 --- a/tailbone/views/master.py +++ b/tailbone/views/master.py @@ -36,7 +36,8 @@ import sqlalchemy as sa from sqlalchemy import orm import sqlalchemy_continuum as continuum -from sqlalchemy_utils.functions import get_primary_keys +from sqlalchemy_utils.functions import get_primary_keys, get_columns + from rattail.db import model, Session as RattailSession from rattail.db.continuum import model_transaction_query @@ -1110,6 +1111,9 @@ class MasterView(View): self.configure_clone_form(form) if self.request.method == 'POST' and self.request.POST.get('clone') == 'clone': cloned = self.clone_instance(instance) + self.request.session.flash("{} has been cloned: {}".format( + self.get_model_title(), self.get_instance_title(instance))) + self.request.session.flash("(NOTE, you are now viewing the clone!)") return self.redirect(self.get_action_url('view', cloned)) return self.render_to_response('clone', { 'instance': instance, @@ -1122,7 +1126,21 @@ class MasterView(View): pass def clone_instance(self, instance): - raise NotImplementedError + """ + This method should create and return a *new* instance, which has been + "cloned" from the given instance. Default behavior assumes a typical + SQLAlchemy record instance, and the new one has all "column" values + copied *except* for the ``'uuid'`` column. + """ + cloned = self.model_class() + + for column in get_columns(instance): + if column.name != 'uuid': + setattr(cloned, column.name, getattr(instance, column.name)) + + self.Session.add(cloned) + self.Session.flush() + return cloned def touch(self): """