2023-02-07 11:45:12 +00:00
|
|
|
from rest_framework import status
|
|
|
|
from rest_framework.decorators import action
|
|
|
|
from rest_framework.response import Response
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
|
2023-02-07 05:27:44 +00:00
|
|
|
from accounts import serializers
|
2023-02-07 11:45:12 +00:00
|
|
|
from accounts.const import Source
|
2023-02-07 05:27:44 +00:00
|
|
|
from accounts.models import GatheredAccount
|
2023-02-07 11:45:12 +00:00
|
|
|
from accounts.filters import GatheredAccountFilterSet
|
2023-02-07 05:27:44 +00:00
|
|
|
from orgs.mixins.api import OrgBulkModelViewSet
|
|
|
|
|
|
|
|
__all__ = [
|
|
|
|
'GatheredAccountViewSet',
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
class GatheredAccountViewSet(OrgBulkModelViewSet):
|
|
|
|
model = GatheredAccount
|
2023-02-07 11:45:12 +00:00
|
|
|
search_fields = ('username',)
|
|
|
|
filterset_class = GatheredAccountFilterSet
|
2023-02-07 05:27:44 +00:00
|
|
|
serializer_classes = {
|
2023-02-07 11:45:12 +00:00
|
|
|
'default': serializers.GatheredAccountSerializer,
|
|
|
|
}
|
|
|
|
rbac_perms = {
|
|
|
|
'sync_account': 'assets.add_gatheredaccount',
|
2023-02-07 05:27:44 +00:00
|
|
|
}
|
2023-02-07 11:45:12 +00:00
|
|
|
|
|
|
|
@action(methods=['post'], detail=True, url_path='sync')
|
|
|
|
def sync_account(self, request, *args, **kwargs):
|
|
|
|
gathered_account = super().get_object()
|
|
|
|
asset = gathered_account.asset
|
|
|
|
username = gathered_account.username
|
|
|
|
accounts = asset.accounts.filter(username=username)
|
|
|
|
if accounts.exists():
|
|
|
|
accounts.update(source=Source.COLLECTED)
|
|
|
|
else:
|
|
|
|
asset.accounts.model.objects.create(
|
|
|
|
asset=asset, username=username,
|
|
|
|
name=f'{username}-{_("Collected")}',
|
|
|
|
source=Source.COLLECTED
|
|
|
|
)
|
|
|
|
return Response(status=status.HTTP_201_CREATED)
|