summaryrefslogtreecommitdiff
path: root/addons/website/models
diff options
context:
space:
mode:
authorParth Gajjar (Open ERP) <pga@tinyerp.com>2014-03-28 18:48:22 +0530
committerParth Gajjar (Open ERP) <pga@tinyerp.com>2014-03-28 18:48:22 +0530
commit3de6738dbbfeb5f5dda0eae434e3ccef62aa4e8e (patch)
tree7dfdb25e57640ffdc5d12169b372bf51318dc04b /addons/website/models
parent1ccbc213e7f39c1902d70714d0e75418155dab6d (diff)
parenta224b88d80bc5311a684a6ae59e8584a2eec9150 (diff)
merged with trunk addons
bzr revid: pga@tinyerp.com-20140328131822-0kc0f5j5qhupkhbr
Diffstat (limited to 'addons/website/models')
-rw-r--r--addons/website/models/ir_http.py32
-rw-r--r--addons/website/models/website.py15
2 files changed, 44 insertions, 3 deletions
diff --git a/addons/website/models/ir_http.py b/addons/website/models/ir_http.py
index c0a9e209d00..272e3480fb5 100644
--- a/addons/website/models/ir_http.py
+++ b/addons/website/models/ir_http.py
@@ -1,8 +1,9 @@
# -*- coding: utf-8 -*-
+import datetime
+import hashlib
import logging
import re
import traceback
-
import werkzeug
import werkzeug.routing
@@ -99,9 +100,38 @@ class ir_http(orm.AbstractModel):
path = '/' + request.lang + path
return werkzeug.utils.redirect(path)
+ def _serve_attachment(self):
+ domain = [('type', '=', 'binary'), ('url', '=', request.httprequest.path)]
+ attach = self.pool['ir.attachment'].search_read(request.cr, openerp.SUPERUSER_ID, domain, ['__last_update', 'datas', 'mimetype'], context=request.context)
+ if attach:
+ wdate = attach[0]['__last_update']
+ datas = attach[0]['datas']
+ response = werkzeug.wrappers.Response()
+ server_format = openerp.tools.misc.DEFAULT_SERVER_DATETIME_FORMAT
+ try:
+ response.last_modified = datetime.datetime.strptime(wdate, server_format + '.%f')
+ except ValueError:
+ # just in case we have a timestamp without microseconds
+ response.last_modified = datetime.datetime.strptime(wdate, server_format)
+
+ response.set_etag(hashlib.sha1(datas).hexdigest())
+ response.make_conditional(request.httprequest)
+
+ if response.status_code == 304:
+ return response
+
+ response.mimetype = attach[0]['mimetype']
+ response.set_data(datas.decode('base64'))
+ return response
+
def _handle_exception(self, exception=None, code=500):
if isinstance(exception, werkzeug.exceptions.HTTPException) and hasattr(exception, 'response') and exception.response:
return exception.response
+
+ attach = self._serve_attachment()
+ if attach:
+ return attach
+
if getattr(request, 'website_enabled', False) and request.website:
values = dict(
exception=exception,
diff --git a/addons/website/models/website.py b/addons/website/models/website.py
index 15e1a643d3d..a516f5cb96f 100644
--- a/addons/website/models/website.py
+++ b/addons/website/models/website.py
@@ -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', {
@@ -574,7 +575,7 @@ class ir_attachment(osv.osv):
def _compute_checksum(self, attachment_dict):
if attachment_dict.get('res_model') == 'ir.ui.view'\
- and not attachment_dict.get('res_id')\
+ and not attachment_dict.get('res_id') and not attachment_dict.get('url')\
and attachment_dict.get('type', 'binary') == 'binary'\
and attachment_dict.get('datas'):
return hashlib.new('sha1', attachment_dict['datas']).hexdigest()
@@ -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)