-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
This reverts commit a9c57ae.
- Loading branch information
labhvam5
committed
Oct 5, 2023
1 parent
683bc31
commit 9107762
Showing
14 changed files
with
558 additions
and
258 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
from datetime import datetime | ||
from typing import List | ||
from apps.mappings.imports.modules.base import Base | ||
from fyle_accounting_mappings.models import DestinationAttribute | ||
|
||
|
||
class CostCenter(Base): | ||
""" | ||
Class for CostCenter module | ||
""" | ||
def __init__(self, workspace_id: int, destination_field: str, sync_after: datetime): | ||
super().__init__( | ||
workspace_id=workspace_id, | ||
source_field='COST_CENTER', | ||
destination_field=destination_field, | ||
platform_class_name='cost_centers', | ||
sync_after=sync_after | ||
) | ||
|
||
def trigger_import(self): | ||
""" | ||
Trigger import for CostCenter module | ||
""" | ||
self.check_import_log_and_start_import() | ||
|
||
# remove the is_auto_sync_status_allowed parameter | ||
def construct_fyle_payload( | ||
self, | ||
paginated_destination_attributes: List[DestinationAttribute], | ||
existing_fyle_attributes_map: object, | ||
is_auto_sync_status_allowed: bool | ||
): | ||
""" | ||
Construct Fyle payload for CostCenter module | ||
:param paginated_destination_attributes: List of paginated destination attributes | ||
:param existing_fyle_attributes_map: Existing Fyle attributes map | ||
:param is_auto_sync_status_allowed: Is auto sync status allowed | ||
:return: Fyle payload | ||
""" | ||
payload = [] | ||
|
||
for attribute in paginated_destination_attributes: | ||
cost_center = { | ||
'name': attribute.value, | ||
'is_enabled': True if attribute.active is None else attribute.active, | ||
'description': 'Cost Center - {0}, Id - {1}'.format( | ||
attribute.value, | ||
attribute.destination_id | ||
) | ||
} | ||
|
||
# Create a new cost-center if it does not exist in Fyle | ||
if attribute.value.lower() not in existing_fyle_attributes_map: | ||
payload.append(cost_center) | ||
|
||
return payload |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,24 @@ | ||
from apps.mappings.models import ImportLog | ||
from apps.mappings.imports.modules.projects import Project | ||
from apps.mappings.imports.modules.categories import Category | ||
from apps.mappings.imports.modules.cost_centers import CostCenter | ||
|
||
SOURCE_FIELD_CLASS_MAP = { | ||
'PROJECT': Project, | ||
'CATEGORY': Category, | ||
'COST_CENTER': CostCenter, | ||
} | ||
|
||
# TODO: Add a common function later when we add more modules | ||
def trigger_projects_import_via_schedule(workspace_id: int, destination_field: str): | ||
def trigger_import_via_schedule(workspace_id: int, destination_field: str, source_field: str): | ||
""" | ||
Trigger projects import via schedule | ||
Trigger import via schedule | ||
:param workspace_id: Workspace id | ||
:param destination_field: Destination field | ||
:param source_field: Type of attribute (e.g., 'PROJECT', 'CATEGORY', 'COST_CENTER') | ||
""" | ||
import_log = ImportLog.objects.filter(workspace_id=workspace_id, attribute_type='PROJECT').first() | ||
import_log = ImportLog.objects.filter(workspace_id=workspace_id, attribute_type=source_field).first() | ||
sync_after = import_log.last_successful_run_at if import_log else None | ||
project = Project(workspace_id, destination_field, sync_after) | ||
project.trigger_import() | ||
|
||
def trigger_categories_import_via_schedule(workspace_id: int, destination_field: str): | ||
""" | ||
Trigger Categories import via schedule | ||
:param workspace_id: Workspace id | ||
:param destination_field: Destination field | ||
""" | ||
import_log = ImportLog.objects.filter(workspace_id=workspace_id, attribute_type='CATEGORY').first() | ||
sync_after = import_log.last_successful_run_at if import_log else None | ||
category = Category(workspace_id, destination_field, sync_after) | ||
category.trigger_import() | ||
module_class = SOURCE_FIELD_CLASS_MAP[source_field] | ||
item = module_class(workspace_id, destination_field, sync_after) | ||
item.trigger_import() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from django.db import transaction | ||
from datetime import datetime | ||
from django_q.models import Schedule | ||
from apps.workspaces.models import Configuration | ||
from fyle_accounting_mappings.models import MappingSetting | ||
existing_import_enabled_schedules = Schedule.objects.filter( | ||
func__in=['apps.mappings.tasks.auto_create_cost_center_mappings'] | ||
).values('args') | ||
try: | ||
with transaction.atomic(): | ||
for schedule in existing_import_enabled_schedules: | ||
mapping_setting = MappingSetting.objects.filter(source_field='COST_CENTER', workspace_id=schedule['args'], import_to_fyle=True).first() | ||
if mapping_setting: | ||
Schedule.objects.update_or_create( | ||
func='apps.mappings.imports.queues.chain_import_fields_to_fyle', | ||
args=schedule['args'], | ||
defaults={ | ||
'schedule_type': Schedule.MINUTES, | ||
'minutes':24 * 60, | ||
'next_run':datetime.now() | ||
} | ||
) | ||
categories = Configuration.objects.filter(import_categories=True).values_list('workspace_id', flat=True) | ||
project = MappingSetting.objects.filter(source_field='PROJECT', import_to_fyle=True).values_list('workspace_id', flat=True) | ||
cost_center = MappingSetting.objects.filter(source_field='COST_CENTER', import_to_fyle=True).values_list('workspace_id', flat=True) | ||
unique_states = list(set(categories) | set(project) | set(cost_center)) | ||
tot_count = len(unique_states) | ||
schedule_count = Schedule.objects.filter(func='apps.mappings.imports.queues.chain_import_fields_to_fyle').count() | ||
#make the sanity check a bit more clear | ||
print("tot_count: {}".format(tot_count)) | ||
print("schedule_count: {}".format(schedule_count)) | ||
raise Exception("This is a sanity check") | ||
except Exception as e: | ||
print(e) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.