# _*_ coding: utf-8 _*_
# Author: JasonWu( Jaronemo@msn.com)
from odoo import models, fields, api


class DemoWizard(models.TransientModel):
    _name = 'jt.teacher.batch.wiz'

    line_ids = fields.Many2many(
        'jt.teacher.batch.line', default=lambda self: self._default_line_ids())

    def _default_line_ids(self):
        recs = self.env['jt.teacher.batch'].search([])
        # same with users
        teachers = self.env['jt.teacher.batch.line'].search([]).mapped('teacher_id')
        return [
            (0, 0, {
                'name': "{}'s task on {}".format(usr.name, rec.name),
                'demo_id': rec.id,
                'user_id': usr.id,
                'value': 0,
            })
            # if the project doesn't have a task for the user, create a new one
            if not rec.line_ids.filtered(lambda x: x.teacher_id == usr) else
            # otherwise, return the task
            (4, rec.line_ids.filtered(lambda x: x.teacher_id == usr)[0].id)
            for rec in recs
            for usr in teachers
        ]
