/
res_config.py
103 lines (85 loc) · 3.84 KB
/
res_config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# Copyright 2016 Ivan Yelizariev <https://it-projects.info/team/yelizariev>
# Copyright 2016,2018 Dinar Gabbasov <https://it-projects.info/team/GabbasovDinar>
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html).
import hashlib
from openerp import models, fields, api
CUSTOM_CSS_ARCH = '''<?xml version="1.0"?>
<t t-name="web_theme_kit.custom_css">
%s
</t>
'''
class Config(models.TransientModel):
_name = 'theme_kit.config'
_inherit = 'res.config.settings'
_description = 'Theme Kit Config'
theme_id = fields.Many2one('theme_kit.theme', string="Color Scheme")
favicon_id = fields.Many2one('ir.attachment', string="Favicon")
page_title = fields.Char('Page Title', help='''Anything you want to see in page title, e.g.
* CompanyName
* CompanyName's Portal
* CompanyName's Operation System
* etc.
''')
system_name = fields.Char('System Name', help='''e.g.
* CompanyName's Portal
* CompanyName's Operation System
* etc.
''')
company_logo = fields.Binary('Company Logo', help="Due to browser cache, old logo may be still shown. To fix that, clear browser cache")
wallpapers_count = fields.Integer('Wallpapers', readonly=True)
new_documentation_website = fields.Char('Documentation Website', help='''Replaces links to documentation to custom website e.g.
* "Help" in Import tool
* "How-to" in paypal
* etc.
''')
@api.model
def get_values(self):
res = super(Config, self).get_values()
ICPSudo = self.env['ir.config_parameter'].sudo()
theme_id = ICPSudo.get_param("theme_kit.current_theme_id", default=False)
if theme_id:
theme_id = int(theme_id)
favicon_id = ICPSudo.get_param("theme_kit.current_favicon_id", default=False)
if favicon_id:
favicon_id = int(favicon_id)
page_title = ICPSudo.get_param("web_debranding.new_title", default=False)
system_name = ICPSudo.get_param("web_debranding.new_name", default=False)
new_documentation_website = ICPSudo.get_param("web_debranding.new_documentation_website", default=False)
company_logo = self.env.user.company_id.logo
wallpapers_count = self.env['ir.attachment'].search_count([('use_as_background', '=', True)])
res.update(
company_logo=company_logo,
theme_id=int(theme_id),
favicon_id=favicon_id,
page_title=page_title,
system_name=system_name,
new_documentation_website=new_documentation_website,
wallpapers_count=wallpapers_count
)
return res
@api.multi
def set_values(self):
super(Config, self).set_values()
ICPSudo = self.env['ir.config_parameter'].sudo()
ICPSudo.set_param('theme_kit.current_theme_id', getattr(self, 'theme_id').id or '')
ICPSudo.set_param('theme_kit.current_favicon_id', getattr(self, 'favicon_id').id or '')
ICPSudo.set_param('web_debranding.new_title', getattr(self, 'page_title') or '')
ICPSudo.set_param('web_debranding.new_name', getattr(self, 'system_name') or '')
ICPSudo.set_param('web_debranding.new_documentation_website', getattr(self, 'new_documentation_website') or '')
# set company logo
self.env.user.company_id.logo = self.company_logo
# set theme
custom_css = self.env.ref('web_theme_kit.custom_css')
code = ''
if self.theme_id:
code = self.theme_id.code
arch = CUSTOM_CSS_ARCH % code
custom_css.write({'arch': arch})
# set favicon
url = ''
if self.favicon_id:
url = self.favicon_id.url or self._attachment2url(self.favicon_id)
ICPSudo.set_param('web_debranding.favicon_url', url)
def _attachment2url(self, att):
sha = hashlib.sha1(getattr(att, '__last_update').encode('utf-8')).hexdigest()[0:7]
return '/web/image/%s-%s' % (att.id, sha)