2019-10-24 06:36:01 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
#
|
|
|
|
|
|
|
|
from django.db.models import F
|
|
|
|
|
2023-01-16 11:02:09 +00:00
|
|
|
from common.api import JMSBulkRelationModelViewSet
|
2019-10-24 06:36:01 +00:00
|
|
|
from .. import serializers
|
2022-03-11 11:31:55 +00:00
|
|
|
from ..models import User, UserGroup
|
2019-10-24 06:36:01 +00:00
|
|
|
|
|
|
|
__all__ = ['UserUserGroupRelationViewSet']
|
|
|
|
|
|
|
|
|
2020-08-12 09:21:04 +00:00
|
|
|
class UserUserGroupRelationViewSet(JMSBulkRelationModelViewSet):
|
2022-03-11 11:31:55 +00:00
|
|
|
perm_model = UserGroup
|
2021-01-07 02:53:10 +00:00
|
|
|
filterset_fields = ('user', 'usergroup')
|
|
|
|
search_fields = filterset_fields
|
2023-08-23 07:47:22 +00:00
|
|
|
serializer_class = serializers.User2GroupRelationSerializer
|
2020-08-12 09:21:04 +00:00
|
|
|
m2m_field = User.groups.field
|
2023-12-29 02:19:41 +00:00
|
|
|
rbac_perms = {
|
|
|
|
'create': 'users.change_usergroup',
|
2023-12-29 05:31:46 +00:00
|
|
|
'bulk_destroy': 'users.change_usergroup',
|
2023-12-29 02:19:41 +00:00
|
|
|
}
|
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
|