From a32c1d3c8dfd3d4684f89a6181f3a093dde26ae6 Mon Sep 17 00:00:00 2001 From: fwi-odoo Date: Tue, 26 Jul 2016 12:06:35 +0200 Subject: [MIG] web_editor: migrate to new api --- addons/web_editor/controllers/main.py | 69 ++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 34 deletions(-) (limited to 'addons/web_editor/controllers/main.py') diff --git a/addons/web_editor/controllers/main.py b/addons/web_editor/controllers/main.py index fa891ee296c..324b3caf564 100644 --- a/addons/web_editor/controllers/main.py +++ b/addons/web_editor/controllers/main.py @@ -1,14 +1,17 @@ # -*- coding: utf-8 -*- -from openerp.http import request, STATIC_CACHE -from openerp import http -import json -import io -from PIL import Image, ImageFont, ImageDraw -from openerp import tools +# Part of Odoo. See LICENSE file for full copyright and licensing details. + import cStringIO -import werkzeug.wrappers -import time +import io +import json import logging +import time +import werkzeug.wrappers +from PIL import Image, ImageFont, ImageDraw + +from odoo.http import request +from odoo import http, tools + logger = logging.getLogger(__name__) @@ -25,8 +28,6 @@ class Web_Editor(http.Controller): #------------------------------------------------------ @http.route('/web_editor/field/html', type='http', auth="user") def FieldTextHtml(self, model=None, res_id=None, field=None, callback=None, **kwargs): - cr, uid, context = request.cr, request.uid, request.context - kwargs.update( model=model, res_id=res_id, @@ -39,17 +40,16 @@ class Web_Editor(http.Controller): kwargs[k] = int(kwargs[k]) trans = dict( - lang=kwargs.get('lang', context.get('lang')), + lang=kwargs.get('lang', request.env.context.get('lang')), translatable=kwargs.get('translatable'), edit_translations=kwargs.get('edit_translations'), editable=kwargs.get('enable_editor')) - context.update(trans) kwargs.update(trans) record = None if model and kwargs.get('res_id'): - record = request.registry[model].browse(cr, uid, kwargs.get('res_id'), context) + record = request.env[model].with_context(trans).browse(kwargs.get('res_id')) kwargs.update(content=record and getattr(record, field) or "") @@ -141,23 +141,23 @@ class Web_Editor(http.Controller): # the upload argument doesn't allow us to access the files if more than # one file is uploaded, as upload references the first file # therefore we have to recover the files from the request object - Attachments = request.registry['ir.attachment'] # registry for the attachment table + Attachments = request.env['ir.attachment'] # registry for the attachment table uploads = [] message = None if not upload: # no image provided, storing the link and the image name name = url.split("/").pop() # recover filename - attachment_id = Attachments.create(request.cr, request.uid, { + attachment = Attachments.create({ 'name': name, 'type': 'url', 'url': url, 'public': True, 'res_model': 'ir.ui.view', - }, request.context) - uploads += Attachments.read(request.cr, request.uid, [attachment_id], ['name', 'mimetype', 'checksum', 'url'], request.context) + }) + uploads += attachment.read(['name', 'mimetype', 'checksum', 'url']) else: # images provided try: - attachment_ids = [] + attachments = request.env['ir.attachment'] for c_file in request.httprequest.files.getlist('upload'): data = c_file.read() try: @@ -172,15 +172,15 @@ class Web_Editor(http.Controller): except IOError, e: pass - attachment_id = Attachments.create(request.cr, request.uid, { + attachment = Attachments.create({ 'name': c_file.filename, 'datas': data.encode('base64'), 'datas_fname': c_file.filename, 'public': True, 'res_model': 'ir.ui.view', - }, request.context) - attachment_ids.append(attachment_id) - uploads += Attachments.read(request.cr, request.uid, attachment_ids, ['name', 'mimetype', 'checksum', 'url'], request.context) + }) + attachments += attachment + uploads += attachments.read(['name', 'mimetype', 'checksum', 'url']) except Exception, e: logger.exception("Failed to upload image to attachment") message = unicode(e) @@ -199,25 +199,26 @@ class Web_Editor(http.Controller): Returns a dict mapping attachments which would not be removed (if any) mapped to the views preventing their removal """ - cr, uid, context = request.cr, request.uid, request.context - Attachment = request.registry['ir.attachment'] - Views = request.registry['ir.ui.view'] + Attachment = attachments_to_remove = request.env['ir.attachment'] + Views = request.env['ir.ui.view'] - attachments_to_remove = [] # views blocking removal of the attachment removal_blocked_by = {} - for attachment in Attachment.browse(cr, uid, ids, context=context): + for attachment in Attachment.browse(ids): # in-document URLs are html-escaped, a straight search will not # find them url = tools.html_escape(attachment.local_url) - ids = Views.search(cr, uid, ["|", ('arch_db', 'like', '"%s"' % url), ('arch_db', 'like', "'%s'" % url)], context=context) - - if ids: - removal_blocked_by[attachment.id] = Views.read( - cr, uid, ids, ['name'], context=context) + views = Views.search([ + "|", + ('arch_db', 'like', '"%s"' % url), + ('arch_db', 'like', "'%s'" % url) + ]) + + if views: + removal_blocked_by[attachment.id] = views.read(['name']) else: - attachments_to_remove.append(attachment.id) + attachments_to_remove += attachment if attachments_to_remove: - Attachment.unlink(cr, uid, attachments_to_remove, context=context) + attachments_to_remove.unlink() return removal_blocked_by -- cgit v1.2.3