X-Git-Url: http://git.inspyration.org/?a=blobdiff_plain;f=bin%2Fosv%2Form.py;h=92fb7f9400bacd0e4a36735d1157c4cf6ceb5f75;hb=4f09c9ffa5ef1c45b8480d17fe4af72f74f6edcd;hp=15effe8b84d48d6e2c7ff610e4b1c658f142a2bb;hpb=fdb1682d9403efe4780882d76712f68d3a2e703a;p=odoo%2Fodoo.git diff --git a/bin/osv/orm.py b/bin/osv/orm.py index 15effe8..92fb7f9 100644 --- a/bin/osv/orm.py +++ b/bin/osv/orm.py @@ -69,6 +69,15 @@ from tools.config import config regex_order = re.compile('^(([a-z0-9_]+|"[a-z0-9_]+")( *desc| *asc)?( *, *|))+$', re.I) + +POSTGRES_CONFDELTYPES = { + 'RESTRICT': 'r', + 'NO ACTION': 'a', + 'CASCADE': 'c', + 'SET NULL': 'n', + 'SET DEFAULT': 'd', +} + def last_day_of_current_month(): today = datetime.date.today() last_day = str(calendar.monthrange(today.year, today.month)[1]) @@ -247,6 +256,18 @@ class browse_record(object): new_data[n] = browse_null() elif f._type in ('one2many', 'many2many') and len(data[n]): new_data[n] = self._list_class([browse_record(self._cr, self._uid, id, self._table.pool.get(f._obj), self._cache, context=self._context, list_class=self._list_class, fields_process=self._fields_process) for id in data[n]], self._context) + elif f._type in ('reference'): + if data[n]: + if isinstance(data[n], browse_record): + new_data[n] = data[n] + else: + ref_obj, ref_id = data[n].split(',') + ref_id = long(ref_id) + obj = self._table.pool.get(ref_obj) + compids = False + new_data[n] = browse_record(self._cr, self._uid, ref_id, obj, self._cache, context=self._context, list_class=self._list_class, fields_process=self._fields_process) + else: + new_data[n] = browse_null() else: new_data[n] = data[n] self._data[data['id']].update(new_data) @@ -370,6 +391,10 @@ class orm_template(object): CONCURRENCY_CHECK_FIELD = '__last_update' + def view_init(self, cr , uid , fields_list, context=None): + """Override this method to do specific things when a view on the object is opened.""" + pass + def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None): raise _('The read_group method is not implemented on this object !') @@ -383,7 +408,7 @@ class orm_template(object): model_id = cr.fetchone()[0] if 'module' in context: name_id = 'model_'+self._name.replace('.','_') - cr.execute('select * from ir_model_data where name=%s and res_id=%s', (name_id,model_id)) + cr.execute('select * from ir_model_data where name=%s and res_id=%s and module=%s', (name_id,model_id,context['module'])) if not cr.rowcount: cr.execute("INSERT INTO ir_model_data (name,date_init,date_update,module,model,res_id) VALUES (%s, now(), now(), %s, %s, %s)", \ (name_id, context['module'], 'ir.model', model_id) @@ -711,6 +736,7 @@ class orm_template(object): else: module, xml_id = current_module, line[i] id = ir_model_data_obj._get_id(cr, uid, module, xml_id) + res_res_id = ir_model_data_obj.read(cr, uid, [id], ['res_id']) if res_res_id: @@ -1008,7 +1034,7 @@ class orm_template(object): continue res[f] = {'type': self._columns[f]._type} for arg in ('string', 'readonly', 'states', 'size', 'required', 'group_operator', - 'change_default', 'translate', 'help', 'select', 'selectable'): + 'change_default', 'translate', 'help', 'select', 'selectable','parent_field'): if getattr(self._columns[f], arg): res[f][arg] = getattr(self._columns[f], arg) if not read_access: @@ -1476,7 +1502,7 @@ class orm_template(object): if context and context.get('active_id',False): data_menu = self.pool.get('ir.ui.menu').browse(cr, user, context['active_id'], context).action if data_menu: - act_id = int(data_menu.split(',')[1]) + act_id = data_menu.id if act_id: data_action = self.pool.get('ir.actions.act_window').browse(cr, user, [act_id], context)[0] result['submenu'] = getattr(data_action,'menus', False) @@ -1514,6 +1540,10 @@ class orm_template(object): 'action': resaction, 'relate': resrelate } + if result['type']=='form' and result['arch'].count("default_focus")>1: + msg = "Form View contain more than one default_focus attribute" + netsvc.Logger().notifyChannel('orm', netsvc.LOG_ERROR, msg) + raise except_orm('View Error !',msg) return result _view_look_dom_arch = __view_look_dom_arch @@ -1692,6 +1722,7 @@ class orm_memory(orm_template): return id_new def default_get(self, cr, uid, fields_list, context=None): + self.view_init(cr, uid, fields_list, context) if not context: context = {} value = {} @@ -1847,13 +1878,16 @@ class orm(orm_template): _protected = ['read','write','create','default_get','perm_read','unlink','fields_get','fields_view_get','search','name_get','distinct_field_get','name_search','copy','import_data','search_count', 'exists'] def read_group(self, cr, uid, domain, fields, groupby, offset=0, limit=None, context=None): + groupby_list = groupby + if isinstance(groupby, list): + groupby = groupby[0] context = context or {} self.pool.get('ir.model.access').check(cr, uid, self._name, 'read', context=context) if not fields: fields = self._columns.keys() (where_clause, where_params, tables) = self._where_calc(cr, uid, domain, context=context) - dom = self.pool.get('ir.rule').domain_get(cr, uid, self._name, context=context) + dom = self.pool.get('ir.rule').domain_get(cr, uid, self._name, 'read', context=context) where_clause = where_clause + dom[0] where_params = where_params + dom[1] for t in dom[2]: @@ -1893,6 +1927,8 @@ class orm(orm_template): alldata = {} groupby = group_by for r in cr.dictfetchall(): + for fld,val in r.items(): + if val == None:r[fld] = False alldata[r['id']] = r del r['id'] data = self.read(cr, uid, alldata.keys(), [groupby], context=context) @@ -1900,6 +1936,7 @@ class orm(orm_template): for d in data: d['__domain'] = [(groupby,'=',alldata[d['id']][groupby] or False)] + domain + d['__context'] = {'group_by':groupby_list[1:]} if fget.has_key(groupby): if d[groupby] and fget[groupby]['type'] in ('date','datetime'): dt = datetime.datetime.strptime(alldata[d['id']][groupby][:7],'%Y-%m') @@ -2255,14 +2292,7 @@ class orm(orm_template): "AND con.contype = 'f'", (self._table, ref, k, 'id')) res = cr.dictfetchall() if res: - confdeltype = { - 'RESTRICT': 'r', - 'NO ACTION': 'a', - 'CASCADE': 'c', - 'SET NULL': 'n', - 'SET DEFAULT': 'd', - } - if res[0]['confdeltype'] != confdeltype.get(f.ondelete.upper(), 'a'): + if res[0]['confdeltype'] != POSTGRES_CONFDELTYPES.get(f.ondelete.upper(), 'a'): cr.execute('ALTER TABLE "' + self._table + '" DROP CONSTRAINT "' + res[0]['conname'] + '"') cr.execute('ALTER TABLE "' + self._table + '" ADD FOREIGN KEY ("' + k + '") REFERENCES "' + ref + '" ON DELETE ' + f.ondelete) cr.commit() @@ -2455,6 +2485,7 @@ class orm(orm_template): # # Update objects that uses this one to update their _inherits fields # + def _inherits_reload_src(self): for obj in self.pool.obj_pool.values(): if self._name in obj._inherits: @@ -2521,8 +2552,7 @@ class orm(orm_template): fields_to_read = self._columns.keys() # construct a clause for the rules : - d1, d2, tables = self.pool.get('ir.rule').domain_get(cr, user, self._name, context=context) - + d1, d2, tables = self.pool.get('ir.rule').domain_get(cr, user, self._name, 'read', context=context) # all inherited fields + all non inherited fields for which the attribute whose name is in load is True fields_pre = [f for f in fields_to_read if f == self.CONCURRENCY_CHECK_FIELD @@ -2543,17 +2573,16 @@ class orm(orm_template): return '"%s"' % (f,) fields_pre2 = map(convert_field, fields_pre) order_by = self._parent_order or self._order - for i in range(0, len(ids), cr.IN_MAX): - sub_ids = ids[i:i+cr.IN_MAX] + for sub_ids in cr.split_for_in_conditions(ids): if d1: - cr.execute('SELECT %s FROM %s WHERE %s.id = ANY (%%s) AND %s ORDER BY %s' % \ + cr.execute('SELECT %s FROM %s WHERE %s.id IN %%s AND %s ORDER BY %s' % \ (','.join(fields_pre2 + [self._table + '.id']), ','.join(tables), self._table, ' and '.join(d1), order_by),[sub_ids,]+d2) - if not cr.rowcount == len({}.fromkeys(sub_ids)): + if cr.rowcount != len(sub_ids): raise except_orm(_('AccessError'), _('You try to bypass an access rule while reading (Document type: %s).') % self._description) else: - cr.execute('SELECT %s FROM \"%s\" WHERE id = ANY (%%s) ORDER BY %s' % + cr.execute('SELECT %s FROM \"%s\" WHERE id IN %%s ORDER BY %s' % (','.join(fields_pre2 + ['id']), self._table, order_by), (sub_ids,)) res.extend(cr.dictfetchall()) @@ -2705,6 +2734,25 @@ class orm(orm_template): if res and res[0]: raise except_orm('ConcurrencyException', _('Records were modified in the meanwhile')) + def check_access_rule(self, cr, uid, ids, operation, context=None): + """Verifies that the operation given by ``operation`` is allowed for the user + according to ir.rules. + @param ``operation``: one of ``'read'``, ``'write'``, ``'unlink'`` + @raise ``except_orm``: if current ir.rules do not permit this operation. + @return: ``None`` if the operation is allowed + """ + where_clause, where_params, tables = self.pool.get('ir.rule').domain_get(cr, uid, self._name, operation, context=context) + if where_clause: + where_clause = ' and ' + ' and '.join(where_clause) + for sub_ids in cr.split_for_in_conditions(ids): + cr.execute('SELECT ' + self._table + '.id FROM ' + ','.join(tables) + + ' WHERE ' + self._table + '.id IN %s' + where_clause, + [sub_ids] + where_params) + if cr.rowcount != len(sub_ids): + raise except_orm(_('AccessError'), + _('Operation prohibited by access rules (Operation: %s, Document type: %s).') + % (operation, self._name)) + def unlink(self, cr, uid, ids, context=None): if not ids: return True @@ -2734,26 +2782,13 @@ class orm(orm_template): # ids2 = [x[self._inherits[key]] for x in res] # self.pool.get(key).unlink(cr, uid, ids2) - d1, d2,tables = self.pool.get('ir.rule').domain_get(cr, uid, self._name, context=context) - if d1: - d1 = ' AND '+' and '.join(d1) - - for i in range(0, len(ids), cr.IN_MAX): - sub_ids = ids[i:i+cr.IN_MAX] - str_d = string.join(('%s',)*len(sub_ids), ',') - if d1: - cr.execute('SELECT '+self._table+'.id FROM '+','.join(tables)+' ' \ - 'WHERE '+self._table+'.id IN ('+str_d+')'+d1, sub_ids+d2) - if not cr.rowcount == len(sub_ids): - raise except_orm(_('AccessError'), - _('You try to bypass an access rule (Document type: %s).') % \ - self._description) - - cr.execute('delete from '+self._table+' ' \ - 'where id in ('+str_d+')', sub_ids) + self.check_access_rule(cr, uid, ids, 'unlink', context=context) + for sub_ids in cr.split_for_in_conditions(ids): + cr.execute('delete from ' + self._table + ' ' \ + 'where id in %s', sub_ids) for order, object, store_ids, fields in result_store: - if object<>self._name: + if object != self._name: obj = self.pool.get(object) cr.execute('select id from '+obj._table+' where id in ('+','.join(map(str, store_ids))+')') rids = map(lambda x: x[0], cr.fetchall()) @@ -2849,29 +2884,10 @@ class orm(orm_template): upd1.append(user) if len(upd0): - - d1, d2,tables = self.pool.get('ir.rule').domain_get(cr, user, self._name, context=context) - if d1: - d1 = ' and '+' and '.join(d1) - - for i in range(0, len(ids), cr.IN_MAX): - sub_ids = ids[i:i+cr.IN_MAX] - ids_str = string.join(map(str, sub_ids), ',') - if d1: - cr.execute('SELECT '+self._table+'.id FROM '+','.join(tables)+' ' \ - 'WHERE '+self._table+'.id IN ('+ids_str+')'+d1, d2) - if not cr.rowcount == len({}.fromkeys(sub_ids)): - raise except_orm(_('AccessError'), - _('You try to bypass an access rule while writing (Document type: %s).') % \ - self._description) - else: - cr.execute('SELECT id FROM "'+self._table+'" WHERE id IN ('+ids_str+')') - if not cr.rowcount == len({}.fromkeys(sub_ids)): - raise except_orm(_('AccessError'), - _('You try to write on an record that doesn\'t exist ' \ - '(Document type: %s).') % self._description) - cr.execute('update '+self._table+' set '+string.join(upd0, ',')+' ' \ - 'where id in ('+ids_str+')', upd1) + self.check_access_rule(cr, user, ids, 'write', context=context) + for sub_ids in cr.split_for_in_conditions(ids): + cr.execute('update ' + self._table + ' set ' + ','.join(upd0) + ' ' \ + 'where id in %s', upd1 + [sub_ids]) if totranslate: # TODO: optimize @@ -2902,11 +2918,9 @@ class orm(orm_template): for table in self._inherits: col = self._inherits[table] nids = [] - for i in range(0, len(ids), cr.IN_MAX): - sub_ids = ids[i:i+cr.IN_MAX] - ids_str = string.join(map(str, sub_ids), ',') + for sub_ids in cr.split_for_in_conditions(ids): cr.execute('select distinct "'+col+'" from "'+self._table+'" ' \ - 'where id in ('+ids_str+')', upd1) + 'where id in %s', (sub_ids,)) nids.extend([x[0] for x in cr.fetchall()]) v = {} @@ -3009,7 +3023,7 @@ class orm(orm_template): if self._inherits[v] not in vals: tocreate[v] = {} else: - tocreate[v] = {self._inherits[v]:vals[self._inherits[v]]} + tocreate[v] = {'id' : vals[self._inherits[v]]} (upd0, upd1, upd2) = ('', '', []) upd_todo = [] for v in vals.keys(): @@ -3033,10 +3047,17 @@ class orm(orm_template): for table in tocreate: if self._inherits[table] in vals: del vals[self._inherits[table]] - id = self.pool.get(table).create(cr, user, tocreate[table]) + + record_id = tocreate[table].pop('id', None) + + if record_id is None: + record_id = self.pool.get(table).create(cr, user, tocreate[table], context=context) + else: + self.pool.get(table).write(cr, user, [record_id], tocreate[table], context=context) + upd0 += ','+self._inherits[table] upd1 += ',%s' - upd2.append(id) + upd2.append(record_id) #Start : Set bool fields to be False if they are not touched(to make search more powerful) bool_fields = [x for x in self._columns.keys() if self._columns[x]._type=='boolean'] @@ -3078,7 +3099,8 @@ class orm(orm_template): upd1 = upd1 + ',' + self._columns[field]._symbol_set[0] upd2.append(self._columns[field]._symbol_set[1](vals[field])) else: - upd_todo.append(field) + if not isinstance(self._columns[field], fields.related): + upd_todo.append(field) if field in self._columns \ and hasattr(self._columns[field], 'selection') \ and vals[field]: @@ -3102,6 +3124,15 @@ class orm(orm_template): upd1 += ',%s,now()' upd2.append(user) cr.execute('insert into "'+self._table+'" (id'+upd0+") values ("+str(id_new)+upd1+')', tuple(upd2)) + d1, d2, tables = self.pool.get('ir.rule').domain_get(cr, user, self._name, 'create', context=context) + if d1: + d1 = ' AND '+' AND '.join(d1) + cr.execute('SELECT '+self._table+'.id FROM '+','.join(tables)+' ' \ + 'WHERE '+self._table+'.id = ' +str(id_new)+d1,d2) + if not cr.rowcount: + raise except_orm(_('AccessError'), + _('You try to bypass an access rule to create (Document type: %s).') \ + % self._name) upd_todo.sort(lambda x, y: self._columns[x].priority-self._columns[y].priority) if self._parent_store: @@ -3239,8 +3270,9 @@ class orm(orm_template): upd0.append('"'+v+'"='+self._columns[v]._symbol_set[0]) upd1.append(self._columns[v]._symbol_set[1](value[v])) upd1.append(id) - cr.execute('update "' + self._table + '" set ' + \ - string.join(upd0, ',') + ' where id = %s', upd1) + if upd0 and upd1: + cr.execute('update "' + self._table + '" set ' + \ + string.join(upd0, ',') + ' where id = %s', upd1) else: for f in val: @@ -3308,7 +3340,7 @@ class orm(orm_template): context = {} # compute the where, order by, limit and offset clauses (qu1, qu2, tables) = self._where_calc(cr, user, args, context=context) - dom = self.pool.get('ir.rule').domain_get(cr, user, self._name, context=context) + dom = self.pool.get('ir.rule').domain_get(cr, user, self._name, 'read', context=context) qu1 = qu1 + dom[0] qu2 = qu2 + dom[1] for t in dom[2]: