-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathviews.py
70 lines (57 loc) · 2.56 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from rest_framework import generics
from rest_framework.request import Request
from rest_framework.views import Response, status
from fyle_accounting_mappings.models import MappingSetting
from apps.mappings.models import ImportLog
from apps.workspaces.models import Workspace, Configuration
from apps.workspaces.apis.import_settings.serializers import ImportSettingsSerializer
class ImportSettingsView(generics.RetrieveUpdateAPIView):
"""
Import Settings View
"""
serializer_class = ImportSettingsSerializer
def get_object(self) -> Workspace:
"""
Get workspace object
"""
return Workspace.objects.filter(id=self.kwargs['workspace_id']).first()
def get_serializer_context(self) -> dict:
"""
Override to include the request in the serializer context.
This allows serializers to access the current user.
"""
context = super().get_serializer_context()
context['request'] = self.request
return context
class ImportCodeFieldView(generics.GenericAPIView):
"""
Import Code Field View
"""
def get(self, request: Request, *args, **kwargs) -> Response:
"""
Get import code fields
"""
workspace_id = kwargs['workspace_id']
import_log_attributes = ImportLog.objects.filter(workspace_id=workspace_id).values_list('attribute_type', flat=True)
configuration = Configuration.objects.filter(workspace_id=workspace_id).first()
response_data = {
'PROJECT': True,
'DEPARTMENT': True,
'ACCOUNT': True,
'EXPENSE_TYPE': True
}
mapping_settings = MappingSetting.objects.filter(workspace_id=workspace_id).values('destination_field', 'source_field')
for mapping in mapping_settings:
if mapping['destination_field'] == 'PROJECT' and mapping['source_field'] in import_log_attributes:
response_data['PROJECT'] = False
elif mapping['destination_field'] == 'DEPARTMENT' and mapping['source_field'] in import_log_attributes:
response_data['DEPARTMENT'] = False
if configuration:
if 'ACCOUNT' in configuration.import_code_fields or '_ACCOUNT' in configuration.import_code_fields:
response_data['ACCOUNT'] = False
if 'EXPENSE_TYPE' in configuration.import_code_fields or '_EXPENSE_TYPE' in configuration.import_code_fields:
response_data['EXPENSE_TYPE'] = False
return Response(
data=response_data,
status=status.HTTP_200_OK
)