Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions web_company_color/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ Web Company Color

This module change navbar colors based in the company logo colors.

The navbar background color is also used as the browser theme color, so
mobile browsers paint their address bar with the same color as the
navbar.

**Table of contents**

.. contents::
Expand Down
1 change: 1 addition & 0 deletions web_company_color/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
from . import assetsbundle
from . import res_company
from . import ir_qweb
from . import ir_http
12 changes: 1 addition & 11 deletions web_company_color/models/assetsbundle.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,10 @@
# Copyright 2020 Alexandre Díaz <dev@redneboa.es>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo.http import request

from odoo.addons.base.models.assetsbundle import AssetsBundle


class AssetsBundleCompanyColor(AssetsBundle):
def get_company_color_asset_node(self):
"""Process the user active company scss and returns the node to inject"""
try:
active_company_id = int(
request.httprequest.cookies.get("cids", "").split("-")[0]
)
except Exception:
active_company_id = False
company_id = (
self.env["res.company"].browse(active_company_id) or self.env.company
)
company_id = self.env["res.company"]._get_web_company_color_active_company()
return company_id.scss_get_url()
12 changes: 12 additions & 0 deletions web_company_color/models/ir_http.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import models


class IrHttp(models.AbstractModel):
_inherit = "ir.http"

def webclient_rendering_context(self):
res = super().webclient_rendering_context()
company = self.env["res.company"]._get_web_company_color_active_company()
res["web_company_color_theme_color"] = company._get_theme_color()
return res
38 changes: 36 additions & 2 deletions web_company_color/models/res_company.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Copyright 2019 Alexandre Díaz <dev@redneboa.es>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import base64
import re
from colorsys import hls_to_rgb, rgb_to_hls

from odoo import api, fields, models
from odoo import api, fields, models, tools
from odoo.http import request

from odoo.addons.base.models.assetsbundle import ScssStylesheetAsset
from odoo.addons.base.models.assetsbundle import CompileError, ScssStylesheetAsset

from ..utils import convert_to_image, image_to_rgb, n_rgb_to_hex

Expand Down Expand Up @@ -209,6 +211,38 @@ def button_compute_color(self):
)
self.update(values)

@api.model
def _get_web_company_color_active_company(self):
try:
active_company_id = int(
request.httprequest.cookies.get("cids", "").split("-")[0]
)
except Exception:
active_company_id = False
return (
self.env.user.company_ids.filtered(lambda c: c.id == active_company_id)
or self.env.company
)

def _get_theme_color(self):
self.ensure_one()
if not self.color_navbar_bg:
return False
return self._compile_theme_color(self.color_navbar_bg)

@api.model
@tools.ormcache("color")
def _compile_theme_color(self, color):
scss_asset = ScssStylesheetAsset(
"web_company_color.company_color_assets", url=URL_BASE
)
try:
css = scss_asset.compile(f".o_theme_color {{ color: {color}; }}")
except CompileError:
return False
match = re.search(r"color:\s*([^;]+);", css)
return match and match.group(1).strip()

def _scss_get_sanitized_values(self):
self.ensure_one()
# Clone company_color as dictionary to avoid ORM operations
Expand Down
3 changes: 3 additions & 0 deletions web_company_color/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
This module change navbar colors based in the company logo colors.

The navbar background color is also used as the browser theme color, so mobile
browsers paint their address bar with the same color as the navbar.
5 changes: 4 additions & 1 deletion web_company_color/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils: https://docutils.sourceforge.io/" />
<title>README.rst</title>
<title>Web Company Color</title>
<style type="text/css">

/*
Expand Down Expand Up @@ -376,6 +376,9 @@ <h1>Web Company Color</h1>
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/license-AGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/web/tree/18.0/web_company_color"><img alt="OCA/web" src="https://img.shields.io/badge/github-OCA%2Fweb-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/web-18-0/web-18-0-web_company_color"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/web&amp;target_branch=18.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>This module change navbar colors based in the company logo colors.</p>
<p>The navbar background color is also used as the browser theme color, so
mobile browsers paint their address bar with the same color as the
navbar.</p>
<p><strong>Table of contents</strong></p>
<div class="contents local topic" id="contents">
<ul class="simple">
Expand Down
1 change: 1 addition & 0 deletions web_company_color/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import test_res_company
from . import test_theme_color
58 changes: 58 additions & 0 deletions web_company_color/tests/test_theme_color.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo.tests import HttpCase, tagged


@tagged("post_install", "-at_install")
class TestThemeColor(HttpCase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls.company = cls.env.ref("base.main_company")
cls.company_2 = cls.env["res.company"].create({"name": "Company Test"})
cls.user = cls.env.ref("base.user_admin")
cls.user.company_ids |= cls.company_2

def _get_theme_color_meta(self, cids=None):
self.authenticate("admin", "admin")
if cids:
self.opener.cookies["cids"] = cids
response = self.url_open("/odoo")
self.assertEqual(response.status_code, 200)
return response.text

def test_theme_color_plain(self):
self.company.color_navbar_bg = "#DEAD00"
self.assertEqual(self.company._get_theme_color(), "#DEAD00")

def test_theme_color_scss_expression(self):
self.company.color_navbar_bg = "desaturate(#d2e1dd, 30%)"
self.assertEqual(self.company._get_theme_color(), "#dadada")

def test_theme_color_unset(self):
self.company.color_navbar_bg = False
self.assertFalse(self.company._get_theme_color())

def test_theme_color_invalid(self):
self.assertFalse(self.env["res.company"]._compile_theme_color("not-a-color("))

def test_webclient_theme_color_default(self):
self.company.color_navbar_bg = False
html = self._get_theme_color_meta(cids=str(self.company.id))
self.assertIn('<meta name="theme-color" content="#71639e"', html)

def test_webclient_theme_color_active_company(self):
self.company.color_navbar_bg = "#DEAD00"
self.company_2.color_navbar_bg = "#00FF00"
html = self._get_theme_color_meta(cids=str(self.company.id))
self.assertIn('<meta name="theme-color" content="#DEAD00"', html)
html = self._get_theme_color_meta(cids=f"{self.company_2.id}-{self.company.id}")
self.assertIn('<meta name="theme-color" content="#00FF00"', html)

def test_webclient_theme_color_not_allowed_company(self):
company_3 = self.env["res.company"].create(
{"name": "Company Test 3", "color_navbar_bg": "#0000FF"}
)
self.company.color_navbar_bg = "#DEAD00"
self.user.company_id = self.company
html = self._get_theme_color_meta(cids=str(company_3.id))
self.assertIn('<meta name="theme-color" content="#DEAD00"', html)
6 changes: 6 additions & 0 deletions web_company_color/view/assets.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,11 @@
<xpath expr="//t[@t-set='head_web']">
<t t-call-assets="web_company_color.company_color_assets" />
</xpath>
<xpath expr="//meta[@name='theme-color']" position="attributes">
<attribute name="content" />
<attribute
name="t-att-content"
>web_company_color_theme_color or '#71639e'</attribute>
</xpath>
</template>
</odoo>
Loading