Wednesday 16 December 2015

Constriants In Odoo

SQL Constraints


_sql_constraints = [
('name_uniq', 'unique(name, company_id)', 'Order Reference must be unique per Company!'),
]


V7 Constriants


def _check_duration(self, cr, uid, ids, context=None):
obj_fy = self.browse(cr, uid, ids[0], context=context)
    if obj_fy.date_stop < obj_fy.date_start:
        return False
    return True

_constraints = [
(_check_duration, 'Error!\nThe start date of a fiscal year must precede its end date.', ['date_start','date_stop'])
    ]


V8 Constriants

Example1


@api.multi
@api.one
@api.constrains('stage_id','user_id')
def _check_constriant(self):
stage_id = self.stage_id
stage_type = stage_id.stage_type
stage_ids = self.env['od.project.task.stages'].search([('stage_type', '=', 'progress')])
project_task_ids = self.env['od.project.task'].search([('user_id', '=', self.user_id.id),('stage_id', 'in', [x.id for x in stage_ids])])
if len(project_task_ids) >1:
            raise Warning(_("Job already assigned, %s"))




@api.multi
@api.one
@api.constrains('stage_id','user_id')
def _check_constriant(self):
stage_id = self.stage_id
stage_type = stage_id.stage_type
stage_ids = self.env['od.project.task.stages'].search([('stage_type', '=', 'progress')])
project_task_ids = self.env['od.project.task'].search([('user_id', '=', self.user_id.id),('stage_id', 'in', [x.id for x in stage_ids])])
if len(project_task_ids) >1:
raise Warning(_("Job already assigned, %s"))


Example2

@api.multi
@api.one
@api.constrains('date_from','date_to','fiscalyear_id')
def _check_month(self):
fiscalyear_id = self.fiscalyear_id.id
fiscal_year_obj = self.env['account.fiscalyear'].browse(fiscalyear_id)
date_start = fiscal_year_obj.date_start
date_stop = fiscal_year_obj.date_stop
date_from = self.date_from
date_to = self.date_to
if date_start<= date_from <=date_stop and date_start<= date_to <=date_stop:
print "date_start"
else:
raise Warning(_("Date From or Date To Is Not In The Fiscal Year,Please Enter Proper Dates %s") % self.date_to,self.date_from)
date_from =str(date_from)
date_to = str(date_to)
date_from = datetime.datetime.strptime(date_from, '%Y-%m-%d')
date_to = datetime.datetime.strptime(date_to, "%Y-%m-%d")
diff = (12 * date_to.year + date_to.month) - (12 * date_from.year + date_from.month)
if diff == 0:
raise Warning(_("If You Want To Check Current Month Target,Please Put Date To As First Day Of Next Month %s") % self.date_to)
elif diff < 1:
            raise Warning(_("Invalid Date,Date To is Less Than Date From, %s") % self.date_to)  



No comments:

Post a Comment