<commit_before>from django.contrib.auth import authenticate, login
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.shortcuts import render

from django.contrib.auth.models import User
from zephyr.models import Zephyr, UserProfile, ZephyrClass, Recipient
from zephyr.forms import RegistrationForm

import datetime
import simplejson

def register(request):
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            username = request.POST['username']
            password = request.POST['password']
            u = User.objects.create_user(username=username, password=password)
            u.save()
            user = authenticate(username=username, password=password)
            login(request, user)
            return HttpResponseRedirect(reverse('zephyr.views.home'))
    else:
        form = RegistrationForm()

    return render(request, 'zephyr/register.html', {
        'form': form,
    })

def accounts_home(request):
    return render_to_response('zephyr/accounts_home.html',
                              context_instance=RequestContext(request))    
    
def home(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect('accounts/home/')

    zephyrs = Zephyr.objects.all()
    for zephyr in zephyrs:
        if zephyr.recipient.type == "class":
            zephyr_class = ZephyrClass.objects.get(pk=zephyr.recipient.user_or_class)
            zephyr.display_recipient = zephyr_class.name
        else:
            user = User.objects.get(pk=zephyr.recipient.user_or_class)
            zephyr.display_recipient = user.username

    user = request.user
    user_profile = UserProfile.objects.get(user=user)
    if user_profile.pointer == -1 and zephyrs:
        user_profile.pointer = min([zephyr.id for zephyr in zephyrs])
        user_profile.save()
    return render_to_response('zephyr/index.html', {'zephyrs': zephyrs, 'user_profile': user_profile},
                              context_instance=RequestContext(request))

def update(request):
    if not request.POST:
        # Do something
        pass
    user = request.user
    user_profile = UserProfile.objects.get(user=user)
    if request.POST.get('pointer'):
        user_profile.pointer = request.POST.get("pointer")
        user_profile.save()
    return HttpResponse(simplejson.dumps({}), mimetype='application/javascript')

def get_updates(request):
    if not request.POST:
        # Do something
        pass
    pointer = request.POST.get('pointer')
    new_zephyrs = Zephyr.objects.filter(id__gt=pointer)
    new_zephyr_list = []
    for zephyr in new_zephyrs:
        if zephyr.recipient.type == "class":
            zephyr_class = ZephyrClass.objects.get(pk=zephyr.recipient.user_or_class)
            display_recipient = zephyr_class.name
        else:
            user = User.objects.get(pk=zephyr.recipient.user_or_class)
            display_recipient = user.username

        new_zephyr_list.append({"id": zephyr.id,
                                "sender": zephyr.sender.user.username,
                                "display_recipient": display_recipient,
                                "instance": zephyr.instance,
                                "content": zephyr.content
                                })

    return HttpResponse(simplejson.dumps(new_zephyr_list),
                        mimetype='application/javascript')

@login_required
def personal_zephyr(request):
    username = request.POST['recipient']
    if User.objects.filter(username=username):
        user = User.objects.get(username=username)
    else:
        # Do something reasonable.
        return HttpResponseRedirect(reverse('zephyr.views.home'))

    recipient = Recipient()
    recipient.user_or_class = user.pk
    recipient.type = "personal"
    recipient.save()

    new_zephyr = Zephyr()
    new_zephyr.sender = UserProfile.objects.get(user=request.user)
    new_zephyr.content = request.POST['new_personal_zephyr']
    new_zephyr.recipient = recipient
    new_zephyr.instance = u''
    new_zephyr.pub_date = datetime.datetime.utcnow()
    new_zephyr.save()

    return HttpResponseRedirect(reverse('zephyr.views.home'))

@login_required
def zephyr(request):<commit_msg>Use the correct MIME type for the JSON we are sending and receiving.<commit_after>from django.contrib.auth import authenticate, login
from django.contrib.auth.decorators import login_required
from django.core.urlresolvers import reverse
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.shortcuts import render

from django.contrib.auth.models import User
from zephyr.models import Zephyr, UserProfile, ZephyrClass, Recipient
from zephyr.forms import RegistrationForm

import datetime
import simplejson

def register(request):
    if request.method == 'POST':
        form = RegistrationForm(request.POST)
        if form.is_valid():
            username = request.POST['username']
            password = request.POST['password']
            u = User.objects.create_user(username=username, password=password)
            u.save()
            user = authenticate(username=username, password=password)
            login(request, user)
            return HttpResponseRedirect(reverse('zephyr.views.home'))
    else:
        form = RegistrationForm()

    return render(request, 'zephyr/register.html', {
        'form': form,
    })

def accounts_home(request):
    return render_to_response('zephyr/accounts_home.html',
                              context_instance=RequestContext(request))    
    
def home(request):
    if not request.user.is_authenticated():
        return HttpResponseRedirect('accounts/home/')

    zephyrs = Zephyr.objects.all()
    for zephyr in zephyrs:
        if zephyr.recipient.type == "class":
            zephyr_class = ZephyrClass.objects.get(pk=zephyr.recipient.user_or_class)
            zephyr.display_recipient = zephyr_class.name
        else:
            user = User.objects.get(pk=zephyr.recipient.user_or_class)
            zephyr.display_recipient = user.username

    user = request.user
    user_profile = UserProfile.objects.get(user=user)
    if user_profile.pointer == -1 and zephyrs:
        user_profile.pointer = min([zephyr.id for zephyr in zephyrs])
        user_profile.save()
    return render_to_response('zephyr/index.html', {'zephyrs': zephyrs, 'user_profile': user_profile},
                              context_instance=RequestContext(request))

def update(request):
    if not request.POST:
        # Do something
        pass
    user = request.user
    user_profile = UserProfile.objects.get(user=user)
    if request.POST.get('pointer'):
        user_profile.pointer = request.POST.get("pointer")
        user_profile.save()
    return HttpResponse(simplejson.dumps({}), mimetype='application/json')

def get_updates(request):
    if not request.POST:
        # Do something
        pass
    pointer = request.POST.get('pointer')
    new_zephyrs = Zephyr.objects.filter(id__gt=pointer)
    new_zephyr_list = []
    for zephyr in new_zephyrs:
        if zephyr.recipient.type == "class":
            zephyr_class = ZephyrClass.objects.get(pk=zephyr.recipient.user_or_class)
            display_recipient = zephyr_class.name
        else:
            user = User.objects.get(pk=zephyr.recipient.user_or_class)
            display_recipient = user.username

        new_zephyr_list.append({"id": zephyr.id,
                                "sender": zephyr.sender.user.username,
                                "display_recipient": display_recipient,
                                "instance": zephyr.instance,
                                "content": zephyr.content
                                })

    return HttpResponse(simplejson.dumps(new_zephyr_list),
                        mimetype='application/json')

@login_required
def personal_zephyr(request):
    username = request.POST['recipient']
    if User.objects.filter(username=username):
        user = User.objects.get(username=username)
    else:
        # Do something reasonable.
        return HttpResponseRedirect(reverse('zephyr.views.home'))

    recipient = Recipient()
    recipient.user_or_class = user.pk
    recipient.type = "personal"
    recipient.save()

    new_zephyr = Zephyr()
    new_zephyr.sender = UserProfile.objects.get(user=request.user)
    new_zephyr.content = request.POST['new_personal_zephyr']
    new_zephyr.recipient = recipient
    new_zephyr.instance = u''
    new_zephyr.pub_date = datetime.datetime.utcnow()
    new_zephyr.save()

    return HttpResponseRedirect(reverse('zephyr.views.home'))

@login_required
def zephyr(request):
{'fraction_non_alphanumeric': 0.09628472609617315, 'fraction_numerical': 0.00022313957380341404, 'mean_word_length': 2.7950889077053342, 'pattern_counts': {'":': 14, '<': 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': '4373520', 'n_tokens_mistral': 2666, 'n_tokens_neox': 2583, 'n_words': 483}