Skip to content
Permalink
Browse files

[IMP] improve the code

  • Loading branch information...
mba-odoo committed Mar 18, 2019
1 parent 83f97c2 commit 90d659db2f5a16b9ce8bf4652e261a830f5b7f75
Showing with 47 additions and 44 deletions.
  1. +47 −44 addons/portal/controllers/mail.py
@@ -1,22 +1,23 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

import werkzeug
import base64
import json
import logging
import unicodedata
import werkzeug

from ast import literal_eval
from werkzeug import urls
from werkzeug.exceptions import NotFound, Forbidden
from ast import literal_eval
import unicodedata
import logging
import json

from odoo import http, tools
from odoo.http import request
from odoo.osv import expression
from odoo.tools import consteq, plaintext2html
from odoo.tools.translate import _
from odoo.addons.mail.controllers.main import MailController
from odoo.exceptions import AccessError
from odoo.exceptions import AccessError, MissingError
from odoo.modules import get_module_resource

_logger = logging.getLogger(__name__)
@@ -158,55 +159,57 @@ def portal_message_fetch(self, res_model, res_id, domain=False, limit=10, offset
}

@http.route('/portal/binary/upload_attachment', type='http', auth="public")
def upload_attachment(self, callback, ufile, **kw):
def upload_attachment(self, callback, model, id, ufile, **kw):
files = request.httprequest.files.getlist('ufile')
Model = request.env['ir.attachment']
out = """<script language="javascript" type="text/javascript">
var win = window.top.window;
win.jQuery(win).trigger(%s, %s);
</script>"""

res_model = kw.get('model')
res_id = int(kw.get('id', 0))
access_token = kw.get('access_token')
authorized_user = False
args = []

if not access_token:
authorized_user = request.env[res_model].browse(res_id).check_access_rights('write')

if access_token or authorized_user:
if access_token:
Model = Model.sudo()
if not _check_special_access(res_model, res_id, token=access_token):
try:
document = request.env[model].browse(int(id))
document_sudo = document.sudo().exists()
if not document_sudo:
raise MissingError("This document does not exist.")
try:
document.check_access_rights('write')
document.check_access_rule('write')
except AccessError:
if not access_token or not _check_special_access(model, int(id), token=access_token):
raise Forbidden()

for ufile in files:
filename = ufile.filename
if request.httprequest.user_agent.browser == 'safari':
# Safari sends NFD UTF-8 (where é is composed by 'e' and [accent])
# we need to send it the same stuff, otherwise it'll fail
filename = unicodedata.normalize('NFD', ufile.filename)
try:
attachment = Model.create({
'name': filename,
'datas': base64.encodestring(ufile.read()),
'datas_fname': filename,
'res_model': 'mail.compose.message',
'res_id': res_id,
})
except Exception:
args.append({'error': _("Something horrible happened")})
_logger.exception("Fail to upload attachment %s" % ufile.filename)
else:
args.append({
'filename': filename,
'mimetype': ufile.content_type,
'id': attachment.id,
'size': attachment.file_size
})
else:
except (AccessError, MissingError):
raise Forbidden()

Model = request.env['ir.attachment'].sudo()

for ufile in files:
filename = ufile.filename
if request.httprequest.user_agent.browser == 'safari':
# Safari sends NFD UTF-8 (where é is composed by 'e' and [accent])
# we need to send it the same stuff, otherwise it'll fail
filename = unicodedata.normalize('NFD', ufile.filename)
try:
attachment = Model.create({
'name': filename,
'datas': base64.encodestring(ufile.read()),
'datas_fname': filename,
'res_model': 'mail.compose.message',
'res_id': int(id),
})
except Exception:
args.append({'error': _("Something horrible happened")})
_logger.exception("Fail to upload attachment %s" % ufile.filename)
else:
args.append({
'filename': filename,
'mimetype': ufile.content_type,
'id': attachment.id,
'size': attachment.file_size
})

return out % (json.dumps(callback), json.dumps(args))

@http.route([

0 comments on commit 90d659d

Please sign in to comment.
You can’t perform that action at this time.