Skip to content

Commit

Permalink
Refactor deps schedule to run post Job import
Browse files Browse the repository at this point in the history
  • Loading branch information
Hrishabh17 committed Jun 3, 2024
1 parent 601e3cb commit fb78432
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 50 deletions.
10 changes: 7 additions & 3 deletions apps/fyle/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
10 changes: 10 additions & 0 deletions apps/mappings/imports/queues.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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:
Expand Down Expand Up @@ -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()
35 changes: 0 additions & 35 deletions apps/mappings/imports/schedules.py
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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)
17 changes: 6 additions & 11 deletions apps/mappings/imports/tasks.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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:
Expand Down
4 changes: 4 additions & 0 deletions scripts/001_delete_dep_fields_schedule.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
rollback;
begin;

delete from django_q_schedule where func = 'apps.mappings.imports.tasks.auto_import_and_map_fyle_fields';
5 changes: 4 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit fb78432

Please sign in to comment.