<commit_before>from django.conf import settings
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.utils.datastructures import SortedDict
from django.core.paginator import Paginator
from django.utils.translation import ugettext, ugettext_lazy as _

from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType

from threadedcomments.models import ThreadedComment
from threadedcomments.forms import RichCommentForm

from issues.models import Issue, IssueContributor
from issues.forms import IssueForm, InviteContributorForm, ResolutionForm

from disciplines.models import Discipline
from tagging.models import Tag


@login_required
def create(request, form_class=IssueForm, template_name="issues/create.html"):
    issue_form = form_class(request.POST or None, auto_id='new_issue_%s')
    
    if issue_form.is_valid():
        issue = issue_form.save(commit=False)
        issue.creator = request.user
        issue.save()
        issue_form.save_m2m() # to save disciplines after commit=False
        
        issue.register_action(request.user, 'create', issue)
        
        issue_contributor = IssueContributor(issue=issue, user=request.user)
        issue.contributors.add(issue_contributor)
        issue_contributor.save()
        
        return HttpResponseRedirect(issue.get_absolute_url())
    
    return render_to_response(template_name, {
        "issue_form": issue_form,
    }, context_instance=RequestContext(request))

@login_required
def edit(request, slug=None, form_class=IssueForm, template_name="issues/edit.html"):
    issue = get_object_or_404(Issue, slug=slug)
    
    if issue.creator != request.user:
        return render_to_response('issues/forbidden.html', {}, context_instance=RequestContext(request))
    
    issue_form = form_class(request.POST or None, instance=issue, auto_id='edit_issue_%s')
    
    if issue_form.is_valid():
        issue = issue_form.save()
        
        issue.register_action(request.user, 'edit', issue)
        
        return HttpResponseRedirect(issue.get_absolute_url())
    
    return render_to_response(template_name, {
        'issue': issue,
        'issue_form': issue_form,
    }, context_instance=RequestContext(request))

@login_required
def delete(request, slug=None, redirect_url=None):
    issue = get_object_or_404(Issue, slug=slug)
    redirect_url = issue.get_absolute_url()
    
    if request.user == issue.creator:
        if not Paper.objects.get_for_object(issue).exists():
            if not ThreadedComment.objects.get_for_object(issue).exists():
                issue.feed.delete()
                issue.delete()
                messages.add_message(request, messages.SUCCESS,
                    _("Issue %s deleted.") % issue.title
                )
                redirect_url = reverse("issue_list")
            else:
                messages.add_message(request, messages.ERROR,
                    _("Please delete comments before deleting the issue.")
                )
        else:
            messages.add_message(request, messages.SUCCESS,
                _("Please delete papers before deleting the issue.")
            )
    else:
        messages.add_message(request, messages.SUCCESS,
            _("You are not the creator of this issue.")
        )
    
    return HttpResponseRedirect(redirect_url)

def issues(request, mine=False, template_name="issues/issues.html"):
    authenticated = request.user.is_authenticated()
    
    if authenticated and mine:
        issues = Issue.objects.filter(contributors__contains=request.user)
    else:
        issues = Issue.objects.filter(private=False)
    
    search_terms = request.GET.get('search', '')
    if search_terms:
        issues = (issues.filter(title__icontains=search_terms) |
            issues.filter(description__icontains=search_terms))
    
    # Additional filtering
    discipline = request.GET.get('discipline', None)
    tag = request.GET.get('tag', None)
    if discipline:
        try:
            the_discipline = Discipline.objects.get(slug=discipline) # make sure the discpline exists
            issues = issues.filter(disciplines__id=the_discipline.id)
        except Discipline.DoesNotExist:
            messages.add_message(request, messages.ERROR, _("That discipline does not exist."))
    if tag:
        try:
            the_tag = Tag.objects.get(id=tag) # make sure the tag exists
            issues = issues.filter(tags__id=the_tag.id)
        except Tag.DoesNotExist:
            messages.add_message(request, messages.ERROR, _("That tag does not exist."))
    
    # Figure out sorting to replace the title
    list_title = ''
    if mine:
        list_title += 'My '<commit_msg>Filters work and links work. Now I just need to fix the filter_url (issues templatetag) to check if the get var is already set.<commit_after>from django.conf import settings
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.utils.datastructures import SortedDict
from django.core.paginator import Paginator
from django.utils.translation import ugettext, ugettext_lazy as _

from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType

from threadedcomments.models import ThreadedComment
from threadedcomments.forms import RichCommentForm

from issues.models import Issue, IssueContributor
from issues.forms import IssueForm, InviteContributorForm, ResolutionForm

from disciplines.models import Discipline
from tagging.models import Tag, TaggedItem


@login_required
def create(request, form_class=IssueForm, template_name="issues/create.html"):
    issue_form = form_class(request.POST or None, auto_id='new_issue_%s')
    
    if issue_form.is_valid():
        issue = issue_form.save(commit=False)
        issue.creator = request.user
        issue.save()
        issue_form.save_m2m() # to save disciplines after commit=False
        
        issue.register_action(request.user, 'create', issue)
        
        issue_contributor = IssueContributor(issue=issue, user=request.user)
        issue.contributors.add(issue_contributor)
        issue_contributor.save()
        
        return HttpResponseRedirect(issue.get_absolute_url())
    
    return render_to_response(template_name, {
        "issue_form": issue_form,
    }, context_instance=RequestContext(request))

@login_required
def edit(request, slug=None, form_class=IssueForm, template_name="issues/edit.html"):
    issue = get_object_or_404(Issue, slug=slug)
    
    if issue.creator != request.user:
        return render_to_response('issues/forbidden.html', {}, context_instance=RequestContext(request))
    
    issue_form = form_class(request.POST or None, instance=issue, auto_id='edit_issue_%s')
    
    if issue_form.is_valid():
        issue = issue_form.save()
        
        issue.register_action(request.user, 'edit', issue)
        
        return HttpResponseRedirect(issue.get_absolute_url())
    
    return render_to_response(template_name, {
        'issue': issue,
        'issue_form': issue_form,
    }, context_instance=RequestContext(request))

@login_required
def delete(request, slug=None, redirect_url=None):
    issue = get_object_or_404(Issue, slug=slug)
    redirect_url = issue.get_absolute_url()
    
    if request.user == issue.creator:
        if not Paper.objects.get_for_object(issue).exists():
            if not ThreadedComment.objects.get_for_object(issue).exists():
                issue.feed.delete()
                issue.delete()
                messages.add_message(request, messages.SUCCESS,
                    _("Issue %s deleted.") % issue.title
                )
                redirect_url = reverse("issue_list")
            else:
                messages.add_message(request, messages.ERROR,
                    _("Please delete comments before deleting the issue.")
                )
        else:
            messages.add_message(request, messages.SUCCESS,
                _("Please delete papers before deleting the issue.")
            )
    else:
        messages.add_message(request, messages.SUCCESS,
            _("You are not the creator of this issue.")
        )
    
    return HttpResponseRedirect(redirect_url)

def issues(request, mine=False, template_name="issues/issues.html"):
    authenticated = request.user.is_authenticated()
    
    if authenticated and mine:
        issues = Issue.objects.filter(contributors__contains=request.user)
    else:
        issues = Issue.objects.filter(private=False)
    
    search_terms = request.GET.get('search', '')
    if search_terms:
        issues = (issues.filter(title__icontains=search_terms) |
            issues.filter(description__icontains=search_terms))
    
    # Additional filtering
    discipline = request.GET.get('discipline', None)
    tag = request.GET.get('tag', None)
    if discipline:
        try:
            the_discipline = Discipline.objects.get(slug=discipline) # make sure the discpline exists
            issues = issues.filter(disciplines=the_discipline)
        except Discipline.DoesNotExist:
            messages.add_message(request, messages.ERROR, _("That discipline does not exist."))
    if tag:
        try:
            the_tag = Tag.objects.get(id=tag) # make sure the tag exists
            issues = TaggedItem.objects.get_by_model(issues, the_tag)
        except Tag.DoesNotExist:
            messages.add_message(request, messages.ERROR, _("That tag does not exist."))
    
    # Figure out sorting to replace the title
    list_title = ''
    if mine:
        list_title += 'My '
{'fraction_non_alphanumeric': 0.08792299295425225, 'fraction_numerical': 0.0019847176739108863, 'mean_word_length': 3.0884381338742393, 'pattern_counts': {'":': 2, '<': 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': '5869517', 'n_tokens_mistral': 2851, 'n_tokens_neox': 2746, 'n_words': 623}