def set_workflow_state(row, ar, state_field, target_state): """Called by workflow actions (:class:`ChangeStateAction <lino.core.workflows.ChangeStateAction>`) to perform the actual state change. """ watcher = ChangeWatcher(row) old = getattr(row, state_field.attname) target_state.choicelist.before_state_change(row, ar, old, target_state) row.before_state_change(ar, old, target_state) setattr(row, state_field.attname, target_state) row.save() target_state.choicelist.after_state_change(row, ar, old, target_state) row.after_state_change(ar, old, target_state) watcher.send_update(ar.request) row.after_ui_save(ar, watcher)
def set_workflow_state(row, ar, state_field, target_state): watcher = ChangeWatcher(row) #~ old = row.state old = getattr(row, state_field.attname) target_state.choicelist.before_state_change(row, ar, old, target_state) row.before_state_change(ar, old, target_state) #~ row.state = target_state setattr(row, state_field.attname, target_state) #~ self.before_row_save(row,ar) row.save() target_state.choicelist.after_state_change(row, ar, old, target_state) row.after_state_change(ar, old, target_state) watcher.send_update(ar.request) row.after_ui_save(ar, watcher)
def run_from_code(self, ar, **known_values): obj = ar.selected_rows[0] related = [] for m, fk in obj._lino_ddh.fklist: if fk.name in m.allow_cascaded_delete: related.append((fk, m.objects.filter(**{fk.name: obj}))) if AFTER17: fields_list = obj._meta.concrete_fields else: fields_list = obj._meta.fields if True: for f in fields_list: if not f.primary_key: known_values[f.name] = getattr(obj, f.name) new = obj.__class__(**known_values) # 20120704 create_instances causes fill_from_person() on a # CBSS request. else: # doesn't seem to want to work new = obj for f in fields_list: if f.primary_key: # causes Django to consider this an unsaved instance setattr(new, f.name, None) new.save(force_insert=True) cw = ChangeWatcher(new) new.on_duplicate(ar, None) if cw.is_dirty(): new.full_clean() new.save() for fk, qs in related: for relobj in qs: relobj.pk = None # causes Django to save a copy setattr(relobj, fk.name, new) relobj.on_duplicate(ar, new) relobj.save(force_insert=True) return new
def save_existing_instance(self, elem, ar): watcher = ChangeWatcher(elem) ar.ah.store.form2obj(ar, ar.rqdata, elem, False) elem.full_clean() if watcher.is_dirty(): pre_ui_save.send(sender=elem.__class__, instance=elem, ar=ar) elem.before_ui_save(ar) elem.save(force_update=True) watcher.send_update(ar.request) ar.success(_("%s has been updated.") % obj2unicode(elem)) else: ar.success(_("%s : nothing to save.") % obj2unicode(elem)) elem.after_ui_save(ar, watcher) # TODO: in fact we need *either* `rows` (when this was called # from a Grid) *or* `goto_instance` (when this was called from a # form). But how to find out which one is needed? # if ar.edit_mode == constants.EDIT_MODE_GRID: ar.set_response(rows=[ar.ah.store.row2list(ar, elem)])
def form2obj_and_save(ar, data, elem, is_new): """Parses the data from HttpRequest to the model instance and saves it This is used by `ApiList.post` and `ApiElement.put`, and by `Restful.post` and `Restful.put`. 20140505 : no longer used by ApiList and ApiElement, but still by Restful.* """ if is_new: watcher = None else: watcher = ChangeWatcher(elem) ar.ah.store.form2obj(ar, data, elem, is_new) elem.full_clean() if is_new or watcher.is_dirty(): pre_ui_save.send(sender=elem.__class__, instance=elem, ar=ar) elem.before_ui_save(ar) kw2save = {} if is_new: kw2save.update(force_insert=True) else: kw2save.update(force_update=True) elem.save(**kw2save) if is_new: on_ui_created.send(elem, request=ar.request) ar.success(_("%s has been created.") % obj2unicode(elem)) else: watcher.send_update(ar.request) ar.success(_("%s has been updated.") % obj2unicode(elem)) else: ar.success(_("%s : nothing to save.") % obj2unicode(elem)) elem.after_ui_save(ar, watcher)
def process_row(self, ar, obj, attrs): """Generate a confirmation which asks to update the given data row `obj` using the data read from the eid card (given in `attr`). """ oldobj = obj watcher = ChangeWatcher(obj) objects, diffs = obj.get_beid_diffs(attrs) if len(diffs) == 0: return self.goto_client_response( ar, obj, _("Client %s is up-to-date") % unicode(obj)) msg = _("Click OK to apply the following changes for %s") % obj msg = simulate_wrap(msg) msg += ' :<br/>' msg += '\n<br/>'.join(diffs) def yes(ar2): msg = _("%s has been saved.") % dd.obj2unicode(obj) if not config.read_only_simulate: for o in objects: o.full_clean() o.save() watcher.send_update(ar2.request) msg = simulate_wrap(msg) return self.goto_client_response(ar2, obj, msg) def no(ar2): return self.goto_client_response(ar2, 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 sync_primary_address(self, request): watcher = ChangeWatcher(self) self.sync_primary_address_() watcher.send_update(request)