def save(self, **kw): params, data = TinyDict.split(kw) error = None error_field = None id = params.id or 0 id = (id > 0) and id or 0 ids = params.ids or [] model = params.parent.model if model != params.model and not params.parent.id: error = _("Parent record doesn't exists...") if error: return dict(error=error) try: proxy = rpc.RPCProxy(model) frm = TinyForm(**kw).to_python() data = {} ctx = context_with_concurrency_info(params.parent.context, params.concurrency_info) source = params.source if source and source != '_terp_list': data = frm.chain_get(source) if data is None: return dict(error_field=error_field, error=error, id=id, ids=str([int(i) for i in ids])) if '__id' in data: data.pop('__id') if 'id' in data: data.pop('id') fld = source.split('/')[-1] data = {fld : [(id and 1, id, data.copy())]} proxy.write([params.parent.id], data, ctx) if not id: all_ids = proxy.read([params.parent.id], [fld])[0][fld] new_ids = [i for i in all_ids if i not in ids] ids = all_ids if new_ids: id = new_ids[0] else: data = frm.copy() if 'id' in data: data.pop('id') if id > 0: proxy.write([id], data, ctx) else: id = proxy.create(data, params.parent.context or {}) ids = [id] + ids except TinyFormError, e: error_field = e.field error = ustr(e)
def get(self, **kw): params, data = TinyDict.split(kw) field = params.field.split('/') prefix = '.'.join(field[:-1]) field = field[-1] pctx = TinyForm(**kw).to_python(safe=True) ctx = pctx.chain_get(prefix) or pctx fields = rpc.RPCProxy(params.model).fields_get(False, rpc.session.context) if field not in fields: return {} text = fields[field].get('string') deps = [] for name, attrs in fields.iteritems(): if attrs.get('change_default'): value = ctx.get(name) if value: deps.append((name, name, value, value)) return dict(text=text, deps=str(deps))
def save(self, **kw): params, data = TinyDict.split(kw) error = None error_field = None id = params.id or 0 id = (id > 0) and id or 0 ids = params.ids or [] model = params.parent.model if model != params.model and not params.parent.id: error = _("Parent record doesn't exists...") if error: return dict(error=error) try: proxy = rpc.RPCProxy(model) frm = TinyForm(**kw).to_python() data = {} ctx = context_with_concurrency_info(params.parent.context, params.concurrency_info) source = params.source if source and source != '_terp_list': data = frm.chain_get(source) if '__id' in data: data.pop('__id') if 'id' in data: data.pop('id') fld = source.split('/')[-1] data = {fld: [(id and 1, id, data.copy())]} proxy.write([params.parent.id], data, ctx) if not id: all_ids = proxy.read([params.parent.id], [fld])[0][fld] new_ids = [i for i in all_ids if i not in ids] ids = all_ids if new_ids: id = new_ids[0] else: data = frm.copy() if 'id' in data: data.pop('id') if id > 0: proxy.write([id], data, ctx) else: id = proxy.create(data, params.parent.context or {}) ids = [id] + ids except TinyFormError, e: error_field = e.field error = ustr(e)
def on_change(self, **kw): data = kw.copy() callback = data.pop('_terp_callback') caller = data.pop('_terp_caller') model = data.pop('_terp_model') context = data.pop('_terp_context') try: context = eval(context) # convert to python dict except: context = {} match = re.match('^(.*?)\((.*)\)$', callback) if not match: raise common.error(_('Application Error!'), _('Wrong on_change trigger: %s') % callback) for k, v in data.items(): try: data[k] = eval(v) except: pass result = {} prefix = '' if '/' in caller: prefix = caller.rsplit('/', 1)[0] ctx = TinyForm(**kw).to_python(safe=True) pctx = ctx if prefix: ctx = ctx.chain_get(prefix) if '/' in prefix: pprefix = prefix.rsplit('/', 1)[0] pctx = pctx.chain_get(pprefix) ctx2 = rpc.session.context.copy() ctx2.update(context or {}) ctx['parent'] = pctx ctx['context'] = ctx2 func_name = match.group(1) arg_names = [n.strip() for n in match.group(2).split(',')] ctx_dict = dict(**ctx) args = [tools.expr_eval(arg, ctx_dict) for arg in arg_names] proxy = rpc.RPCProxy(model) ids = ctx.id and [ctx.id] or [] try: response = getattr(proxy, func_name)(ids, *args) except Exception, e: return dict(error=ustr(e))
def on_change(self, **kw): data = kw.copy() callback = data.pop('_terp_callback') caller = data.pop('_terp_caller') model = data.pop('_terp_model') context = data.pop('_terp_context') change_default = False if '_terp_change_default' in data: change_default = data.pop('_terp_change_default') try: context = eval(context) # convert to python dict except: context = {} match = re.match('^(.*?)\((.*)\)$', callback) if not match: raise common.error(_('Application Error'), _('Wrong on_change trigger: %s') % callback) for k, v in data.items(): try: data[k] = eval(v) except: pass result = {} prefix = '' if '/' in caller: prefix = caller.rsplit('/', 1)[0] ctx = TinyForm(**kw).to_python(safe=True) pctx = ctx if prefix: ctx = ctx.chain_get(prefix) if '/' in prefix: pprefix = prefix.rsplit('/', 1)[0] pctx = pctx.chain_get(pprefix) ctx2 = dict(rpc.session.context, **context or {}) ctx['parent'] = pctx ctx['context'] = ctx2 func_name = match.group(1) arg_names = [n.strip() for n in match.group(2).split(',')] args = [utils.expr_eval(arg, ctx) for arg in arg_names] # TODO: If the eval fails in expr_eval (because `arg` does not exist in `ctx`), it returns `{}` # This is a value we don't want, but not sure where that behavior # comes from/is used so in order not to risk breakage throughout # patch it here args = [(False if arg == {} else arg) for arg in args] proxy = rpc.RPCProxy(model) ids = ctx.id and [ctx.id] or [] try: response = getattr(proxy, func_name)(ids, *args) except Exception, e: return dict(error=_ep.render())
def on_change(self, **kw): data = kw.copy() callback = data.pop('_terp_callback') caller = data.pop('_terp_caller') model = data.pop('_terp_model') context = data.pop('_terp_context') try: context = eval(context) # convert to python dict except: context = {} match = re.match('^(.*?)\((.*)\)$', callback) if not match: raise common.error(_('Application Error'), _('Wrong on_change trigger: %s') % callback) for k, v in data.items(): try: data[k] = eval(v) except: pass result = {} prefix = '' if '/' in caller: prefix = caller.rsplit('/', 1)[0] ctx = TinyForm(**kw).to_python(safe=True) pctx = ctx if prefix: ctx = ctx.chain_get(prefix) if '/' in prefix: pprefix = prefix.rsplit('/', 1)[0] pctx = pctx.chain_get(pprefix) ctx2 = dict(rpc.get_session().context, **context or {}) ctx['parent'] = pctx ctx['context'] = ctx2 func_name = match.group(1) arg_names = [n.strip() for n in match.group(2).split(',')] args = [utils.expr_eval(arg, ctx) for arg in arg_names] # TODO: If the eval fails in expr_eval (because `arg` does not exist in `ctx`), it returns `{}` # This is a value we don't want, but not sure where that behavior # comes from/is used so in order not to risk breakage throughout # patch it here args = [(False if arg == {} else arg) for arg in args] proxy = rpc.RPCProxy(model) ids = ctx.id and [ctx.id] or [] try: response = getattr(proxy, func_name)(ids, *args) except Exception, e: return dict(error=_ep.render())