<commit_before>from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.utils.datastructures import SortedDict

from django.conf import settings

if "notification" in settings.INSTALLED_APPS:
    from notification import models as notification
else:
    notification = None

from newtribes.models import Tribe
from newtribes.forms import TribeForm, TribeUpdateForm

TOPIC_COUNT_SQL = """
SELECT COUNT(*)
FROM topics_topic
WHERE
    topics_topic.object_id = newtribes_tribe.id AND
    topics_topic.content_type_id = %s
"""
MEMBER_COUNT_SQL = """
SELECT COUNT(*)
FROM newtribes_tribe_members
WHERE newtribes_tribe_members.tribe_id = newtribes_tribe.id
"""

@login_required
def create(request, form_class=TribeForm, template_name="newtribes/create.html"):
    tribe_form = form_class(request.POST or None)
    
    if tribe_form.is_valid():
        tribe = tribe_form.save(commit=False)
        tribe.creator = request.user
        tribe.save()
        tribe.members.add(request.user)
        tribe.save()
        if notification:
            # @@@ might be worth having a shortcut for sending to all users
            notification.send(User.objects.all(), "tribes_new_tribe",
                {"tribe": tribe}, queue=True)
        return HttpResponseRedirect(tribe.get_absolute_url())
    
    return render_to_response(template_name, {
        "tribe_form": tribe_form,
    }, context_instance=RequestContext(request))


def tribes(request, template_name="newtribes/tribes.html"):
    
    ctype = ContentType.objects.get_for_model(Tribe)
    
    tribes = Tribe.objects.all()
    search_terms = request.GET.get('search', '')
    if search_terms:
        tribes = (tribes.filter(name__icontains=search_terms) |
            tribes.filter(description__icontains=search_terms))
    
    content_type = ContentType.objects.get_for_model(Tribe)
    
    tribes = tribes.extra(select=SortedDict([
        ('member_count', MEMBER_COUNT_SQL),
        ('topic_count', TOPIC_COUNT_SQL),
    ]), select_params=(content_type.id,))
    
    return render_to_response(template_name, {
        'tribes': tribes,
    }, context_instance=RequestContext(request))


def delete(request, group_slug=None, redirect_url=None):
    tribe = get_object_or_404(Tribe, slug=group_slug)
    if not redirect_url:
        redirect_url = reverse('tribe_list')
    
    # @@@ eventually, we'll remove restriction that tribe.creator can't leave tribe but we'll still require tribe.members.all().count() == 1
    if (request.user.is_authenticated() and request.method == "POST" and
            request.user == tribe.creator and tribe.members.all().count() == 1):
        tribe.delete()
        request.user.message_set.create(message=_("Tribe %s deleted.") % (
            tribe,))
        # no notification required as the deleter must be the only member
    
    return HttpResponseRedirect(redirect_url)<commit_msg>Ensure that the search results on tribes persist, like they used ot.<commit_after>from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.http import HttpResponseRedirect
from django.core.urlresolvers import reverse
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.contrib.contenttypes.models import ContentType
from django.utils.datastructures import SortedDict

from django.conf import settings

if "notification" in settings.INSTALLED_APPS:
    from notification import models as notification
else:
    notification = None

from newtribes.models import Tribe
from newtribes.forms import TribeForm, TribeUpdateForm

TOPIC_COUNT_SQL = """
SELECT COUNT(*)
FROM topics_topic
WHERE
    topics_topic.object_id = newtribes_tribe.id AND
    topics_topic.content_type_id = %s
"""
MEMBER_COUNT_SQL = """
SELECT COUNT(*)
FROM newtribes_tribe_members
WHERE newtribes_tribe_members.tribe_id = newtribes_tribe.id
"""

@login_required
def create(request, form_class=TribeForm, template_name="newtribes/create.html"):
    tribe_form = form_class(request.POST or None)
    
    if tribe_form.is_valid():
        tribe = tribe_form.save(commit=False)
        tribe.creator = request.user
        tribe.save()
        tribe.members.add(request.user)
        tribe.save()
        if notification:
            # @@@ might be worth having a shortcut for sending to all users
            notification.send(User.objects.all(), "tribes_new_tribe",
                {"tribe": tribe}, queue=True)
        return HttpResponseRedirect(tribe.get_absolute_url())
    
    return render_to_response(template_name, {
        "tribe_form": tribe_form,
    }, context_instance=RequestContext(request))


def tribes(request, template_name="newtribes/tribes.html"):
    
    ctype = ContentType.objects.get_for_model(Tribe)
    
    tribes = Tribe.objects.all()
    search_terms = request.GET.get('search', '')
    if search_terms:
        tribes = (tribes.filter(name__icontains=search_terms) |
            tribes.filter(description__icontains=search_terms))
    
    content_type = ContentType.objects.get_for_model(Tribe)
    
    tribes = tribes.extra(select=SortedDict([
        ('member_count', MEMBER_COUNT_SQL),
        ('topic_count', TOPIC_COUNT_SQL),
    ]), select_params=(content_type.id,))
    
    return render_to_response(template_name, {
        'tribes': tribes,
        'search_terms': search_terms,
    }, context_instance=RequestContext(request))


def delete(request, group_slug=None, redirect_url=None):
    tribe = get_object_or_404(Tribe, slug=group_slug)
    if not redirect_url:
        redirect_url = reverse('tribe_list')
    
    # @@@ eventually, we'll remove restriction that tribe.creator can't leave tribe but we'll still require tribe.members.all().count() == 1
    if (request.user.is_authenticated() and request.method == "POST" and
            request.user == tribe.creator and tribe.members.all().count() == 1):
        tribe.delete()
        request.user.message_set.create(message=_("Tribe %s deleted.") % (
            tribe,))
        # no notification required as the deleter must be the only member
    
    return HttpResponseRedirect(redirect_url)
{'fraction_non_alphanumeric': 0.09616273693943597, 'fraction_numerical': 0.0024657112035752814, 'mean_word_length': 3.825278810408922, 'pattern_counts': {'":': 4, '<': 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': '253723', 'n_tokens_mistral': 1974, 'n_tokens_neox': 1946, 'n_words': 422}