2012-09-19 11:40:47 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
import functools
|
|
|
|
import operator
|
2012-09-20 15:04:43 +00:00
|
|
|
import itertools
|
2012-10-01 13:05:30 +00:00
|
|
|
import psycopg2
|
2012-09-19 11:40:47 +00:00
|
|
|
from openerp.osv import orm, fields
|
|
|
|
from openerp.tools.translate import _
|
|
|
|
|
2012-09-20 10:25:45 +00:00
|
|
|
REFERENCING_FIELDS = set([None, 'id', '.id'])
|
|
|
|
def only_ref_fields(record):
|
|
|
|
return dict((k, v) for k, v in record.iteritems()
|
|
|
|
if k in REFERENCING_FIELDS)
|
|
|
|
def exclude_ref_fields(record):
|
|
|
|
return dict((k, v) for k, v in record.iteritems()
|
|
|
|
if k not in REFERENCING_FIELDS)
|
|
|
|
|
|
|
|
CREATE = lambda values: (0, False, values)
|
|
|
|
UPDATE = lambda id, values: (1, id, values)
|
|
|
|
DELETE = lambda id: (2, id, False)
|
|
|
|
FORGET = lambda id: (3, id, False)
|
|
|
|
LINK_TO = lambda id: (4, id, False)
|
|
|
|
DELETE_ALL = lambda: (5, False, False)
|
|
|
|
REPLACE_WITH = lambda ids: (6, False, ids)
|
|
|
|
|
2012-09-19 11:40:47 +00:00
|
|
|
class ConversionNotFound(ValueError): pass
|
|
|
|
|
|
|
|
class ir_fields_converter(orm.Model):
|
|
|
|
_name = 'ir.fields.converter'
|
|
|
|
|
|
|
|
def to_field(self, cr, uid, model, column, fromtype=str, context=None):
|
|
|
|
""" Fetches a converter for the provided column object, from the
|
|
|
|
specified type.
|
|
|
|
|
|
|
|
A converter is simply a callable taking a value of type ``fromtype``
|
|
|
|
(or a composite of ``fromtype``, e.g. list or dict) and returning a
|
|
|
|
value acceptable for a write() on the column ``column``.
|
|
|
|
|
|
|
|
By default, tries to get a method on itself with a name matching the
|
|
|
|
pattern ``_$fromtype_$column._type`` and returns it.
|
|
|
|
|
2012-09-27 14:43:41 +00:00
|
|
|
Converter callables can either return a value and a list of warnings
|
|
|
|
to their caller or raise ``ValueError``, which will be interpreted as a
|
|
|
|
validation & conversion failure.
|
2012-09-24 15:04:17 +00:00
|
|
|
|
|
|
|
ValueError can have either one or two parameters. The first parameter
|
|
|
|
is mandatory, **must** be a unicode string and will be used as the
|
|
|
|
user-visible message for the error (it should be translatable and
|
|
|
|
translated). It can contain a ``field`` named format placeholder so the
|
|
|
|
caller can inject the field's translated, user-facing name (@string).
|
|
|
|
|
|
|
|
The second parameter is optional and, if provided, must be a mapping.
|
|
|
|
This mapping will be merged into the error dictionary returned to the
|
|
|
|
client.
|
|
|
|
|
|
|
|
If a converter can perform its function but has to make assumptions
|
2012-09-27 14:43:41 +00:00
|
|
|
about the data, it can send a warning to the user through adding an
|
|
|
|
instance of :class:`~openerp.osv.orm.ImportWarning` to the second value
|
|
|
|
it returns. The handling of a warning at the upper levels is the same
|
|
|
|
as ``ValueError`` above.
|
2012-09-24 15:04:17 +00:00
|
|
|
|
2012-09-19 11:40:47 +00:00
|
|
|
:param cr: openerp cursor
|
|
|
|
:param uid: ID of user calling the converter
|
|
|
|
:param column: column object to generate a value for
|
|
|
|
:type column: :class:`fields._column`
|
|
|
|
:param type fromtype: type to convert to something fitting for ``column``
|
|
|
|
:param context: openerp request context
|
|
|
|
:return: a function (fromtype -> column.write_type), if a converter is found
|
|
|
|
:rtype: Callable | None
|
|
|
|
"""
|
|
|
|
# FIXME: return None
|
|
|
|
converter = getattr(
|
2012-09-25 13:59:55 +00:00
|
|
|
self, '_%s_to_%s' % (fromtype.__name__, column._type), None)
|
2012-09-19 11:40:47 +00:00
|
|
|
if not converter: return None
|
|
|
|
|
|
|
|
return functools.partial(
|
|
|
|
converter, cr, uid, model, column, context=context)
|
|
|
|
|
|
|
|
def _str_to_boolean(self, cr, uid, model, column, value, context=None):
|
2012-09-20 15:04:43 +00:00
|
|
|
# all translatables used for booleans
|
|
|
|
true, yes, false, no = _(u"true"), _(u"yes"), _(u"false"), _(u"no")
|
|
|
|
# potentially broken casefolding? What about locales?
|
|
|
|
trues = set(word.lower() for word in itertools.chain(
|
|
|
|
[u'1', u"true", u"yes"], # don't use potentially translated values
|
|
|
|
self._get_translations(cr, uid, ['code'], u"true", context=context),
|
|
|
|
self._get_translations(cr, uid, ['code'], u"yes", context=context),
|
|
|
|
))
|
2012-09-27 14:43:41 +00:00
|
|
|
if value.lower() in trues: return True, []
|
2012-09-20 15:04:43 +00:00
|
|
|
|
|
|
|
# potentially broken casefolding? What about locales?
|
|
|
|
falses = set(word.lower() for word in itertools.chain(
|
|
|
|
[u'', u"0", u"false", u"no"],
|
|
|
|
self._get_translations(cr, uid, ['code'], u"false", context=context),
|
|
|
|
self._get_translations(cr, uid, ['code'], u"no", context=context),
|
|
|
|
))
|
2012-09-27 14:43:41 +00:00
|
|
|
if value.lower() in falses: return False, []
|
2012-09-20 15:04:43 +00:00
|
|
|
|
2012-09-27 14:43:41 +00:00
|
|
|
return True, [orm.ImportWarning(
|
2012-09-20 15:04:43 +00:00
|
|
|
_(u"Unknown value '%s' for boolean field '%%(field)s', assuming '%s'")
|
2012-09-27 14:43:41 +00:00
|
|
|
% (value, yes))]
|
2012-09-19 11:40:47 +00:00
|
|
|
|
|
|
|
def _str_to_integer(self, cr, uid, model, column, value, context=None):
|
2012-09-27 10:34:52 +00:00
|
|
|
try:
|
2012-09-27 14:43:41 +00:00
|
|
|
return int(value), []
|
2012-09-27 10:34:52 +00:00
|
|
|
except ValueError:
|
|
|
|
raise ValueError(
|
|
|
|
_(u"'%s' does not seem to be an integer for field '%%(field)s'")
|
|
|
|
% value)
|
2012-09-19 11:40:47 +00:00
|
|
|
|
|
|
|
def _str_to_float(self, cr, uid, model, column, value, context=None):
|
2012-09-27 10:34:52 +00:00
|
|
|
try:
|
2012-09-27 14:43:41 +00:00
|
|
|
return float(value), []
|
2012-09-27 10:34:52 +00:00
|
|
|
except ValueError:
|
|
|
|
raise ValueError(
|
|
|
|
_(u"'%s' does not seem to be a number for field '%%(field)s'")
|
|
|
|
% value)
|
2012-09-19 11:40:47 +00:00
|
|
|
|
|
|
|
def _str_to_char(self, cr, uid, model, column, value, context=None):
|
2012-09-27 14:43:41 +00:00
|
|
|
return value, []
|
2012-09-19 11:40:47 +00:00
|
|
|
|
|
|
|
def _str_to_text(self, cr, uid, model, column, value, context=None):
|
2012-09-27 14:43:41 +00:00
|
|
|
return value, []
|
2012-09-19 11:40:47 +00:00
|
|
|
|
2012-09-25 13:59:55 +00:00
|
|
|
def _str_to_binary(self, cr, uid, model, column, value, context=None):
|
2012-09-27 14:43:41 +00:00
|
|
|
return value, []
|
2012-09-25 13:59:55 +00:00
|
|
|
|
2012-09-19 11:40:47 +00:00
|
|
|
def _get_translations(self, cr, uid, types, src, context):
|
2012-09-24 10:32:57 +00:00
|
|
|
types = tuple(types)
|
|
|
|
# Cache translations so they don't have to be reloaded from scratch on
|
|
|
|
# every row of the file
|
|
|
|
tnx_cache = cr.cache.setdefault(self._name, {})
|
|
|
|
if tnx_cache.setdefault(types, {}) and src in tnx_cache[types]:
|
|
|
|
return tnx_cache[types][src]
|
|
|
|
|
2012-09-19 11:40:47 +00:00
|
|
|
Translations = self.pool['ir.translation']
|
|
|
|
tnx_ids = Translations.search(
|
|
|
|
cr, uid, [('type', 'in', types), ('src', '=', src)], context=context)
|
|
|
|
tnx = Translations.read(cr, uid, tnx_ids, ['value'], context=context)
|
2012-09-24 10:32:57 +00:00
|
|
|
result = tnx_cache[types][src] = map(operator.itemgetter('value'), tnx)
|
|
|
|
return result
|
|
|
|
|
2012-09-19 11:40:47 +00:00
|
|
|
def _str_to_selection(self, cr, uid, model, column, value, context=None):
|
|
|
|
|
|
|
|
selection = column.selection
|
|
|
|
if not isinstance(selection, (tuple, list)):
|
|
|
|
# FIXME: Don't pass context to avoid translations?
|
|
|
|
# Or just copy context & remove lang?
|
|
|
|
selection = selection(model, cr, uid)
|
|
|
|
for item, label in selection:
|
|
|
|
labels = self._get_translations(
|
2012-09-20 15:04:43 +00:00
|
|
|
cr, uid, ('selection', 'model', 'code'), label, context=context)
|
2012-09-19 11:40:47 +00:00
|
|
|
labels.append(label)
|
|
|
|
if value == unicode(item) or value in labels:
|
2012-09-27 14:43:41 +00:00
|
|
|
return item, []
|
2012-09-19 11:40:47 +00:00
|
|
|
raise ValueError(
|
|
|
|
_(u"Value '%s' not found in selection field '%%(field)s'") % (
|
2012-09-24 15:04:17 +00:00
|
|
|
value), {
|
|
|
|
'moreinfo': map(operator.itemgetter(1), selection)
|
|
|
|
})
|
2012-09-19 11:40:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
def db_id_for(self, cr, uid, model, column, subfield, value, context=None):
|
|
|
|
""" Finds a database id for the reference ``value`` in the referencing
|
|
|
|
subfield ``subfield`` of the provided column of the provided model.
|
|
|
|
|
|
|
|
:param cr: OpenERP cursor
|
|
|
|
:param uid: OpenERP user id
|
|
|
|
:param model: model to which the column belongs
|
|
|
|
:param column: relational column for which references are provided
|
|
|
|
:param subfield: a relational subfield allowing building of refs to
|
|
|
|
existing records: ``None`` for a name_get/name_search,
|
|
|
|
``id`` for an external id and ``.id`` for a database
|
|
|
|
id
|
|
|
|
:param value: value of the reference to match to an actual record
|
|
|
|
:param context: OpenERP request context
|
2012-09-27 14:43:41 +00:00
|
|
|
:return: a pair of the matched database identifier (if any), the
|
|
|
|
translated user-readable name for the field and the list of
|
|
|
|
warnings
|
|
|
|
:rtype: (ID|None, unicode, list)
|
2012-09-19 11:40:47 +00:00
|
|
|
"""
|
2012-09-27 14:43:41 +00:00
|
|
|
if context is None: context = {}
|
2012-09-19 11:40:47 +00:00
|
|
|
id = None
|
2012-09-27 14:43:41 +00:00
|
|
|
warnings = []
|
2012-10-01 13:05:30 +00:00
|
|
|
extras = {'moreinfo': {
|
|
|
|
'type': 'ir.actions.act_window', 'target': 'new',
|
|
|
|
'res_model': column._obj, 'view_mode': 'tree,form',
|
|
|
|
'view_type': 'form',
|
|
|
|
'views': [(False, 'tree', (False, 'form'))],
|
|
|
|
'help': _(u"See all possible values")
|
|
|
|
}}
|
2012-09-19 11:40:47 +00:00
|
|
|
RelatedModel = self.pool[column._obj]
|
|
|
|
if subfield == '.id':
|
|
|
|
field_type = _(u"database id")
|
|
|
|
try: tentative_id = int(value)
|
|
|
|
except ValueError: tentative_id = value
|
2012-10-01 13:05:30 +00:00
|
|
|
try:
|
|
|
|
if RelatedModel.search(cr, uid, [('id', '=', tentative_id)],
|
|
|
|
context=context):
|
|
|
|
id = tentative_id
|
|
|
|
except psycopg2.DataError:
|
|
|
|
# type error
|
|
|
|
raise ValueError(
|
|
|
|
_(u"Invalid database id '%s' for the field '%%(field)s'") % value,
|
|
|
|
extras)
|
2012-09-19 11:40:47 +00:00
|
|
|
elif subfield == 'id':
|
|
|
|
field_type = _(u"external id")
|
|
|
|
if '.' in value:
|
|
|
|
module, xid = value.split('.', 1)
|
|
|
|
else:
|
2012-09-27 14:43:41 +00:00
|
|
|
module, xid = context.get('_import_current_module', ''), value
|
2012-09-19 11:40:47 +00:00
|
|
|
ModelData = self.pool['ir.model.data']
|
|
|
|
try:
|
|
|
|
md_id = ModelData._get_id(cr, uid, module, xid)
|
|
|
|
model_data = ModelData.read(cr, uid, [md_id], ['res_id'],
|
|
|
|
context=context)
|
|
|
|
if model_data:
|
|
|
|
id = model_data[0]['res_id']
|
|
|
|
except ValueError: pass # leave id is None
|
|
|
|
elif subfield is None:
|
|
|
|
field_type = _(u"name")
|
|
|
|
ids = RelatedModel.name_search(
|
|
|
|
cr, uid, name=value, operator='=', context=context)
|
|
|
|
if ids:
|
|
|
|
if len(ids) > 1:
|
2012-09-27 14:43:41 +00:00
|
|
|
warnings.append(orm.ImportWarning(
|
2012-09-19 11:40:47 +00:00
|
|
|
_(u"Found multiple matches for field '%%(field)s' (%d matches)")
|
2012-09-24 15:04:17 +00:00
|
|
|
% (len(ids))))
|
2012-09-19 11:40:47 +00:00
|
|
|
id, _name = ids[0]
|
|
|
|
else:
|
|
|
|
raise Exception(u"Unknown sub-field '%s'" % subfield)
|
2012-09-25 07:42:56 +00:00
|
|
|
|
|
|
|
if id is None:
|
|
|
|
raise ValueError(
|
|
|
|
_(u"No matching record found for %(field_type)s '%(value)s' in field '%%(field)s'")
|
2012-10-01 13:05:30 +00:00
|
|
|
% {'field_type': field_type, 'value': value}, extras)
|
2012-09-27 14:43:41 +00:00
|
|
|
return id, field_type, warnings
|
2012-09-19 11:40:47 +00:00
|
|
|
|
|
|
|
def _referencing_subfield(self, record):
|
|
|
|
""" Checks the record for the subfields allowing referencing (an
|
|
|
|
existing record in an other table), errors out if it finds potential
|
|
|
|
conflicts (multiple referencing subfields) or non-referencing subfields
|
|
|
|
returns the name of the correct subfield.
|
|
|
|
|
|
|
|
:param record:
|
2012-09-27 14:43:41 +00:00
|
|
|
:return: the record subfield to use for referencing and a list of warnings
|
|
|
|
:rtype: str, list
|
2012-09-19 11:40:47 +00:00
|
|
|
"""
|
|
|
|
# Can import by name_get, external id or database id
|
|
|
|
fieldset = set(record.iterkeys())
|
2012-09-20 10:25:45 +00:00
|
|
|
if fieldset - REFERENCING_FIELDS:
|
2012-09-19 11:40:47 +00:00
|
|
|
raise ValueError(
|
|
|
|
_(u"Can not create Many-To-One records indirectly, import the field separately"))
|
|
|
|
if len(fieldset) > 1:
|
|
|
|
raise ValueError(
|
|
|
|
_(u"Ambiguous specification for field '%(field)s', only provide one of name, external id or database id"))
|
|
|
|
|
|
|
|
# only one field left possible, unpack
|
|
|
|
[subfield] = fieldset
|
2012-09-27 14:43:41 +00:00
|
|
|
return subfield, []
|
2012-09-19 11:40:47 +00:00
|
|
|
|
|
|
|
def _str_to_many2one(self, cr, uid, model, column, values, context=None):
|
|
|
|
# Should only be one record, unpack
|
|
|
|
[record] = values
|
|
|
|
|
2012-09-27 14:43:41 +00:00
|
|
|
subfield, w1 = self._referencing_subfield(record)
|
2012-09-19 11:40:47 +00:00
|
|
|
|
|
|
|
reference = record[subfield]
|
2012-09-27 14:43:41 +00:00
|
|
|
id, subfield_type, w2 = self.db_id_for(
|
2012-09-19 11:40:47 +00:00
|
|
|
cr, uid, model, column, subfield, reference, context=context)
|
2012-09-27 14:43:41 +00:00
|
|
|
return id, w1 + w2
|
2012-09-25 07:42:56 +00:00
|
|
|
|
2012-09-19 11:40:47 +00:00
|
|
|
def _str_to_many2many(self, cr, uid, model, column, value, context=None):
|
|
|
|
[record] = value
|
|
|
|
|
2012-09-27 14:43:41 +00:00
|
|
|
subfield, warnings = self._referencing_subfield(record)
|
2012-09-19 11:40:47 +00:00
|
|
|
|
|
|
|
ids = []
|
|
|
|
for reference in record[subfield].split(','):
|
2012-09-27 14:43:41 +00:00
|
|
|
id, subfield_type, ws = self.db_id_for(
|
2012-09-19 11:40:47 +00:00
|
|
|
cr, uid, model, column, subfield, reference, context=context)
|
|
|
|
ids.append(id)
|
2012-09-27 14:43:41 +00:00
|
|
|
warnings.extend(ws)
|
|
|
|
return [(6, 0, ids)], warnings
|
2012-09-25 07:42:56 +00:00
|
|
|
|
2012-09-20 11:09:14 +00:00
|
|
|
def _str_to_one2many(self, cr, uid, model, column, records, context=None):
|
2012-09-20 10:25:45 +00:00
|
|
|
commands = []
|
2012-09-27 14:43:41 +00:00
|
|
|
warnings = []
|
2012-09-20 10:56:12 +00:00
|
|
|
|
2012-09-20 11:09:14 +00:00
|
|
|
if len(records) == 1 and exclude_ref_fields(records[0]) == {}:
|
|
|
|
# only one row with only ref field, field=ref1,ref2,ref3 as in
|
|
|
|
# m2o/m2m
|
|
|
|
record = records[0]
|
2012-09-27 14:43:41 +00:00
|
|
|
subfield, ws = self._referencing_subfield(record)
|
|
|
|
warnings.extend(ws)
|
2012-09-20 11:09:14 +00:00
|
|
|
# transform [{subfield:ref1,ref2,ref3}] into
|
|
|
|
# [{subfield:ref1},{subfield:ref2},{subfield:ref3}]
|
|
|
|
records = ({subfield:item} for item in record[subfield].split(','))
|
|
|
|
|
|
|
|
for record in records:
|
2012-09-20 10:56:12 +00:00
|
|
|
id = None
|
|
|
|
refs = only_ref_fields(record)
|
|
|
|
# there are ref fields in the record
|
|
|
|
if refs:
|
2012-09-27 14:43:41 +00:00
|
|
|
subfield, w1 = self._referencing_subfield(refs)
|
|
|
|
warnings.extend(w1)
|
2012-09-20 10:56:12 +00:00
|
|
|
reference = record[subfield]
|
2012-09-27 14:43:41 +00:00
|
|
|
id, subfield_type, w2 = self.db_id_for(
|
2012-09-20 10:56:12 +00:00
|
|
|
cr, uid, model, column, subfield, reference, context=context)
|
2012-09-27 14:43:41 +00:00
|
|
|
warnings.extend(w2)
|
2012-09-20 10:56:12 +00:00
|
|
|
|
2012-09-20 10:25:45 +00:00
|
|
|
writable = exclude_ref_fields(record)
|
|
|
|
if id:
|
|
|
|
commands.append(LINK_TO(id))
|
|
|
|
commands.append(UPDATE(id, writable))
|
|
|
|
else:
|
|
|
|
commands.append(CREATE(writable))
|
2012-09-20 10:56:12 +00:00
|
|
|
|
2012-09-27 14:43:41 +00:00
|
|
|
return commands, warnings
|