from datetime import datetime from netforce.model import Model, fields class PeriodLine(Model): _name="clinic.period.line" _string="Period Line" def _get_total(self,ids,context={}): res={} fmt='%Y-%m-%d' for obj in self.browse(ids): start=datetime.strptime(obj.date_start,fmt) stop=datetime.strptime(obj.date_stop,fmt) days=(stop-start).days res[obj.id]=days return res def _get_name(self,ids,context={}): res={} for obj in self.browse(ids): res[obj.id]={ 'name': '%s - %s'%(obj.date_start, obj.date_stop), } return res _fields={ 'name': fields.Char("Name",function="_get_name", function_multi=True, store=True), "period_id": fields.Many2One("clinic.period", "Period"), 'date_start': fields.Date("Date Start"), 'date_stop': fields.Date("Date End"), 'day_total': fields.Integer("Duration (Day)",function="_get_total"), 'state': fields.Selection([['open','Open'],['close','Close']],"State"), } _defaults={ 'state': 'open', } def delete(self,ids,context={}): for obj in self.browse(ids): if obj.state=='close': raise Exception("Can delete object which state is closed!") super().delete(ids,context) def create(self,vals,**kw): id=super().create(vals,**kw) self.function_store([id]) return id def write(self,ids,vals,**kw): super().write(ids,vals,**kw) self.function_store(ids) def do_close(self,ids,context={}): obj=self.browse(ids)[0] obj.write({ 'state': 'close', }) return { 'next': { 'name': 'clinic_period_line', 'mode': 'form', 'active_id': obj.id, }, } def do_open(self,ids,context={}): obj=self.browse(ids)[0] obj.write({ 'state': 'open', }) return { 'next': { 'name': 'clinic_period_line', 'mode': 'form', 'active_id': obj.id, }, } PeriodLine.register()