2019-10-24 06:36:01 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
|
|
|
|
from django.db.models import F
|
|
|
|
|
2020-08-12 09:21:04 +00:00
|
|
|
from common.drf.api import JMSBulkRelationModelViewSet
|
2019-10-24 06:36:01 +00:00
|
|
|
from common.permissions import IsOrgAdmin
|
|
|
|
from .. import serializers
|
|
|
|
from ..models import User
|
|
|
|
|
|
|
|
__all__ = ['UserUserGroupRelationViewSet']
|
|
|
|
|
|
|
|
|
2020-08-12 09:21:04 +00:00
|
|
|
class UserUserGroupRelationViewSet(JMSBulkRelationModelViewSet):
|
2021-01-07 02:53:10 +00:00
|
|
|
filterset_fields = ('user', 'usergroup')
|
|
|
|
search_fields = filterset_fields
|
2019-10-24 06:36:01 +00:00
|
|
|
serializer_class = serializers.UserUserGroupRelationSerializer
|
|
|
|
permission_classes = (IsOrgAdmin,)
|
2020-08-12 09:21:04 +00:00
|
|
|
m2m_field = User.groups.field
|
2019-10-24 06:36:01 +00:00
|
|
|
|
|
|
|
def get_queryset(self):
|
2020-08-12 09:21:04 +00:00
|
|
|
return super().get_queryset().annotate(
|
|
|
|
user_display=F('user__name'), usergroup_display=F('usergroup__name')
|
|
|
|
)
|
2019-10-24 06:36:01 +00:00
|
|
|
|
|
|
|
def allow_bulk_destroy(self, qs, filtered):
|
|
|
|
if filtered.count() != 1:
|
|
|
|
return False
|
|
|
|
else:
|
|
|
|
return True
|