<commit_before>from django.shortcuts import render, redirect
from django.core.urlresolvers import reverse
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse, HttpResponseServerError
from django.contrib.auth import authenticate, login, logout
from django.contrib.sites.models import Site
from django.views.decorators.csrf import csrf_protect
from django.shortcuts import get_object_or_404
from django.utils.encoding import smart_str
from django.conf import settings
from django.utils import timezone

from web.models import Event, User, SignUp
from web.forms import EventForm, RegisterForm, LoginForm, SignUpForm
from web.extra import send_signup_confirmation, send_new_event

import sys
import datetime
import csv

def index(request):
  events = Event.objects.filter(approved=True)
  form = SignUpForm()
  return render(request, 'index.html', {'events': events, 'form': form, 'key': settings.GOOGLE_KEY,
                                        'time_now': timezone.now()})

def register(request):
  if request.method == 'POST':
    form = RegisterForm(data=request.POST)
    if form.is_valid():
      user = User.objects.create_user(
        email=form.cleaned_data['email'],
        first_name=form.cleaned_data['first_name'],
        last_name=form.cleaned_data['last_name'],
        password=form.cleaned_data['password']
      )
      user.backend = 'django.contrib.auth.backends.ModelBackend'
      user.save()
      login(request, user)

      return redirect(request.GET.get('next', reverse('organize') + '#create-event'))
  else:
    form = RegisterForm()

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

def login_view(request):
  next_page = reverse('organize') # Hard-coded for now

  if request.user.is_authenticated():
    return redirect(next_page)

  if request.method == 'POST':
    form = LoginForm(data=request.POST)
    if form.is_valid():
      user = form.login(request)
      if user:
        login(request, user)
        return redirect(next_page + '#create-event')
  else:
    form = LoginForm()

  return render(request, 'organize/organize.html', {
    'login_form': form,
    'create_event_form': EventForm(),
  })

def logout_view(request):
  logout(request)
  return redirect(reverse('index'))

def organize(request):
  if request.method == 'POST':
    form = EventForm(data=request.POST)
    if form.is_valid():
      event = form.save(commit=False)
      event.start_time = datetime.time(form.cleaned_data['start_hours'], form.cleaned_data['start_minutes'])
      event.end_time = datetime.time(form.cleaned_data['end_hours'], form.cleaned_data['end_minutes'])
      <commit_msg>Check that user is also authenticated when creating an event<commit_after>from django.shortcuts import render, redirect
from django.core.urlresolvers import reverse
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse, HttpResponseServerError
from django.contrib.auth import authenticate, login, logout
from django.contrib.sites.models import Site
from django.views.decorators.csrf import csrf_protect
from django.shortcuts import get_object_or_404
from django.utils.encoding import smart_str
from django.conf import settings
from django.utils import timezone

from web.models import Event, User, SignUp
from web.forms import EventForm, RegisterForm, LoginForm, SignUpForm
from web.extra import send_signup_confirmation, send_new_event

import sys
import datetime
import csv

def index(request):
  events = Event.objects.filter(approved=True)
  form = SignUpForm()
  return render(request, 'index.html', {'events': events, 'form': form, 'key': settings.GOOGLE_KEY,
                                        'time_now': timezone.now()})

def register(request):
  if request.method == 'POST':
    form = RegisterForm(data=request.POST)
    if form.is_valid():
      user = User.objects.create_user(
        email=form.cleaned_data['email'],
        first_name=form.cleaned_data['first_name'],
        last_name=form.cleaned_data['last_name'],
        password=form.cleaned_data['password']
      )
      user.backend = 'django.contrib.auth.backends.ModelBackend'
      user.save()
      login(request, user)

      return redirect(request.GET.get('next', reverse('organize') + '#create-event'))
  else:
    form = RegisterForm()

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

def login_view(request):
  next_page = reverse('organize') # Hard-coded for now

  if request.user.is_authenticated():
    return redirect(next_page)

  if request.method == 'POST':
    form = LoginForm(data=request.POST)
    if form.is_valid():
      user = form.login(request)
      if user:
        login(request, user)
        return redirect(next_page + '#create-event')
  else:
    form = LoginForm()

  return render(request, 'organize/organize.html', {
    'login_form': form,
    'create_event_form': EventForm(),
  })

def logout_view(request):
  logout(request)
  return redirect(reverse('index'))

def organize(request):
  if request.method == 'POST':
    form = EventForm(data=request.POST)
    if form.is_valid() and request.user.is_authenticated():
      event = form.save(commit=False)
      event.start_time = datetime.time(form.cleaned_data['start_hours'], form.cleaned_data['start_minutes'])
      event.end_time = datetime.time(form.cleaned_data['end_hours'], form.cleaned_data['end_minutes'])
      
{'fraction_non_alphanumeric': 0.11550487401140334, 'fraction_numerical': 0.001103549751701306, 'mean_word_length': 4.169201520912548, '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': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7408833', 'n_tokens_mistral': 1641, 'n_tokens_neox': 1598, 'n_words': 371}