from datetime import datetime, timedelta import time from netforce.model import Model, fields class Period(Model): _name="clinic.period" _string="Period" _fields={ "name": fields.Char("Name",required=True,search=True), 'date_start': fields.Date("Start Date",required=True), 'duration': fields.Integer("Day in Period",required=True), 'nmonth': fields.Integer("Number of Month",required=True), 'lines': fields.One2Many("clinic.period.line","period_id", "Lines"), } def _get_start(self,context={}): year=time.strftime("%Y") return '%s-01-01'%year _defaults={ 'name': lambda *a: time.strftime("%Y"), 'date_start': _get_start, 'duration': 30, 'nmonth': 12, } def gen_period(self,ids,context={}): obj=self.browse(ids)[0] for line in obj.lines: line.delete() fmt="%Y-%m-%d" start=obj.date_start lines=[] for i in range(obj.nmonth): start=datetime.strptime(start,fmt) stop=start+timedelta(days=obj.duration) lines.append(('create',{ 'date_start': start.strftime(fmt), 'date_stop': stop.strftime(fmt), })) print(i+1, ' ', start.strftime(fmt), ' ', stop.strftime(fmt)) stop=stop+timedelta(days=1) start=stop.strftime(fmt) obj.write({ 'lines': lines, }) Period.register()