@ -47,10 +47,11 @@ class Task(models.Model):
revert_info = fields . Text ( copy = False )
revert_info = fields . Text ( copy = False )
working_mode = fields . Selection ( related = ' worker_id.working_mode ' )
working_mode = fields . Selection ( related = ' worker_id.working_mode ' )
@api.constrains ( ' is_regular ' , ' is_compensation ' , ' worker_id ' )
def _check_compensation ( self ) :
for task in self :
if task . working_mode == ' regular ' :
def _compensation_validation ( self , task ) :
"""
Raise a validation error if the fields is_regular and
is_compensation are not properly set .
"""
if ( task . is_regular == task . is_compensation
if ( task . is_regular == task . is_compensation
or not ( task . is_regular or task . is_compensation ) ) :
or not ( task . is_regular or task . is_compensation ) ) :
raise ValidationError (
raise ValidationError (
@ -58,12 +59,29 @@ class Task(models.Model):
" Compensation Shift. "
" Compensation Shift. "
)
)
@api.onchange ( ' worker_id ' )
def _onchange_worker_id ( self ) :
@api.constrains ( ' is_regular ' , ' is_compensation ' )
def _check_compensation ( self ) :
for task in self :
if task . working_mode == ' regular ' :
self . _compensation_validation ( task )
@api.constrains ( ' worker_id ' )
def _check_worker_id ( self ) :
"""
When worker_id changes we need to check whether is_regular
and is_compensation are set correctly .
When worker_id is set to a worker that doesn ' t need field
is_regular and is_compensation , these two fields are set to
False .
"""
for task in self :
for task in self :
if task . working_mode != ' regular ' :
task . is_regular = False
task . is_compensation = False
if task . working_mode == ' regular ' :
self . _compensation_validation ( task )
else :
task . write ( {
' is_regular ' : False ,
' is_compensation ' : False ,
} )
def message_auto_subscribe ( self , updated_fields , values = None ) :
def message_auto_subscribe ( self , updated_fields , values = None ) :
self . _add_follower ( values )
self . _add_follower ( values )
@ -97,11 +115,11 @@ class Task(models.Model):
date_domain . append ( ( ' end_time ' , ' <= ' , end_date ) )
date_domain . append ( ( ' end_time ' , ' <= ' , end_date ) )
to_unsubscribe = self . search ( [ ( ' worker_id ' , ' in ' , worker_ids ) ] + date_domain )
to_unsubscribe = self . search ( [ ( ' worker_id ' , ' in ' , worker_ids ) ] + date_domain )
to_unsubscribe . write ( { ' worker_id ' : False , ' is_regular ' : False } )
to_unsubscribe . write ( { ' worker_id ' : False } )
# What about replacement ?
# What about replacement ?
# Remove worker, replaced_id and regular
# Remove worker, replaced_id and regular
to_unsubscribe_replace = self . search ( [ ( ' replaced_id ' , ' in ' , worker_ids ) ] + date_domain )
to_unsubscribe_replace = self . search ( [ ( ' replaced_id ' , ' in ' , worker_ids ) ] + date_domain )
to_unsubscribe_replace . write ( { ' worker_id ' : False , ' is_regular ' : False , ' replaced_id' : False } )
to_unsubscribe_replace . write ( { ' worker_id ' : False , ' replaced_id ' : False } )
# If worker is Super cooperator, remove it from planning
# If worker is Super cooperator, remove it from planning
super_coop_ids = self . env [ ' res.users ' ] . search (
super_coop_ids = self . env [ ' res.users ' ] . search (
@ -110,8 +128,7 @@ class Task(models.Model):
if super_coop_ids :
if super_coop_ids :
to_unsubscribe_super_coop = self . search (
to_unsubscribe_super_coop = self . search (
[ ( ' super_coop_id ' , ' in ' , super_coop_ids ) ] + date_domain )
[ ( ' super_coop_id ' , ' in ' , super_coop_ids ) ] + date_domain )
to_unsubscribe_super_coop . write ( { ' super_coop_id ' : False ,
' is_regular ' : False } )
to_unsubscribe_super_coop . write ( { ' super_coop_id ' : False } )
@api.multi
@api.multi
def write ( self , vals ) :
def write ( self , vals ) :