<commit_before>from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect, Http404
from django.template import RequestContext
from django.shortcuts import get_object_or_404, render_to_response

from django.contrib import messages
from django.contrib.auth.decorators import login_required

from agora.forms import ThreadForm, ReplyForm
from agora.models import ForumCategory, Forum, UserPostCount
from agora.models import ForumThread, ForumReply, ThreadSubscription


def ajax(func):
    def _wrapped_view(request, *args, **kwargs):
        try:
            return func(request, *args, **kwargs)
        except:
            import sys
            print sys.exc_info()
            raise
    return _wrapped_view


def forums(request):
    
    categories = ForumCategory.objects.filter(parent__isnull=True)
    categories = categories.order_by("title")
    
    most_active_forums = Forum.objects.order_by("-post_count")[:5]
    most_viewed_forums = Forum.objects.order_by("-view_count")[:5]
    most_active_members = UserPostCount.objects.order_by("-count")[:5]
    
    latest_posts = ForumReply.objects.order_by("-created")[:10]
    latest_threads = ForumThread.objects.order_by("-last_modified")
    most_active_threads = ForumThread.objects.order_by("-reply_count")
    most_viewed_threads = ForumThread.objects.order_by("-view_count")
    
    return render_to_response("agora/forums.html", {
        "categories": categories,
        "most_active_forums": most_active_forums,
        "most_viewed_forums": most_viewed_forums,
        "most_active_members": most_active_members,
        "latest_posts": latest_posts,
        "latest_threads": latest_threads,
        "most_active_threads": most_active_threads,
        "most_viewed_threads": most_viewed_threads,
    }, context_instance=RequestContext(request))


def forum_category(request, category_id):
    
    category = get_object_or_404(ForumCategory, id=category_id)
    
    forums = category.forums.order_by("title")
    
    return render_to_response("agora/category.html", {
        "category": category,
        "forums": forums,
    }, context_instance=RequestContext(request))


def forum(request, forum_id):
    
    forum = get_object_or_404(Forum, id=forum_id)
    threads = forum.threads.order_by("-sticky", "-last_modified")
    
    can_create_thread = request.user.has_perms("agora.add_forumthread", obj=forum)
    
    return render_to_response("agora/forum.html", {
        "forum": forum,
        "threads": threads,
        "can_create_thread": can_create_thread,
    }, context_instance=RequestContext(request))


def forum_thread(request, thread_id):
    thread = get_object_or_404(ForumThread, id=thread_id)
    
    can_create_reply = request.user.has_perms("agora.add_forumreply", obj=thread)
    
    if request.user.is_authenticated() and not thread.closed and can_create_reply:
        if request.method == "POST":
            reply_form = ReplyForm(request.POST)
            
            if form.is_valid():
                reply = reply_form.save(commit=False)
                reply.thread = thread
                reply.author = request.user
                reply.save()
                
                # subscribe the poster to the thread if requested (default value is True)
                if form.cleaned_data["subscribe"]:
                    thread.subscribe(reply.author, "email")
                
                # all users are automatically subscribed to onsite
                thread.subscribe(reply.author, "onsite")
                
                return HttpResponseRedirect(reverse("agora_thread", args=[thread.id]))
        else:
            reply_form = ReplyForm()
    else:
        reply_form = None
    
    order_type = request.GET.get("order_type", "asc")
    
    posts = ForumThread.objects.posts(thread, reverse=(order_type == "desc"))
    
    thread.inc_views()
    
    return render_to_response("agora/thread.html", {
        "thread": thread,
        "posts": posts,
        "order_type": order_type,
        "subscribed": thread.subscribed(request.user, "email"),
        "reply_form": reply_form,
        "can_create_reply": can_create_reply,
    }, context_instance=RequestContext(request))


@ajax
@login_required
def post_create(request, forum_id):
    
    member = request.user.get_profile()
    forum = get_object_or_404(Forum, id=forum_id)
    
    can_create_thread = request.user.has_perms("agora.add_forumthread", obj=forum)
    
    if not can_create_thread:
        messages.error(request, "You do not have permission to create a thread.")
        return HttpResponseRedirect(reverse("agora_forum", args=[forum.id]))
    
    if request.method == "POST":
        form = ThreadForm(request.POST)
        
        if form.is_valid():
            thread = form.save(commit=False)
            thread.forum = forum
            thread.author = request.user
            thread.save()
            
            # subscribe the poster to the thread if requested (default value is True)
            if form.cleaned_data["subscribe"]:
                thread.subscribe(thread.author, "email")
            
            # all users are automatically subscribed to onsite
            thread.subscribe(thread.author, "onsite")
            
            return HttpResponseRedirect(reverse("agora_thread", args=[thread.id]))
    else:
        form = ThreadForm()
    
    return render_to_response("agora/post_create.html", {
        "form": form,
        "member": member,
        "forum_id": forum_id,
    }, context_instance=RequestContext(request))


@ajax
@login_required
def reply_create(request, thread_id):
    
    member = request.user.get_profile()
    thread = get_object_or_404(ForumThread, id=thread_id)
    
    if thread.closed:
        messages.error(request, "This thread is closed.")
    
    can_create_reply = request.user.has_perms("agora.add_forumreply", obj=thread)
    
    if not can_create_reply:
        messages.error(request, "You do not have permission to reply to this thread.")
        return HttpResponseRedirect(reverse("agora_thread", args=[thread.id]))
    
    if request.method == "POST":
        form = ReplyForm(request.POST)
        
        if form.is_valid():
            reply = form.save(commit=False)
            reply.thread = thread
            reply.author = request.user
            reply.save()
            
            # subscribe the poster to the thread if requested (default value is True)
            if form.cleaned_data["subscribe"]:<commit_msg>fixed call to has_perm<commit_after>from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect, Http404
from django.template import RequestContext
from django.shortcuts import get_object_or_404, render_to_response

from django.contrib import messages
from django.contrib.auth.decorators import login_required

from agora.forms import ThreadForm, ReplyForm
from agora.models import ForumCategory, Forum, UserPostCount
from agora.models import ForumThread, ForumReply, ThreadSubscription


def ajax(func):
    def _wrapped_view(request, *args, **kwargs):
        try:
            return func(request, *args, **kwargs)
        except:
            import sys
            print sys.exc_info()
            raise
    return _wrapped_view


def forums(request):
    
    categories = ForumCategory.objects.filter(parent__isnull=True)
    categories = categories.order_by("title")
    
    most_active_forums = Forum.objects.order_by("-post_count")[:5]
    most_viewed_forums = Forum.objects.order_by("-view_count")[:5]
    most_active_members = UserPostCount.objects.order_by("-count")[:5]
    
    latest_posts = ForumReply.objects.order_by("-created")[:10]
    latest_threads = ForumThread.objects.order_by("-last_modified")
    most_active_threads = ForumThread.objects.order_by("-reply_count")
    most_viewed_threads = ForumThread.objects.order_by("-view_count")
    
    return render_to_response("agora/forums.html", {
        "categories": categories,
        "most_active_forums": most_active_forums,
        "most_viewed_forums": most_viewed_forums,
        "most_active_members": most_active_members,
        "latest_posts": latest_posts,
        "latest_threads": latest_threads,
        "most_active_threads": most_active_threads,
        "most_viewed_threads": most_viewed_threads,
    }, context_instance=RequestContext(request))


def forum_category(request, category_id):
    
    category = get_object_or_404(ForumCategory, id=category_id)
    
    forums = category.forums.order_by("title")
    
    return render_to_response("agora/category.html", {
        "category": category,
        "forums": forums,
    }, context_instance=RequestContext(request))


def forum(request, forum_id):
    
    forum = get_object_or_404(Forum, id=forum_id)
    threads = forum.threads.order_by("-sticky", "-last_modified")
    
    can_create_thread = request.user.has_perm("agora.add_forumthread", obj=forum)
    
    return render_to_response("agora/forum.html", {
        "forum": forum,
        "threads": threads,
        "can_create_thread": can_create_thread,
    }, context_instance=RequestContext(request))


def forum_thread(request, thread_id):
    thread = get_object_or_404(ForumThread, id=thread_id)
    
    can_create_reply = request.user.has_perm("agora.add_forumreply", obj=thread)
    
    if request.user.is_authenticated() and not thread.closed and can_create_reply:
        if request.method == "POST":
            reply_form = ReplyForm(request.POST)
            
            if form.is_valid():
                reply = reply_form.save(commit=False)
                reply.thread = thread
                reply.author = request.user
                reply.save()
                
                # subscribe the poster to the thread if requested (default value is True)
                if form.cleaned_data["subscribe"]:
                    thread.subscribe(reply.author, "email")
                
                # all users are automatically subscribed to onsite
                thread.subscribe(reply.author, "onsite")
                
                return HttpResponseRedirect(reverse("agora_thread", args=[thread.id]))
        else:
            reply_form = ReplyForm()
    else:
        reply_form = None
    
    order_type = request.GET.get("order_type", "asc")
    
    posts = ForumThread.objects.posts(thread, reverse=(order_type == "desc"))
    
    thread.inc_views()
    
    return render_to_response("agora/thread.html", {
        "thread": thread,
        "posts": posts,
        "order_type": order_type,
        "subscribed": thread.subscribed(request.user, "email"),
        "reply_form": reply_form,
        "can_create_reply": can_create_reply,
    }, context_instance=RequestContext(request))


@ajax
@login_required
def post_create(request, forum_id):
    
    member = request.user.get_profile()
    forum = get_object_or_404(Forum, id=forum_id)
    
    can_create_thread = request.user.has_perm("agora.add_forumthread", obj=forum)
    
    if not can_create_thread:
        messages.error(request, "You do not have permission to create a thread.")
        return HttpResponseRedirect(reverse("agora_forum", args=[forum.id]))
    
    if request.method == "POST":
        form = ThreadForm(request.POST)
        
        if form.is_valid():
            thread = form.save(commit=False)
            thread.forum = forum
            thread.author = request.user
            thread.save()
            
            # subscribe the poster to the thread if requested (default value is True)
            if form.cleaned_data["subscribe"]:
                thread.subscribe(thread.author, "email")
            
            # all users are automatically subscribed to onsite
            thread.subscribe(thread.author, "onsite")
            
            return HttpResponseRedirect(reverse("agora_thread", args=[thread.id]))
    else:
        form = ThreadForm()
    
    return render_to_response("agora/post_create.html", {
        "form": form,
        "member": member,
        "forum_id": forum_id,
    }, context_instance=RequestContext(request))


@ajax
@login_required
def reply_create(request, thread_id):
    
    member = request.user.get_profile()
    thread = get_object_or_404(ForumThread, id=thread_id)
    
    if thread.closed:
        messages.error(request, "This thread is closed.")
    
    can_create_reply = request.user.has_perm("agora.add_forumreply", obj=thread)
    
    if not can_create_reply:
        messages.error(request, "You do not have permission to reply to this thread.")
        return HttpResponseRedirect(reverse("agora_thread", args=[thread.id]))
    
    if request.method == "POST":
        form = ReplyForm(request.POST)
        
        if form.is_valid():
            reply = form.save(commit=False)
            reply.thread = thread
            reply.author = request.user
            reply.save()
            
            # subscribe the poster to the thread if requested (default value is True)
            if form.cleaned_data["subscribe"]:
{'fraction_non_alphanumeric': 0.09991610098390664, 'fraction_numerical': 0.003966135306231409, 'mean_word_length': 2.7313602731929425, 'pattern_counts': {'":': 50, '<': 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': '7435785', 'n_tokens_mistral': 3818, 'n_tokens_neox': 3717, 'n_words': 748}