def add(self, view_id, xpath_expr): view_id = int(view_id) proxy = rpc.RPCProxy('ir.ui.view') res = proxy.read([view_id], ['model', 'arch'])[0] doc = xml.dom.minidom.parseString(res['arch'].encode('utf-8')) model = res['model'] field_node = tools.xml_locate(xpath_expr, doc)[0] model = _get_model(field_node, parent_model=model) # get the fields proxy = rpc.RPCProxy(model) fields = proxy.fields_get().keys() nodes = _CHILDREN.keys() nodes.remove('view') nodes.sort() fields.sort() positions = [('inside', 'Inside'), ('after', 'After'), ('before', 'Before')] if field_node.localName in [k for k,v in _CHILDREN.items() if not v] + ['field']: positions = [('after', 'After'), ('before', 'Before'), ('inside', 'Inside')] return dict(view_id=view_id, xpath_expr=xpath_expr, nodes=nodes, fields=fields, model=model, positions=positions)
def create(self): tg_errors = None proxy = rpc.RPCProxy('res.users') action_id = proxy.action_get({}) action = rpc.RPCProxy('ir.actions.act_window').read( [action_id], False, rpc.session.context)[0] view_ids = [] if action.get('views', []): view_ids = [x[0] for x in action['views']] elif action.get('view_id', False): view_ids = [action['view_id'][0]] params = TinyDict() params.id = rpc.session.uid params.ids = [params.id] params.model = 'res.users' params.view_type = 'form' params.view_mode = ['form'] params.view_ids = view_ids params.string = _('Preferences') params.editable = True form = self.create_form(params, tg_errors) return dict(form=form, params=params, editable=True)
def get_namelist(self, **kw): params, data = TinyDict.split(kw) ctx = params.context or {} ctx.update(rpc.session.context.copy()) res = [] ids = [] id = params.id res = self.get_data(params.model, ctx) ir_export = rpc.RPCProxy('ir.exports') ir_export_line = rpc.RPCProxy('ir.exports.line') field = ir_export.read(id) fields = ir_export_line.read(field['export_fields']) name_list = [] ids = [f['name'] for f in fields] for name in ids: name_list += [(name, res.get(name))] return dict(name_list=name_list)
def get_info(self, id, **kw): proxy = rpc.RPCProxy("workflow") search_ids = proxy.search([('id', '=', int(id))], 0, 0, 0, rpc.session.context) graph_search = proxy.graph_get(search_ids[0], (140, 180), rpc.session.context) nodes = graph_search['nodes'] transitions = graph_search['transitions'] connectors = {} list_tr = [] for tr in transitions: list_tr.append(tr) t = connectors.setdefault(tr, {}) t['id'] = tr t['s_id'] = transitions[tr][0] t['d_id'] = transitions[tr][1] proxy_tr = rpc.RPCProxy("workflow.transition") search_trs = proxy_tr.search([('id', 'in', list_tr)], 0, 0, 0, rpc.session.context) data_trs = proxy_tr.read(search_trs, ['signal', 'condition', 'act_from', 'act_to'], rpc.session.context) for tr in data_trs: t = connectors.get(tr['id']) t['signal'] = tr['signal'] t['condition'] = tr['condition'] t['source'] = tr['act_from'][1] t['destination'] = tr['act_to'][1] proxy_act = rpc.RPCProxy("workflow.activity") search_acts = proxy_act.search([('wkf_id', '=', int(id))], 0, 0, 0, rpc.session.context) data_acts = proxy_act.read( search_acts, ['action', 'kind', 'flow_start', 'flow_stop', 'subflow_id'], rpc.session.context) for act in data_acts: n = nodes.get(str(act['id'])) n['id'] = act['id'] n['flow_start'] = act['flow_start'] n['flow_stop'] = act['flow_stop'] n['action'] = act['action'] n['kind'] = act['kind'] n['subflow_id'] = act['subflow_id'] return dict(nodes=nodes, conn=connectors)
def __init__(self, name, default=None): default = default or '' default = default.split(',') group_ids = rpc.RPCProxy('res.groups').search([]) groups = rpc.RPCProxy('ir.model.data').search([('res_id', 'in', group_ids ), ('model', '=', 'res.groups')]) groups = rpc.RPCProxy('ir.model.data').read(groups, ['module', 'res_id', 'name']) groups_names = rpc.RPCProxy('res.groups').read(group_ids, ['name']) names = dict([(n['id'], n['name']) for n in groups_names]) options = [('%s.%s' % (g['module'], g['name']), names[g['res_id']]) for g in groups] super(GroupsProperty, self).__init__(name=name, options=options, default=default)
def __init__(self, model, toolbar=None, id=None, view_type="form", multi=True, context={}, **kw): super(Sidebar, self).__init__(model=model, id=id) self.multi = multi self.context = context or {} self.view_type = view_type act = 'client_action_multi' toolbar = toolbar or {} self.reports = toolbar.get('print', []) self.actions = toolbar.get('action', []) self.relates = toolbar.get('relate', []) self.attachments = [] proxy = rpc.RPCProxy('ir.values') if self.view_type == 'form': act = 'tree_but_action' actions = proxy.get('action', act, [(self.model, False)], False, self.context) actions = [a[-1] for a in actions] ids = [a['id'] for a in self.actions] for act in actions: if act['id'] not in ids: act['context'] = self.context self.actions.append(act) reports = proxy.get('action', 'client_print_multi', [(self.model, False)], False, self.context) reports = [a[-1] for a in reports] ids = [a['id'] for a in self.reports] for rep in reports: if rep['id'] not in ids: rep['context'] = self.context self.reports.append(rep) if self.view_type == 'form' and id: proxy = rpc.RPCProxy('ir.attachment') ids = proxy.search([('res_model', '=', model), ('res_id', '=', id)], 0, 0, 0, self.context) if ids: attach = [] datas = proxy.read(ids, ['datas_fname', 'name']) self.attachments = [(d['id'], d['datas_fname'] or d['name']) for d in datas if d['datas_fname'] or d['name']]
def duplicate(self, **kw): params, data = TinyDict.split(kw) id = params.id ctx = params.context model = params.model proxy = rpc.RPCProxy(model) new_id = proxy.copy(id, {}, ctx) if new_id: params.id = new_id params.ids += [int(new_id)] params.count += 1 args = { 'model': params.model, 'id': params.id, 'ids': ustr(params.ids), 'view_ids': ustr(params.view_ids), 'view_mode': ustr(params.view_mode), 'domain': ustr(params.domain), 'context': ustr(params.context), 'offset': params.offset, 'limit': params.limit, 'count': params.count, 'search_domain': ustr(params.search_domain) } if new_id: raise redirect(self.path + '/edit', **args) raise redirect(self.path + '/view', **args)
def addInternalLinks(self, text): from openerp import rpc proxy = rpc.RPCProxy('wiki.wiki') def link(path): link = path.group().replace('[', '').replace('[', '').replace( ']', '').replace(']', '').split("|") name_to_search = link[0].strip() mids = proxy.search([('name', 'ilike', name_to_search)]) link_str = "" if mids: if len(link) == 2: link_str = "<a href='/form/view?model=wiki.wiki&id=%s'>%s</a>" % ( mids[0], link[1]) elif len(link) == 1: link_str = "<a href='/form/view?model=wiki.wiki&id=%s'>%s</a>" % ( mids[0], link[0]) else: if len(link) == 2: link_str = "<a href='%s'>%s</a>" % (link[0], link[1]) elif len(link) == 1: link_str = "<a href='/form/edit?model=wiki.wiki&id=False'>%s</a>" % ( link[0]) return link_str bits = _internalLinks.sub(link, text) return bits
def __init__(self, **attrs): super(Selection, self).__init__(**attrs) # m2o as selection if attrs.get('relation') and attrs.get('widget') == 'selection': proxy = rpc.RPCProxy(attrs['relation']) try: domain = attrs.get('domain', []) if isinstance(domain, (str, unicode)): try: domain = eval(domain) except: domain = [] ids = proxy.search(domain) ctx = rpc.session.context.copy() ctx.update(attrs.get('context', {})) self.options = proxy.name_get(ids, ctx) except: self.options = [] else: self.options = attrs.get('selection', []) # determine the actual type if self.options and isinstance(self.options[0][0], basestring): self.kind = 'char' self.validator = validators.String() else: self.validator = validators.Selection()
def moveDown(self, **kw): params, data = TinyDict.split(kw) id = params.id ids = params.ids or [] if id not in ids or ids.index(id) == len(ids) - 1: return dict() proxy = rpc.RPCProxy(params.model) ctx = rpc.session.context.copy() next_id = ids[ids.index(id) + 1] try: res = proxy.read([id, next_id], ['sequence'], ctx) records = dict([(r['id'], r['sequence']) for r in res]) cur_seq = records[id] next_seq = records[next_id] if cur_seq == next_seq: proxy.write([next_id], {'sequence': cur_seq + 1}, ctx) proxy.write([id], {'sequence': next_seq}, ctx) else: proxy.write([id], {'sequence': next_seq}, ctx) proxy.write([next_id], {'sequence': cur_seq}, ctx) return dict() except Exception, e: return dict(error=str(e))
def default(self, id=False, res_model=None, res_id=False): id = (id or False) and int(id) res_id = int(res_id) title = _("Select Workflow") selection = None proxy = rpc.RPCProxy('process.process') if id: res = proxy.read([id], ['name'], rpc.session.context)[0] title = res['name'] else: selection = proxy.search_by_model(res_model, rpc.session.context) if res_model and not selection: selection = proxy.search_by_model(False, rpc.session.context) if len(selection) == 1: id, title = selection[0] selection = None return dict(id=id, res_model=res_model, res_id=res_id, title=title, selection=selection)
def default(self, view_id): try: view_id = eval(view_id) except: pass if isinstance(view_id, basestring) or not view_id: raise common.message(_("Invalid view id.")) proxy = rpc.RPCProxy('ir.ui.view') res = proxy.read([view_id], ['model', 'type'])[0] model = res['model'] view_type = res['type'] headers = [{'string' : 'Name', 'name' : 'string', 'type' : 'char'}, {'string' : '', 'name': 'add', 'type' : 'image', 'width': 2}, {'string' : '', 'name': 'delete', 'type' : 'image', 'width': 2}, {'string' : '', 'name': 'edit', 'type' : 'image', 'width': 2}, {'string' : '', 'name': 'up', 'type' : 'image', 'width': 2}, {'string' : '', 'name': 'down', 'type' : 'image', 'width': 2}] tree = tw.treegrid.TreeGrid('view_tree', model=model, headers=headers, url='/viewed/data?view_id='+str(view_id)) tree.showheaders = False tree.onselection = 'onSelect' tree.onbuttonclick = 'onButtonClick' tree.expandall = True return dict(view_id=view_id, view_type=view_type, model=model, tree=tree)
def _get_model(node, parent_model): parents = [] pnode = node.parentNode while pnode: if pnode.localName == 'field': ch = [] ch += tools.xml_locate('./form[1]', pnode) ch += tools.xml_locate('./tree[1]', pnode) ch += tools.xml_locate('./graph[1]', pnode) ch += tools.xml_locate('./calendar[1]', pnode) if ch: parents += [pnode.getAttribute('name')] pnode = pnode.parentNode parents.reverse() for parent in parents: proxy = rpc.RPCProxy(parent_model) field = proxy.fields_get([parent]) if field: if field[parent].get('relation'): parent_model = field[parent]['relation'] return parent_model
def __init__(self, **attrs): super(Picture, self).__init__(**attrs) height = attrs.get('img_height', attrs.get('height', None)) self.height = height and 'height="%s"' % height or '' width = attrs.get('img_width', attrs.get('width', None)) self.width = width and 'width="%s"' % width or '' self.validator = validators.Binary() ctx = rpc.session.context.copy() ctx.update(self.context or {}) ctx['bin_size'] = False proxy = rpc.RPCProxy(self.model) if '/' in self.name: name = self.name.rsplit('/', 1)[-1] else: name = self.name if not self.id: value = proxy.default_get([name], ctx) else: value = proxy.read([self.id], [name], ctx)[0] value = value.get(name) or (None, None) self.url = generate_url_for_picture(self.model, name, self.id, value)
def get_groups(self, events): if not self.level: return [] obj = self.level['object'] field = self.level['link'] domain = self.level['domain'] keys = [] groups = {} for evt in events: group_id = evt.record[field] group_title = 'None' if not group_id: # create dummy group group_id = 0 if isinstance(group_id, (list, tuple)): group_id, group_title = evt.record[field] elif group_id: group_id, group_title = rpc.RPCProxy(obj).name_get([group_id], rpc.session.context)[0] group = groups.setdefault(group_id, {'id': group_id, 'title': group_title, 'model': obj, 'items': []}) group['items'].append(evt.record_id) if group_id not in keys: keys.append(group_id) return [groups[i] for i in keys]
def clear_binary_data(self, **kw): params, data = TinyDict.split(kw) proxy = rpc.RPCProxy(params.model) ctx = tools.context_with_concurrency_info(params.context, params.concurrency_info) if params.fname: proxy.write([params.id], { params.field: False, params.fname: False }, ctx) else: proxy.write([params.id], {params.field: False}, ctx) args = { 'model': params.model, 'id': params.id, 'ids': ustr(params.ids), 'view_ids': ustr(params.view_ids), 'view_mode': ustr(params.view_mode), 'domain': ustr(params.domain), 'context': ustr(params.context), 'offset': params.offset, 'limit': params.limit, 'count': params.count, 'search_domain': ustr(params.search_domain) } raise redirect(self.path + '/edit', **args)
def __render(self, value): maintenance = None concurrency = False all_params = cherrypy.request.params title = value.title error = value.message target = cherrypy.request.path_info or '/form/save' if isinstance(value, common.Concurrency): concurrency = True if isinstance(value, common.TinyError): proxy = rpc.RPCProxy('maintenance.contract') maintenance = proxy.status() show_header_footer = int( cherrypy.request.params.get('_terp_header_footer', 0)) return dict(title=title, error=error, maintenance=maintenance, nb=self.nb, show_header_footer=show_header_footer, concurrency=concurrency, all_params=all_params, target=target)
def _get_gantt_records(self, model, ids=None, group=None): if group: record = {'id': group['id']} record['items'] = {'name': group['title']} record['action'] = None record['target'] = None record['icon'] = None record['children'] = self._get_gantt_records(model, group['items']) return [record] proxy = rpc.RPCProxy(model) ctx = rpc.session.context.copy() records = [] for id in ids: record = {'id': id} record['items'] = {'name': proxy.name_get([id], ctx)[0][-1]} record['action'] = 'javascript: void(0)' record['target'] = None record['icon'] = None record['children'] = None records.append(record) return records
def get_events(self, days): proxy = rpc.RPCProxy(self.model) #XXX: how to get events not falling between the given day range but spans the range? #domain = self.domain + [(self.date_start, '>', days[0].prev().isoformat()), # (self.date_start, '<', days[-1].next().isoformat())] first = days[0].month2.prev()[0] #HACK: add prev month domain = self.domain + [ (self.date_start, '>', first.isoformat()), (self.date_start, '<', days[-1].next().isoformat()) ] # convert color values from string to python values if self.color_values and self.color_field in self.fields: try: import openerp.widgets as tw atr = self.fields[self.color_field] atr['required'] = False wid = tw.form.WIDGETS[atr['type']](**atr) vals = self.color_values[:] for i, v in enumerate(vals): try: vals[i] = wid.validator.to_python(v) except: pass domain += [(self.color_field, "in", vals)] except Exception, e: pass
def get_info(self, id, **kw): proxy_act = rpc.RPCProxy('workflow.activity') search_act = proxy_act.search([('id', '=', int(id))], 0, 0, 0, rpc.session.context) data = proxy_act.read(search_act, [], rpc.session.context) return dict(data=data[0])
def edit(self, view_id, xpath_expr): view_id = int(view_id) proxy = rpc.RPCProxy('ir.ui.view') res = proxy.read([view_id], ['model', 'arch'])[0] doc = xml.dom.minidom.parseString(res['arch'].encode('utf-8')) field = tools.xml_locate(xpath_expr, doc)[0] attrs = tools.node_attributes(field) editors = [] properties = _PROPERTIES.get(field.localName, []) if field.localName == 'field': kind = 'char' try: model = _get_model(field, parent_model=res['model']) proxy = rpc.RPCProxy(model) attrs2 = proxy.fields_get([attrs['name']])[attrs['name']] attrs2.update(attrs) if attrs2.get('widget', False): if attrs2['widget']=='one2many_list': attrs2['widget']='one2many' attrs2['type'] = attrs2['widget'] kind = attrs2.get('type', kind) except: pass properties = _PROPERTIES_FIELDS.get(kind) or properties properties = properties[:] properties += list(set(attrs.keys()) - set(properties)) for prop in properties: if field.localName == 'action' and prop == 'name': ed = ActionProperty(prop, attrs.get(prop)) elif field.localName == 'button' and prop in _PROPERTY_WIDGETS_BUTTON: ed = _PROPERTY_WIDGETS_BUTTON[prop](prop, attrs.get(prop)) else: ed = get_property_widget(prop, attrs.get(prop)) ed.label = prop editors += [ed] return dict(view_id=view_id, xpath_expr=xpath_expr, editors=editors)
def delete(self, model, id): id = int(id) proxy = rpc.RPCProxy('workflow') proxy.unlink(id) raise redirect('/workflowlist', model=model)
def get_info(self, id, **kw): proxy_tr = rpc.RPCProxy('workflow.transition') search_tr = proxy_tr.search([('id', '=', int(id))], 0, 0, 0, rpc.session.context) data = proxy_tr.read(search_tr, [], rpc.session.context) return dict(data=data[0])
def get_default_value(self, model, field): field = field.split('/')[-1] res = rpc.RPCProxy(model).default_get([field]) value = res.get(field) return dict(value=value)
def delete(self, model, id): id = int(id) proxy = rpc.RPCProxy('ir.ui.view') proxy.unlink(id) raise redirect('/viewlist', model=model)
def delete_listname(self, **kw): params, data = TinyDict.split(kw) proxy = rpc.RPCProxy('ir.exports') proxy.unlink(params.id) raise redirect('/impex/exp', **kw)
def save_as(self, fname=None, record=False, **kw): record = int(record) proxy = rpc.RPCProxy('ir.attachment') data = proxy.read([record], [], rpc.session.context) if len(data) and not data[0]['link'] and data[0]['datas']: return base64.decodestring(data[0]['datas']) else: return ''
def remove(self, **kw): params, data = TinyDict.split(kw) error = None proxy = rpc.RPCProxy(params.model) if params.ids: try: ctx = tools.context_with_concurrency_info( params.context, params.concurrency_info) if isinstance(params.ids, list): res = proxy.unlink(params.ids, ctx) else: res = proxy.unlink([params.ids], ctx) if params.model == 'res.request': ids, ids2 = rpc.RPCProxy(params.model).request_get() cherrypy.session['terp_requests'] = (ids, ids2) return dict(msg=_('%s request(s)') % len(ids)) except Exception, e: error = ustr(e)
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 = tools.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) 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 add(self, id): id = int(id) proxy = rpc.RPCProxy('ir.ui.view_sc') sc = cherrypy.session.get('terp_shortcuts', False) if sc: for s in sc: if s['res_id'] == id: raise redirect('/tree/open', id=id, model='ir.ui.menu') name = rpc.RPCProxy('ir.ui.menu').name_get([id], rpc.session.context)[0][1] proxy.create({'user_id': rpc.session.uid, 'res_id': id, 'resource': 'ir.ui.menu', 'name': name}) sc = proxy.get_sc(rpc.session.uid, 'ir.ui.menu', rpc.session.context) cherrypy.session['terp_shortcuts'] = sc raise redirect('/tree/open', id=id, model='ir.ui.menu')