From 8c55278cdc846d87157a3301c947f05e213c5975 Mon Sep 17 00:00:00 2001 From: labhvam5 Date: Mon, 18 Dec 2023 22:35:22 +0530 Subject: [PATCH 1/7] adding test --- .../test_modules/fixtures.py | 264 ++++++++++++++++++ .../test_expense_custom_fields.py | 151 ++++++++++ 2 files changed, 415 insertions(+) create mode 100644 tests/test_fyle_integrations_imports/test_modules/test_expense_custom_fields.py diff --git a/tests/test_fyle_integrations_imports/test_modules/fixtures.py b/tests/test_fyle_integrations_imports/test_modules/fixtures.py index ba1dcb2d..efe380de 100644 --- a/tests/test_fyle_integrations_imports/test_modules/fixtures.py +++ b/tests/test_fyle_integrations_imports/test_modules/fixtures.py @@ -7977,3 +7977,267 @@ } ] } +expense_custom_field_data={ + 'create_new_auto_create_expense_custom_fields_expense_attributes_0':[ + { + "count": 2, + "data": [ + { + "category_ids": [ + 259385 + ], + "code": None, + "column_name": "text_column3", + "created_at": "2023-10-10T11:06:12.906551+00:00", + "default_value": None, + "field_name": "Luke", + "id": 229506, + "is_custom": True, + "is_enabled": True, + "is_mandatory": False, + "options": [ + "France", + "Denmark" + ], + "org_id": "or5qYLrvnoF9", + "parent_field_id": None, + "placeholder": "Select Luke", + "seq": 1, + "type": "SELECT", + "updated_at": "2023-10-10T13:24:33.787371+00:00" + }, + { + "category_ids": [ + 259385, + ], + "code": None, + "column_name": "text_column4", + "created_at": "2023-10-10T11:07:08.534779+00:00", + "default_value": None, + "field_name": "Cube", + "id": 229507, + "is_custom": True, + "is_enabled": True, + "is_mandatory": False, + "options": [ + "Butter Cookies 1", + "Butter Cookies 2", + "Enterprise", + "Killua Class", + "Midsize Business", + "Naruto test 1", + "Serizawa test 1", + "Serizawa test 2", + "Service Line 1", + "Service Line 2", + "Service Line 3", + "Small Business" + ], + "org_id": "or5qYLrvnoF9", + "parent_field_id": None, + "placeholder": "Select Cube", + "seq": 1, + "type": "SELECT", + "updated_at": "2023-10-10T13:24:33.787371+00:00" + } + ], + "offset": 0 + } + ], + 'create_new_auto_create_expense_custom_fields_expense_attributes_1':[ + { + "count": 2, + "data": [ + { + "category_ids": [ + 259385 + ], + "code": None, + "column_name": "text_column3", + "created_at": "2023-10-10T11:06:12.906551+00:00", + "default_value": None, + "field_name": "Luke", + "id": 229506, + "is_custom": True, + "is_enabled": True, + "is_mandatory": False, + "options": [ + "France", + "Denmark", + "Australia", + "Poland" + ], + "org_id": "or5qYLrvnoF9", + "parent_field_id": None, + "placeholder": "Select Luke", + "seq": 1, + "type": "SELECT", + "updated_at": "2023-10-10T13:24:33.787371+00:00" + }, + { + "category_ids": [ + 259385, + ], + "code": None, + "column_name": "text_column4", + "created_at": "2023-10-10T11:07:08.534779+00:00", + "default_value": None, + "field_name": "Cube", + "id": 229507, + "is_custom": True, + "is_enabled": True, + "is_mandatory": False, + "options": [ + "Butter Cookies 1", + "Butter Cookies 2", + "Enterprise", + "Killua Class", + "Midsize Business", + "Naruto test 1", + "Serizawa test 1", + "Serizawa test 2", + "Service Line 1", + "Service Line 2", + "Service Line 3", + "Small Business" + ], + "org_id": "or5qYLrvnoF9", + "parent_field_id": None, + "placeholder": "Select Cube", + "seq": 1, + "type": "SELECT", + "updated_at": "2023-10-10T13:24:33.787371+00:00" + } + ], + "offset": 0 + } + ], + "create_new_auto_create_expense_custom_fields_destination_attributes": [ + { + "FullyQualifiedName": "France", + "domain": "QBO", + "Name": "France", + "SyncToken": "0", + "SubClass": False, + "sparse": False, + "Active": True, + "Id": "5000000000000007280", + "MetaData": {"CreateTime": "2015-07-22T13:57:27-07:00", "LastUpdatedTime": "2015-07-22T13:57:27-07:00"}, + }, + { + "FullyQualifiedName": "Denmark", + "domain": "QBO", + "Name": "Denmark", + "SyncToken": "0", + "SubClass": False, + "sparse": False, + "Active": True, + "Id": "9", + "MetaData": {"CreateTime": "2015-07-22T13:57:27-07:00", "LastUpdatedTime": "2015-07-22T13:57:27-07:00"}, + }, + ], + "create_new_auto_create_expense_custom_fields_destination_attributes_subsequent_run": [ + { + "FullyQualifiedName": "France", + "domain": "QBO", + "Name": "France", + "SyncToken": "0", + "SubClass": False, + "sparse": False, + "Active": True, + "Id": "5000000000000007280", + "MetaData": {"CreateTime": "2015-07-22T13:57:27-07:00", "LastUpdatedTime": "2015-07-22T13:57:27-07:00"}, + }, + { + "FullyQualifiedName": "Denmark", + "domain": "QBO", + "Name": "Denmark", + "SyncToken": "0", + "SubClass": False, + "sparse": False, + "Active": True, + "Id": "9", + "MetaData": {"CreateTime": "2015-07-22T13:57:27-07:00", "LastUpdatedTime": "2015-07-22T13:57:27-07:00"}, + }, + { + "FullyQualifiedName": "Australia", + "domain": "QBO", + "Name": "Australia", + "SyncToken": "0", + "SubClass": False, + "sparse": False, + "Active": True, + "Id": "5000000000000007282", + "MetaData": {"CreateTime": "2015-07-22T13:57:27-07:00", "LastUpdatedTime": "2015-07-22T13:57:27-07:00"}, + }, + { + "FullyQualifiedName": "Poland", + "domain": "QBO", + "Name": "Poland", + "SyncToken": "0", + "SubClass": False, + "sparse": False, + "Active": True, + "Id": "10", + "MetaData": {"CreateTime": "2015-07-22T13:57:27-07:00", "LastUpdatedTime": "2015-07-22T13:57:27-07:00"}, + }, + ], + 'create_new_auto_create_expense_custom_fields_get_by_id': { + 'category_ids':[ + 259385, + 259386, + 259387, + 259388, + 259389, + 259390, + 259391, + 259392, + 259393, + 259394, + 259395, + 259396, + 259397, + 259398, + 259399, + ], + 'code':None, + 'column_name':'text_column3', + 'created_at':'2023-10-10T11:06:12.906551+00:00', + 'default_value':None, + 'field_name':'Luke', + 'id':229506, + 'is_custom':True, + 'is_enabled':True, + 'is_mandatory':False, + "options": [ + "France", + "Denmark" + ], + 'org_id':'or5qYLrvnoF9', + 'parent_field_id':None, + 'placeholder':'Select Luke', + 'seq':1, + 'type':'SELECT', + 'updated_at':'2023-10-11T07:42:24.133074+00:00' + }, + "create_fyle_expense_custom_fields_payload_create_new_case": { + 'field_name':'Luke', + 'type':'SELECT', + 'is_enabled':True, + 'is_mandatory':False, + 'placeholder':'Select Luke', + 'options':[ + 'Adidas', + 'cc1', + 'cc2', + 'Coachella', + 'Employees', + 'Parties', + 'Promotional Items', + 'Radio', + 'Retreats', + 'Test' + ], + 'code':None + } +} diff --git a/tests/test_fyle_integrations_imports/test_modules/test_expense_custom_fields.py b/tests/test_fyle_integrations_imports/test_modules/test_expense_custom_fields.py new file mode 100644 index 00000000..0269e12d --- /dev/null +++ b/tests/test_fyle_integrations_imports/test_modules/test_expense_custom_fields.py @@ -0,0 +1,151 @@ +from unittest import mock +from fyle_accounting_mappings.models import ( + DestinationAttribute, + ExpenseAttribute, + Mapping, +) +from apps.quickbooks_online.utils import QBOConnector +from apps.workspaces.models import QBOCredential, FyleCredential, Workspace +from fyle_integrations_platform_connector import PlatformConnector +from fyle_integrations_imports.modules.expense_custom_fields import ExpenseCustomField +from tests.test_fyle_integrations_imports.test_modules.fixtures import expense_custom_field_data + + +def test_sync_expense_atrributes(mocker, db): + workspace_id = 3 + qbo_credentials = QBOCredential.get_active_qbo_credentials(workspace_id) + qbo_connection = QBOConnector(credentials_object=qbo_credentials, workspace_id=workspace_id) + expense_custom_field = ExpenseCustomField(workspace_id, 'LUKE', 'CLASS', None, qbo_connection, ['classes']) + expense_custom_field.sync_after = None + + Workspace.objects.filter(id=workspace_id).update(fyle_org_id='or5qYLrvnoF9') + fyle_credentials = FyleCredential.objects.get(workspace_id=workspace_id) + platform = PlatformConnector(fyle_credentials=fyle_credentials) + + expense_attribute_count = ExpenseAttribute.objects.filter(workspace_id=workspace_id, attribute_type='LUKE').count() + assert expense_attribute_count == 0 + + mocker.patch( + 'fyle.platform.apis.v1beta.admin.expense_fields.list_all', + return_value=[] + ) + + expense_custom_field.sync_expense_attributes(platform) + + expense_attribute_count = ExpenseAttribute.objects.filter(workspace_id=workspace_id, attribute_type='LUKE').count() + assert expense_attribute_count == 0 + + mocker.patch( + 'fyle.platform.apis.v1beta.admin.expense_fields.list_all', + return_value=expense_custom_field_data['create_new_auto_create_expense_custom_fields_expense_attributes_0'] + ) + + expense_custom_field.sync_expense_attributes(platform) + + expense_attribute_count = ExpenseAttribute.objects.filter(workspace_id=workspace_id, attribute_type='LUKE').count() + assert expense_attribute_count == 2 + +def test_auto_create_destination_attributes(mocker, db): + workspace_id = 3 + qbo_credentials = QBOCredential.get_active_qbo_credentials(workspace_id) + qbo_connection = QBOConnector(credentials_object=qbo_credentials, workspace_id=workspace_id) + expense_custom_field = ExpenseCustomField(workspace_id, 'LUKE', 'CLASS', None, qbo_connection, ['classes']) + expense_custom_field.sync_after = None + + Workspace.objects.filter(id=workspace_id).update(fyle_org_id='or5qYLrvnoF9') + + # delete all destination attributes, expense attributes and mappings + Mapping.objects.filter(workspace_id=workspace_id, source_type='LUKE').delete() + Mapping.objects.filter(workspace_id=workspace_id, destination_type='CLASS').delete() + DestinationAttribute.objects.filter(workspace_id=workspace_id, attribute_type='CLASS').delete() + ExpenseAttribute.objects.filter(workspace_id=workspace_id, attribute_type='LUKE').delete() + + # create new case for projects import + with mock.patch('fyle.platform.apis.v1beta.admin.expense_fields.list_all') as mock_call: + mocker.patch( + 'fyle_integrations_platform_connector.apis.ExpenseCustomFields.post', + return_value=[] + ) + mocker.patch( + 'qbosdk.apis.Classes.get', + return_value=expense_custom_field_data['create_new_auto_create_expense_custom_fields_destination_attributes'] + ) + mock_call.side_effect = [ + expense_custom_field_data['create_new_auto_create_expense_custom_fields_expense_attributes_0'], + ] + + expense_attributes_count = ExpenseAttribute.objects.filter(workspace_id=workspace_id, attribute_type = 'LUKE').count() + + assert expense_attributes_count == 0 + + mappings_count = Mapping.objects.filter(workspace_id=workspace_id, source_type='LUKE', destination_type='CLASS').count() + + assert mappings_count == 0 + + expense_custom_field.trigger_import() + + expense_attributes_count = ExpenseAttribute.objects.filter(workspace_id=workspace_id, attribute_type = 'LUKE').count() + + assert expense_attributes_count == 2 + + mappings_count = Mapping.objects.filter(workspace_id=workspace_id, source_type='LUKE', destination_type='CLASS').count() + + assert mappings_count == 2 + + # create new case for projects import + with mock.patch('fyle.platform.apis.v1beta.admin.expense_fields.list_all') as mock_call: + mocker.patch( + 'fyle_integrations_platform_connector.apis.ExpenseCustomFields.post', + return_value=[] + ) + mocker.patch( + 'fyle_integrations_platform_connector.apis.ExpenseCustomFields.get_by_id', + return_value=expense_custom_field_data['create_new_auto_create_expense_custom_fields_get_by_id'] + ) + mocker.patch( + 'qbosdk.apis.Classes.get', + return_value=expense_custom_field_data['create_new_auto_create_expense_custom_fields_destination_attributes_subsequent_run'] + ) + mock_call.side_effect = [ + expense_custom_field_data['create_new_auto_create_expense_custom_fields_expense_attributes_1'], + ] + + expense_attributes_count = ExpenseAttribute.objects.filter(workspace_id=workspace_id, attribute_type = 'LUKE').count() + + assert expense_attributes_count == 2 + + mappings_count = Mapping.objects.filter(workspace_id=workspace_id, source_type='LUKE', destination_type='CLASS').count() + + assert mappings_count == 2 + + expense_custom_field.trigger_import() + + expense_attributes_count = ExpenseAttribute.objects.filter(workspace_id=workspace_id, attribute_type = 'LUKE').count() + + assert expense_attributes_count == 4 + + mappings_count = Mapping.objects.filter(workspace_id=workspace_id, source_type='LUKE', destination_type='CLASS').count() + + assert mappings_count == 4 + + +def test_construct_fyle_payload(db): + workspace_id = 5 + qbo_credentials = QBOCredential.get_active_qbo_credentials(workspace_id) + qbo_connection = QBOConnector(credentials_object=qbo_credentials, workspace_id=workspace_id) + expense_custom_field = ExpenseCustomField(workspace_id, 'LUKE', 'CLASS', None, qbo_connection, ['classes']) + expense_custom_field.sync_after = None + fyle_credentials = FyleCredential.objects.get(workspace_id=workspace_id) + platform = PlatformConnector(fyle_credentials=fyle_credentials) + + # create new case + paginated_destination_attributes = DestinationAttribute.objects.filter(workspace_id=workspace_id, attribute_type='CLASS') + + fyle_payload = expense_custom_field.construct_fyle_expense_custom_field_payload( + paginated_destination_attributes, + platform, + ) + + print(fyle_payload) + + assert fyle_payload == expense_custom_field_data['create_fyle_expense_custom_fields_payload_create_new_case'] From b5d7dba327f41e320b6c2d6a46482bc83a699076 Mon Sep 17 00:00:00 2001 From: labhvam5 Date: Mon, 18 Dec 2023 22:38:13 +0530 Subject: [PATCH 2/7] lint fixes --- .../test_modules/fixtures.py | 2 +- .../test_modules/test_expense_custom_fields.py | 9 +++++---- tests/test_mappings/test_signals.py | 1 - 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/tests/test_fyle_integrations_imports/test_modules/fixtures.py b/tests/test_fyle_integrations_imports/test_modules/fixtures.py index efe380de..cc58c755 100644 --- a/tests/test_fyle_integrations_imports/test_modules/fixtures.py +++ b/tests/test_fyle_integrations_imports/test_modules/fixtures.py @@ -7977,7 +7977,7 @@ } ] } -expense_custom_field_data={ +expense_custom_field_data = { 'create_new_auto_create_expense_custom_fields_expense_attributes_0':[ { "count": 2, diff --git a/tests/test_fyle_integrations_imports/test_modules/test_expense_custom_fields.py b/tests/test_fyle_integrations_imports/test_modules/test_expense_custom_fields.py index 0269e12d..e6fdb6e5 100644 --- a/tests/test_fyle_integrations_imports/test_modules/test_expense_custom_fields.py +++ b/tests/test_fyle_integrations_imports/test_modules/test_expense_custom_fields.py @@ -45,6 +45,7 @@ def test_sync_expense_atrributes(mocker, db): expense_attribute_count = ExpenseAttribute.objects.filter(workspace_id=workspace_id, attribute_type='LUKE').count() assert expense_attribute_count == 2 + def test_auto_create_destination_attributes(mocker, db): workspace_id = 3 qbo_credentials = QBOCredential.get_active_qbo_credentials(workspace_id) @@ -79,7 +80,7 @@ def test_auto_create_destination_attributes(mocker, db): assert expense_attributes_count == 0 mappings_count = Mapping.objects.filter(workspace_id=workspace_id, source_type='LUKE', destination_type='CLASS').count() - + assert mappings_count == 0 expense_custom_field.trigger_import() @@ -89,7 +90,7 @@ def test_auto_create_destination_attributes(mocker, db): assert expense_attributes_count == 2 mappings_count = Mapping.objects.filter(workspace_id=workspace_id, source_type='LUKE', destination_type='CLASS').count() - + assert mappings_count == 2 # create new case for projects import @@ -115,7 +116,7 @@ def test_auto_create_destination_attributes(mocker, db): assert expense_attributes_count == 2 mappings_count = Mapping.objects.filter(workspace_id=workspace_id, source_type='LUKE', destination_type='CLASS').count() - + assert mappings_count == 2 expense_custom_field.trigger_import() @@ -125,7 +126,7 @@ def test_auto_create_destination_attributes(mocker, db): assert expense_attributes_count == 4 mappings_count = Mapping.objects.filter(workspace_id=workspace_id, source_type='LUKE', destination_type='CLASS').count() - + assert mappings_count == 4 diff --git a/tests/test_mappings/test_signals.py b/tests/test_mappings/test_signals.py index 4adedb84..5437bfc0 100644 --- a/tests/test_mappings/test_signals.py +++ b/tests/test_mappings/test_signals.py @@ -1,5 +1,4 @@ import pytest -from django_q.models import Schedule from fyle_accounting_mappings.models import EmployeeMapping, ExpenseAttribute, Mapping, MappingSetting from apps.fyle.models import ExpenseGroupSettings From 1d61d344182703081bf323393696d9f7864426ef Mon Sep 17 00:00:00 2001 From: labhvam5 Date: Mon, 18 Dec 2023 22:54:38 +0530 Subject: [PATCH 3/7] fix test --- .../test_apis/test_clone_settings/test_views.py | 8 +++++++- .../test_apis/test_import_settings/test_views.py | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/tests/test_workspaces/test_apis/test_clone_settings/test_views.py b/tests/test_workspaces/test_apis/test_clone_settings/test_views.py index 5469b794..470fbf50 100644 --- a/tests/test_workspaces/test_apis/test_clone_settings/test_views.py +++ b/tests/test_workspaces/test_apis/test_clone_settings/test_views.py @@ -3,6 +3,7 @@ from apps.workspaces.models import Workspace from tests.helper import dict_compare_keys from .fixtures import data +from tests.test_fyle_integrations_imports.test_modules.fixtures import expense_custom_field_data def assert_4xx_cases(api_client, url, payload): @@ -15,12 +16,17 @@ def assert_4xx_cases(api_client, url, payload): assert response.status_code == 400 -def test_clone_settings(api_client, test_connection): +def test_clone_settings(mocker, api_client, test_connection): workspace = Workspace.objects.get(id=1) workspace.name_in_journal_entry = 'MERCHANT' workspace.onboarding_state = 'COMPLETE' workspace.save() + mocker.patch( + 'fyle.platform.apis.v1beta.admin.expense_fields.list_all', + return_value=expense_custom_field_data['create_new_auto_create_expense_custom_fields_expense_attributes_0'] + ) + url = '/api/v2/workspaces/1/clone_settings/' api_client.credentials(HTTP_AUTHORIZATION='Bearer {}'.format(test_connection.access_token)) response = api_client.put( diff --git a/tests/test_workspaces/test_apis/test_import_settings/test_views.py b/tests/test_workspaces/test_apis/test_import_settings/test_views.py index 4573f3f0..912b6ac9 100644 --- a/tests/test_workspaces/test_apis/test_import_settings/test_views.py +++ b/tests/test_workspaces/test_apis/test_import_settings/test_views.py @@ -6,7 +6,7 @@ def test_import_settings(mocker, api_client, test_connection): - mocker.patch('fyle_integrations_platform_connector.apis.ExpenseCustomFields.get_by_id', return_value={'options': ['samp'], 'updated_at': '2020-06-11T13:14:55.201598+00:00'}) + mocker.patch('fyle_integrations_platform_connector.apis.ExpenseCustomFields.get_by_id', return_value={'options': ['samp'],"is_mandatory": False, 'updated_at': '2020-06-11T13:14:55.201598+00:00'}) mocker.patch('fyle_integrations_platform_connector.apis.ExpenseCustomFields.post', return_value=None) mocker.patch('fyle_integrations_platform_connector.apis.ExpenseCustomFields.sync', return_value=None) workspace = Workspace.objects.get(id=3) From 1cd8e4251d5e900ab8a088867306a77e4f0df757 Mon Sep 17 00:00:00 2001 From: labhvam5 Date: Tue, 19 Dec 2023 13:57:01 +0530 Subject: [PATCH 4/7] sub module changes --- fyle_integrations_imports | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fyle_integrations_imports b/fyle_integrations_imports index 0461ed30..f5d87e0f 160000 --- a/fyle_integrations_imports +++ b/fyle_integrations_imports @@ -1 +1 @@ -Subproject commit 0461ed30250178b3b0df3a09f8e7183334a442d5 +Subproject commit f5d87e0f1042eaa40d50c915490e25a628e86b8b From 8adb7774fbac2ccaed42a825acf3844a64bc7f40 Mon Sep 17 00:00:00 2001 From: labhvam5 Date: Tue, 19 Dec 2023 16:52:55 +0530 Subject: [PATCH 5/7] fixing test --- tests/test_mappings/test_schedules.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/tests/test_mappings/test_schedules.py b/tests/test_mappings/test_schedules.py index 7c75ba41..ad1947db 100644 --- a/tests/test_mappings/test_schedules.py +++ b/tests/test_mappings/test_schedules.py @@ -16,15 +16,16 @@ def test_schedule_creation(db): configuration.save() MappingSetting.objects.filter(workspace_id=workspace_id).delete() - mapping_setting = MappingSetting.objects.create( - source_field='PROJECT', - destination_field='CUSTOMER', - workspace_id=workspace_id, - import_to_fyle=True, - is_custom=False - ) + mapping_settings = [ + { + 'source_field': 'PROJECT', + 'destination_field': 'CUSTOMER', + 'import_to_fyle': True, + 'is_custom': False + } + ] - schedule_or_delete_fyle_import_tasks(configuration, mapping_setting) + schedule_or_delete_fyle_import_tasks(configuration, mapping_settings) schedule = Schedule.objects.filter( func='apps.mappings.queues.construct_tasks_and_chain_import_fields_to_fyle', From 33314783d75439920874cc5c60ddeb6013591ea2 Mon Sep 17 00:00:00 2001 From: labhvam5 Date: Tue, 19 Dec 2023 16:59:57 +0530 Subject: [PATCH 6/7] fixing test --- tests/test_mappings/test_schedules.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_mappings/test_schedules.py b/tests/test_mappings/test_schedules.py index ad1947db..04fd12ad 100644 --- a/tests/test_mappings/test_schedules.py +++ b/tests/test_mappings/test_schedules.py @@ -16,7 +16,7 @@ def test_schedule_creation(db): configuration.save() MappingSetting.objects.filter(workspace_id=workspace_id).delete() - mapping_settings = [ + mapping_settings = [ { 'source_field': 'PROJECT', 'destination_field': 'CUSTOMER', From 8c57a149f2345537a000da7ab154178ab3ffd5d6 Mon Sep 17 00:00:00 2001 From: labhvam5 <88420539+labhvam5@users.noreply.github.com> Date: Tue, 19 Dec 2023 19:53:08 +0530 Subject: [PATCH 7/7] Migration script (#539) --- .../create-update-new-custom-fields-import.py | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 scripts/python/create-update-new-custom-fields-import.py diff --git a/scripts/python/create-update-new-custom-fields-import.py b/scripts/python/create-update-new-custom-fields-import.py new file mode 100644 index 00000000..0c9021c0 --- /dev/null +++ b/scripts/python/create-update-new-custom-fields-import.py @@ -0,0 +1,51 @@ +from django.db import transaction +import random +from datetime import datetime, timedelta +from django_q.models import Schedule +from fyle_accounting_mappings.models import MappingSetting + +existing_import_enabled_schedules = Schedule.objects.filter( + func__in=['apps.mappings.tasks.async_auto_create_custom_field_mappings'] +).values('args') + +try: + with transaction.atomic(): + count = 0 + for schedule in existing_import_enabled_schedules: + random_number = random.randint(1, 23) + mapping_setting = MappingSetting.objects.filter(workspace_id=schedule['args'], import_to_fyle=True, is_custom=True).first() + if mapping_setting: + print('Creating schedule for workspace_id: ', schedule['args']) + # adding the new schedule + Schedule.objects.update_or_create( + func='apps.mappings.queues.construct_tasks_and_chain_import_fields_to_fyle', + args=schedule['args'], + defaults={ + 'schedule_type': Schedule.MINUTES, + 'minutes':24 * 60, + 'next_run':datetime.now() + timedelta(hours=random_number) + } + ) + # deleting the old schedule + Schedule.objects.filter( + func='apps.mappings.tasks.async_auto_create_custom_field_mappings', + args=schedule['args'] + ).delete() + count += 1 + print(""" + + Schedules created + + """) + print(count) + # remove this sanity check after running this script + raise Exception("This is a sanity check") +except Exception as e: + print(e) + + +# Run this in sql +# select * from django_q_schedule where func = 'apps.mappings.tasks.async_auto_create_custom_field_mappings'; +# --rows should be 0 +# If not check the workspace_id and delete the row +# delete from django_q_schedule where func = 'apps.mappings.tasks.async_auto_create_custom_field_mappings' and args = 'workspace_id';