diff options
Diffstat (limited to 'addons/web/controllers/main.py')
| -rw-r--r-- | addons/web/controllers/main.py | 124 |
1 files changed, 56 insertions, 68 deletions
diff --git a/addons/web/controllers/main.py b/addons/web/controllers/main.py index 3fcd074aeb0..5be3a1a3ef6 100644 --- a/addons/web/controllers/main.py +++ b/addons/web/controllers/main.py @@ -33,7 +33,8 @@ from odoo.tools import topological_sort from odoo.tools.translate import _ from odoo.tools.misc import str2bool, xlwt from odoo import http -from odoo.http import request, serialize_exception as _serialize_exception, content_disposition +from odoo.http import content_disposition, dispatch_rpc, request, \ + serialize_exception as _serialize_exception from odoo.exceptions import AccessError from odoo.models import check_method_name @@ -291,8 +292,7 @@ def set_cookie_and_redirect(redirect_url): return redirect def load_actions_from_ir_values(action_slot, model, res_id): - ir_values = request.session.model('ir.values') - actions = ir_values.get_actions(action_slot=action_slot, model=model, res_id=res_id, context=request.context) + actions = request.env['ir.values'].get_actions(action_slot, model, res_id) return [(id, name, clean_action(action)) for id, name, action in actions] def clean_action(action): @@ -632,7 +632,7 @@ class Database(http.Controller): try: # country code could be = "False" which is actually True in python country_code = post.get('country_code') or False - request.session.proxy("db").create_database(master_pwd, name, bool(post.get('demo')), lang, password, post.get('login'), country_code) + dispatch_rpc('db', 'create_database', [master_pwd, name, bool(post.get('demo')), lang, password, post.get('login'), country_code]) request.session.authenticate(name, 'admin', password) return http.local_redirect('/web/') except Exception, e: @@ -642,7 +642,7 @@ class Database(http.Controller): @http.route('/web/database/duplicate', type='http', auth="none", methods=['POST'], csrf=False) def duplicate(self, master_pwd, name, new_name): try: - request.session.proxy("db").duplicate_database(master_pwd, name, new_name) + dispatch_rpc('db', 'duplicate_database', [master_pwd, name, new_name]) return http.local_redirect('/web/database/manager') except Exception, e: error = "Database duplication error: %s" % e @@ -651,7 +651,7 @@ class Database(http.Controller): @http.route('/web/database/drop', type='http', auth="none", methods=['POST'], csrf=False) def drop(self, master_pwd, name): try: - request.session.proxy("db").drop(master_pwd, name) + dispatch_rpc('db','drop', [master_pwd, name]) return http.local_redirect('/web/database/manager') except Exception, e: error = "Database deletion error: %s" % e @@ -679,7 +679,7 @@ class Database(http.Controller): def restore(self, master_pwd, backup_file, name, copy=False): try: data = base64.b64encode(backup_file.read()) - request.session.proxy("db").restore(master_pwd, name, data, str2bool(copy)) + dispatch_rpc('db', 'restore', [master_pwd, name, data, str2bool(copy)]) return http.local_redirect('/web/database/manager') except Exception, e: error = "Database restore error: %s" % e @@ -688,7 +688,7 @@ class Database(http.Controller): @http.route('/web/database/change_password', type='http', auth="none", methods=['POST'], csrf=False) def change_password(self, master_pwd, master_pwd_new): try: - request.session.proxy("db").change_admin_password(master_pwd, master_pwd_new) + dispatch_rpc('db', 'change_admin_password', [master_pwd, master_pwd_new]) return http.local_redirect('/web/database/manager') except Exception, e: error = "Master password update error: %s" % e @@ -717,8 +717,7 @@ class Session(http.Controller): if new_password != confirm_password: return {'error': _('The new password and its confirmation must be identical.'),'title': _('Change Password')} try: - if request.session.model('res.users').change_password( - old_password, new_password): + if request.env['res.users'].change_password(old_password, new_password): return {'new_password':new_password} except Exception: return {'error': _('The old password you provided is incorrect, your password was not changed.'), 'title': _('Change Password')} @@ -727,7 +726,7 @@ class Session(http.Controller): @http.route('/web/session/get_lang_list', type='json', auth="none") def get_lang_list(self): try: - return request.session.proxy("db").list_lang() or [] + return dispatch_rpc('db', 'list_lang', []) or [] except Exception, e: return {"error": e, "title": _("Languages")} @@ -765,7 +764,7 @@ class Session(http.Controller): @http.route('/web/session/check', type='json', auth="user") def check(self): - request.session.assert_valid() + request.session.check_security() return None @http.route('/web/session/destroy', type='json', auth="user") @@ -786,13 +785,14 @@ class Menu(http.Controller): :return: needaction data :rtype: dict(menu_id: {'needaction_enabled': boolean, 'needaction_counter': int}) """ - return request.session.model('ir.ui.menu').get_needaction_data(menu_ids, request.context) + return request.env['ir.ui.menu'].browse(menu_ids).get_needaction_data() class DataSet(http.Controller): @http.route('/web/dataset/search_read', type='json', auth="user") def search_read(self, model, fields=False, offset=0, limit=False, domain=None, sort=None): return self.do_search_read(model, fields, offset, limit, domain, sort) + def do_search_read(self, model, fields=False, offset=0, limit=False, domain=None , sort=None): """ Performs a search() followed by a read() (if needed) using the @@ -810,17 +810,17 @@ class DataSet(http.Controller): matching fields selection set) :rtype: list """ - Model = request.session.model(model) + Model = request.env[model] - records = Model.search_read(domain, fields, offset or 0, limit or False, sort or False, - request.context) + records = Model.search_read(domain, fields, + offset=offset or 0, limit=limit or False, order=sort or False) if not records: return { 'length': 0, 'records': [] } if limit and len(records) == limit: - length = Model.search_count(domain, request.context) + length = Model.search_count(domain) else: length = len(records) + (offset or 0) return { @@ -830,9 +830,8 @@ class DataSet(http.Controller): @http.route('/web/dataset/load', type='json', auth="user") def load(self, model, id, fields): - m = request.session.model(model) value = {} - r = m.read([id], False, request.context) + r = request.env[model].browse([id]).read() if r: value = r[0] return {'value': value} @@ -861,7 +860,8 @@ class DataSet(http.Controller): @http.route('/web/dataset/exec_workflow', type='json', auth="user") def exec_workflow(self, model, id, signal): - return request.session.exec_workflow(model, id, signal) + request.session.check_security() + return request.env[model].browse(id).signal_workflow(signal)[id] @http.route('/web/dataset/resequence', type='json', auth="user") def resequence(self, model, ids, field='sequence', offset=0): @@ -878,24 +878,24 @@ class DataSet(http.Controller): starting the resequencing from an arbitrary number, defaults to ``0`` """ - m = request.session.model(model) + m = request.env[model] if not m.fields_get([field]): return False # python 2.6 has no start parameter - for i, id in enumerate(ids): - m.write(id, { field: i + offset }) + for i, record in enumerate(m.browse(ids)): + record.write({field: i + offset}) return True class View(http.Controller): @http.route('/web/view/add_custom', type='json', auth="user") def add_custom(self, view_id, arch): - CustomView = request.session.model('ir.ui.view.custom') + CustomView = request.env['ir.ui.view.custom'] CustomView.create({ 'user_id': request.session.uid, 'ref_id': view_id, 'arch': arch - }, request.context) + }) return {'result': True} class TreeView(View): @@ -1016,23 +1016,23 @@ class Binary(http.Controller): @http.route('/web/binary/upload_attachment', type='http', auth="user") @serialize_exception def upload_attachment(self, callback, model, id, ufile): - Model = request.session.model('ir.attachment') + Model = request.env['ir.attachment'] out = """<script language="javascript" type="text/javascript"> var win = window.top.window; win.jQuery(win).trigger(%s, %s); </script>""" try: - attachment_id = Model.create({ + attachment = Model.create({ 'name': ufile.filename, 'datas': base64.encodestring(ufile.read()), 'datas_fname': ufile.filename, 'res_model': model, 'res_id': int(id) - }, request.context) + }) args = { 'filename': ufile.filename, 'mimetype': ufile.content_type, - 'id': attachment_id + 'id': attachment.id } except Exception: args = {'error': _("Something horrible happened")} @@ -1088,19 +1088,19 @@ class Action(http.Controller): @http.route('/web/action/load', type='json', auth="user") def load(self, action_id, additional_context=None): - Actions = request.session.model('ir.actions.actions') + Actions = request.env['ir.actions.actions'] value = False try: action_id = int(action_id) except ValueError: try: - module, xmlid = action_id.split('.', 1) - model, action_id = request.session.model('ir.model.data').get_object_reference(module, xmlid) - assert model.startswith('ir.actions.') + action = request.env.ref(action_id) + assert action._name.startswith('ir.actions.') + action_id = action.id except Exception: action_id = 0 # force failed read - base_action = Actions.read([action_id], ['type'], context=request.context) + base_action = Actions.browse([action_id]).read(['type']) if base_action: ctx = request.context action_type = base_action[0]['type'] @@ -1108,19 +1108,16 @@ class Action(http.Controller): ctx.update({'bin_size': True}) if additional_context: ctx.update(additional_context) - action = request.session.model(action_type).read([action_id], False, context=ctx) + env = request.env(context=ctx) + action = env[action_type].browse([action_id]).read() if action: value = clean_action(action[0]) return value @http.route('/web/action/run', type='json', auth="user") def run(self, action_id): - return_action = request.session.model('ir.actions.server').run( - [action_id], request.context) - if return_action: - return clean_action(return_action) - else: - return False + result = request.env['ir.actions.server'].browse([action_id]).run() + return clean_action(result) if result else False class Export(http.Controller): @@ -1195,9 +1192,8 @@ class Export(http.Controller): @http.route('/web/export/namelist', type='json', auth="user") def namelist(self, model, export_id): # TODO: namelist really has no reason to be in Python (although itertools.groupby helps) - export = request.session.model("ir.exports").read([export_id])[0] - export_fields_list = request.session.model("ir.exports.line").read( - export['export_fields']) + export = request.env['ir.exports'].browse([export_id]).read()[0] + export_fields_list = request.env['ir.exports.line'].browse(export['export_fields']).read() fields_data = self.fields_info( model, map(operator.itemgetter('name'), export_fields_list)) @@ -1290,19 +1286,16 @@ class ExportFormat(object): def base(self, data, token): params = json.loads(data) model, fields, ids, domain, import_compat = \ - operator.itemgetter('model', 'fields', 'ids', 'domain', - 'import_compat')( - params) + operator.itemgetter('model', 'fields', 'ids', 'domain', 'import_compat')(params) - Model = request.session.model(model) - context = dict(request.context or {}, **params.get('context', {})) - ids = ids or Model.search(domain, offset=0, limit=False, order=False, context=context) + Model = request.env[model].with_context(**params.get('context', {})) + records = Model.browse(ids) or Model.search(domain, offset=0, limit=False, order=False) - if not request.env[model]._is_an_ordinary_table(): + if not Model._is_an_ordinary_table(): fields = [field for field in fields if field['name'] != 'id'] field_names = map(operator.itemgetter('name'), fields) - import_data = Model.export_data(ids, field_names, self.raw_data, context=context).get('datas',[]) + import_data = records.export_data(field_names, self.raw_data).get('datas',[]) if import_compat: columns_headers = field_names @@ -1420,7 +1413,6 @@ class Reports(http.Controller): def index(self, action, token): action = json.loads(action) - report_srv = request.session.proxy("report") context = dict(request.context) context.update(action["context"]) @@ -1433,15 +1425,14 @@ class Reports(http.Controller): report_ids = action['datas'].pop('ids') report_data.update(action['datas']) - report_id = report_srv.report( + report_id = dispatch_rpc('report', 'report', [ request.session.db, request.session.uid, request.session.password, - action["report_name"], report_ids, - report_data, context) + action["report_name"], report_ids, report_data, context]) report_struct = None while True: - report_struct = report_srv.report_get( - request.session.db, request.session.uid, request.session.password, report_id) + report_struct = dispatch_rpc('report', 'report_get', [ + request.session.db, request.session.uid, request.session.password, report_id]) if report_struct["state"]: break @@ -1454,11 +1445,10 @@ class Reports(http.Controller): report_struct['format'], 'octet-stream') file_name = action.get('name', 'report') if 'name' not in action: - reports = request.session.model('ir.actions.report.xml') - res_id = reports.search([('report_name', '=', action['report_name']),], - context=context) - if len(res_id) > 0: - file_name = reports.read(res_id[0], ['name'], context)['name'] + reports = request.env['ir.actions.report.xml'] + reports = reports.search([('report_name', '=', action['report_name'])]) + if reports: + file_name = reports[0].name else: file_name = action['report_name'] file_name = '%s.%s' % (file_name, report_struct['format']) @@ -1473,16 +1463,14 @@ class Reports(http.Controller): class Apps(http.Controller): @http.route('/apps/<app>', auth='user') def get_app_url(self, req, app): - act_window_obj = request.session.model('ir.actions.act_window') - ir_model_data = request.session.model('ir.model.data') try: - action_id = ir_model_data.get_object_reference('base', 'open_module_tree')[1] - action = act_window_obj.read(action_id, ['name', 'type', 'res_model', 'view_mode', 'view_type', 'context', 'views', 'domain']) + record = request.env.ref('base.open_module_tree') + action = record.read(['name', 'type', 'res_model', 'view_mode', 'view_type', 'context', 'views', 'domain'])[0] action['target'] = 'current' except ValueError: action = False try: - app_id = ir_model_data.get_object_reference('base', 'module_%s' % app)[1] + app_id = request.env.ref('base.module_%s' % app).id except ValueError: app_id = False |
