def post(request, group_id, username=None): """ 如果username为None 则是请求加入一个群 否则是同意某人的加入 """ myself = request.user.user group = check_exist_group(group_id) if username: # 同意某人加入 return Intention._consent_intention(myself, group, username) return Intention._new_intention(myself, group)
def _get_group_detail(myself, group_id): """ 获取某群组详细信息 """ group = check_exist_group(group_id) check_user_in_group(myself, group) memberships = group.membership_set group_serializer = GroupSerializer(group) membership_serializer = GroupMemberSerializer(memberships, many=True) rtn = dict(group_serializer.data) rtn['members'] = membership_serializer.data return Response(rtn)
def put(request, group_id=None): """ 修改群组资料 """ myself = request.user.user group = check_exist_group(group_id) membership = check_user_in_group(myself, group) check_user_is_master_or_manager(membership) serializer = check_serializer_is_valid(GroupSerializer, group, request.data) serializer.save() return Response(serializer.data)
def delete(request, group_id=None): """ 解散/退出群组 """ myself = request.user.user group = check_exist_group(group_id) membership = check_user_in_group(myself, group) if membership.permission == 'master': # 解散群组 group.delete() else: # 退出群组 membership.delete() return Response()
def get(request, group_id, username=None): """ 获取某群请求加入列表 """ check_is_none(username) myself = request.user.user group = check_exist_group(group_id) my_membership = check_user_in_group(myself, group) check_user_is_master_or_manager(my_membership) intentions = my_membership.group.intention_set serializer = IntentionSerializer(intentions, many=True) return Response(serializer.data)
def delete(request, group_id, username): """ 移出成员 """ myself = request.user.user the_other = check_exist_user(username) check_not_same(myself, the_other) # 不可移出自己 group = check_exist_group(group_id) my_membership = check_user_in_group(myself, group) others_membership = check_user_in_group(the_other, group) check_higher_permission(my_membership, others_membership) others_membership.delete() return Response()
def delete(request, group_id, username): """ 拒绝某人的加群申请 """ myself = request.user.user group = check_exist_group(group_id) the_other = check_exist_user(username) others_intention = check_exist_intention(the_other, group) my_membership = check_user_in_group(myself, group) check_user_is_master_or_manager(my_membership) others_intention.reject() return Response()
def put(request, group_id, username): """ 修改成员权限 """ myself = request.user.user the_other = check_exist_user(username) check_not_same(myself, the_other) # 不可修改自己的权限 group = check_exist_group(group_id) my_membership = check_user_in_group(myself, group) others_membership = check_user_in_group(the_other, group) check_user_is_master(my_membership) # 只可以修改权限为manager或member check_in_list(request.data.get('permission', None), ('manager', 'member')) serializer = check_serializer_is_valid(MyGroupSerializer, others_membership, request.data) serializer.save() return Response(serializer.data)