This commit is contained in:
virusdefender 2018-05-20 13:30:52 +08:00
parent 796f748d10
commit 5447dd90ac
3 changed files with 23 additions and 4 deletions

View File

@ -33,15 +33,21 @@ class SimpleGroupSerializer(serializers.ModelSerializer):
created_by = UsernameSerializer()
password = serializers.SerializerMethodField()
me = serializers.BooleanField()
members = UsernameSerializer(many=True)
def get_password(self, obj):
return obj.password != ""
class Meta:
model = Group
fields = ["id", "created_by", "name", "password", "me"]
fields = ["id", "created_by", "name", "password", "me", "members"]
class JoinGroupSerializer(serializers.Serializer):
group_name = serializers.CharField()
password = serializers.CharField(allow_blank=True)
password = serializers.CharField(allow_blank=True)
class AddUserToGroupSerializer(serializers.Serializer):
group_id = serializers.IntegerField()
username = serializers.CharField()

View File

@ -1,7 +1,8 @@
from django.conf.urls import url
from ..views.admin import GroupAPI
from ..views.admin import GroupAPI, AddUserToGroupAPI
urlpatterns = [
url(r"^group/?$", GroupAPI.as_view(), name="group_api"),
url(r"^group/add_user?$", AddUserToGroupAPI.as_view(), name="add_user_to_group_api"),
]

View File

@ -5,7 +5,7 @@ from account.models import User
from utils.api import APIView, validate_serializer
from ..models import Group
from ..serializers import GroupSerializer, CreateGroupSerializer, EditGroupSerializer, DeleteGroupSerializer
from ..serializers import GroupSerializer, CreateGroupSerializer, EditGroupSerializer, DeleteGroupSerializer, AddUserToGroupSerializer
class GroupAPI(APIView):
@ -68,3 +68,15 @@ class GroupAPI(APIView):
else:
group.members.remove(User.objects.get(id=data["user_id"]))
return self.success(GroupSerializer(group).data)
class AddUserToGroupAPI(APIView):
@validate_serializer(AddUserToGroupSerializer)
def post(self, request):
try:
user = User.objects.get(username=request.data["username"])
group = Group.objects.get(id=request.data["group_id"])
except Exception:
return self.error("用户不存在")
group.members.add(user)
return self.success(GroupSerializer(group).data)