Refactor the `load_views` API so it no longer sends multiple times the same fields description. e.g. When `load_views` is called to get the kanban, tree and form views, the list of fields of the model was sent 4 times: - Once for each view, with only the fields used in the view, in `['fields_views']['kanban']['fields']` for instance - Once globally, with all the fields of the model, in `['fields']` The goal of this revision is to change that so it sends the list of all fields only once. In addition, if a view contains x2many fields, the fields description of the comodel is also sent. It was sent in the `views` key of the view fields dict. e.g. When calling `load_views` of `res.partner` to get the kanban, tree and form views, the `res.partner` fields description was actually sent 6 times: - Once for each view - Once globally - Once for each view of the many2many field `child_ids` of the form view, in - `['fields_views']['form']['fields']['child_ids']['views']['kanban']['fields']` - `['fields_views']['form']['fields']['child_ids']['views']['form']['fields']` The change suggested in this revision is to: - Remove the fields description for each view in `['fields_views']`. As it no longer contains the fields, the key becomes `['views']` instead of `['fields_views']`. - Replace the dict key `['fields']` by `['models']`, which is a dict with as key the model name and as values the model fields description. It contains the fields description for all models implied in the view: the model of the main view and the model of all one2many and many2many fields. With this change, the fields description will only be sent once by model implied in the view. In addition, the web client was getting the information about the fields sometimes in the global fields description list (e.g. `['fields']`), sometimes in the fields description list of the view type (e.g. `['fields_views']['form']['fields']`), making it a pain to try to make changes / performance gain in these field description dictionaries, because you never knew in which dict the web client was getting its info. Now, as there is only one place to get the fields description from, it's clearer and cleaner. - one2many and many2many fields views are passed directly in the main view architecture rather than being put in the `views` key of the field description. This is actually easier to treat by the web client, and this will allow in a future work to cache an entire view in one block of text rather than having to combine multiple cached blocks of text to return one view. - one2many and many2many fields which do not have directly embedded views have their views directly injected in the architecture, so the web client doesn't have to do RPC calls to `load_views` for each one2many and many2many fields not having embedded views. For instance, this allow to reduce the number of RPC calls to `load_views` from 8 to 1 when loading the form of `product.product`. Currently, this behavior is limited to 1 level deep but we consider making it go all the way down in future works. We did not do it for the moment because in certain cases it rises the processing time and the size (bytes) too much. e.g. the sale.order view can be 5 levels deep, meaning you can reach 4 dialogs on top the main view. ``` sale.order form > order_line > sale.order.line form > invoice_lines > account.move.line form > asset_ids > account.asset form > depreciation_move_ids > account.move form. ``` This will also benefit in future works to cache an entire view in one block of text rather to having to combine multiple cached block of text to get one view. - `fields_view_get` becomes `get_view`. As it no longer returns the fields description, keeping the `fields` in the name `fields_view_get` no longer makes sense. Hence removing `fields` from the method name, it becomes `view_get`. As it gets renamed anyway, we take the opportunity to rename it `get_view`, which is more in line with the general getter/setter guidelines in the model object world. - `_fields_view_get` becomes `_get_view`. For the same reasons than above. - `load_views` becomes `get_views`. This is not mandatory, there is no technical reason to rename `load_views` as it practically sends the same info as before, the view architectures and their fields description. Just in another way. We just take the opportunity of this pull request to suggest a cleaner API: `_get_view`, `get_view` and `get_views`. - Arguments `toolbar=False, submenu=False` fo the methods `_fields_view_get` and `fields_view_get` are converted to a kwargs `**options` in `_get_view` and `get_view`. The rationale is that submenu was already no longer used (deprecated) and the mobile options is introduced. The mobile options is necessary to tell the server to send the mobile views for x2many fields (kanban instead of tree). Instead of adding a new argument each time we add a new option to `fields_view_get`, it seems wiser to have a kwargs `**options` to avoid to re-write all overrides each time a new option is introduced. - `_fields_view_get` returned a dict containing the arch in text and some of the view information. Now, `get_view` returns a tuple with the view architecture as an `etree` node, and the view as a browse record. The rationale is that all overrides of `_fields_view_get` were about modifying the arch only (e.g. changing the address format/re-organizing the address related field nodes of the partner according to the company country). To do so, all these overrides were doing `etree.fromstring` to parse the arch which was sent in text to convert it to an `etree`, then operations were done on the `etree`, and then `etree.tostring` was called to convert back the arch to string. With this change of signature to send the arch as an `etree`, all these back and forth `etree.fromstring` -> `etree.tostring` are avoided, allowing some performance gain and less code in the end. - A cleanup of the keys returned in the dict of `fields_view_get` has been performed in `get_view`: - `fields` is removed, as explained above, - `view_id` is renamed `id`, - `name` is removed, it was unused by the web client, - `type` is removed, it was unused by the web client, - `field_parent` is removed, it was unused by the web client, - `base_model` is removed, it was unused by the web client. - `filters` is moved from the global dict returned by `load_views` (now `get_views`) to the dict returned by `fields_view_get` (now `get_view`) as it applies only to the `search` view type. - Retro-compatible methods for the 3 methods `fields_view_get`, `_fields_view_get` and `load_views` are provided, with deprecation warnings in them. - The web client could cache the model fields description (as it already caches the views), so it doesn't need to fetch them again if it asks for another view of a model for which he already has the fields description. If we do so, `get_views` could return only the list of models used by the views, without the fields description as of now, and the web client would then call `fields_get` independently only for the models for which it doesn't have yet the fields description. This would avoid the server to return the fields description and to call `fields_get`, which is costly, for each `get_views`, therefore gaining performances. - Inject the views of the one2many and many2many fields all the way down, unlimited depth level, as explained above. - Cache with `ormcache` the architecture of back-end views. This is already done for qweb views, it's not done for back-end views. Therefore the postprocessing of the views is performed for each `get_views`, which is costly, while the view architecture doesn't change for users belonging to the same groups, according to the groups implied by the view. This pull request is co-authored by Aaron Bohy (aab) for the web client part and Denis Ledoux (dle) for the server part. Part-of: odoo/odoo#87522
629 lines
22 KiB
Python
629 lines
22 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
"""
|
|
Web_editor-context rendering needs to add some metadata to rendered and allow to edit fields,
|
|
as well as render a few fields differently.
|
|
|
|
Also, adds methods to convert values back to Odoo models.
|
|
"""
|
|
|
|
import babel
|
|
import base64
|
|
import io
|
|
import itertools
|
|
import json
|
|
import logging
|
|
import os
|
|
import re
|
|
import hashlib
|
|
from datetime import datetime
|
|
|
|
import pytz
|
|
import requests
|
|
from datetime import datetime
|
|
from lxml import etree, html
|
|
from PIL import Image as I
|
|
from werkzeug import urls
|
|
|
|
import odoo.modules
|
|
|
|
from odoo import _, api, models, fields
|
|
from odoo.exceptions import ValidationError
|
|
from odoo.tools import ustr, posix_to_ldml, pycompat
|
|
from odoo.tools import html_escape as escape
|
|
from odoo.tools.misc import get_lang, babel_locale_parse
|
|
from odoo.addons.base.models import ir_qweb
|
|
|
|
REMOTE_CONNECTION_TIMEOUT = 2.5
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class IrQWeb(models.AbstractModel):
|
|
""" IrQWeb object for rendering editor stuff
|
|
"""
|
|
_inherit = 'ir.qweb'
|
|
|
|
def _compile_node(self, el, compile_context, indent):
|
|
snippet_key = compile_context.get('snippet-key')
|
|
if snippet_key == compile_context['template'] \
|
|
or compile_context.get('snippet-sub-call-key') == compile_context['template']:
|
|
# Get the path of element to only consider the first node of the
|
|
# snippet template content (ignoring all ancestors t elements which
|
|
# are not t-call ones)
|
|
nb_real_elements_in_hierarchy = 0
|
|
node = el
|
|
while node is not None and nb_real_elements_in_hierarchy < 2:
|
|
if node.tag != 't' or 't-call' in node.attrib:
|
|
nb_real_elements_in_hierarchy += 1
|
|
node = node.getparent()
|
|
if nb_real_elements_in_hierarchy == 1:
|
|
# The first node might be a call to a sub template
|
|
sub_call = el.get('t-call')
|
|
if sub_call:
|
|
el.set('t-options', f"{{'snippet-key': '{snippet_key}', 'snippet-sub-call-key': '{sub_call}'}}")
|
|
# If it already has a data-snippet it is a saved snippet.
|
|
# Do not override it.
|
|
elif 'data-snippet' not in el.attrib:
|
|
el.attrib['data-snippet'] = snippet_key.split('.', 1)[-1]
|
|
|
|
return super()._compile_node(el, compile_context, indent)
|
|
|
|
# compile directives
|
|
|
|
def _compile_directive_snippet(self, el, compile_context, indent):
|
|
key = el.attrib.pop('t-snippet')
|
|
el.set('t-call', key)
|
|
el.set('t-options', f"{{'snippet-key': {key!r}}}")
|
|
view = self.env['ir.ui.view']._get(key).sudo()
|
|
name = view.name
|
|
thumbnail = el.attrib.pop('t-thumbnail', "oe-thumbnail")
|
|
div = '<div name="%s" data-oe-type="snippet" data-oe-thumbnail="%s" data-oe-snippet-id="%s" data-oe-keywords="%s">' % (
|
|
escape(pycompat.to_text(name)),
|
|
escape(pycompat.to_text(thumbnail)),
|
|
escape(pycompat.to_text(view.id)),
|
|
escape(pycompat.to_text(el.findtext('keywords')))
|
|
)
|
|
self._append_text(div, compile_context)
|
|
code = self._compile_node(el, compile_context, indent)
|
|
self._append_text('</div>', compile_context)
|
|
return code
|
|
|
|
def _compile_directive_snippet_call(self, el, compile_context, indent):
|
|
key = el.attrib.pop('t-snippet-call')
|
|
el.set('t-call', key)
|
|
el.set('t-options', f"{{'snippet-key': {key!r}}}")
|
|
return self._compile_node(el, compile_context, indent)
|
|
|
|
def _compile_directive_install(self, el, compile_context, indent):
|
|
key = el.attrib.pop('t-install')
|
|
thumbnail = el.attrib.pop('t-thumbnail', 'oe-thumbnail')
|
|
if self.user_has_groups('base.group_system'):
|
|
module = self.env['ir.module.module'].search([('name', '=', key)])
|
|
if not module or module.state == 'installed':
|
|
return []
|
|
name = el.attrib.get('string') or 'Snippet'
|
|
div = '<div name="%s" data-oe-type="snippet" data-module-id="%s" data-oe-thumbnail="%s"><section/></div>' % (
|
|
escape(pycompat.to_text(name)),
|
|
module.id,
|
|
escape(pycompat.to_text(thumbnail))
|
|
)
|
|
self._append_text(div, compile_context)
|
|
return []
|
|
|
|
def _compile_directive_placeholder(self, el, compile_context, indent):
|
|
el.set('t-att-placeholder', el.attrib.pop('t-placeholder'))
|
|
return []
|
|
|
|
# order and ignore
|
|
|
|
def _directives_eval_order(self):
|
|
directives = super()._directives_eval_order()
|
|
# Insert before "att" as those may rely on static attributes like
|
|
# "string" and "att" clears all of those
|
|
index = directives.index('att') - 1
|
|
directives.insert(index, 'placeholder')
|
|
directives.insert(index, 'snippet')
|
|
directives.insert(index, 'snippet-call')
|
|
directives.insert(index, 'install')
|
|
return directives
|
|
|
|
|
|
#------------------------------------------------------
|
|
# QWeb fields
|
|
#------------------------------------------------------
|
|
|
|
|
|
class Field(models.AbstractModel):
|
|
_name = 'ir.qweb.field'
|
|
_description = 'Qweb Field'
|
|
_inherit = 'ir.qweb.field'
|
|
|
|
@api.model
|
|
def attributes(self, record, field_name, options, values):
|
|
attrs = super(Field, self).attributes(record, field_name, options, values)
|
|
field = record._fields[field_name]
|
|
|
|
placeholder = options.get('placeholder') or getattr(field, 'placeholder', None)
|
|
if placeholder:
|
|
attrs['placeholder'] = placeholder
|
|
|
|
if options['translate'] and field.type in ('char', 'text'):
|
|
name = "%s,%s" % (record._name, field_name)
|
|
domain = [('name', '=', name), ('res_id', '=', record.id), ('type', '=', 'model'), ('lang', '=', options.get('lang'))]
|
|
translation = record.env['ir.translation'].search(domain, limit=1)
|
|
attrs['data-oe-translation-state'] = translation and translation.state or 'to_translate'
|
|
|
|
return attrs
|
|
|
|
def value_from_string(self, value):
|
|
return value
|
|
|
|
@api.model
|
|
def from_html(self, model, field, element):
|
|
return self.value_from_string(element.text_content().strip())
|
|
|
|
|
|
class Integer(models.AbstractModel):
|
|
_name = 'ir.qweb.field.integer'
|
|
_description = 'Qweb Field Integer'
|
|
_inherit = 'ir.qweb.field.integer'
|
|
|
|
value_from_string = int
|
|
|
|
|
|
class Float(models.AbstractModel):
|
|
_name = 'ir.qweb.field.float'
|
|
_description = 'Qweb Field Float'
|
|
_inherit = 'ir.qweb.field.float'
|
|
|
|
@api.model
|
|
def from_html(self, model, field, element):
|
|
lang = self.user_lang()
|
|
value = element.text_content().strip()
|
|
try:
|
|
return float(value.replace(lang.thousands_sep, '')
|
|
.replace(lang.decimal_point, '.'))
|
|
except:
|
|
raise ValidationError(_('You entered an invalid value, please try again.'))
|
|
|
|
|
|
class ManyToOne(models.AbstractModel):
|
|
_name = 'ir.qweb.field.many2one'
|
|
_description = 'Qweb Field Many to One'
|
|
_inherit = 'ir.qweb.field.many2one'
|
|
|
|
@api.model
|
|
def attributes(self, record, field_name, options, values):
|
|
attrs = super(ManyToOne, self).attributes(record, field_name, options, values)
|
|
if options.get('inherit_branding'):
|
|
many2one = getattr(record, field_name)
|
|
if many2one:
|
|
attrs['data-oe-many2one-id'] = many2one.id
|
|
attrs['data-oe-many2one-model'] = many2one._name
|
|
return attrs
|
|
|
|
@api.model
|
|
def from_html(self, model, field, element):
|
|
Model = self.env[element.get('data-oe-model')]
|
|
id = int(element.get('data-oe-id'))
|
|
M2O = self.env[field.comodel_name]
|
|
field_name = element.get('data-oe-field')
|
|
many2one_id = int(element.get('data-oe-many2one-id'))
|
|
record = many2one_id and M2O.browse(many2one_id)
|
|
if record and record.exists():
|
|
# save the new id of the many2one
|
|
Model.browse(id).write({field_name: many2one_id})
|
|
|
|
# not necessary, but might as well be explicit about it
|
|
return None
|
|
|
|
|
|
class Contact(models.AbstractModel):
|
|
_name = 'ir.qweb.field.contact'
|
|
_description = 'Qweb Field Contact'
|
|
_inherit = 'ir.qweb.field.contact'
|
|
|
|
@api.model
|
|
def attributes(self, record, field_name, options, values):
|
|
attrs = super(Contact, self).attributes(record, field_name, options, values)
|
|
if options.get('inherit_branding'):
|
|
attrs['data-oe-contact-options'] = json.dumps(options)
|
|
return attrs
|
|
|
|
# helper to call the rendering of contact field
|
|
@api.model
|
|
def get_record_to_html(self, ids, options=None):
|
|
return self.value_to_html(self.env['res.partner'].search([('id', '=', ids[0])]), options=options)
|
|
|
|
|
|
class Date(models.AbstractModel):
|
|
_name = 'ir.qweb.field.date'
|
|
_description = 'Qweb Field Date'
|
|
_inherit = 'ir.qweb.field.date'
|
|
|
|
@api.model
|
|
def attributes(self, record, field_name, options, values):
|
|
attrs = super(Date, self).attributes(record, field_name, options, values)
|
|
if options.get('inherit_branding'):
|
|
attrs['data-oe-original'] = record[field_name]
|
|
|
|
if record._fields[field_name].type == 'datetime':
|
|
attrs = self.env['ir.qweb.field.datetime'].attributes(record, field_name, options, values)
|
|
attrs['data-oe-type'] = 'datetime'
|
|
return attrs
|
|
|
|
lg = self.env['res.lang']._lang_get(self.env.user.lang) or get_lang(self.env)
|
|
locale = babel_locale_parse(lg.code)
|
|
babel_format = value_format = posix_to_ldml(lg.date_format, locale=locale)
|
|
|
|
if record[field_name]:
|
|
date = fields.Date.from_string(record[field_name])
|
|
value_format = pycompat.to_text(babel.dates.format_date(date, format=babel_format, locale=locale))
|
|
|
|
attrs['data-oe-original-with-format'] = value_format
|
|
return attrs
|
|
|
|
@api.model
|
|
def from_html(self, model, field, element):
|
|
value = element.text_content().strip()
|
|
if not value:
|
|
return False
|
|
|
|
lg = self.env['res.lang']._lang_get(self.env.user.lang) or get_lang(self.env)
|
|
date = datetime.strptime(value, lg.date_format)
|
|
return fields.Date.to_string(date)
|
|
|
|
|
|
class DateTime(models.AbstractModel):
|
|
_name = 'ir.qweb.field.datetime'
|
|
_description = 'Qweb Field Datetime'
|
|
_inherit = 'ir.qweb.field.datetime'
|
|
|
|
@api.model
|
|
def attributes(self, record, field_name, options, values):
|
|
attrs = super(DateTime, self).attributes(record, field_name, options, values)
|
|
|
|
if options.get('inherit_branding'):
|
|
value = record[field_name]
|
|
|
|
lg = self.env['res.lang']._lang_get(self.env.user.lang) or get_lang(self.env)
|
|
locale = babel_locale_parse(lg.code)
|
|
babel_format = value_format = posix_to_ldml('%s %s' % (lg.date_format, lg.time_format), locale=locale)
|
|
tz = record.env.context.get('tz') or self.env.user.tz
|
|
|
|
if isinstance(value, str):
|
|
value = fields.Datetime.from_string(value)
|
|
|
|
if value:
|
|
# convert from UTC (server timezone) to user timezone
|
|
value = fields.Datetime.context_timestamp(self.with_context(tz=tz), timestamp=value)
|
|
value_format = pycompat.to_text(babel.dates.format_datetime(value, format=babel_format, locale=locale))
|
|
value = fields.Datetime.to_string(value)
|
|
|
|
attrs['data-oe-original'] = value
|
|
attrs['data-oe-original-with-format'] = value_format
|
|
attrs['data-oe-original-tz'] = tz
|
|
return attrs
|
|
|
|
@api.model
|
|
def from_html(self, model, field, element):
|
|
value = element.text_content().strip()
|
|
if not value:
|
|
return False
|
|
|
|
# parse from string to datetime
|
|
lg = self.env['res.lang']._lang_get(self.env.user.lang) or get_lang(self.env)
|
|
dt = datetime.strptime(value, '%s %s' % (lg.date_format, lg.time_format))
|
|
|
|
# convert back from user's timezone to UTC
|
|
tz_name = element.attrib.get('data-oe-original-tz') or self.env.context.get('tz') or self.env.user.tz
|
|
if tz_name:
|
|
try:
|
|
user_tz = pytz.timezone(tz_name)
|
|
utc = pytz.utc
|
|
|
|
dt = user_tz.localize(dt).astimezone(utc)
|
|
except Exception:
|
|
logger.warning(
|
|
"Failed to convert the value for a field of the model"
|
|
" %s back from the user's timezone (%s) to UTC",
|
|
model, tz_name,
|
|
exc_info=True)
|
|
|
|
# format back to string
|
|
return fields.Datetime.to_string(dt)
|
|
|
|
|
|
class Text(models.AbstractModel):
|
|
_name = 'ir.qweb.field.text'
|
|
_description = 'Qweb Field Text'
|
|
_inherit = 'ir.qweb.field.text'
|
|
|
|
@api.model
|
|
def from_html(self, model, field, element):
|
|
return html_to_text(element)
|
|
|
|
|
|
class Selection(models.AbstractModel):
|
|
_name = 'ir.qweb.field.selection'
|
|
_description = 'Qweb Field Selection'
|
|
_inherit = 'ir.qweb.field.selection'
|
|
|
|
@api.model
|
|
def from_html(self, model, field, element):
|
|
value = element.text_content().strip()
|
|
selection = field.get_description(self.env)['selection']
|
|
for k, v in selection:
|
|
if isinstance(v, str):
|
|
v = ustr(v)
|
|
if value == v:
|
|
return k
|
|
|
|
raise ValueError(u"No value found for label %s in selection %s" % (
|
|
value, selection))
|
|
|
|
|
|
class HTML(models.AbstractModel):
|
|
_name = 'ir.qweb.field.html'
|
|
_description = 'Qweb Field HTML'
|
|
_inherit = 'ir.qweb.field.html'
|
|
|
|
@api.model
|
|
def from_html(self, model, field, element):
|
|
content = []
|
|
if element.text:
|
|
content.append(element.text)
|
|
content.extend(html.tostring(child, encoding='unicode')
|
|
for child in element.iterchildren(tag=etree.Element))
|
|
return '\n'.join(content)
|
|
|
|
|
|
class Image(models.AbstractModel):
|
|
"""
|
|
Widget options:
|
|
|
|
``class``
|
|
set as attribute on the generated <img> tag
|
|
"""
|
|
_name = 'ir.qweb.field.image'
|
|
_description = 'Qweb Field Image'
|
|
_inherit = 'ir.qweb.field.image'
|
|
|
|
local_url_re = re.compile(r'^/(?P<module>[^]]+)/static/(?P<rest>.+)$')
|
|
|
|
@api.model
|
|
def from_html(self, model, field, element):
|
|
if element.find('img') is None:
|
|
return False
|
|
url = element.find('img').get('src')
|
|
|
|
url_object = urls.url_parse(url)
|
|
if url_object.path.startswith('/web/image'):
|
|
fragments = url_object.path.split('/')
|
|
query = url_object.decode_query()
|
|
url_id = fragments[3].split('-')[0]
|
|
# ir.attachment image urls: /web/image/<id>[-<checksum>][/...]
|
|
if url_id.isdigit():
|
|
model = 'ir.attachment'
|
|
oid = url_id
|
|
field = 'datas'
|
|
# url of binary field on model: /web/image/<model>/<id>/<field>[/...]
|
|
else:
|
|
model = query.get('model', fragments[3])
|
|
oid = query.get('id', fragments[4])
|
|
field = query.get('field', fragments[5])
|
|
item = self.env[model].browse(int(oid))
|
|
return item[field]
|
|
|
|
if self.local_url_re.match(url_object.path):
|
|
return self.load_local_url(url)
|
|
|
|
return self.load_remote_url(url)
|
|
|
|
def load_local_url(self, url):
|
|
match = self.local_url_re.match(urls.url_parse(url).path)
|
|
|
|
rest = match.group('rest')
|
|
for sep in os.sep, os.altsep:
|
|
if sep and sep != '/':
|
|
rest.replace(sep, '/')
|
|
|
|
path = odoo.modules.get_module_resource(
|
|
match.group('module'), 'static', *(rest.split('/')))
|
|
|
|
if not path:
|
|
return None
|
|
|
|
try:
|
|
with open(path, 'rb') as f:
|
|
# force complete image load to ensure it's valid image data
|
|
image = I.open(f)
|
|
image.load()
|
|
f.seek(0)
|
|
return base64.b64encode(f.read())
|
|
except Exception:
|
|
logger.exception("Failed to load local image %r", url)
|
|
return None
|
|
|
|
def load_remote_url(self, url):
|
|
try:
|
|
# should probably remove remote URLs entirely:
|
|
# * in fields, downloading them without blowing up the server is a
|
|
# challenge
|
|
# * in views, may trigger mixed content warnings if HTTPS CMS
|
|
# linking to HTTP images
|
|
# implement drag & drop image upload to mitigate?
|
|
|
|
req = requests.get(url, timeout=REMOTE_CONNECTION_TIMEOUT)
|
|
# PIL needs a seekable file-like image so wrap result in IO buffer
|
|
image = I.open(io.BytesIO(req.content))
|
|
# force a complete load of the image data to validate it
|
|
image.load()
|
|
except Exception:
|
|
logger.exception("Failed to load remote image %r", url)
|
|
return None
|
|
|
|
# don't use original data in case weird stuff was smuggled in, with
|
|
# luck PIL will remove some of it?
|
|
out = io.BytesIO()
|
|
image.save(out, image.format)
|
|
return base64.b64encode(out.getvalue())
|
|
|
|
|
|
class Monetary(models.AbstractModel):
|
|
_name = 'ir.qweb.field.monetary'
|
|
_inherit = 'ir.qweb.field.monetary'
|
|
|
|
@api.model
|
|
def from_html(self, model, field, element):
|
|
lang = self.user_lang()
|
|
|
|
value = element.find('span').text.strip()
|
|
|
|
try:
|
|
return float(value.replace(lang.thousands_sep, '')
|
|
.replace(lang.decimal_point, '.'))
|
|
except:
|
|
raise ValidationError(_('You entered an invalid value, please try again.'))
|
|
|
|
|
|
class Duration(models.AbstractModel):
|
|
_name = 'ir.qweb.field.duration'
|
|
_description = 'Qweb Field Duration'
|
|
_inherit = 'ir.qweb.field.duration'
|
|
|
|
@api.model
|
|
def attributes(self, record, field_name, options, values):
|
|
attrs = super(Duration, self).attributes(record, field_name, options, values)
|
|
if options.get('inherit_branding'):
|
|
attrs['data-oe-original'] = record[field_name]
|
|
return attrs
|
|
|
|
@api.model
|
|
def from_html(self, model, field, element):
|
|
value = element.text_content().strip()
|
|
|
|
# non-localized value
|
|
return float(value)
|
|
|
|
|
|
class RelativeDatetime(models.AbstractModel):
|
|
_name = 'ir.qweb.field.relative'
|
|
_description = 'Qweb Field Relative'
|
|
_inherit = 'ir.qweb.field.relative'
|
|
|
|
# get formatting from ir.qweb.field.relative but edition/save from datetime
|
|
|
|
|
|
class QwebView(models.AbstractModel):
|
|
_name = 'ir.qweb.field.qweb'
|
|
_description = 'Qweb Field qweb'
|
|
_inherit = 'ir.qweb.field.qweb'
|
|
|
|
|
|
def html_to_text(element):
|
|
""" Converts HTML content with HTML-specified line breaks (br, p, div, ...)
|
|
in roughly equivalent textual content.
|
|
|
|
Used to replace and fixup the roundtripping of text and m2o: when using
|
|
libxml 2.8.0 (but not 2.9.1) and parsing HTML with lxml.html.fromstring
|
|
whitespace text nodes (text nodes composed *solely* of whitespace) are
|
|
stripped out with no recourse, and fundamentally relying on newlines
|
|
being in the text (e.g. inserted during user edition) is probably poor form
|
|
anyway.
|
|
|
|
-> this utility function collapses whitespace sequences and replaces
|
|
nodes by roughly corresponding linebreaks
|
|
* p are pre-and post-fixed by 2 newlines
|
|
* br are replaced by a single newline
|
|
* block-level elements not already mentioned are pre- and post-fixed by
|
|
a single newline
|
|
|
|
ought be somewhat similar (but much less high-tech) to aaronsw's html2text.
|
|
the latter produces full-blown markdown, our text -> html converter only
|
|
replaces newlines by <br> elements at this point so we're reverting that,
|
|
and a few more newline-ish elements in case the user tried to add
|
|
newlines/paragraphs into the text field
|
|
|
|
:param element: lxml.html content
|
|
:returns: corresponding pure-text output
|
|
"""
|
|
|
|
# output is a list of str | int. Integers are padding requests (in minimum
|
|
# number of newlines). When multiple padding requests, fold them into the
|
|
# biggest one
|
|
output = []
|
|
_wrap(element, output)
|
|
|
|
# remove any leading or tailing whitespace, replace sequences of
|
|
# (whitespace)\n(whitespace) by a single newline, where (whitespace) is a
|
|
# non-newline whitespace in this case
|
|
return re.sub(
|
|
r'[ \t\r\f]*\n[ \t\r\f]*',
|
|
'\n',
|
|
''.join(_realize_padding(output)).strip())
|
|
|
|
_PADDED_BLOCK = set('p h1 h2 h3 h4 h5 h6'.split())
|
|
# https://developer.mozilla.org/en-US/docs/HTML/Block-level_elements minus p
|
|
_MISC_BLOCK = set((
|
|
'address article aside audio blockquote canvas dd dl div figcaption figure'
|
|
' footer form header hgroup hr ol output pre section tfoot ul video'
|
|
).split())
|
|
|
|
|
|
def _collapse_whitespace(text):
|
|
""" Collapses sequences of whitespace characters in ``text`` to a single
|
|
space
|
|
"""
|
|
return re.sub('\s+', ' ', text)
|
|
|
|
|
|
def _realize_padding(it):
|
|
""" Fold and convert padding requests: integers in the output sequence are
|
|
requests for at least n newlines of padding. Runs thereof can be collapsed
|
|
into the largest requests and converted to newlines.
|
|
"""
|
|
padding = 0
|
|
for item in it:
|
|
if isinstance(item, int):
|
|
padding = max(padding, item)
|
|
continue
|
|
|
|
if padding:
|
|
yield '\n' * padding
|
|
padding = 0
|
|
|
|
yield item
|
|
# leftover padding irrelevant as the output will be stripped
|
|
|
|
|
|
def _wrap(element, output, wrapper=''):
|
|
""" Recursively extracts text from ``element`` (via _element_to_text), and
|
|
wraps it all in ``wrapper``. Extracted text is added to ``output``
|
|
|
|
:type wrapper: basestring | int
|
|
"""
|
|
output.append(wrapper)
|
|
if element.text:
|
|
output.append(_collapse_whitespace(element.text))
|
|
for child in element:
|
|
_element_to_text(child, output)
|
|
output.append(wrapper)
|
|
|
|
|
|
def _element_to_text(e, output):
|
|
if e.tag == 'br':
|
|
output.append('\n')
|
|
elif e.tag in _PADDED_BLOCK:
|
|
_wrap(e, output, 2)
|
|
elif e.tag in _MISC_BLOCK:
|
|
_wrap(e, output, 1)
|
|
else:
|
|
# inline
|
|
_wrap(e, output)
|
|
|
|
if e.tail:
|
|
output.append(_collapse_whitespace(e.tail))
|