trytond-stock_location_comb.../location.py

93 lines
3.2 KiB
Python

# The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
from trytond.model import ModelSQL, ModelView, fields, DeactivableMixin
from trytond.pool import PoolMeta, Pool
from trytond.pyson import Eval
__all__ = ['Combined', 'LocationCombined', 'Location']
class Combined(DeactivableMixin, ModelView, ModelSQL):
"""Combined location"""
__name__ = 'stock.location.combined'
name = fields.Char('Name', required=True, translate=True,
states={'readonly': ~Eval('active')},
depends=['active'])
code = fields.Char('Code')
type = fields.Function(
fields.Selection([], 'Locations type'),
'on_change_with_type')
locations = fields.Many2Many('stock.location-stock.location.combined',
'combined', 'location', 'Locations',
states={'readonly': ~Eval('active')},
depends=['active'])
@classmethod
def __setup__(cls):
pool = Pool()
Location = pool.get('stock.location')
super(Combined, cls).__setup__()
cls._error_messages.update({
'location_type': 'Locations in Combined location "%s" must be of same type.'})
if not cls.type._field.selection:
cls.type._field.selection.extend(Location.type.selection)
cls.locations.domain.append(
('type', 'in', cls._locations_domain()))
@classmethod
def _locations_domain(cls):
return ['production']
@fields.depends('locations')
def on_change_with_type(self, name=None):
if self.locations:
return self.locations[0].type
return 'production'
def _get_locations(self):
return self.locations
@classmethod
def validate(cls, records):
super(Combined, cls).validate(records)
_types = {}
for record in records:
values = [l.type for l in record.locations]
_types.setdefault(record.id, set(values))
if len(_types[record.id]) > 1:
cls.raise_user_error('location_type', record.rec_name)
class LocationCombined(ModelView, ModelSQL):
"""Location - Combined location"""
__name__ = 'stock.location-stock.location.combined'
_table = 'stock_location_combined_rel'
combined = fields.Many2One('stock.location.combined', 'Combined',
ondelete='CASCADE')
location = fields.Many2One('stock.location', 'Location',
ondelete='RESTRICT')
class Location:
__name__ = 'stock.location'
__metaclass__ = PoolMeta
combined = fields.Many2Many('stock.location-stock.location.combined',
'location', 'combined', 'Combined locations')
@classmethod
def validate(cls, records):
pool = Pool()
Combined = pool.get('stock.location.combined')
super(Location, cls).validate(records)
for record in records:
if not record.combined:
continue
for _combined in record.combined:
if any(l.type != record.type for l in _combined.locations):
Combined.raise_user_error('location_type', _combined.rec_name)