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 .. 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
|
2019-10-24 06:36:01 +00:00
|
|
|
serializer_class = serializers.UserUserGroupRelationSerializer
|
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
|