<commit_before>from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.shortcuts import render, get_object_or_404

from projects.models import Project, Membership


def index(request):
    projs = Project.objects.all()
    context = {
        'projs': projs,
    }
    return render(request, 'projects/index.html', context)


def details(request, slug):
    project = get_object_or_404(Project, slug=slug)
    try:
        membership = Membership.objects.get(user=request.user, project=project)
    except Membership.DoesNotExist:
        membership = None
    context = {
        'project': project,
        'membership': membership,
    }
    return render(request, 'projects/details.html', context)


def join(request, slug):
    """Toggles whether user has an active membership in the project while maintaining the membership type."""
    if request.method == 'POST' and request.user.is_authenticated() and request.user.is_active:
        project = get_object_or_404(Project, slug=slug)
        action = request.POST.get('action')
        if action == 'unjoin':
            try:
                membership = Membership.objects.get(user=request.user, project=project)
                membership.is_active = False
                membership.save()
            except Membership.DoesNotExist:
                pass
        elif action == 'join':
            membership, _ = Membership.objects.update_or_create(user=request.user, project=project,
                                                                defaults={'is_active': True})<commit_msg>Fix AnonymousUser case.<commit_after>from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
from django.shortcuts import render, get_object_or_404

from projects.models import Project, Membership


def index(request):
    projs = Project.objects.all()
    context = {
        'projs': projs,
    }
    return render(request, 'projects/index.html', context)


def details(request, slug):
    project = get_object_or_404(Project, slug=slug)
    context = {
        'project': project,
    }
    if request.user.is_authenticated():
        try:
            membership = Membership.objects.get(user=request.user, project=project)
        except Membership.DoesNotExist:
            membership = None
        context['membership'] = membership
    return render(request, 'projects/details.html', context)


def join(request, slug):
    """Toggles whether user has an active membership in the project while maintaining the membership type."""
    if request.method == 'POST' and request.user.is_authenticated() and request.user.is_active:
        project = get_object_or_404(Project, slug=slug)
        action = request.POST.get('action')
        if action == 'unjoin':
            try:
                membership = Membership.objects.get(user=request.user, project=project)
                membership.is_active = False
                membership.save()
            except Membership.DoesNotExist:
                pass
        elif action == 'join':
            membership, _ = Membership.objects.update_or_create(user=request.user, project=project,
                                                                defaults={'is_active': True})
{'fraction_non_alphanumeric': 0.09574793514836341, 'fraction_numerical': 0.005506271030896299, 'mean_word_length': 2.5659760087241, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '185234', 'n_tokens_mistral': 875, 'n_tokens_neox': 853, 'n_words': 209}