I want to create a task and assign it to a new project
for project in data:
project_tasks = project['tasks']
for task in project_tasks:
project_id = get_or_create_project(project['name'])
employee_id = get_or_create_employee(task['assigned_to'])
new_task = request.env['project.task'].sudo().with_context(check_move_validity=False).create({
'name': task['name'],
'description': task['desc'],
})
new_task.update({'user_id': [( 4, employee_id, 0 )]})
new_task.update({'project_id': [( 4, project_id, 0 )]})
def get_or_create_project(project_name):
p_exists = request.env['project.project'].search([('display_name', '=', project_name)])
if p_exists:
project_id = p_exists.id
else:
new_project = request.env['project.project'].sudo().with_context(check_move_validity=False).create({
'name': project_name,
})
project_id = new_project.id
return project_id
def get_or_create_employee(employee_name):
e_exists = request.env['hr.employee'].search([('name', '=', employee_name)])
if e_exists:
employee_id = e_exists.id
else:
new_employee = request.env['hr.employee'].sudo().with_context(check_move_validity=False).create({
'name': employee_name,
})
employee_id = new_employee.id
return employee_id
Thank you