<commit_before>import settings

from django.http import Http404, HttpResponseRedirect, HttpResponse
from django.shortcuts import render, redirect, get_object_or_404
from django.core.urlresolvers import reverse, NoReverseMatch
from django.db.models import Q

from models import Question, Response, Category, Company, Author
from forms import QuestionForm, ResponseForm
from utils import paginate
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger


ALLOWED_MODS = {
    'question': [
        'private', 'public',
        'delete', 'lock',
        'clear_accepted'
    ],
    'response': [
        'internal', 'inherit',
        'private', 'public',
        'delete', 'accept'
    ]
}


def get_my_questions(request):

    if settings.LOGIN_REQUIRED and not request.user.is_authenticated():
        return HttpResponseRedirect(settings.LOGIN_URL+"?next=%s" % request.path)

    if request.user.is_anonymous():
        return None
    else:
        return Question.objects.can_view(request.user)\
                               .filter(user=request.user)


def knowledge_index(request,
                    template='django_knowledge/index.html'):

    if settings.LOGIN_REQUIRED and not request.user.is_authenticated():
        return HttpResponseRedirect(settings.LOGIN_URL+"?next=%s" % request.path)

    questions = Question.objects.can_view(request.user)\
                                .prefetch_related('responses__question')[0:20]

    questions_pop = Question.objects.can_view(request.user)\
                            .prefetch_related('responses__question')
    questions_pop = questions_pop.order_by('-hits')
    # this is for get_responses()
    [setattr(q, '_requesting_user', request.user) for q in questions]
    author = ''
    try:
        author = Author.objects.get(user=request.user)
    except:
        pass

    paginator = Paginator(questions, 5)
    page = request.GET.get('page')
    try:
        articles = paginator.page(page)
    except PageNotAnInteger:
        articles = paginator.page(1)
    except EmptyPage:
        articles = paginator.page(paginator.num_pages)

    return render(request, template, {
        'request': request,
        'questions': questions,
        'author': author,
        'questions_pop': questions_pop,
        'articles': articles,
        'my_questions': get_my_questions(request),
        'categories': Category.objects.all(),
        'BASE_TEMPLATE' : settings.BASE_TEMPLATE,
    })


def knowledge_list(request,
                   category_slug=None,
                   template='django_knowledge/list.html',
                   Form=QuestionForm):

    if settings.LOGIN_REQUIRED and not request.user.is_authenticated():
        return HttpResponseRedirect(settings.LOGIN_URL+"?next=%s" % request.path)

    search = request.GET.get('title', None)
    questions = Question.objects.can_view(request.user)\
                                .prefetch_related('responses__question')

    if search:
        questions = questions.filter(
            Q(title__icontains=search) | Q(body__icontains=search)
        )

    category = None
    if category_slug:
        category = get_object_or_404(Category, slug=category_slug)
        questions = questions.filter(categories=category)

    # this is for get_responses()
    [setattr(q, '_requesting_user', request.user) for q in questions]
    author = ''
    try:
        author = Author.objects.get(user=request.user)
    except:
        pass

    paginator = Paginator(questions, 5)
    page = request.GET.get('page')
    try:
        articles = paginator.page(page)
    except PageNotAnInteger:
        articles = paginator.page(1)
    except EmptyPage:
        articles = paginator.page(paginator.num_pages)

    return render(request, template, {
        'request': request,
        'search': search,
        'questions': questions,
        'articles': articles,
        'author': author,
        'my_questions': get_my_questions(request),
        'category': category,
        'categories': Category.objects.all(),
        'form': Form(request.user, initial={'title': search}),  # prefill title
        'BASE_TEMPLATE' : settings.BASE_TEMPLATE,
    })


def knowledge_thread(request,
                     question_id,
                     slug=None,
                     template='django_knowledge/thread.html',
                     Form=ResponseForm):
<commit_msg>Code for lists at index<commit_after>import settings

from django.http import Http404, HttpResponseRedirect, HttpResponse
from django.shortcuts import render, redirect, get_object_or_404
from django.core.urlresolvers import reverse, NoReverseMatch
from django.db.models import Q

from models import Question, Response, Category, Company, Author
from forms import QuestionForm, ResponseForm
from utils import paginate
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger


ALLOWED_MODS = {
    'question': [
        'private', 'public',
        'delete', 'lock',
        'clear_accepted'
    ],
    'response': [
        'internal', 'inherit',
        'private', 'public',
        'delete', 'accept'
    ]
}


def get_my_questions(request):

    if settings.LOGIN_REQUIRED and not request.user.is_authenticated():
        return HttpResponseRedirect(settings.LOGIN_URL+"?next=%s" % request.path)

    if request.user.is_anonymous():
        return None
    else:
        return Question.objects.can_view(request.user)\
                               .filter(user=request.user)


def knowledge_index(request,
                    template='django_knowledge/index.html'):

    if settings.LOGIN_REQUIRED and not request.user.is_authenticated():
        return HttpResponseRedirect(settings.LOGIN_URL+"?next=%s" % request.path)

    questions = Question.objects.can_view(request.user)\
                                .prefetch_related('responses__question')[0:20]

    questions_pop = Question.objects.can_view(request.user)\
                            .prefetch_related('responses__question')
    questions_pop = questions_pop.order_by('-hits')
    questions_rec = None
    if Question.objects.can_view(request.user) & Question.objects.filter(recommended=True):
        questions_rec = Question.objects.filter(recommended=True)
        questions_rec = questions_rec.order_by('-lastchanged')


    # this is for get_responses()
    [setattr(q, '_requesting_user', request.user) for q in questions]
    author = ''
    try:
        author = Author.objects.get(user=request.user)
    except:
        pass

    paginator = Paginator(questions, 5)
    page = request.GET.get('page')
    try:
        articles = paginator.page(page)
    except PageNotAnInteger:
        articles = paginator.page(1)
    except EmptyPage:
        articles = paginator.page(paginator.num_pages)

    return render(request, template, {
        'request': request,
        'questions': questions,
        'author': author,
        'questions_rec': questions_rec,
        'questions_pop': questions_pop,
        'articles': articles,
        'my_questions': get_my_questions(request),
        'categories': Category.objects.all(),
        'BASE_TEMPLATE' : settings.BASE_TEMPLATE,
    })


def knowledge_list(request,
                   category_slug=None,
                   template='django_knowledge/list.html',
                   Form=QuestionForm):

    if settings.LOGIN_REQUIRED and not request.user.is_authenticated():
        return HttpResponseRedirect(settings.LOGIN_URL+"?next=%s" % request.path)

    search = request.GET.get('title', None)
    questions = Question.objects.can_view(request.user)\
                                .prefetch_related('responses__question')

    if search:
        questions = questions.filter(
            Q(title__icontains=search) | Q(body__icontains=search)
        )

    category = None
    if category_slug:
        category = get_object_or_404(Category, slug=category_slug)
        questions = questions.filter(categories=category)

    # this is for get_responses()
    [setattr(q, '_requesting_user', request.user) for q in questions]
    author = ''
    try:
        author = Author.objects.get(user=request.user)
    except:
        pass

    paginator = Paginator(questions, 5)
    page = request.GET.get('page')
    try:
        articles = paginator.page(page)
    except PageNotAnInteger:
        articles = paginator.page(1)
    except EmptyPage:
        articles = paginator.page(paginator.num_pages)


    return render(request, template, {
        'request': request,
        'search': search,
        'questions': questions,
        'articles': articles,
        'author': author,
        'my_questions': get_my_questions(request),
        'category': category,
        'categories': Category.objects.all(),
        'form': Form(request.user, initial={'title': search}),  # prefill title
        'BASE_TEMPLATE' : settings.BASE_TEMPLATE,
    })


def knowledge_thread(request,
                     question_id,
                     slug=None,
                     template='django_knowledge/thread.html',
                     Form=ResponseForm):

{'fraction_non_alphanumeric': 0.10528629519727442, 'fraction_numerical': 0.0035168699857127156, 'mean_word_length': 2.660498793242156, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5153697', 'n_tokens_mistral': 2639, 'n_tokens_neox': 2524, 'n_words': 513}