[IMP] Added a mimetype field in ir.attachment
bzr revid: fme@openerp.com-20140325160413-jvxjii6rbqcrjxor
This commit is contained in:
@@ -2,7 +2,6 @@
|
||||
import datetime
|
||||
import hashlib
|
||||
import logging
|
||||
import mimetypes
|
||||
import re
|
||||
import traceback
|
||||
import werkzeug
|
||||
@@ -98,11 +97,10 @@ class ir_http(orm.AbstractModel):
|
||||
|
||||
def _serve_attachment(self):
|
||||
domain = [('type', '=', 'binary'), ('url', '=', request.httprequest.path)]
|
||||
attach = self.pool['ir.attachment'].search_read(request.cr, request.uid, domain, ['__last_update', 'datas', 'datas_fname'], context=request.context)
|
||||
attach = self.pool['ir.attachment'].search_read(request.cr, request.uid, domain, ['__last_update', 'datas', 'mimetype'], context=request.context)
|
||||
if attach:
|
||||
wdate = attach[0]['__last_update']
|
||||
datas = attach[0]['datas']
|
||||
fname = attach[0]['datas_fname']
|
||||
response = werkzeug.wrappers.Response()
|
||||
server_format = openerp.tools.misc.DEFAULT_SERVER_DATETIME_FORMAT
|
||||
try:
|
||||
@@ -117,8 +115,7 @@ class ir_http(orm.AbstractModel):
|
||||
if response.status_code == 304:
|
||||
return response
|
||||
|
||||
guessed_mimetype = mimetypes.guess_type(fname)
|
||||
response.mimetype = guessed_mimetype[0] or 'application/octet-stream'
|
||||
response.mimetype = attach[0]['mimetype']
|
||||
response.set_data(datas.decode('base64'))
|
||||
return response
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ import inspect
|
||||
import itertools
|
||||
import logging
|
||||
import math
|
||||
import mimetypes
|
||||
import re
|
||||
import urlparse
|
||||
|
||||
@@ -553,7 +554,7 @@ class ir_attachment(osv.osv):
|
||||
def _website_url_get(self, cr, uid, ids, name, arg, context=None):
|
||||
result = {}
|
||||
for attach in self.browse(cr, uid, ids, context=context):
|
||||
if attach.type == 'url':
|
||||
if attach.url:
|
||||
result[attach.id] = attach.url
|
||||
else:
|
||||
result[attach.id] = urlplus('/website/image', {
|
||||
@@ -600,17 +601,27 @@ class ir_attachment(osv.osv):
|
||||
'website_url': fields.function(_website_url_get, string="Attachment URL", type='char'),
|
||||
'datas_big': fields.function (_datas_big, type='binary', store=True,
|
||||
string="Resized file content"),
|
||||
'mimetype': fields.char('Mime Type', readonly=True),
|
||||
}
|
||||
|
||||
def _add_mimetype_if_needed(self, values):
|
||||
if values.get('datas_fname'):
|
||||
values['mimetype'] = mimetypes.guess_type(values.get('datas_fname'))[0] or 'application/octet-stream'
|
||||
|
||||
def create(self, cr, uid, values, context=None):
|
||||
chk = self._compute_checksum(values)
|
||||
if chk:
|
||||
match = self.search(cr, uid, [('datas_checksum', '=', chk)], context=context)
|
||||
if match:
|
||||
return match[0]
|
||||
self._add_mimetype_if_needed(values)
|
||||
return super(ir_attachment, self).create(
|
||||
cr, uid, values, context=context)
|
||||
|
||||
def write(self, cr, uid, ids, values, context=None):
|
||||
self._add_mimetype_if_needed(values)
|
||||
return super(ir_attachment, self).write(cr, uid, ids, values, context=context)
|
||||
|
||||
def try_remove(self, cr, uid, ids, context=None):
|
||||
""" Removes a web-based image attachment if it is used by no view
|
||||
(template)
|
||||
|
||||
@@ -1500,7 +1500,8 @@
|
||||
args: [],
|
||||
kwargs: {
|
||||
fields: ['name', 'website_url'],
|
||||
domain: [['res_model', '=', 'ir.ui.view']],
|
||||
domain: [['res_model', '=', 'ir.ui.view'], '|',
|
||||
['mimetype', '=', false], ['mimetype', '=like', 'image/%']],
|
||||
order: 'id desc',
|
||||
context: website.get_context(),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user