<commit_before># -*- coding: utf-8 -*-
# Copyright 2015-2017 <NAME> - AvanzOSC
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

from openerp import api, fields, models


class ProductCategory(models.Model):
    _inherit = 'product.category'

    sequence_id = fields.Many2one(
        comodel_name='ir.sequence', string='Sequence')


class ProductProduct(models.Model):
    _inherit = 'product.product'

    @api.model
    def create(self, values):
        if not values.get('default_code'):
            categ = self.env['product.category'].browse(values.get('categ_id'))
            values['default_code'] = (
                categ.sequence_id.next_by_id(categ.sequence_id.id))
        return super(ProductProduct, self).create(values)

    @api.multi
    def write(self, values):
        for record in self:
            if 'categ_id' in values and not values.get('default_code',
                                                       record.default_code):
                categ = self.env['product.category'].browse(
                    values.get('categ_id'))
                values['default_code'] = categ.sequence_id.next_by_id(
                    categ.sequence_id.id)
            super(ProductProduct, record).write(values)
        return True

    @api.multi
    def rewrite_default_code(self):
        for record in self.filtered('categ_id.sequence_id'):
            record.write({
                'categ_id': record.categ_id.id,
                'default_code': False
            })
        return True


class ProductTemplate(models.Model):
    _inherit = 'product.template'

    @api.model
    def create(self, values):
        if not values.get('default_code', False):
            categ = self.env['product.category'].browse(values.get('categ_id'))
            values['default_code'] = (
                categ.sequence_id.next_by_id(categ.sequence_id.id))
        return super(ProductTemplate, self).create(values)

    @api.multi
    def write(self, values):
        for record in self:
            if 'categ_id' in values and not values.get('default_code',
                                                       record.default_code):
                categ = self.env['product.category'].browse(
                    values.get('categ_id'))
                values['default_code'] = categ.sequence_id.next_by_id(
                    categ.sequence_id.id)
            super(ProductTemplate, record).write(values)
        return True

    @api.multi
    def rewrite_default_code(self):
        for record in self.filtered('categ_id.sequence_id'):
            record.write({
                'categ_id': record.categ_id.id,
                'default_code': False
            })
        return True
<commit_msg>[IMP] product_sequence_by_category: avoid increasing twice the counter (#574)<commit_after># -*- coding: utf-8 -*-
# Copyright 2015-2017 <NAME> - AvanzOSC
# License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html

from openerp import api, fields, models


class ProductCategory(models.Model):
    _inherit = 'product.category'

    sequence_id = fields.Many2one(
        comodel_name='ir.sequence', string='Sequence')


class ProductProduct(models.Model):
    _inherit = 'product.product'

    @api.model
    def create(self, values):
        if not values.get('default_code'):
            categ = self.env['product.category'].browse(values.get('categ_id'))
            if values.get('product_tmpl_id', False) and not categ:
                tmpl = self.env['product.template'].browse(values.get(
                    'product_tmpl_id'))
                categ = tmpl.categ_id
            values['default_code'] = (
                categ.sequence_id.next_by_id(categ.sequence_id.id))
        return super(ProductProduct, self).create(values)

    @api.multi
    def write(self, values):
        for record in self:
            if 'categ_id' in values and not values.get('default_code',
                                                       record.default_code):
                categ = self.env['product.category'].browse(
                    values.get('categ_id'))
                values['default_code'] = categ.sequence_id.next_by_id(
                    categ.sequence_id.id)
            super(ProductProduct, record).write(values)
        return True

    @api.multi
    def rewrite_default_code(self):
        for record in self.filtered('categ_id.sequence_id'):
            record.write({
                'categ_id': record.categ_id.id,
                'default_code': False
            })
        return True


class ProductTemplate(models.Model):
    _inherit = 'product.template'

    @api.multi
    def write(self, values):
        for record in self:
            super(ProductTemplate, record).write(values)
            if 'categ_id' in values and not values.get('default_code',
                                                       record.default_code):
                record.product_variant_ids.rewrite_default_code()
        return True

    @api.multi
    def rewrite_default_code(self):
        for record in self.filtered('categ_id.sequence_id'):
            record.write({
                'categ_id': record.categ_id.id,
                'default_code': False
            })
        return True

{'fraction_non_alphanumeric': 0.11096307575180815, 'fraction_numerical': 0.005519604111153407, 'mean_word_length': 1.9588963963963963, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6605431', 'n_tokens_mistral': 1518, 'n_tokens_neox': 1452, 'n_words': 282}