def get_beid_diffs(obj, attrs): """Return two lists, one with the objects to save, and another with text lines to build a confirmation message explaining which changes are going to be applied after confirmation. The default implemantion is for the simple case where the holder is also a contacts.AddressLocation and the address is within the same database row. """ raise Exception("not tested") diffs = [] objects = [] # model = holder_model() model = obj.__class__ # the holder for fldname, new in attrs.items(): fld = get_field(model, fldname) old = getattr(obj, fldname) if old != new: diffs.append( "%s : %s -> %s" % ( unicode(fld.verbose_name), dd.obj2str(old), dd.obj2str(new))) setattr(obj, fld.name, new) return objects, diffs
def process_row(self,ar,obj,attrs): oldobj = obj watcher = dd.ChangeWatcher(obj) diffs = [] for fldname,new in attrs.items(): fld = get_field(self.client_model,fldname) old = getattr(obj,fldname) if old != new: diffs.append("%s : %s -> %s" % (unicode(fld.verbose_name),dd.obj2str(old),dd.obj2str(new))) setattr(obj,fld.name,new) if len(diffs) == 0: #~ return self.no_diffs_response(ar,obj) return self.goto_client_response(ar,obj,_("Client %s is up-to-date") % unicode(obj)) msg = unicode(_("Click OK to apply the following changes for %s") % obj) msg += ' :<br/>' msg += '\n<br/>'.join(diffs) def yes(ar): obj.full_clean() obj.save() watcher.send_update(ar.request) #~ return self.saved_diffs_response(ar,obj) return self.goto_client_response(ar,obj,_("%s has been saved.") % dd.obj2unicode(obj)) def no(ar): return self.goto_client_response(ar,oldobj) #~ print 20131108, msg cb = ar.add_callback(msg) cb.add_choice('yes',yes,_("Yes")) cb.add_choice('no',no,_("No")) ar.set_callback(cb)
def unused_save(self, *args, **kw): # see blog/2012/0929 if not isinstance(self.owner, Postable): # raise Exception("Controller of a Posting must be a Postable.") raise ValidationError("Controller %s (%r,%r) is not a Postable" % ( dd.obj2str(self.owner), self.owner_type, self.owner_id)) #~ raise ValidationError("Controller %s is not a Postable" % dd.obj2str(self.owner)) super(Posting, self).save(*args, **kw)
def on_create(sender=None,request=None,**kw): """ To be called when a new instance has actually been created and saved. """ master = get_master(sender) if master is None: return log_change(ChangeTypes.create,request,master,sender,dd.obj2str(sender,True))
def on_delete(sender=None,request=None,**kw): """ Calls :func:`log_change` with `ChangeTypes.delete`. Note that you must call this before actually deleting the object, otherwise mysql (not sqlite) says ERROR: (1048, "Column 'object_id' cannot be null") """ master = get_master(sender) if master is None: return log_change(ChangeTypes.delete,request,master,sender,dd.obj2str(sender,True))
def on_update(sender=None, request=None, **kw): # Note that sender is a Watcher instance master = get_master(sender.watched) if master is None: # No master, nothing to log return cs = sender.watched.change_watcher_spec changes = [] for k, old in sender.original_state.iteritems(): if not k in cs.ignored_fields: new = sender.watched.__dict__.get(k, dd.NOT_PROVIDED) if old != new: changes.append("%s : %s --> %s" % (k, dd.obj2str(old), dd.obj2str(new))) if len(changes) == 0: msg = '(no changes)' elif len(changes) == 1: msg = changes[0] else: msg = '- ' + ('\n- '.join(changes)) log_change(ChangeTypes.update, request, master, sender.watched, msg)
def place2objects(country, place, parent=None): t = cd2type(place) if t is None: logger.info("20140612 ignoring place %s", place) return obj = countries.Place( country=country, type=t, name=place.name, parent=parent, zip_code=place.zip_code) # We must save the parent before we can generate children. try: obj.full_clean() except Exception as e: raise Exception("Could not save %s : %r" % ( dd.obj2str(obj), e)) obj.save() yield obj for cp in place.children: yield place2objects(country, cp, obj)