Example #1
0
    def set_default(self, model, field_name, value, for_all_users=True, company_id=False, condition=False):
        """Defines a default value for the given model and field_name. Any previous
           default for the same scope (model, field_name, value, for_all_users, company_id, condition)
           will be replaced and lost in the process.

           Defaults can be later retrieved via :meth:`~.get_defaults`, which will return
           the highest priority default for any given field. Defaults that are more specific
           have a higher priority, in the following order (highest to lowest):

               * specific to user and company
               * specific to user only
               * specific to company only
               * global to everyone

           :param string model: model name
           :param string field_name: field name to which the default applies
           :param value: the default field value to set
           :type value: any serializable Python value
           :param bool for_all_users: whether the default should apply to everybody or only
                                      the user calling the method
           :param int company_id: optional ID of the company to which the default should
                                  apply. If omitted, the default will be global. If True
                                  is passed, the current user's company will be used.
           :param string condition: optional condition specification that can be used to
                                    restrict the applicability of the default values
                                    (e.g. based on another field's value). This is an
                                    opaque string as far as the API is concerned, but client
                                    stacks typically use single-field conditions in the
                                    form ``'key=stringified_value'``.
                                    (Currently, the condition is trimmed to 200 characters,
                                    so values that share the same first 200 characters always
                                    match)
           :return: the newly created ir.values entry
        """
        if isinstance(value, unicode):
            value = value.encode('utf8')
        if company_id is True:
            # should be company-specific, need to get company id
            company_id = self.env.user.company_id.id

        # remove existing defaults for the same scope
        search_criteria = [
            ('key', '=', 'default'),
            ('key2', '=', condition and condition[:200]),
            ('model', '=', model),
            ('name', '=', field_name),
            ('user_id', '=', False if for_all_users else self._uid),
            ('company_id', '=', company_id)
        ]
        self.search(search_criteria).unlink()

        return self.create({
            'name': field_name,
            'value': pickle.dumps(value),
            'model': model,
            'key': 'default',
            'key2': condition and condition[:200],
            'user_id': False if for_all_users else self._uid,
            'company_id': company_id,
        })
Example #2
0
    def set_default(self, model, field_name, value, for_all_users=True, company_id=False, condition=False):
        """Defines a default value for the given model and field_name. Any previous
           default for the same scope (model, field_name, value, for_all_users, company_id, condition)
           will be replaced and lost in the process.

           Defaults can be later retrieved via :meth:`~.get_defaults`, which will return
           the highest priority default for any given field. Defaults that are more specific
           have a higher priority, in the following order (highest to lowest):

               * specific to user and company
               * specific to user only
               * specific to company only
               * global to everyone

           :param string model: model name
           :param string field_name: field name to which the default applies
           :param value: the default field value to set
           :type value: any serializable Python value
           :param bool for_all_users: whether the default should apply to everybody or only
                                      the user calling the method
           :param int company_id: optional ID of the company to which the default should
                                  apply. If omitted, the default will be global. If True
                                  is passed, the current user's company will be used.
           :param string condition: optional condition specification that can be used to
                                    restrict the applicability of the default values
                                    (e.g. based on another field's value). This is an
                                    opaque string as far as the API is concerned, but client
                                    stacks typically use single-field conditions in the
                                    form ``'key=stringified_value'``.
                                    (Currently, the condition is trimmed to 200 characters,
                                    so values that share the same first 200 characters always
                                    match)
           :return: the newly created ir.values entry
        """
        if isinstance(value, unicode):
            value = value.encode('utf8')
        if company_id is True:
            # should be company-specific, need to get company id
            company_id = self.env.user.company_id.id

        # remove existing defaults for the same scope
        search_criteria = [
            ('key', '=', 'default'),
            ('key2', '=', condition and condition[:200]),
            ('model', '=', model),
            ('name', '=', field_name),
            ('user_id', '=', False if for_all_users else self._uid),
            ('company_id', '=', company_id)
        ]
        self.search(search_criteria).unlink()

        return self.create({
            'name': field_name,
            'value': pickle.dumps(value),
            'model': model,
            'key': 'default',
            'key2': condition and condition[:200],
            'user_id': False if for_all_users else self._uid,
            'company_id': company_id,
        })
Example #3
0
 def _value_pickle(self):
     context = dict(self._context)
     context.pop(self.CONCURRENCY_CHECK_FIELD, None)
     for record in self.with_context(context):
         value = record.value_unpickle
         if record.key == 'default':
             value = pickle.dumps(value)
         record.value = value
Example #4
0
 def _value_pickle(self):
     context = dict(self._context)
     context.pop(self.CONCURRENCY_CHECK_FIELD, None)
     for record in self.with_context(context):
         value = record.value_unpickle
         if record.key == 'default':
             value = pickle.dumps(value)
         record.value = value
Example #5
0
    def refresh_cache(self):
        products = self.env["product.product"].search(self.get_product_domain())
        prod_ctx = products.with_context(
            pricelist=self.config_id.pricelist_id.id, display_default_code=False, lang=self.compute_user_id.lang
        )
        prod_ctx = prod_ctx.sudo(self.compute_user_id.id)
        res = prod_ctx.read(self.get_product_fields())
        datas = {"cache": base64.encodestring(cPickle.dumps(res))}

        self.write(datas)
Example #6
0
 def _dj_xmlid_to_values(self, vals):
     """Convert xmlid to db values."""
     if (self.env.context.get('xmlid_value_reference', False)
             and vals.get('value')):
         if vals.get('key') == 'action':
             vals['value'] = xmlid_to_property(self.env, vals['value'])
         elif vals.get('key') == 'default':
             field = self._get_relation_field(vals)
             if field:
                 vals['value'] = \
                     pickle.dumps(self.env.ref(vals['value']).id)
Example #7
0
    def refresh_cache(self):
        partners = self.env['res.partner'].search(self.get_partner_domain())
        prod_ctx = partners.with_context(
            pricelist=self.config_id.pricelist_id.id,
            display_default_code=False,
            lang=self.compute_user_id.lang)
        prod_ctx = prod_ctx.sudo(self.compute_user_id.id)
        res = prod_ctx.read(self.get_partner_fields())
        datas = {
            'cache': base64.encodestring(cPickle.dumps(res)),
        }

        self.write(datas)
Example #8
0
 def _value_pickle(self):
     context = dict(self._context)
     context.pop(self.CONCURRENCY_CHECK_FIELD, None)
     for record in self.with_context(context):
         value = record.value_unpickle
         # Only char-like fields should be written directly. Other types should be converted to
         # their appropriate type first.
         if record.model in self.env and record.name in self.env[record.model]._fields:
             field = self.env[record.model]._fields[record.name]
             if field.type not in ['char', 'text', 'html', 'selection']:
                 value = literal_eval(value)
         if record.key == 'default':
             value = pickle.dumps(value)
         record.value = value
Example #9
0
 def _value_pickle(self):
     context = dict(self._context)
     context.pop(self.CONCURRENCY_CHECK_FIELD, None)
     for record in self.with_context(context):
         value = record.value_unpickle
         # Only char-like fields should be written directly. Other types should be converted to
         # their appropriate type first.
         if record.model in self.env and record.name in self.env[record.model]._fields:
             field = self.env[record.model]._fields[record.name]
             if field.type not in ['char', 'text', 'html', 'selection']:
                 value = literal_eval(value)
         if record.key == 'default':
             value = pickle.dumps(value)
         record.value = value
Example #10
0
    def refresh_cache(self):
        products = self.env['product.product'].search(
            self.get_product_domain())
        prod_ctx = products.with_context(
            pricelist=self.config_id.pricelist_id.id,
            display_default_code=False,
            lang=self.compute_user_id.lang)
        prod_ctx = prod_ctx.sudo(self.compute_user_id.id)
        res = prod_ctx.read(self.get_product_fields())
        datas = {
            'cache': cPickle.dumps(res),
        }

        self.write(datas)