1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from trytond.model import ModelView, Workflow, fields
from trytond.pool import PoolMeta
from trytond.pyson import Eval
from trytond.tools import grouped_slice, reduce_ids
from trytond.transaction import Transaction
class RevisionMixin:
__slots__ = ()
revision = fields.Integer(
"Revision", required=True, readonly=True,
states={
'invisible': Eval('revision', 0) == 0,
})
@classmethod
def default_revision(cls):
return 0
@property
def full_number(self):
number = super().full_number
if number is not None and self.revision:
number += '/%d' % self.revision
return number
@classmethod
def copy(cls, records, default=None):
default = default.copy() if default is not None else {}
default.setdefault('revision', cls.default_revision())
return super().copy(records, default=default)
@classmethod
@ModelView.button
@Workflow.transition('draft')
def draft(cls, records):
cursor = Transaction().connection.cursor()
table = cls.__table__()
# Use SQL and before super to avoid two history entries
for sub_records in grouped_slice(records):
cursor.execute(*table.update(
[table.revision],
[table.revision + 1],
where=reduce_ids(table.id, sub_records)))
super().draft(records)
class Sale(RevisionMixin, metaclass=PoolMeta):
__name__ = 'sale.sale'
_history = True
class Line(metaclass=PoolMeta):
__name__ = 'sale.line'
_history = True
class LineTax(metaclass=PoolMeta):
__name__ = 'sale.line-account.tax'
_history = True
class Subscription(RevisionMixin, metaclass=PoolMeta):
__name__ = 'sale.subscription'
_history = True
class SubscriptionLine(metaclass=PoolMeta):
__name__ = 'sale.subscription.line'
_history = True
|