コード例 #1
0
    def _str_to_boolean(self, cr, uid, model, column, value, context=None):
        # 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),
        ))
        if value.lower() in trues: return True, []

        # 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),
        ))
        if value.lower() in falses: return False, []

        return True, [
            orm.ImportWarning(
                _(u"Unknown value '%s' for boolean field '%%(field)s', assuming '%s'"
                  ) % (value, yes),
                {'moreinfo': _(u"Use '1' for yes and '0' for no")})
        ]
コード例 #2
0
    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 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
        :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)
        """
        if context is None: context = {}
        id = None
        warnings = []
        action = {
            'type': 'ir.actions.act_window',
            'target': 'new',
            'view_mode': 'tree,form',
            'view_type': 'form',
            'views': [(False, 'tree'), (False, 'form')],
            'help': _(u"See all possible values")
        }
        if subfield is None:
            action['res_model'] = column._obj
        elif subfield in ('id', '.id'):
            action['res_model'] = 'ir.model.data'
            action['domain'] = [('model', '=', column._obj)]

        RelatedModel = self.pool[column._obj]
        if subfield == '.id':
            field_type = _(u"database id")
            try:
                tentative_id = int(value)
            except ValueError:
                tentative_id = value
            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, {'moreinfo': action})
        elif subfield == 'id':
            field_type = _(u"external id")
            if '.' in value:
                module, xid = value.split('.', 1)
            else:
                module, xid = context.get('_import_current_module', ''), value
            ModelData = self.pool['ir.model.data']
            try:
                _model, id = ModelData.get_object_reference(
                    cr, uid, module, xid)
            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:
                    warnings.append(
                        orm.ImportWarning(
                            _(u"Found multiple matches for field '%%(field)s' (%d matches)"
                              ) % (len(ids))))
                id, _name = ids[0]
        else:
            raise Exception(_(u"Unknown sub-field '%s'") % subfield)

        if id is None:
            raise ValueError(
                _(u"No matching record found for %(field_type)s '%(value)s' in field '%%(field)s'"
                  ) % {
                      'field_type': field_type,
                      'value': value
                  }, {'moreinfo': action})
        return id, field_type, warnings