from netforce.model import Model, fields, get_model class LoadNurse(Model): _name="clinic.schedule.load.nurse" _transient=True _fields={ "schedule_id": fields.Many2One("clinic.schedule","Schedule",on_delete="cascade"), 'categ_id': fields.Many2One("clinic.personal.categ","Category", domain=[['type','=','nurse']]), "lines": fields.One2Many("clinic.schedule.load.nurse.line",'load_id', 'Lines'), } def _get_schedule_id(self,context={}): schedule_id=context.get("refer_id") if not schedule_id: return None return int(schedule_id) def _get_lines(self,context={}): cycle_ids=get_model("clinic.cycle").search([]) cycle_id=None if cycle_ids: cycle_id=cycle_ids[0] lines=[] for nurse in get_model("clinic.personal").search_browse([['type','=','nurse']]): in_cycle_id=None if nurse.cycle_id.id: in_cycle_id=nurse.cycle_id.id lines.append({ 'nurse_id': nurse.id, 'cycle_id': in_cycle_id and in_cycle_id or cycle_id, }) return lines _defaults={ 'schedule_id': _get_schedule_id, 'lines': _get_lines, } def copy2schedule(self,ids,context): obj=self.browse(ids)[0] if not obj.schedule_id: raise Exception("Please save schedule before copy") schedule=get_model("clinic.schedule").browse(obj.schedule_id.id) vals={ 'lines': [], } cycles=set() for line in obj.lines: nurse=line.nurse_id cycle_id=line.cycle_id.id cycles.update({cycle_id}) vals['lines'].append(('create',{ 'cycle_id': cycle_id, 'nurse_id': nurse.id, })) for line in schedule.lines: if line.cycle_id.id in list(cycles): line.delete() schedule.write(vals) return { 'next': { 'name': 'clinic_schedule', 'mode': 'form', 'active_id': schedule.id, }, 'flash': 'Copy nurse to schedule succesfully' } def onchange_categ(self,context={}): data=context['data'] return data LoadNurse.register()