Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import settings APIs #12

Merged
merged 2 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion apps/workspaces/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
StringOptionsField,
TextNotNullField,
StringNullField,
BooleanTrueField
BooleanTrueField,
BooleanFalseField
)

User = get_user_model()
Expand Down Expand Up @@ -169,3 +170,15 @@ class ExportSetting(BaseModel):

class Meta:
db_table = 'export_settings'


class ImportSetting(BaseModel):
"""
Table to store Import setting
"""
id = models.AutoField(auto_created=True, primary_key=True, verbose_name='ID', serialize=False)
import_categories = BooleanFalseField(help_text='toggle for import of chart of accounts from sage300')
import_vendors_as_merchants = BooleanFalseField(help_text='toggle for import of vendors as merchant from sage300')

class Meta:
db_table = 'import_settings'
32 changes: 31 additions & 1 deletion apps/workspaces/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from apps.workspaces.models import (
Workspace,
FyleCredential,
ExportSetting
ExportSetting,
ImportSetting
)
from apps.users.models import User
from apps.fyle.helpers import get_cluster_domain
Expand Down Expand Up @@ -96,3 +97,32 @@ def create(self, validated_data):
workspace.save()

return export_settings


class ImportSettingsSerializer(serializers.ModelSerializer):
"""
Import Settings serializer
"""
class Meta:
model = ImportSetting
fields = '__all__'
read_only_fields = ('id', 'workspace', 'created_at', 'updated_at')

def create(self, validated_data):
"""
Create Import Settings
"""

workspace_id = self.context['request'].parser_context.get('kwargs').get('workspace_id')
import_settings, _ = ImportSetting.objects.update_or_create(
workspace_id=workspace_id,
defaults=validated_data
)

# Update workspace onboarding state
workspace = import_settings.workspace
if workspace.onboarding_state == 'IMPORT_SETTINGS':
workspace.onboarding_state = 'ADVANCED_SETTINGS'
workspace.save()

return import_settings
4 changes: 3 additions & 1 deletion apps/workspaces/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
from apps.workspaces.views import (
ReadyView,
WorkspaceView,
ExportSettingView
ExportSettingView,
ImportSettingView
)


workspace_app_paths = [
path('', WorkspaceView.as_view(), name='workspaces'),
path('ready/', ReadyView.as_view(), name='ready'),
path('<int:workspace_id>/export_settings/', ExportSettingView.as_view(), name='export-settings'),
path('<int:workspace_id>/import_settings/', ImportSettingView.as_view(), name='import-settings'),

]

Expand Down
16 changes: 14 additions & 2 deletions apps/workspaces/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
from ms_business_central_api.utils import assert_valid
from apps.workspaces.models import (
Workspace,
ExportSetting
ExportSetting,
ImportSetting
)
from apps.workspaces.serializers import (
WorkspaceSerializer,
ExportSettingsSerializer
ExportSettingsSerializer,
ImportSettingsSerializer
)


Expand Down Expand Up @@ -80,3 +82,13 @@ class ExportSettingView(generics.CreateAPIView, generics.RetrieveAPIView):
lookup_field = 'workspace_id'

queryset = ExportSetting.objects.all()


class ImportSettingView(generics.CreateAPIView, generics.RetrieveAPIView):
"""
Retrieve or Create Import Settings
"""
serializer_class = ImportSettingsSerializer
lookup_field = 'workspace_id'

queryset = ImportSetting.objects.all()
34 changes: 33 additions & 1 deletion tests/test_workspaces/test_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from django.urls import reverse
from apps.workspaces.models import (
Workspace,
ExportSetting
ExportSetting,
ImportSetting
)


Expand Down Expand Up @@ -129,3 +130,34 @@ def test_export_settings(api_client, test_connection):
assert export_settings.default_reimbursable_credit_card_account_id == '342'
assert export_settings.default_vendor_name == 'Nilesh'
assert export_settings.default_vendor_id == '123'


def test_import_settings(api_client, test_connection):
'''
Test import settings
'''
url = reverse('workspaces')
api_client.credentials(HTTP_AUTHORIZATION='Bearer {}'.format(test_connection.access_token))
response = api_client.post(url)
workspace_id = response.data['id']
url = reverse(
'import-settings',
kwargs={'workspace_id': workspace_id}
)

payload = {
'import_categories': True,
'import_vendors_as_merchants': True
}
response = api_client.post(url, payload)

import_settings = ImportSetting.objects.filter(workspace_id=workspace_id).first()

assert response.status_code == 201
assert import_settings.import_categories is True
assert import_settings.import_vendors_as_merchants is True

response = api_client.get(url)
assert response.status_code == 200
assert import_settings.import_categories is True
assert import_settings.import_vendors_as_merchants is True
Loading