[IMP] web_company_color: black, isort, prettier

pull/2527/head
Alexandre Díaz 2020-03-19 12:04:11 +01:00 committed by Lois Rilo
parent 5f1fdb9b64
commit e3e6bcd570
7 changed files with 153 additions and 135 deletions

View File

@ -3,20 +3,16 @@
#
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).#
{
'name': "Web Company Color",
'category': "web",
'version': "12.0.1.1.0",
"author": "Alexandre Díaz, "
"Odoo Community Association (OCA)",
'website': 'https://github.com/OCA/web',
'depends': ['web', 'base_sparse_field', 'web_widget_color'],
'data': [
'view/assets.xml',
'view/res_company.xml'
],
'uninstall_hook': 'uninstall_hook',
'post_init_hook': 'post_init_hook',
'license': 'AGPL-3',
'auto_install': False,
'installable': True,
"name": "Web Company Color",
"category": "web",
"version": "13.0.1.0.0",
"author": "Alexandre Díaz, " "Odoo Community Association (OCA)",
"website": "https://github.com/OCA/web",
"depends": ["web", "base_sparse_field", "web_widget_color"],
"data": ["view/assets.xml", "view/res_company.xml"],
"uninstall_hook": "uninstall_hook",
"post_init_hook": "post_init_hook",
"license": "AGPL-3",
"auto_install": False,
"installable": True,
}

View File

@ -1,14 +1,15 @@
# Copyright 2019 Alexandre Díaz <dev@redneboa.es>
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from odoo import api, SUPERUSER_ID
from odoo import SUPERUSER_ID, api
from .models.res_company import URL_BASE
def uninstall_hook(cr, registry):
env = api.Environment(cr, SUPERUSER_ID, {})
env["ir.attachment"].search([('url', 'like', '%s%%' % URL_BASE)]).unlink()
env["ir.attachment"].search([("url", "like", "%s%%" % URL_BASE)]).unlink()
def post_init_hook(cr, registry):
env = api.Environment(cr, SUPERUSER_ID, {})
env['res.company'].search([]).scss_create_or_update_attachment()
env["res.company"].search([]).scss_create_or_update_attachment()

View File

@ -2,17 +2,18 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import base64
import time
from colorsys import rgb_to_hls, hls_to_rgb
from odoo import models, fields, api
from ..utils import image_to_rgb, convert_to_image, n_rgb_to_hex
from colorsys import hls_to_rgb, rgb_to_hls
from odoo import api, fields, models
URL_BASE = '/web_company_color/static/src/scss/'
URL_SCSS_GEN_TEMPLATE = URL_BASE + 'custom_colors.%d.%s.gen.scss'
from ..utils import convert_to_image, image_to_rgb, n_rgb_to_hex
URL_BASE = "/web_company_color/static/src/scss/"
URL_SCSS_GEN_TEMPLATE = URL_BASE + "custom_colors.%d.%s.gen.scss"
class ResCompany(models.Model):
_inherit = 'res.company'
_inherit = "res.company"
SCSS_TEMPLATE = """
.o_main_navbar {
@ -47,13 +48,12 @@ class ResCompany(models.Model):
"""
company_colors = fields.Serialized()
color_navbar_bg = fields.Char('Navbar Background Color',
sparse='company_colors')
color_navbar_bg = fields.Char("Navbar Background Color", sparse="company_colors")
color_navbar_bg_hover = fields.Char(
'Navbar Background Color Hover', sparse='company_colors')
color_navbar_text = fields.Char('Navbar Text Color',
sparse='company_colors')
scss_modif_timestamp = fields.Char('SCSS Modif. Timestamp')
"Navbar Background Color Hover", sparse="company_colors"
)
color_navbar_text = fields.Char("Navbar Text Color", sparse="company_colors")
scss_modif_timestamp = fields.Char("SCSS Modif. Timestamp")
@api.model_create_multi
def create(self, vals_list):
@ -64,22 +64,24 @@ class ResCompany(models.Model):
@api.multi
def unlink(self):
result = super().unlink()
IrAttachmentObj = self.env['ir.attachment']
IrAttachmentObj = self.env["ir.attachment"]
for record in self:
IrAttachmentObj.sudo().search([
('url', 'like', '%s%%' % record._scss_get_url_simplified()),
]).sudo().unlink()
IrAttachmentObj.sudo().search(
[("url", "like", "%s%%" % record._scss_get_url_simplified())]
).sudo().unlink()
return result
@api.multi
def write(self, values):
if not self.env.context.get('ignore_company_color', False):
fields_to_check = ('color_navbar_bg',
'color_navbar_bg_hover',
'color_navbar_text')
if 'logo' in values:
if values['logo']:
_r, _g, _b = image_to_rgb(convert_to_image(values['logo']))
if not self.env.context.get("ignore_company_color", False):
fields_to_check = (
"color_navbar_bg",
"color_navbar_bg_hover",
"color_navbar_text",
)
if "logo" in values:
if values["logo"]:
_r, _g, _b = image_to_rgb(convert_to_image(values["logo"]))
# Make color 10% darker
_h, _l, _s = rgb_to_hls(_r, _g, _b)
_l = max(0, _l - 0.1)
@ -87,11 +89,13 @@ class ResCompany(models.Model):
# Calc. optimal text color (b/w)
# Grayscale human vision perception (Rec. 709 values)
_a = 1 - (0.2126 * _r + 0.7152 * _g + 0.0722 * _b)
values.update({
'color_navbar_bg': n_rgb_to_hex(_r, _g, _b),
'color_navbar_bg_hover': n_rgb_to_hex(_rd, _gd, _bd),
'color_navbar_text': '#000' if _a < 0.5 else '#fff',
})
values.update(
{
"color_navbar_bg": n_rgb_to_hex(_r, _g, _b),
"color_navbar_bg_hover": n_rgb_to_hex(_rd, _gd, _bd),
"color_navbar_text": "#000" if _a < 0.5 else "#fff",
}
)
else:
values.update(self.default_get(fields_to_check))
@ -110,14 +114,16 @@ class ResCompany(models.Model):
# This allow extend company_colors and only sanitize selected fields
# or add custom values
values = dict(self.company_colors or {})
values.update({
'color_navbar_bg': (values.get('color_navbar_bg')
or '$o-brand-odoo'),
'color_navbar_bg_hover': (
values.get('color_navbar_bg_hover')
or '$o-navbar-inverse-link-hover-bg'),
'color_navbar_text': (values.get('color_navbar_text') or '#FFF'),
})
values.update(
{
"color_navbar_bg": (values.get("color_navbar_bg") or "$o-brand-odoo"),
"color_navbar_bg_hover": (
values.get("color_navbar_bg_hover")
or "$o-navbar-inverse-link-hover-bg"
),
"color_navbar_text": (values.get("color_navbar_text") or "#FFF"),
}
)
return values
@api.multi
@ -132,42 +138,37 @@ class ResCompany(models.Model):
# /web_company_color/static/src/scss/custom_colors.<company_id>
def _scss_get_url_simplified(self):
self.ensure_one()
NTEMPLATE = '.'.join(URL_SCSS_GEN_TEMPLATE.split('.')[:2])
NTEMPLATE = ".".join(URL_SCSS_GEN_TEMPLATE.split(".")[:2])
return NTEMPLATE % self.id
@api.multi
def scss_get_url(self, timestamp=None):
self.ensure_one()
return URL_SCSS_GEN_TEMPLATE % (self.id,
timestamp or self.scss_modif_timestamp)
return URL_SCSS_GEN_TEMPLATE % (self.id, timestamp or self.scss_modif_timestamp)
@api.multi
def scss_create_or_update_attachment(self):
IrAttachmentObj = self.env['ir.attachment']
IrAttachmentObj = self.env["ir.attachment"]
# The time window is 1 second
# This mean that all modifications realized in that second will
# have the same timestamp
modif_timestamp = str(int(time.time()))
for record in self:
datas = base64.b64encode(
record._scss_generate_content().encode('utf-8'))
custom_attachment = IrAttachmentObj.sudo().search([
('url', 'like', '%s%%' % record._scss_get_url_simplified())
])
datas = base64.b64encode(record._scss_generate_content().encode("utf-8"))
custom_attachment = IrAttachmentObj.sudo().search(
[("url", "like", "%s%%" % record._scss_get_url_simplified())]
)
custom_url = record.scss_get_url(timestamp=modif_timestamp)
values = {
'datas': datas,
'url': custom_url,
'name': custom_url,
'datas_fname': custom_url.split("/")[-1],
"datas": datas,
"url": custom_url,
"name": custom_url,
"datas_fname": custom_url.split("/")[-1],
}
if custom_attachment:
custom_attachment.sudo().write(values)
else:
values.update({
'type': 'binary',
'mimetype': 'text/scss',
})
values.update({"type": "binary", "mimetype": "text/scss"})
IrAttachmentObj.sudo().create(values)
self.write({'scss_modif_timestamp': modif_timestamp})
self.env['ir.qweb'].sudo().clear_caches()
self.write({"scss_modif_timestamp": modif_timestamp})
self.env["ir.qweb"].sudo().clear_caches()

View File

@ -1,59 +1,73 @@
# Copyright 2019 Alexandre Díaz <dev@redneboa.es>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo.tests import common
from ..models.res_company import URL_BASE
class TestResCompany(common.TransactionCase):
IMG_GREEN = 'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUl' \
+ 'EQVR42mNk+M/wHwAEBgIApD5fRAAAAABJRU5ErkJggg=='
IMG_GREEN = (
"iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUl"
+ "EQVR42mNk+M/wHwAEBgIApD5fRAAAAABJRU5ErkJggg=="
)
def test_scss_attachment(self):
num_scss = self.env['ir.attachment'].search_count([
('url', 'ilike', '%s%%' % URL_BASE)
])
num_companies = self.env['res.company'].search_count([])
num_scss = self.env["ir.attachment"].search_count(
[("url", "ilike", "%s%%" % URL_BASE)]
)
num_companies = self.env["res.company"].search_count([])
self.assertEqual(num_scss, num_companies, "Invalid scss attachments")
def test_create_unlink_company(self):
company_id = self.env['res.company'].create({
'name': 'Company Test'
})
self.assertEqual(company_id.color_navbar_bg, False,
"Invalid Navbar Background Color")
company_id = self.env["res.company"].create({"name": "Company Test"})
self.assertEqual(
company_id.color_navbar_bg, False, "Invalid Navbar Background Color"
)
self.test_scss_attachment()
company_id.sudo().write({'logo': self.IMG_GREEN})
self.assertEqual(company_id.color_navbar_bg, '#00ff00',
"Invalid Navbar Background Color")
company_id.sudo().write({"logo": self.IMG_GREEN})
self.assertEqual(
company_id.color_navbar_bg, "#00ff00", "Invalid Navbar Background Color"
)
company_id.sudo().unlink()
self.test_scss_attachment()
def test_change_logo(self):
company_id = self.env['res.company'].search([], limit=1)
company_id.sudo().write({'logo': self.IMG_GREEN})
self.assertEqual(company_id.color_navbar_bg, '#00ff00',
"Invalid Navbar Background Color")
company_id = self.env["res.company"].search([], limit=1)
company_id.sudo().write({"logo": self.IMG_GREEN})
self.assertEqual(
company_id.color_navbar_bg, "#00ff00", "Invalid Navbar Background Color"
)
def test_scss_sanitized_values(self):
company_id = self.env['res.company'].search([], limit=1)
company_id.sudo().write({'color_navbar_bg': False})
company_id = self.env["res.company"].search([], limit=1)
company_id.sudo().write({"color_navbar_bg": False})
values = company_id.sudo()._scss_get_sanitized_values()
self.assertEqual(values['color_navbar_bg'], '$o-brand-odoo',
"Invalid Navbar Background Color")
company_id.sudo().write({'color_navbar_bg': '#DEAD00'})
self.assertEqual(
values["color_navbar_bg"],
"$o-brand-odoo",
"Invalid Navbar Background Color",
)
company_id.sudo().write({"color_navbar_bg": "#DEAD00"})
values = company_id.sudo()._scss_get_sanitized_values()
self.assertEqual(values['color_navbar_bg'], '#DEAD00',
"Invalid Navbar Background Color")
self.assertEqual(
values["color_navbar_bg"], "#DEAD00", "Invalid Navbar Background Color"
)
def test_change_color(self):
company_id = self.env['res.company'].search([], limit=1)
company_id.sudo().write({'color_navbar_bg': '#DEAD00'})
self.assertEqual(company_id.color_navbar_bg, '#DEAD00',
"Invalid Navbar Background Color")
self.assertEqual(company_id.company_colors['color_navbar_bg'],
'#DEAD00', "Invalid Navbar Background Color")
company_id.sudo().write({'color_navbar_bg': False})
self.assertFalse(company_id.color_navbar_bg,
"Invalid Navbar Background Color")
self.assertNotIn('color_navbar_bg', company_id.company_colors,
"Invalid Navbar Background Color")
company_id = self.env["res.company"].search([], limit=1)
company_id.sudo().write({"color_navbar_bg": "#DEAD00"})
self.assertEqual(
company_id.color_navbar_bg, "#DEAD00", "Invalid Navbar Background Color"
)
self.assertEqual(
company_id.company_colors["color_navbar_bg"],
"#DEAD00",
"Invalid Navbar Background Color",
)
company_id.sudo().write({"color_navbar_bg": False})
self.assertFalse(company_id.color_navbar_bg, "Invalid Navbar Background Color")
self.assertNotIn(
"color_navbar_bg",
company_id.company_colors,
"Invalid Navbar Background Color",
)

View File

@ -2,12 +2,13 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import base64
import math
from PIL import Image
from io import BytesIO
from PIL import Image
def n_rgb_to_hex(_r, _g, _b):
return '#%02x%02x%02x' % (int(255*_r), int(255*_g), int(255*_b))
return "#{:02x}{:02x}{:02x}".format(int(255 * _r), int(255 * _g), int(255 * _b))
def convert_to_image(field_binary):
@ -16,22 +17,20 @@ def convert_to_image(field_binary):
def image_to_rgb(img):
def normalize_vec3(vec3):
_l = 1.0 / math.sqrt(vec3[0]*vec3[0]
+ vec3[1]*vec3[1]
+ vec3[2]*vec3[2])
return (vec3[0]*_l, vec3[1]*_l, vec3[2]*_l)
_l = 1.0 / math.sqrt(vec3[0] * vec3[0] + vec3[1] * vec3[1] + vec3[2] * vec3[2])
return (vec3[0] * _l, vec3[1] * _l, vec3[2] * _l)
# Force Alpha Channel
if img.mode != 'RGBA':
img = img.convert('RGBA')
if img.mode != "RGBA":
img = img.convert("RGBA")
width, height = img.size
# Reduce pixels
width, height = (max(1, int(width/4)), max(1, int(height/4)))
width, height = (max(1, int(width / 4)), max(1, int(height / 4)))
img = img.resize((width, height))
rgb_sum = [0, 0, 0]
# Mix. image colors using addition method
RGBA_WHITE = (255, 255, 255, 255)
for i in range(0, height*width):
for i in range(0, height * width):
rgba = img.getpixel((i % width, i / width))
if rgba[3] > 128 and rgba != RGBA_WHITE:
rgb_sum[0] += rgba[0]

View File

@ -1,17 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<!--
Copyright 2019 Alexandre Díaz
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
-->
<odoo>
<template id="assets_backend" name="web_company_color assets"
inherit_id="web.assets_backend">
<template
id="assets_backend"
name="web_company_color assets"
inherit_id="web.assets_backend"
>
<xpath expr=".">
<t t-set="company_id" t-value="request.env.user and request.env.user.company_id" />
<link t-if="company_id" rel="stylesheet" type="text/scss"
t-att-href="company_id.scss_get_url()" />
<t
t-set="company_id"
t-value="request.env.user and request.env.user.company_id"
/>
<link
t-if="company_id"
rel="stylesheet"
type="text/scss"
t-att-href="company_id.scss_get_url()"
/>
</xpath>
</template>
</odoo>

View File

@ -1,20 +1,20 @@
<?xml version="1.0"?>
<?xml version="1.0" ?>
<!--
Copyright 2019
@author Alexandre Díaz <dev@redneboa.es>
License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
-->
<odoo>
<record id="view_company_form" model="ir.ui.view">
<field name="model">res.company</field>
<field name="inherit_id" ref="base.view_company_form" />
<field name="arch" type="xml">
<notebook position="inside">
<page string="Company Styles" name="company_styles"
groups="base.group_system">
<page
string="Company Styles"
name="company_styles"
groups="base.group_system"
>
<group string="Navbar Colors" name="navbar_colors">
<field name="color_navbar_bg" widget="color" />
<field name="color_navbar_bg_hover" widget="color" />
@ -29,5 +29,4 @@
</notebook>
</field>
</record>
</odoo>