diff --git a/apps/fyle/signals.py b/apps/fyle/signals.py index 71d6f365..fa86b367 100644 --- a/apps/fyle/signals.py +++ b/apps/fyle/signals.py @@ -10,8 +10,8 @@ from apps.workspaces.models import FyleCredential from apps.fyle.models import DependentFieldSetting from apps.sage300.dependent_fields import create_dependent_custom_field_in_fyle -from apps.mappings.imports.schedules import schedule_or_delete_dependent_field_tasks - +from apps.mappings.imports.schedules import schedule_or_delete_fyle_import_tasks +from apps.workspaces.models import ImportSetting logger = logging.getLogger(__name__) logger.level = logging.INFO @@ -60,4 +60,8 @@ def run_post_save_dependent_field_settings_triggers(sender, instance: DependentF :param instance: Row instance of Sender Class :return: None """ - schedule_or_delete_dependent_field_tasks(instance.workspace_id) + import_settings_instance = ImportSetting.objects.filter(workspace_id=instance.workspace.id).first() + + schedule_or_delete_fyle_import_tasks( + import_settings=import_settings_instance + ) diff --git a/apps/mappings/imports/queues.py b/apps/mappings/imports/queues.py index acadcb6d..232cc356 100644 --- a/apps/mappings/imports/queues.py +++ b/apps/mappings/imports/queues.py @@ -1,6 +1,7 @@ from django_q.tasks import Chain from fyle_accounting_mappings.models import MappingSetting from apps.workspaces.models import ImportSetting +from apps.fyle.models import DependentFieldSetting def chain_import_fields_to_fyle(workspace_id): @@ -11,6 +12,9 @@ def chain_import_fields_to_fyle(workspace_id): mapping_settings = MappingSetting.objects.filter(workspace_id=workspace_id, import_to_fyle=True) custom_field_mapping_settings = MappingSetting.objects.filter(workspace_id=workspace_id, is_custom=True, import_to_fyle=True) import_settings = ImportSetting.objects.get(workspace_id=workspace_id) + dependent_field_settings = DependentFieldSetting.objects.filter(workspace_id=workspace_id, is_import_enabled=True).first() + project_mapping = MappingSetting.objects.filter(workspace_id=workspace_id, source_field='PROJECT', import_to_fyle=True).first() + chain = Chain() if import_settings.import_categories: @@ -47,5 +51,11 @@ def chain_import_fields_to_fyle(workspace_id): True ) + if project_mapping and dependent_field_settings: + chain.append( + 'apps.mappings.imports.tasks.auto_import_and_map_fyle_fields', + workspace_id + ) + if chain.length() > 0: chain.run() diff --git a/apps/mappings/imports/schedules.py b/apps/mappings/imports/schedules.py index 2de4bb55..698f13d6 100644 --- a/apps/mappings/imports/schedules.py +++ b/apps/mappings/imports/schedules.py @@ -1,41 +1,9 @@ from datetime import datetime from django_q.models import Schedule from fyle_accounting_mappings.models import MappingSetting - -from apps.fyle.models import DependentFieldSetting from apps.workspaces.models import ImportSetting -def schedule_or_delete_dependent_field_tasks(workspace_id: int): - """ - :param configuration: Workspace Configuration Instance - :return: None - """ - project_mapping = MappingSetting.objects.filter( - source_field='PROJECT', - workspace_id=workspace_id, - import_to_fyle=True - ).first() - dependent_fields = DependentFieldSetting.objects.filter(workspace_id=workspace_id, is_import_enabled=True).first() - - if project_mapping and dependent_fields: - start_datetime = datetime.now() - Schedule.objects.update_or_create( - func='apps.mappings.imports.tasks.auto_import_and_map_fyle_fields', - args='{}'.format(workspace_id), - defaults={ - 'schedule_type': Schedule.MINUTES, - 'minutes': 24 * 60, - 'next_run': start_datetime - } - ) - elif not (project_mapping and dependent_fields): - Schedule.objects.filter( - func='apps.mappings.imports.tasks.auto_import_and_map_fyle_fields', - args='{}'.format(workspace_id) - ).delete() - - def schedule_or_delete_fyle_import_tasks(import_settings: ImportSetting, mapping_setting_instance: MappingSetting = None): """ Schedule or delete Fyle import tasks based on the import settingss. @@ -78,6 +46,3 @@ def schedule_or_delete_fyle_import_tasks(import_settings: ImportSetting, mapping func='apps.mappings.imports.queues.chain_import_fields_to_fyle', args='{}'.format(import_settings.workspace_id) ).delete() - - # Schedule or delete dependent field tasks - schedule_or_delete_dependent_field_tasks(import_settings.workspace_id) diff --git a/apps/mappings/imports/tasks.py b/apps/mappings/imports/tasks.py index 6166cb6b..98b8d433 100644 --- a/apps/mappings/imports/tasks.py +++ b/apps/mappings/imports/tasks.py @@ -1,14 +1,11 @@ from django_q.tasks import Chain -from fyle_accounting_mappings.models import MappingSetting - from apps.mappings.models import ImportLog from apps.mappings.imports.modules.categories import Category from apps.mappings.imports.modules.projects import Project from apps.mappings.imports.modules.cost_centers import CostCenter from apps.mappings.imports.modules.merchants import Merchant from apps.mappings.imports.modules.expense_custom_fields import ExpenseCustomField -from apps.fyle.models import DependentFieldSetting SOURCE_FIELD_CLASS_MAP = { 'CATEGORY': Category, @@ -41,19 +38,17 @@ def auto_import_and_map_fyle_fields(workspace_id): """ Auto import and map fyle fields """ - project_mapping = MappingSetting.objects.filter( - source_field='PROJECT', + import_log = ImportLog.objects.filter( workspace_id=workspace_id, - import_to_fyle=True + attribute_type = 'PROJECT' ).first() - dependent_fields = DependentFieldSetting.objects.filter(workspace_id=workspace_id, is_import_enabled=True).first() chain = Chain() - if project_mapping and dependent_fields: - chain.append('apps.mappings.tasks.sync_sage300_attributes', 'JOB', workspace_id) - chain.append('apps.mappings.tasks.sync_sage300_attributes', 'COST_CODE', workspace_id) - chain.append('apps.mappings.tasks.sync_sage300_attributes', 'COST_CATEGORY', workspace_id) + chain.append('apps.mappings.tasks.sync_sage300_attributes', 'JOB', workspace_id) + chain.append('apps.mappings.tasks.sync_sage300_attributes', 'COST_CODE', workspace_id) + chain.append('apps.mappings.tasks.sync_sage300_attributes', 'COST_CATEGORY', workspace_id) + if import_log and import_log.status == 'COMPLETE': chain.append('apps.sage300.dependent_fields.import_dependent_fields_to_fyle', workspace_id) if chain.length() > 0: diff --git a/scripts/001_delete_dep_fields_schedule.sql b/scripts/001_delete_dep_fields_schedule.sql new file mode 100644 index 00000000..0d318597 --- /dev/null +++ b/scripts/001_delete_dep_fields_schedule.sql @@ -0,0 +1,4 @@ +rollback; +begin; + +delete from django_q_schedule where func = 'apps.mappings.imports.tasks.auto_import_and_map_fyle_fields'; \ No newline at end of file diff --git a/tests/conftest.py b/tests/conftest.py index 5e7aab11..6e32db40 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -617,13 +617,16 @@ def add_cost_category(create_temp_workspace): @pytest.fixture() @pytest.mark.django_db(databases=['default']) -def add_dependent_field_setting(create_temp_workspace): +def add_dependent_field_setting(create_temp_workspace, mocker): """ Pytest fixture to add dependent fields to a workspace """ workspace_ids = [ 1, 2, 3 ] + + mocker.patch('apps.fyle.signals.schedule_or_delete_fyle_import_tasks', return_value=None) + for workspace_id in workspace_ids: DependentFieldSetting.objects.create( is_import_enabled=True,