예제 #1
0
 def put(self, request, groupname):
     group = self.get_group(groupname)
     update = {"users": request.data}  # spec wants list as request body
     serializer = GroupSerializer(group, update, partial=True)
     if serializer.is_valid():
         serializer.save()
         return Response(status=status.HTTP_200_OK)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
예제 #2
0
 def put(self, request, groupname):
     group = self.get_group(groupname)
     update = {"users": request.data}  # spec wants list as request body
     serializer = GroupSerializer(group, update, partial=True)
     if serializer.is_valid():
         serializer.save()
         return Response(status=status.HTTP_200_OK)
     return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
예제 #3
0
    def update(cls, root, info, **kwargs):
        data = kwargs.pop("input")
        profile_data = data.pop("profile", {})
        profile_data["updater"] = info.context.user.id

        pk = data.pop("id")
        group = get_Object_or_None(Group, pk=pk)
        if not group:
            return cls.get_errors([
                ErrorType(
                    field="id",
                    messages=[
                        "A {} obj with id: {} do not exist".format(
                            "Group", pk)
                    ],
                )
            ])

        group_ser = GroupSerializer(instance=group, data=data, partial=True)
        if not group_ser.is_valid():
            errors = [
                ErrorType(field=key, messages=value)
                for key, value in group_ser.errors.items()
            ]
            return cls.get_errors(errors)

        profile = GroupProfile.objects.filter(group_id=group.id)
        profile_ser = GroupProfileSerializer(instance=profile,
                                             data=profile_data,
                                             partial=True)
        ok, profile = cls.save(profile_ser, root, info)
        if not ok:
            return cls.get_errors(profile)

        group = group_ser.save()
        profile_ser.save()

        return cls.perform_mutate(group, info)
예제 #4
0
    def update(cls, root, info, **kwargs):
        data = kwargs.get("input")
        profile_data = data.pop("profile", None)

        data = kwargs.get("input")
        profile_data = data.pop("profile", {})

        # 只有教师才可以修改其他用户的信息
        # 否则就算是传入了id也只能修改自己的信息
        pk = data.pop("id")
        user = info.context.user
        if user.is_staff:
            user = get_Object_or_None(User, pk=pk)
            if not user:
                return cls.get_errors(__fill_error("id", "用户不存在!"))

            if user.is_staff:
                user = info.context.user

            profile_data["updater"] = user.id

        # 如果更改了name属性,则先检查组内是否有同名组
        name = profile_data.get("name", None)
        if name:
            if get_Object_or_None(group, name=name):
                return cls.get_errors(__fill_error("name", "已经存在同名组:" + name))

        user_ser = UserSerializer(instance=user, data=data, partial=True)
        if not user_ser.is_valid():
            errors = [
                ErrorType(field=key, messages=value)
                for key, value in user_ser.errors.items()
            ]
            return cls.get_errors(errors)

        group = user.groups.all().filter(name=user.profile.name).first()
        group_ser = GroupSerializer(instance=group,
                                    data={"name": name},
                                    partial=True)
        if not group_ser.is_valid():
            errors = [
                ErrorType(field=key, messages=value)
                for key, value in group_ser.errors.items()
            ]
            return cls.get_errors(errors)

        gp_ser = GroupProfileSerializer(instance=group.profile,
                                        data={},
                                        partial=True)
        if not gp_ser.is_valid():
            errors = [
                ErrorType(field=key, messages=value)
                for key, value in gp_ser.errors.items()
            ]
            return cls.get_errors(errors)

        profile = UserProfile.objects.filter(user_id=user.id).first()
        profile_ser = None
        if not profile:
            profile_ser = UserProfileSerializer(data=profile_data)
        else:
            profile_ser = UserProfileSerializer(instance=profile,
                                                data=profile_data,
                                                partial=True)
        ok, profile = cls.save(profile_ser, root, info)
        if not ok:
            return cls.get_errors(profile)
        user_ser.save()
        group_ser.save()
        gp_ser.save()
        return cls.perform_mutate(user, info)