<commit_before># coding=utf-8
from django.shortcuts import render
from django.db import IntegrityError

from rest_framework.views import APIView

from utils.shortcuts import error_response, serializer_invalid_response, success_response, paginate
from account.models import REGULAR_USER, ADMIN, SUPER_ADMIN
from account.decorators import login_required

from .models import Group, JoinGroupRequest, UserGroupRelation
from .serializers import (CreateGroupSerializer, EditGroupSerializer,
                          CreateJoinGroupRequestSerializer, GroupSerializer,
                          GroupMemberSerializer, EditGroupMemberSerializer,
                          JoinGroupRequestSerializer, PutJoinGroupRequestSerializer)


class GroupAPIViewBase(object):
    def get_group(self, request, group_id):
        """
        根据group_id查询指定的小组的信息，结合判断用户权限
        管理员可以查询所有的小组，其他用户查询自己创建的自傲组
        """
        if request.user.admin_type == SUPER_ADMIN:
            group = Group.objects.get(id=group_id, visible=True)
        else:
            group = Group.objects.get(id=group_id, visible=True, admin=request.user)
        return group
        
    def get_groups(self, request):
        """
        如果是超级管理员，就返回全部的小组
        如果是管理员，就返回他创建的全部小组
        """
        if request.user.admin_type == SUPER_ADMIN:
            groups = Group.objects.filter(visible=True)
        else:
            groups = Group.objects.filter(admin=request.user, visible=True)
        return groups


class GroupAdminAPIView(APIView, GroupAPIViewBase):
    def post(self, request):
        """
        创建小组的api
        ---
        request_serializer: CreateGroupSerializer
        response_serializer: GroupSerializer
        """
        serializer = CreateGroupSerializer(data=request.data)
        if serializer.is_valid():
            data = serializer.data
            try:
                group = Group.objects.create(name=data["name"],
                                             description=data["description"],
                                             join_group_setting=data["join_group_setting"],
                                             admin=request.user)
            except IntegrityError:
                return error_response(u"小组名已经存在")
            return success_response(GroupSerializer(group).data)
        else:
            return serializer_invalid_response(serializer)

    def put(self, request):
        """
        修改小组信息的api
        ---
        request_serializer: EditGroupSerializer
        response_serializer: GroupSerializer
        """
        serializer = EditGroupSerializer(data=request.data)
        if serializer.is_valid():
            data = serializer.data
            try:
                group = self.get_group(request, data["group_id"])
            except Group.DoesNotExist:
                return error_response(u"小组不存在")
            try:
                group.name = data["name"]
                group.description = data["description"]
                group.join_group_setting = data["join_group_setting"]
                group.save()
            except IntegrityError:
                return error_response(u"小组名已经存在")

            return success_response(GroupSerializer(group).data)
        else:
            return serializer_invalid_response(serializer)

    def get(self, request):
        """
        查询小组列表或者单个小组的信息，查询单个小组需要传递group_id参数，否则返回全部
        ---
        response_serializer: GroupSerializer
        """
        group_id = request.GET.get("group_id", None)
        if group_id:
            try:
                group = self.get_group(request, group_id)
                return success_response(GroupSerializer(group).data)
            except Group.DoesNotExist:
                return error_response(u"小组不存在")
        else:
            groups = self.get_groups(request)
            if request.GET.get("keyword", None):
                groups = groups.filter(name__contains=request.GET["keyword"])
            return paginate(request, groups, GroupSerializer)
            
            <commit_msg>增加小组筛选条件<commit_after># coding=utf-8
from django.shortcuts import render
from django.db import IntegrityError

from rest_framework.views import APIView

from utils.shortcuts import error_response, serializer_invalid_response, success_response, paginate
from account.models import REGULAR_USER, ADMIN, SUPER_ADMIN
from account.decorators import login_required

from .models import Group, JoinGroupRequest, UserGroupRelation
from .serializers import (CreateGroupSerializer, EditGroupSerializer,
                          CreateJoinGroupRequestSerializer, GroupSerializer,
                          GroupMemberSerializer, EditGroupMemberSerializer,
                          JoinGroupRequestSerializer, PutJoinGroupRequestSerializer)


class GroupAPIViewBase(object):
    def get_group(self, request, group_id):
        """
        根据group_id查询指定的小组的信息，结合判断用户权限
        管理员可以查询所有的小组，其他用户查询自己创建的自傲组
        """
        if request.user.admin_type == SUPER_ADMIN:
            group = Group.objects.get(id=group_id, visible=True)
        else:
            group = Group.objects.get(id=group_id, visible=True, admin=request.user)
        return group
        
    def get_groups(self, request):
        """
        如果是超级管理员，就返回全部的小组
        如果是管理员，就返回他创建的全部小组
        """
        if request.user.admin_type == SUPER_ADMIN:
            groups = Group.objects.filter(visible=True)
        else:
            groups = Group.objects.filter(admin=request.user, visible=True)
        return groups


class GroupAdminAPIView(APIView, GroupAPIViewBase):
    def post(self, request):
        """
        创建小组的api
        ---
        request_serializer: CreateGroupSerializer
        response_serializer: GroupSerializer
        """
        serializer = CreateGroupSerializer(data=request.data)
        if serializer.is_valid():
            data = serializer.data
            try:
                group = Group.objects.create(name=data["name"],
                                             description=data["description"],
                                             join_group_setting=data["join_group_setting"],
                                             admin=request.user)
            except IntegrityError:
                return error_response(u"小组名已经存在")
            return success_response(GroupSerializer(group).data)
        else:
            return serializer_invalid_response(serializer)

    def put(self, request):
        """
        修改小组信息的api
        ---
        request_serializer: EditGroupSerializer
        response_serializer: GroupSerializer
        """
        serializer = EditGroupSerializer(data=request.data)
        if serializer.is_valid():
            data = serializer.data
            try:
                group = self.get_group(request, data["group_id"])
            except Group.DoesNotExist:
                return error_response(u"小组不存在")
            try:
                group.name = data["name"]
                group.description = data["description"]
                group.join_group_setting = data["join_group_setting"]
                group.save()
            except IntegrityError:
                return error_response(u"小组名已经存在")

            return success_response(GroupSerializer(group).data)
        else:
            return serializer_invalid_response(serializer)

    def get(self, request):
        """
        查询小组列表或者单个小组的信息，查询单个小组需要传递group_id参数，否则返回全部
        ---
        response_serializer: GroupSerializer
        """
        group_id = request.GET.get("group_id", None)
        # 根据 id 查询小组信息
        if group_id:
            try:
                group = self.get_group(request, group_id)
                return success_response(GroupSerializer(group).data)
            except Group.DoesNotExist:
                return error_response(u"小组不存在")
        else:
            groups = self.get_groups(request)
            # 搜索小组
            if request.GET.get("keyword", None):
                groups = groups.filter(name__contains=request.GET["keyword"])
            # 只返回我创建的小组 适用于超级管理员
            if request.GET.get("my_group", None):
                groups = groups.filter(admin=request.user)
            # 只返回指定用户的小组 适用于管理员
            elif request.GET.get("admin_id", None):
                groups = groups.filter(admin__id=request.GET["admin_id"])
            return paginate(request, groups, GroupSerializer)
            
            
{'fraction_non_alphanumeric': 0.08, 'fraction_numerical': 0.00023738872403560832, 'mean_word_length': 2.0017812611328822, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7608879', 'n_tokens_mistral': 2357, 'n_tokens_neox': 2356, 'n_words': 396}