[ADD] saving and edition of oddly formatted fields in RTE

bzr revid: xmo@openerp.com-20131205114245-2p57t0z182r8oeyv
This commit is contained in:
Xavier Morel 2013-12-05 12:42:45 +01:00
commit 1049e1c859
4 changed files with 77 additions and 25 deletions

View File

@ -121,33 +121,49 @@ class Date(orm.AbstractModel):
_name = 'website.qweb.field.date'
_inherit = ['website.qweb.field', 'ir.qweb.field.date']
def attributes(self, cr, uid, field_name, record, options,
source_element, g_att, t_att, qweb_context,
context=None):
attrs = super(Date, self).attributes(
cr, uid, field_name, record, options, source_element, g_att, t_att,
qweb_context, context=None)
return itertools.chain(attrs, [('data-oe-original', record[field_name])])
def from_html(self, cr, uid, model, column, element, context=None):
lang = self.user_lang(cr, uid, context=context)
in_format = lang.date_format.encode('utf-8')
value = element.text_content().strip()
try:
dt = datetime.datetime.strptime(in_format, value)
except ValueError:
dt = parse_fuzzy(in_format, value)
if not value: return False
return dt.strftime(DEFAULT_SERVER_DATE_FORMAT)
datetime.datetime.strptime(value, DEFAULT_SERVER_DATE_FORMAT)
return value
class DateTime(orm.AbstractModel):
_name = 'website.qweb.field.datetime'
_inherit = ['website.qweb.field', 'ir.qweb.field.datetime']
def attributes(self, cr, uid, field_name, record, options,
source_element, g_att, t_att, qweb_context,
context=None):
column = record._model._all_columns[field_name].column
value = record[field_name]
if isinstance(value, basestring):
value = datetime.datetime.strptime(
value, DEFAULT_SERVER_DATETIME_FORMAT)
value = column.context_timestamp(
cr, uid, timestamp=value, context=context)
attrs = super(DateTime, self).attributes(
cr, uid, field_name, record, options, source_element, g_att, t_att,
qweb_context, context=None)
return itertools.chain(attrs, [
('data-oe-original', value.strftime(openerp.tools.DEFAULT_SERVER_DATETIME_FORMAT))
])
def from_html(self, cr, uid, model, column, element, context=None):
lang = self.user_lang(cr, uid, context=context)
in_format = (u"%s %s" % (lang.date_format, lang.time_format)).encode('utf-8')
value = element.text_content().strip()
try:
dt = datetime.datetime.strptime(in_format, value)
except ValueError:
dt = parse_fuzzy(in_format, value)
if not value: return False
return dt.strftime(DEFAULT_SERVER_DATETIME_FORMAT)
datetime.datetime.strptime(value, DEFAULT_SERVER_DATETIME_FORMAT)
return value
class Text(orm.AbstractModel):
_name = 'website.qweb.field.text'
@ -302,3 +318,34 @@ class Monetary(orm.AbstractModel):
return float(value.replace(lang.thousands_sep, '')
.replace(lang.decimal_point, '.'))
class Duration(orm.AbstractModel):
_name = 'website.qweb.field.duration'
_inherit = [
'ir.qweb.field.duration',
'website.qweb.field.float',
]
def attributes(self, cr, uid, field_name, record, options,
source_element, g_att, t_att, qweb_context,
context=None):
attrs = super(Duration, self).attributes(
cr, uid, field_name, record, options, source_element, g_att, t_att,
qweb_context, context=None)
return itertools.chain(attrs, [('data-oe-original', record[field_name])])
def from_html(self, cr, uid, model, column, element, context=None):
value = element.text_content().strip()
# non-localized value
return float(value)
class RelativeDatetime(orm.AbstractModel):
_name = 'website.qweb.field.relative'
_inherit = [
'ir.qweb.field.relative',
'website.qweb.field.datetime',
]
# get formatting from ir.qweb.field.relative but edition/save from datetime

View File

@ -206,10 +206,6 @@ ul.oe_menu_editor .disclose {
position: static !important;
}
.cke_widget_drag_handler_container {
display: none !important;
}
.cke_widget_editable:empty:after {
content: " ";
white-space: pre-wrap;

View File

@ -168,9 +168,6 @@ ul.oe_menu_editor
// Breaks completely horribly crazy products listing page, so take it out.
.cke_widget_wrapper
position: static !important
// "remove" drag & drop of CKE widgets
.cke_widget_drag_handler_container
display: none !important
// prevent inline widgets from entirely disappearing when their (textual)
// content is removed
.cke_widget_editable:empty:after

View File

@ -323,14 +323,26 @@
init: function (editor) {
editor.widgets.add('oeref', {
editables: { text: '*' },
draggable: false,
upcast: function (el) {
return el.attributes['data-oe-type']
&& el.attributes['data-oe-type'] !== 'monetary';
var matches = el.attributes['data-oe-type'] && el.attributes['data-oe-type'] !== 'monetary';
if (!matches) { return false; }
if (el.attributes['data-oe-original']) {
while (el.children.length) {
el.children[0].remove();
}
el.add(new CKEDITOR.htmlParser.text(
el.attributes['data-oe-original']
));
}
return true;
},
});
editor.widgets.add('monetary', {
editables: { text: 'span.oe_currency_value' },
draggable: false,
upcast: function (el) {
return el.attributes['data-oe-type'] === 'monetary';