From fde05cb78445416681b1c0166e4d8181748db70f Mon Sep 17 00:00:00 2001 From: Nilesh Pant <58652823+NileshPant1999@users.noreply.github.com> Date: Tue, 28 Nov 2023 16:52:27 +0530 Subject: [PATCH] add support for fyle fields (#94) --- fyle_accounting_mappings/serializers.py | 39 +++++++++++++++++++++++++ fyle_accounting_mappings/urls.py | 4 ++- fyle_accounting_mappings/views.py | 15 +++++++++- 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/fyle_accounting_mappings/serializers.py b/fyle_accounting_mappings/serializers.py index 977e3b3..a35f6e7 100644 --- a/fyle_accounting_mappings/serializers.py +++ b/fyle_accounting_mappings/serializers.py @@ -327,3 +327,42 @@ class CategoryAttributeMappingSerializer(serializers.ModelSerializer): class Meta: model = ExpenseAttribute fields = '__all__' + + +class FyleFieldsSerializer(serializers.Serializer): + """ + Fyle Fields Serializer + """ + + attribute_type = serializers.CharField() + display_name = serializers.CharField() + is_dependant = serializers.BooleanField() + + def format_fyle_fields(self, workspace_id): + """ + Get Fyle Fields + """ + + attribute_types = [ + 'EMPLOYEE', 'CATEGORY', 'PROJECT', 'COST_CENTER', + 'TAX_GROUP', 'CORPORATE_CARD', 'MERCHANT' + ] + + attributes = ExpenseAttribute.objects.filter( + ~Q(attribute_type__in=attribute_types), + workspace_id=workspace_id + ).values('attribute_type', 'display_name', 'detail__is_dependent').distinct() + + attributes_list = [ + {'attribute_type': 'COST_CENTER', 'display_name': 'Cost Center', 'is_dependant': False}, + {'attribute_type': 'PROJECT', 'display_name': 'Project', 'is_dependant': False} + ] + + for attr in attributes: + attributes_list.append({ + 'attribute_type': attr['attribute_type'], + 'display_name': attr['display_name'], + 'is_dependant': attr['detail__is_dependent'] + }) + + return attributes_list diff --git a/fyle_accounting_mappings/urls.py b/fyle_accounting_mappings/urls.py index 69b52c0..6ad8740 100644 --- a/fyle_accounting_mappings/urls.py +++ b/fyle_accounting_mappings/urls.py @@ -26,7 +26,8 @@ ExpenseAttributesMappingView, EmployeeAttributesMappingView, ExpenseFieldView, - DestinationAttributesView + DestinationAttributesView, + FyleFieldsView ) urlpatterns = [ @@ -42,4 +43,5 @@ path('employee_attributes/', EmployeeAttributesMappingView.as_view()), path('expense_fields/', ExpenseFieldView.as_view()), path('destination_attributes/', DestinationAttributesView.as_view()), + path('fyle_fields/', FyleFieldsView.as_view()), ] diff --git a/fyle_accounting_mappings/views.py b/fyle_accounting_mappings/views.py index 2653116..397c1c7 100644 --- a/fyle_accounting_mappings/views.py +++ b/fyle_accounting_mappings/views.py @@ -16,7 +16,8 @@ CategoryMapping, ExpenseField from .serializers import ExpenseAttributeMappingSerializer, MappingSettingSerializer, MappingSerializer, \ EmployeeMappingSerializer, CategoryMappingSerializer, DestinationAttributeSerializer, \ - EmployeeAttributeMappingSerializer, ExpenseFieldSerializer, CategoryAttributeMappingSerializer + EmployeeAttributeMappingSerializer, ExpenseFieldSerializer, CategoryAttributeMappingSerializer, \ + FyleFieldsSerializer logger = logging.getLogger(__name__) @@ -451,3 +452,15 @@ class DestinationAttributesView(LookupFieldMixin, ListAPIView): filter_backends = (DjangoFilterBackend,) filterset_fields = {'attribute_type': {'exact', 'in'}, 'display_name': {'exact', 'in'}, 'active': {'exact'}} ordering_fields = ('value',) + + +class FyleFieldsView(ListAPIView): + """ + Fyle Fields view + """ + + serializer_class = FyleFieldsSerializer + pagination_class = None + + def get_queryset(self): + return FyleFieldsSerializer().format_fyle_fields(self.kwargs["workspace_id"])