108 lines
3.2 KiB
Python
108 lines
3.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
from odoo import models, fields, api, exceptions
|
|
import json
|
|
from odoo.tools import ormcache
|
|
from odoo.tools.misc import file_path
|
|
from odoo.tools.config import config
|
|
|
|
|
|
class AnitaModeTemplate(models.Model):
|
|
'''
|
|
user theme style setting
|
|
'''
|
|
_name = 'anita_theme_setting.mode_template'
|
|
_description = 'anita Mode Template'
|
|
|
|
name = fields.Char(string="Name", required=True)
|
|
template = fields.Text(string="Template")
|
|
color_vars = fields.Text(
|
|
string="Color Vars", help="This is the default var values for this template")
|
|
template_path = fields.Char(
|
|
string="Template Path", help="This is the template path of the template")
|
|
|
|
_sql_constraints = [('name_unique', 'UNIQUE(name)', "Name Must Be Unique!")]
|
|
|
|
@ormcache()
|
|
def get_templates(self):
|
|
"""
|
|
get templates
|
|
:return:
|
|
"""
|
|
records = self.search([])
|
|
return {record.name: record for record in records}
|
|
|
|
@ormcache('self.id')
|
|
def get_template(self):
|
|
"""
|
|
get template
|
|
:return:
|
|
"""
|
|
return json.loads(self.template)
|
|
|
|
@ormcache('self.id')
|
|
def get_color_vars(self):
|
|
"""
|
|
get vars
|
|
:return:
|
|
"""
|
|
return json.loads(self.color_vars)
|
|
|
|
def refresh_templates(self):
|
|
"""
|
|
update template content
|
|
"""
|
|
records = self.search([])
|
|
for record in records:
|
|
if record.template_path:
|
|
tmp_path = file_path(record.template_path.lstrip('/'))
|
|
with open(tmp_path, 'r') as fd:
|
|
template = fd.read()
|
|
record.template = template
|
|
|
|
def _register_hook(self):
|
|
"""
|
|
stuff to do right after the registry is built
|
|
"""
|
|
if config.get('debug', False):
|
|
records = self.search([])
|
|
records.refresh_templates()
|
|
|
|
@api.model
|
|
def install_template(self, name, template_path, theme_mode_paths):
|
|
"""
|
|
install mode
|
|
:return:
|
|
"""
|
|
tmp_path = file_path(template_path.lstrip('/'))
|
|
if not tmp_path:
|
|
raise exceptions.ValidationError("Template Path is not valid!")
|
|
|
|
with open(tmp_path, 'r') as fd:
|
|
template = fd.read()
|
|
if not name:
|
|
raise exceptions.ValidationError(
|
|
'Template {template} must has a name!'.format(template=template_path))
|
|
|
|
template_record = self.search([('name', '=', name)])
|
|
if not template_record:
|
|
self.create([{
|
|
"name": name,
|
|
"template": template,
|
|
"template_path": template_path
|
|
}])
|
|
else:
|
|
template_record.write({
|
|
"name": name,
|
|
"template": template,
|
|
"template_path": template_path
|
|
})
|
|
|
|
for index, mode_path in enumerate(theme_mode_paths):
|
|
record = self.env['anita_theme_setting.default_mode_data'].install_mode(
|
|
mode_path, template_record)
|
|
if index == 0:
|
|
template_record.write({
|
|
"color_vars": record.template_var_vals
|
|
})
|