|
@ -20,13 +20,16 @@ |
|
|
# |
|
|
# |
|
|
############################################################################### |
|
|
############################################################################### |
|
|
import logging |
|
|
import logging |
|
|
|
|
|
from logging import Logger |
|
|
|
|
|
|
|
|
import requests |
|
|
import requests |
|
|
from odoo import fields, models, _ |
|
|
from odoo import fields, models, _ |
|
|
from odoo.exceptions import ValidationError |
|
|
from odoo.exceptions import ValidationError |
|
|
|
|
|
|
|
|
_logger = logging.getLogger(__name__) |
|
|
_logger: Logger = logging.getLogger(__name__) |
|
|
try: |
|
|
try: |
|
|
import asana |
|
|
import asana |
|
|
|
|
|
from asana.rest import ApiException |
|
|
except ImportError: |
|
|
except ImportError: |
|
|
_logger.debug('Cannot `import asana`.') |
|
|
_logger.debug('Cannot `import asana`.') |
|
|
|
|
|
|
|
@ -56,9 +59,13 @@ def action_import_project_stages(project_gid, api_client): |
|
|
asana to odoo |
|
|
asana to odoo |
|
|
""" |
|
|
""" |
|
|
api_instance = asana.SectionsApi(api_client) |
|
|
api_instance = asana.SectionsApi(api_client) |
|
|
section_response = api_instance.get_sections_for_project( |
|
|
opts = {} |
|
|
project_gid) |
|
|
try: |
|
|
|
|
|
section_response = list(api_instance.get_sections_for_project( |
|
|
|
|
|
project_gid, opts)) |
|
|
return section_response |
|
|
return section_response |
|
|
|
|
|
except ApiException as exc: |
|
|
|
|
|
_logger.debug(f"Error while trying to import section {exc}") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ResConfigSettings(models.TransientModel): |
|
|
class ResConfigSettings(models.TransientModel): |
|
@ -101,49 +108,73 @@ class ResConfigSettings(models.TransientModel): |
|
|
""" |
|
|
""" |
|
|
Method action_import_projects to import the project from asana to odoo |
|
|
Method action_import_projects to import the project from asana to odoo |
|
|
""" |
|
|
""" |
|
|
|
|
|
if not self.workspace_gid or not self.app_token: |
|
|
|
|
|
raise ValidationError(_("Please add App Token and Workspace gid")) |
|
|
configuration = asana.Configuration() |
|
|
configuration = asana.Configuration() |
|
|
configuration.access_token = self.app_token |
|
|
configuration.access_token = self.app_token |
|
|
api_client = asana.ApiClient(configuration) |
|
|
api_client = asana.ApiClient(configuration) |
|
|
project_instance = asana.ProjectsApi(api_client) |
|
|
project_instance = asana.ProjectsApi(api_client) |
|
|
workspace = self.workspace_gid |
|
|
opts = {'workspace': self.workspace_gid} |
|
|
try: |
|
|
try: |
|
|
project_response = project_instance.get_projects( |
|
|
project_response = list(project_instance.get_projects(opts)) |
|
|
workspace=workspace) |
|
|
split_data = [project_response[project:project + 5] for project in |
|
|
for project in project_response.data: |
|
|
range(0, len(project_response), 10)] |
|
|
asana_gid = project.gid |
|
|
for project in split_data: |
|
|
|
|
|
delay = self.with_delay(priority=1, eta=5) |
|
|
|
|
|
delay.create_project(items=project, app_token=self.app_token) |
|
|
|
|
|
except ApiException as exc: |
|
|
|
|
|
raise ValidationError( |
|
|
|
|
|
_('Please check the workspace ID or the app token')) from exc |
|
|
|
|
|
|
|
|
|
|
|
def create_project(self, items, app_token): |
|
|
|
|
|
"""Method create_project to create the project from Asana to Odoo""" |
|
|
|
|
|
configuration = asana.Configuration() |
|
|
|
|
|
configuration.access_token = app_token |
|
|
|
|
|
api_client = asana.ApiClient(configuration) |
|
|
|
|
|
for project in items: |
|
|
|
|
|
asana_gid = project['gid'] |
|
|
existing_project = self.env['project.project'].search( |
|
|
existing_project = self.env['project.project'].search( |
|
|
[('asana_gid', '=', asana_gid)]) |
|
|
[('asana_gid', '=', asana_gid)]) |
|
|
if not existing_project: |
|
|
if not existing_project: |
|
|
section_data = action_import_project_stages( |
|
|
section_data = action_import_project_stages( |
|
|
project_gid=asana_gid, |
|
|
project_gid=asana_gid, |
|
|
api_client=api_client) |
|
|
api_client=api_client) |
|
|
type_ids = [ |
|
|
type_ids = [] |
|
|
(0, 0, {'name': section.name, 'asana_gid': section.gid}) |
|
|
for section in section_data: |
|
|
for section in section_data.data] |
|
|
existing_stage = self.env['project.task.type'].search( |
|
|
|
|
|
[('asana_gid', '=', section['gid'])], limit=1) |
|
|
|
|
|
if existing_stage: |
|
|
|
|
|
type_ids.append((4, existing_stage.id)) |
|
|
|
|
|
else: |
|
|
|
|
|
new_stage = self.env['project.task.type'].create({ |
|
|
|
|
|
'name': section['name'], |
|
|
|
|
|
'asana_gid': section['gid'], |
|
|
|
|
|
}) |
|
|
|
|
|
type_ids.append((0, 0, {'name': new_stage.name, |
|
|
|
|
|
'asana_gid': new_stage.asana_gid})) |
|
|
new_project = self.env['project.project'].create({ |
|
|
new_project = self.env['project.project'].create({ |
|
|
'name': project.name, |
|
|
'name': project['name'], |
|
|
'asana_gid': asana_gid, |
|
|
'asana_gid': asana_gid, |
|
|
'type_ids': type_ids |
|
|
'type_ids': type_ids |
|
|
}) |
|
|
}) |
|
|
self.action_import_tasks( |
|
|
self.action_import_tasks( |
|
|
api_client=api_client, section_data=section_data, |
|
|
api_client=api_client, section_data=section_data, |
|
|
project_id=new_project.id) |
|
|
project_id=new_project.id) |
|
|
except Exception as exc: |
|
|
|
|
|
raise ValidationError( |
|
|
|
|
|
_('Please check the workspace ID or the app token')) from exc |
|
|
|
|
|
|
|
|
|
|
|
def action_import_tasks(self, api_client, section_data, project_id): |
|
|
def action_import_tasks(self, api_client, section_data, project_id): |
|
|
""" |
|
|
""" |
|
|
Method action_import_tasks to import tasks from the asana to odoo |
|
|
Method action_import_tasks to import tasks from the asana to odoo |
|
|
""" |
|
|
""" |
|
|
api_instance = asana.TasksApi(api_client) |
|
|
api_instance = asana.TasksApi(api_client) |
|
|
for section in section_data.data: |
|
|
for section in section_data: |
|
|
task_response = api_instance.get_tasks_for_section(section.gid) |
|
|
opts = {} |
|
|
for task in task_response.data: |
|
|
task_response = list( |
|
|
|
|
|
api_instance.get_tasks_for_section(section['gid'], opts)) |
|
|
|
|
|
for task in task_response: |
|
|
self.env['project.task'].create({ |
|
|
self.env['project.task'].create({ |
|
|
'name': task.name, |
|
|
'name': task['name'], |
|
|
'asana_gid': task.gid, |
|
|
'asana_gid': task['gid'], |
|
|
'stage_id': self.env['project.task.type'].search( |
|
|
'stage_id': self.env['project.task.type'].search( |
|
|
[('asana_gid', '=', section.gid)]).id, |
|
|
[('asana_gid', '=', section['gid'])], limit=1).id, |
|
|
'project_id': project_id |
|
|
'project_id': project_id |
|
|
}) |
|
|
}) |
|
|