Search code examples
pythonxmlodooodoo-10

odoo allocation request by employee tag validation error?


Hello all I'am working on leaves conditions module in version 10 , I added a validation that raise when no joining date assigned to the employee , it;s working well,

But it's also raise while requesting allocation by employee tag , although I am tried a tag with only one employee in this tag and I am assigned a joining date for him Here is the code

@api.constrains('state', 'date_from', 'holiday_status_id',)
def _check_hire_date(self):

     from_dt = fields.Datetime.from_string(self.date_from)
     to_dt = fields.Datetime.from_string(self.date_to)
     if self.employee_id.joining_date:
         jo_dt = fields.Datetime.from_string(self.employee_id.joining_date)
     else:
         raise ValidationError("you must define joining date")

What should I do to pass all the validations that i'll do while allocation request and make it work just only while leaves requesting not allocations


Solution

  • The field that you know if this is a leave or an allocation is type

         type = fields.Selection([
            ('remove', 'Leave Request'),
            ('add', 'Allocation Request')
        ],....)
    

    So before starting validation check if it's not an allocation request :

          @api.one # because you didn't loop over self in your code
          @api.constrains('state', 'date_from', 'holiday_status_id',)
          def _check_hire_date(self):
                  if self.type == 'add': return  # skip allocation requests
                  # rest of your code goes