[MERGE] upstream
[odoo/odoo.git] / addons / website / models / website.py
index 15e1a64..a516f5c 100644 (file)
@@ -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)