Skip to content
Permalink
Browse files

[IMP] tools,website: correctly handle favicon and .ico files

Previously the image that we uploaded as a favicon of the website was not
resized, so it could be extremely excessive in size and resolution, and it also
was not guaranteed to be a square.

Now we always resize it appropriately and save it as an ICO file.

The default favicon is now applied to all websites instead of only the first.

task-1958000
PR: #31811
  • Loading branch information...
seb-odoo committed Mar 25, 2019
1 parent 479e22f commit 1e06c7ec77352018b5f588433005b0a89a827ce9
Showing with 24 additions and 4 deletions.
  1. +0 −1 addons/website/data/website_data.xml
  2. +13 −1 addons/website/models/website.py
  3. +10 −2 odoo/tools/image.py
  4. +1 −0 odoo/tools/mimetypes.py
@@ -156,7 +156,6 @@
<field name="domain"></field>
<field name="company_id" ref="base.main_company"/>
<field name="user_id" ref="base.public_user"/>
<field name="favicon" type="base64" file="web/static/src/img/favicon.ico"/>
<field name="homepage_id" ref="homepage_page"/>
</record>

@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

import base64
import inspect
import logging
import hashlib
@@ -14,6 +15,7 @@
from odoo.addons.website.models.ir_http import sitemap_qs2dom
from odoo.addons.portal.controllers.portal import pager
from odoo.http import request
from odoo.modules.module import get_resource_path
from odoo.osv.expression import FALSE_DOMAIN
from odoo.tools.translate import _

@@ -100,7 +102,13 @@ def _default_social_twitter(self):
partner_id = fields.Many2one(related='user_id.partner_id', relation='res.partner', string='Public Partner', readonly=False)
menu_id = fields.Many2one('website.menu', compute='_compute_menu', string='Main Menu')
homepage_id = fields.Many2one('website.page', string='Homepage')
favicon = fields.Binary(string="Website Favicon", help="This field holds the image used to display a favicon on the website.")

def _default_favicon(self):
img_path = get_resource_path('web', 'static/src/img/favicon.ico')
with tools.file_open(img_path, 'rb') as f:
return base64.b64encode(f.read())

favicon = fields.Binary(string="Website Favicon", help="This field holds the image used to display a favicon on the website.", default=_default_favicon)
theme_id = fields.Many2one('ir.module.module', help='Installed theme')

specific_user_account = fields.Boolean('Specific User Account', help='If True, new accounts will be associated to the current website')
@@ -122,6 +130,8 @@ def _compute_menu(self):

@api.model
def create(self, vals):
tools.update_field_value_to_ico(vals)

if 'user_id' not in vals:
company = self.env['res.company'].browse(vals.get('company_id'))
vals['user_id'] = company._get_public_user().id if company else self.env.ref('base.public_user').id
@@ -138,6 +148,8 @@ def create(self, vals):

@api.multi
def write(self, values):
tools.update_field_value_to_ico(values)

self._get_languages.clear_cache(self)
if 'company_id' in values and 'user_id' not in values:
company = self.env['res.company'].browse(values['company_id'])
@@ -5,6 +5,9 @@
import io

from PIL import Image
# Ico is considered safe
from PIL import IcoImagePlugin

from random import randrange

from odoo.tools.translate import _
@@ -72,7 +75,7 @@ def image_process(base64_source, size=(0, 0), verify_resolution=False, quality=8
:param colorize: replace the trasparent background by a random color
:type colorize: bool
:param format: the output format. Can be PNG, JPEG or GIF.
:param format: the output format. Can be PNG, JPEG, GIF, or ICO.
Default to the format of the original image.
BMP is converted to PNG, other formats are converted to JPEG.
:type format: string
@@ -101,7 +104,7 @@ def image_process(base64_source, size=(0, 0), verify_resolution=False, quality=8
format = (format or image.format).upper()
if format == 'BMP':
format = 'PNG'
elif format not in ['PNG', 'JPEG', 'GIF']:
elif format not in ['PNG', 'JPEG', 'GIF', 'ICO']:
format = 'JPEG'

opt = {'format': format}
@@ -282,6 +285,11 @@ def image_resize_images(vals,
vals[small_name] = False


def update_field_value_to_ico(vals, field='favicon'):
if field in vals:
vals[field] = image_process(vals[field], size=(256, 256), crop='center', format='ICO')


def image_data_uri(base64_source):
"""This returns data URL scheme according RFC 2397
(https://tools.ietf.org/html/rfc2397) for all kind of supported images
@@ -125,6 +125,7 @@ def _check_svg(data):
_Entry('image/svg+xml', [b'<'], [
_check_svg,
]),
_Entry('image/x-icon', [b'\x00\x00\x01\x00'], []),
# OLECF files in general (Word, Excel, PPT, default to word because why not?)
_Entry('application/msword', [b'\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1', b'\x0D\x44\x4F\x43'], [
_check_olecf

0 comments on commit 1e06c7e

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