<commit_before>from django.views.generic import View, TemplateView, ListView
from django.contrib.auth import authenticate, login, logout
from django.http.response import JsonResponse, HttpResponseRedirect, HttpResponse
from django.shortcuts import render
from django.http import QueryDict
from django.urls import reverse_lazy, reverse
from django.contrib.auth.models import User, Group
from django.contrib.auth.mixins import LoginRequiredMixin

from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required, permission_required
from permissions.mixins import MyPermissionRequiredMixin

class UserLoginView(View):
    def post(self, request, *args, **kwargs):
        username = request.POST.get('username', None)
        password = request.POST.get('password', None)
        user = authenticate(username = username, password = password)
        response = {'status': 0, 'errmsg': ''}
        if user:
            login(request, user)
            response['next_url'] = request.GET.get('next') if request.GET.get('next', None) else '/'
        else:
            response['status'] = 1
            response['errmsg'] = '用户名密码错误'
        return JsonResponse(response)

    def get(self, request, *args, **kwargs):
        return render(request, 'public/login.html')

class UserLogoutView(View):
    def get(self, request, *args, **kwargs):
        logout(request)
        return HttpResponseRedirect(reverse('user_login'))

    def post(self, request, *args, **kwargs):
        logout(request)
        return HttpResponseRedirect(reverse('user_login'))

class UserLoginTplView(TemplateView):
    template_name = 'public/login.html'

    def post(self, request, *args, **kwargs):
        username = request.POST.get('username', None)
        password = request.POST.get('password', None)
        user = authenticate(username=username, password=password)
        response = {'status': 0}
        if user:
            login(request, user)
            response['next_url'] = request.GET.get('next') if request.GET.get('next', None) else '/'
        else:
            response['status'] = 1
            response['errmsg'] = '用户名密码错误'
        return JsonResponse(response)

class UserlogoutTplView(TemplateView):
    def get(self, request, *args, **kwargs):
        logout(request)
        return HttpResponseRedirect(reverse('user_login'))

class UserListView(LoginRequiredMixin, ListView):
    '''
    template_name = 'user/userlist.html'
    model = User
    paginate_by = 10
    before_range_num = 4
    after_range_num = 5
    
    def get_queryset(self):
        queryset = super(UserListView, self).get_queryset()
        queryset = queryset.filter(is_superuser=False)
        return queryset

    def get_page_range(self, page_obj):
        current_index = page_obj.number
        start = current_index - self.before_range_num
        end = current_index + self.after_range_num
        if start <= 0:
            start = 1
        if end > page_obj.paginator.num_pages:
            end = page_obj.paginator.num_pages
        return range(start, end+1)
    
    def get_context_data(self, **kwargs):
        context = super(UserListView, self).get_context_data(**kwargs)
        context['page_range_obj'] = self.get_page_range(context['page_obj'])
        return context
    '''
    template_name = 'user/userlist.html'
    model = User
    paginate_by = 10
    before_range_num = 4
    after_range_num = 5
    ordering = 'id'

    def get_queryset(self):
        queryset = super(UserListView, self).get_queryset()
        queryset = queryset.filter(is_superuser=False)
        #查询条件
        username = self.request.GET.get('username', None)
        if username:
            queryset = queryset.filter(username__contains=username)
        return queryset

    def get_page_range(self, page_obj):
        current_index = page_obj.number
        start = current_index - self.before_range_num
        end = current_index + self.after_range_num
        if start <= 0:
            start = 1
        if end > page_obj.paginator.num_pages:
            end = page_obj.paginator.num_pages
        return range(start, end+1)

    def get_context_data(self, **kwargs):
        context = super(UserListView, self).get_context_data(**kwargs)
        context['page_range_obj'] = self.get_page_range(context['page_obj'])

        #处理查询条件
        search_data = self.request.GET.copy()
        try:
            search_data.pop("page")
        except:
            pass
        context.update(search_data.dict())
        context['search_data'] = "&" + search_data.urlencode()
        return context

    #权限验证，类视图，需要装饰在view的get方法上，没有get方法重写get方法
    @method_decorator(permission_required("auth.add_user",login_url=reverse_lazy("error" ,kwargs={"next":"index", "msg":"没有相应的权限"})))
    def get(self, request, *args, **kwargs):
        return super(UserListView, self).get(request, *args, **kwargs)

class ModifyUserStatusView(LoginRequiredMixin, View):

    def post(self, request):
        response = {}
        if request.user.has_perm('auth.change_user'):
            uid = request.POST.get('uid', None)
            try:
                user_obj = User.objects.get(id=uid)
                if user_obj.is_active:
                    user_obj.is_active = False
                else:
                    user_obj.is_active = True
                user_obj.save()
                response['status'] = 0
            except User.DoesNotExist:
                response['status'] = 1
                response['errmsg'] = "用户不存在"
            return JsonResponse(response)
        else:
            response['status'] = 1<commit_msg>动态修改page<commit_after>from django.views.generic import View, TemplateView, ListView
from django.contrib.auth import authenticate, login, logout
from django.http.response import JsonResponse, HttpResponseRedirect, HttpResponse
from django.shortcuts import render
from django.http import QueryDict
from django.urls import reverse_lazy, reverse
from django.contrib.auth.models import User, Group
from django.contrib.auth.mixins import LoginRequiredMixin

from django.utils.decorators import method_decorator
from django.contrib.auth.decorators import login_required, permission_required
from permissions.mixins import MyPermissionRequiredMixin

class UserLoginView(View):
    def post(self, request, *args, **kwargs):
        username = request.POST.get('username', None)
        password = request.POST.get('password', None)
        user = authenticate(username = username, password = password)
        response = {'status': 0, 'errmsg': ''}
        if user:
            login(request, user)
            response['next_url'] = request.GET.get('next') if request.GET.get('next', None) else '/'
        else:
            response['status'] = 1
            response['errmsg'] = '用户名密码错误'
        return JsonResponse(response)

    def get(self, request, *args, **kwargs):
        return render(request, 'public/login.html')

class UserLogoutView(View):
    def get(self, request, *args, **kwargs):
        logout(request)
        return HttpResponseRedirect(reverse('user_login'))

    def post(self, request, *args, **kwargs):
        logout(request)
        return HttpResponseRedirect(reverse('user_login'))

class UserLoginTplView(TemplateView):
    template_name = 'public/login.html'

    def post(self, request, *args, **kwargs):
        username = request.POST.get('username', None)
        password = request.POST.get('password', None)
        user = authenticate(username=username, password=password)
        response = {'status': 0}
        if user:
            login(request, user)
            response['next_url'] = request.GET.get('next') if request.GET.get('next', None) else '/'
        else:
            response['status'] = 1
            response['errmsg'] = '用户名密码错误'
        return JsonResponse(response)

class UserlogoutTplView(TemplateView):
    def get(self, request, *args, **kwargs):
        logout(request)
        return HttpResponseRedirect(reverse('user_login'))

class UserListView(LoginRequiredMixin, ListView):
    '''
    template_name = 'user/userlist.html'
    model = User
    paginate_by = 10
    before_range_num = 4
    after_range_num = 5
    
    def get_queryset(self):
        queryset = super(UserListView, self).get_queryset()
        queryset = queryset.filter(is_superuser=False)
        return queryset

    def get_page_range(self, page_obj):
        current_index = page_obj.number
        start = current_index - self.before_range_num
        end = current_index + self.after_range_num
        if start <= 0:
            start = 1
        if end > page_obj.paginator.num_pages:
            end = page_obj.paginator.num_pages
        return range(start, end+1)
    
    def get_context_data(self, **kwargs):
        context = super(UserListView, self).get_context_data(**kwargs)
        context['page_range_obj'] = self.get_page_range(context['page_obj'])
        return context
    '''
    template_name = 'user/userlist.html'
    model = User
    paginate_by = 10
    before_range_num = 4
    after_range_num = 5
    ordering = 'id'

    def get_queryset(self):
        queryset = super(UserListView, self).get_queryset()
        queryset = queryset.filter(is_superuser=False)
        #查询条件
        username = self.request.GET.get('username', None)
        if username:
            queryset = queryset.filter(username__contains=username)
        return queryset

    def get_page_range(self, page_obj):
        current_index = page_obj.number
        start = current_index - self.before_range_num
        end = current_index + self.after_range_num
        if start <= 0:
            start = 1
        if end > page_obj.paginator.num_pages:
            end = page_obj.paginator.num_pages
        return range(start, end+1)

    def get_context_data(self, **kwargs):
        self.set_paginate_by(2)
        context = super(UserListView, self).get_context_data(**kwargs)
        context['page_range_obj'] = self.get_page_range(context['page_obj'])

        #处理查询条件
        search_data = self.request.GET.copy()
        try:
            search_data.pop("page")
        except:
            pass
        context.update(search_data.dict())
        context['search_data'] = "&" + search_data.urlencode()
        return context

    def set_paginate_by(self, user_paginate_by):
        if user_paginate_by:
            self.paginate_by = user_paginate_by

    #权限验证，类视图，需要装饰在view的get方法上，没有get方法重写get方法
    @method_decorator(permission_required("auth.add_user",login_url=reverse_lazy("error" ,kwargs={"next":"index", "msg":"没有相应的权限"})))
    def get(self, request, *args, **kwargs):
        return super(UserListView, self).get(request, *args, **kwargs)

class ModifyUserStatusView(LoginRequiredMixin, View):

    def post(self, request):
        response = {}
        if request.user.has_perm('auth.change_user'):
            uid = request.POST.get('uid', None)
            try:
                user_obj = User.objects.get(id=uid)
                if user_obj.is_active:
                    user_obj.is_active = False
                else:
                    user_obj.is_active = True
                user_obj.save()
                response['status'] = 0
            except User.DoesNotExist:
                response['status'] = 1
                response['errmsg'] = "用户不存在"
            return JsonResponse(response)
        else:
            response['status'] = 1
{'fraction_non_alphanumeric': 0.1087449816721941, 'fraction_numerical': 0.0037528364461511606, 'mean_word_length': 2.7192469977280105, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4515760', 'n_tokens_mistral': 3429, 'n_tokens_neox': 3359, 'n_words': 697}