class ContactViewSet(ArchivableViewSetMixin, CoreViewSet): """Contact ViewSet v3.""" serializer_class = ContactSerializer queryset = get_contact_queryset() filter_backends = ( DjangoFilterBackend, OrderingFilter, ) filterset_fields = ['company_id'] ordering = ('-created_on', ) def get_serializer_class(self): """ Overwrites the built in get_serializer_class method in order to return the ContactDetailSerializer if certain actions are called. """ if self.action in ('create', 'retrieve', 'partial_update'): return ContactDetailSerializer return super().get_serializer_class() def get_additional_data(self, create): """Set adviser to the user on model instance creation.""" data = super().get_additional_data(create) if create: data['adviser'] = self.request.user return data
class CompanyViewSet(ArchivableViewSetMixin, CoreViewSet): """Company view set V3.""" required_scopes = (Scope.internal_front_end, ) serializer_class = CompanySerializer filter_backends = (DjangoFilterBackend, OrderingFilter) filterset_fields = ('global_headquarters_id', ) ordering_fields = ('name', 'created_on') queryset = Company.objects.select_related( 'archived_by', 'business_type', 'classification', 'employee_range', 'export_experience_category', 'headquarter_type', 'one_list_account_owner', 'global_headquarters', 'registered_address_country', 'trading_address_country', 'turnover_range', 'uk_region', ).prefetch_related( Prefetch('contacts', queryset=get_contact_queryset()), Prefetch('investor_investment_projects', queryset=get_slim_investment_project_queryset()), 'export_to_countries', 'future_interest_countries', 'sector', 'sector__parent', 'sector__parent__parent', )
class ContactViewSet(ArchivableViewSetMixin, CoreViewSet): """Contact ViewSet v3.""" required_scopes = (Scope.internal_front_end,) serializer_class = ContactSerializer queryset = get_contact_queryset() filter_backends = ( DjangoFilterBackend, OrderingFilter, ) filterset_fields = ['company_id'] ordering = ('-created_on',) def get_additional_data(self, create): """Set adviser to the user on model instance creation.""" data = super().get_additional_data(create) if create: data['adviser'] = self.request.user return data
class CompanyViewSet(ArchivableViewSetMixin, CoreViewSet): """Company view set.""" serializer_class = CompanySerializer unarchive_validators = (NotATransferredCompanyValidator(), ) filter_backends = (DjangoFilterBackend, OrderingFilter) filterset_fields = ('global_headquarters_id', 'global_ultimate_duns_number') ordering_fields = ('name', 'created_on') queryset = Company.objects.select_related( 'address_country', 'archived_by', 'business_type', 'employee_range', 'export_experience_category', 'global_headquarters__one_list_account_owner__dit_team__country', 'global_headquarters__one_list_account_owner__dit_team__uk_region', 'global_headquarters__one_list_account_owner__dit_team', 'global_headquarters__one_list_account_owner', 'global_headquarters__one_list_tier', 'global_headquarters', 'headquarter_type', 'one_list_account_owner__dit_team__country', 'one_list_account_owner__dit_team__uk_region', 'one_list_account_owner__dit_team', 'one_list_account_owner', 'one_list_tier', 'registered_address_country', 'transferred_to', 'turnover_range', 'uk_region', ).prefetch_related( Prefetch('contacts', queryset=get_contact_queryset()), Prefetch('investor_investment_projects', queryset=get_slim_investment_project_queryset()), 'export_to_countries', 'future_interest_countries', 'sector__parent__parent', 'sector__parent', 'sector', Prefetch('export_countries', queryset=get_export_country_queryset()), ) @action( methods=['post'], detail=True, permission_classes=[ HasPermissions( f'company.{CompanyPermission.change_company}', f'company.{CompanyPermission.change_regional_account_manager}', ), ], schema=StubSchema(), ) def assign_regional_account_manager(self, request, *args, **kwargs): """ Sets the company to be an international trade adviser-managed One List company, and assigns the requested user as the account manager. This means: - setting the One List tier to 'Tier D - Interaction Trade Adviser Accounts' (using the tier ID, not the name) - setting the requested user as the One List account manager (overwriting the existing value) The operation is not allowed if: - the company is a subsidiary of a One List company - the company is already a One List company on a different tier (i.e. not 'Tier D - Interaction Trade Adviser Accounts') """ instance = self.get_object() serializer = AssignRegionalAccountManagerSerializer(instance=instance, data=request.data) serializer.is_valid(raise_exception=True) serializer.save(request.user) return Response(None, status=status.HTTP_204_NO_CONTENT) @action( methods=['post'], detail=True, permission_classes=[ HasPermissions( f'company.{CompanyPermission.change_company}', f'company.{CompanyPermission.change_regional_account_manager}', ), ], schema=StubSchema(), ) def self_assign_account_manager(self, request, *args, **kwargs): """ Sets the company to be an international trade adviser-managed One List company, and assigns the authenticated user as the account manager. This means: - setting the One List tier to 'Tier D - Interaction Trade Adviser Accounts' (using the tier ID, not the name) - setting the authenticated user as the One List account manager (overwriting the existing value) The operation is not allowed if: - the company is a subsidiary of a One List company - the company is already a One List company on a different tier (i.e. not 'Tier D - Interaction Trade Adviser Accounts') """ instance = self.get_object() serializer = SelfAssignAccountManagerSerializer(instance=instance, data=request.data) serializer.is_valid(raise_exception=True) serializer.save(request.user) return Response(None, status=status.HTTP_204_NO_CONTENT) @action( methods=['post'], detail=True, permission_classes=[ HasPermissions( f'company.{CompanyPermission.change_company}', f'company.{CompanyPermission.change_regional_account_manager}', ), ], schema=StubSchema(), ) def remove_account_manager(self, request, *args, **kwargs): """ Remove the One List account manager and tier from a company if it is an international trade adviser-managed One List company. The operation is not allowed if the company is a One List company that isn't on 'Tier D - Interaction Trade Adviser Accounts'. """ instance = self.get_object() serializer = RemoveAccountManagerSerializer(instance=instance, data=request.data) serializer.is_valid(raise_exception=True) serializer.save(request.user) return Response(None, status=status.HTTP_204_NO_CONTENT) @action( methods=['post'], detail=True, permission_classes=[ HasPermissions( f'company.{CompanyPermission.change_company}', f'company.{CompanyPermission.change_one_list_tier_and_global_account_manager}', ), ], schema=StubSchema(), ) def assign_one_list_tier_and_global_account_manager( self, request, *args, **kwargs): """ Assign One List tier and Global Account Manager. This endpoint enables a user with correct permissions to assign company one list tier and global account manager except when company is on 'Tier D - Interaction Trade Adviser Accounts'. One List tier and Global Account Manager cannot be assigned to a subsidiary. """ instance = self.get_object() serializer = AssignOneListTierAndGlobalAccountManagerSerializer( instance=instance, data=request.data, ) serializer.is_valid(raise_exception=True) serializer.save(request.user) return Response(None, status=status.HTTP_204_NO_CONTENT) @action( methods=['post'], detail=True, permission_classes=[ HasPermissions( f'company.{CompanyPermission.change_company}', f'company.{CompanyPermission.change_one_list_tier_and_global_account_manager}', ), ], schema=StubSchema(), ) def remove_from_one_list(self, request, *args, **kwargs): """ Remove company from One List. The operation is not allowed if the company is on 'Tier D - Interaction Trade Adviser Accounts'. """ instance = self.get_object() serializer = RemoveCompanyFromOneListSerializer(instance=instance, data=request.data) serializer.is_valid(raise_exception=True) serializer.save(request.user) return Response(None, status=status.HTTP_204_NO_CONTENT) @action( methods=['patch'], detail=True, permission_classes=[ HasPermissions( f'company.{CompanyPermission.change_company}', f'company.{CompanyPermission.change_one_list_core_team_member}', ), ], schema=StubSchema(), ) def update_one_list_core_team(self, request, *args, **kwargs): """Updates core team for the company.""" instance = self.get_object() serializer = UpdateOneListCoreTeamMembersSerializer( instance=instance, data=request.data, partial=True, ) serializer.is_valid(raise_exception=True) serializer.save(request.user) return Response(None, status=status.HTTP_204_NO_CONTENT) @action( methods=['patch'], permission_classes=[ HasPermissions( f'company.{CompanyPermission.change_company}', 'company.change_companyexportcountry', ), ], detail=True, ) def update_export_detail(self, request, *args, **kwargs): """ Update export related information for the company. """ instance = self.get_object() serializer = UpdateExportDetailsSerializer( instance=instance, data=request.data, partial=True, ) serializer.is_valid(raise_exception=True) serializer.save(request.user) return Response(None, status=status.HTTP_204_NO_CONTENT)