From cf719f492d1fab6cb48c630ba39acdce0a293bbb Mon Sep 17 00:00:00 2001 From: ibuler Date: Thu, 24 Oct 2019 14:36:01 +0800 Subject: [PATCH] =?UTF-8?q?[Update]=20=E4=BF=AE=E6=94=B9user=20group=20rel?= =?UTF-8?q?ations=20api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/users/api/__init__.py | 1 + apps/users/api/relation.py | 30 ++++++++++ apps/users/serializers/__init__.py | 4 +- apps/users/serializers/group.py | 68 +++++++++++++++++++++++ apps/users/serializers/realtion.py | 16 ++++++ apps/users/serializers/{v1.py => user.py} | 66 +++------------------- apps/users/urls/api_urls.py | 1 + 7 files changed, 126 insertions(+), 60 deletions(-) create mode 100644 apps/users/api/relation.py create mode 100644 apps/users/serializers/group.py create mode 100644 apps/users/serializers/realtion.py rename apps/users/serializers/{v1.py => user.py} (69%) diff --git a/apps/users/api/__init__.py b/apps/users/api/__init__.py index 97e1f1088..965337c42 100644 --- a/apps/users/api/__init__.py +++ b/apps/users/api/__init__.py @@ -3,3 +3,4 @@ from .user import * from .group import * +from .relation import * diff --git a/apps/users/api/relation.py b/apps/users/api/relation.py new file mode 100644 index 000000000..a579419d5 --- /dev/null +++ b/apps/users/api/relation.py @@ -0,0 +1,30 @@ +# -*- coding: utf-8 -*- +# + +from rest_framework_bulk import BulkModelViewSet +from django.db.models import F + +from common.permissions import IsOrgAdmin +from .. import serializers +from ..models import User + +__all__ = ['UserUserGroupRelationViewSet'] + + +class UserUserGroupRelationViewSet(BulkModelViewSet): + filter_fields = ('user', 'usergroup') + search_fields = filter_fields + serializer_class = serializers.UserUserGroupRelationSerializer + permission_classes = (IsOrgAdmin,) + + def get_queryset(self): + queryset = User.groups.through.objects.all()\ + .annotate(user_name=F('user__name'))\ + .annotate(usergroup_name=F('usergroup__name')) + return queryset + + def allow_bulk_destroy(self, qs, filtered): + if filtered.count() != 1: + return False + else: + return True diff --git a/apps/users/serializers/__init__.py b/apps/users/serializers/__init__.py index 94ef71f28..41812a5c8 100644 --- a/apps/users/serializers/__init__.py +++ b/apps/users/serializers/__init__.py @@ -1,3 +1,5 @@ # -*- coding: utf-8 -*- # -from .v1 import * \ No newline at end of file +from .user import * +from .group import * +from .realtion import * diff --git a/apps/users/serializers/group.py b/apps/users/serializers/group.py new file mode 100644 index 000000000..e48b23070 --- /dev/null +++ b/apps/users/serializers/group.py @@ -0,0 +1,68 @@ +# -*- coding: utf-8 -*- +# +from django.utils.translation import ugettext_lazy as _ + +from rest_framework import serializers + +from common.fields import StringManyToManyField +from common.serializers import AdaptedBulkListSerializer +from orgs.mixins.serializers import BulkOrgResourceModelSerializer +from ..models import User, UserGroup +from .. import utils + +__all__ = [ + 'UserGroupSerializer', 'UserGroupListSerializer', + 'UserGroupUpdateMemberSerializer', +] + + +class UserGroupSerializer(BulkOrgResourceModelSerializer): + users = serializers.PrimaryKeyRelatedField( + required=False, many=True, queryset=User.objects, label=_('User') + ) + + class Meta: + model = UserGroup + list_serializer_class = AdaptedBulkListSerializer + fields = [ + 'id', 'name', 'users', 'comment', 'date_created', + 'created_by', + ] + extra_kwargs = { + 'created_by': {'label': _('Created by'), 'read_only': True} + } + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.set_fields_queryset() + + def set_fields_queryset(self): + users_field = self.fields['users'] + users_field.child_relation.queryset = utils.get_current_org_members() + + def validate_users(self, users): + for user in users: + if user.is_super_auditor: + msg = _('Auditors cannot be join in the user group') + raise serializers.ValidationError(msg) + return users + + +class UserGroupListSerializer(UserGroupSerializer): + users = StringManyToManyField(many=True, read_only=True) + + +class UserGroupUpdateMemberSerializer(serializers.ModelSerializer): + users = serializers.PrimaryKeyRelatedField(many=True, queryset=User.objects) + + class Meta: + model = UserGroup + fields = ['id', 'users'] + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + self.set_fields_queryset() + + def set_fields_queryset(self): + users_field = self.fields['users'] + users_field.child_relation.queryset = utils.get_current_org_members() diff --git a/apps/users/serializers/realtion.py b/apps/users/serializers/realtion.py new file mode 100644 index 000000000..15cc9e43c --- /dev/null +++ b/apps/users/serializers/realtion.py @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- +# +from rest_framework import serializers + +from ..models import User + +__all__ = ['UserUserGroupRelationSerializer'] + + +class UserUserGroupRelationSerializer(serializers.ModelSerializer): + user_name = serializers.CharField(read_only=True) + usergroup_name = serializers.CharField(read_only=True) + + class Meta: + model = User.groups.through + fields = ['id', 'user', 'user_name', 'usergroup', 'usergroup_name'] diff --git a/apps/users/serializers/v1.py b/apps/users/serializers/user.py similarity index 69% rename from apps/users/serializers/v1.py rename to apps/users/serializers/user.py index 847afe885..16e2fc0b8 100644 --- a/apps/users/serializers/v1.py +++ b/apps/users/serializers/user.py @@ -1,24 +1,18 @@ # -*- coding: utf-8 -*- # from django.utils.translation import ugettext_lazy as _ - from rest_framework import serializers from common.utils import validate_ssh_public_key from common.mixins import BulkSerializerMixin -from common.fields import StringManyToManyField from common.serializers import AdaptedBulkListSerializer from common.permissions import CanUpdateDeleteUser -from orgs.mixins.serializers import BulkOrgResourceModelSerializer from ..models import User, UserGroup -from .. import utils __all__ = [ 'UserSerializer', 'UserPKUpdateSerializer', 'UserUpdateGroupSerializer', - 'UserGroupSerializer', 'UserGroupListSerializer', - 'UserGroupUpdateMemberSerializer', 'ChangeUserPasswordSerializer', - 'ResetOTPSerializer', + 'ChangeUserPasswordSerializer', 'ResetOTPSerializer', ] @@ -127,58 +121,6 @@ class UserUpdateGroupSerializer(serializers.ModelSerializer): fields = ['id', 'groups'] -class UserGroupSerializer(BulkOrgResourceModelSerializer): - users = serializers.PrimaryKeyRelatedField( - required=False, many=True, queryset=User.objects, label=_('User') - ) - - class Meta: - model = UserGroup - list_serializer_class = AdaptedBulkListSerializer - fields = [ - 'id', 'name', 'users', 'comment', 'date_created', - 'created_by', - ] - extra_kwargs = { - 'created_by': {'label': _('Created by'), 'read_only': True} - } - - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - self.set_fields_queryset() - - def set_fields_queryset(self): - users_field = self.fields['users'] - users_field.child_relation.queryset = utils.get_current_org_members() - - def validate_users(self, users): - for user in users: - if user.is_super_auditor: - msg = _('Auditors cannot be join in the user group') - raise serializers.ValidationError(msg) - return users - - -class UserGroupListSerializer(UserGroupSerializer): - users = StringManyToManyField(many=True, read_only=True) - - -class UserGroupUpdateMemberSerializer(serializers.ModelSerializer): - users = serializers.PrimaryKeyRelatedField(many=True, queryset=User.objects) - - class Meta: - model = UserGroup - fields = ['id', 'users'] - - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - self.set_fields_queryset() - - def set_fields_queryset(self): - users_field = self.fields['users'] - users_field.child_relation.queryset = utils.get_current_org_members() - - class ChangeUserPasswordSerializer(serializers.ModelSerializer): class Meta: @@ -188,3 +130,9 @@ class ChangeUserPasswordSerializer(serializers.ModelSerializer): class ResetOTPSerializer(serializers.Serializer): msg = serializers.CharField(read_only=True) + + def create(self, validated_data): + pass + + def update(self, instance, validated_data): + pass diff --git a/apps/users/urls/api_urls.py b/apps/users/urls/api_urls.py index 27d5fd5fa..e40f5d3a7 100644 --- a/apps/users/urls/api_urls.py +++ b/apps/users/urls/api_urls.py @@ -14,6 +14,7 @@ app_name = 'users' router = BulkRouter() router.register(r'users', api.UserViewSet, 'user') router.register(r'groups', api.UserGroupViewSet, 'user-group') +router.register(r'users-groups-relations', api.UserUserGroupRelationViewSet, 'user-group-relation') urlpatterns = [