2022-11-08 12:33:55 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
2023-02-21 05:00:04 +00:00
|
|
|
from rest_framework import status
|
|
|
|
from rest_framework.decorators import action
|
|
|
|
from rest_framework.response import Response
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
from accounts import serializers
|
2023-03-23 10:57:22 +00:00
|
|
|
from accounts.const import AutomationTypes
|
2023-02-21 05:00:04 +00:00
|
|
|
from accounts.filters import GatheredAccountFilterSet
|
2023-03-23 10:57:22 +00:00
|
|
|
from accounts.models import GatherAccountsAutomation
|
2023-02-21 05:00:04 +00:00
|
|
|
from accounts.models import GatheredAccount
|
2022-12-27 07:27:33 +00:00
|
|
|
from orgs.mixins.api import OrgBulkModelViewSet
|
|
|
|
from .base import AutomationExecutionViewSet
|
2022-11-08 12:33:55 +00:00
|
|
|
|
|
|
|
__all__ = [
|
2023-02-21 05:00:04 +00:00
|
|
|
'GatherAccountsAutomationViewSet', 'GatherAccountsExecutionViewSet',
|
|
|
|
'GatheredAccountViewSet'
|
2022-11-08 12:33:55 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class GatherAccountsAutomationViewSet(OrgBulkModelViewSet):
|
|
|
|
model = GatherAccountsAutomation
|
|
|
|
filter_fields = ('name',)
|
|
|
|
search_fields = filter_fields
|
|
|
|
serializer_class = serializers.GatherAccountAutomationSerializer
|
2022-12-27 07:27:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GatherAccountsExecutionViewSet(AutomationExecutionViewSet):
|
|
|
|
rbac_perms = (
|
2023-01-16 11:02:09 +00:00
|
|
|
("list", "accounts.view_gatheraccountsexecution"),
|
|
|
|
("retrieve", "accounts.view_gatheraccountsexecution"),
|
|
|
|
("create", "accounts.add_gatheraccountsexecution"),
|
2022-12-27 07:27:33 +00:00
|
|
|
)
|
2023-01-16 11:02:09 +00:00
|
|
|
|
|
|
|
tp = AutomationTypes.gather_accounts
|
|
|
|
|
|
|
|
def get_queryset(self):
|
|
|
|
queryset = super().get_queryset()
|
|
|
|
queryset = queryset.filter(automation__type=self.tp)
|
|
|
|
return queryset
|
2023-02-21 05:00:04 +00:00
|
|
|
|
|
|
|
|
|
|
|
class GatheredAccountViewSet(OrgBulkModelViewSet):
|
|
|
|
model = GatheredAccount
|
|
|
|
search_fields = ('username',)
|
|
|
|
filterset_class = GatheredAccountFilterSet
|
|
|
|
serializer_classes = {
|
|
|
|
'default': serializers.GatheredAccountSerializer,
|
|
|
|
}
|
|
|
|
rbac_perms = {
|
2023-03-23 07:24:19 +00:00
|
|
|
'sync_accounts': 'assets.add_gatheredaccount',
|
2023-02-21 05:00:04 +00:00
|
|
|
}
|
|
|
|
|
2023-03-23 07:24:19 +00:00
|
|
|
@action(methods=['post'], detail=False, url_path='sync-accounts')
|
|
|
|
def sync_accounts(self, request, *args, **kwargs):
|
|
|
|
gathered_account_ids = request.data.get('gathered_account_ids')
|
|
|
|
gathered_accounts = self.model.objects.filter(id__in=gathered_account_ids)
|
2023-03-23 10:57:22 +00:00
|
|
|
self.model.sync_accounts(gathered_accounts)
|
2023-02-21 05:00:04 +00:00
|
|
|
return Response(status=status.HTTP_201_CREATED)
|