<commit_before># Create your views here.
from django.http import HttpResponse, HttpResponseRedirect
from django.template import RequestContext
from django.shortcuts import render_to_response
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required

from rango.models import Category, Page
from rango.forms import CategoryForm, UserForm, UserProfileForm, PageForm

def encode_url(str):
  return str.replace(' ', '_')

def decode_url(str):
  return str.replace('_', ' ')

def index(request):
  context = RequestContext(request)

  category_list = Category.objects.order_by('-name')[:5]
  context_dict = {'categories': category_list}

  for category in category_list:
    category.url = category.name.replace(' ', '_')

  return render_to_response('rango/index.html', context_dict, context)

def category(request, category_name_url):
  context = RequestContext(request)

  category_name = category_name_url.replace('_', ' ')

  context_dict = {'category_name': category_name}

  try:
    category = Category.objects.get(name=category_name)
    pages = Page.objects.filter(category=category)
    context_dict['pages'] = pages
    context_dict['category'] = category
  except Category.DoesNotExist:
    pass

  return render_to_response('rango/category.html', context_dict, context)

def add_category(request):
  context = RequestContext(request)

  if request.method == 'POST':
    form = CategoryForm(request.POST)

    if form.is_valid():
      form.save(commit=True)

      return index(request)
    else:
      print form.errors

  else:
    form = CategoryForm()

  return render_to_response('rango/add_category.html', {'form': form}, context)

def register(request):
  context = RequestContext(request)
  registered = False

  if request.method == 'POST':
    user_form = UserForm(data=request.POST)
    profile_form = UserProfileForm(data=request.POST)

    if user_form.is_valid() and profile_form.is_valid():
      user = user_form.save()
      user.set_password(user.password)<commit_msg>Chapter 10: Checking cookies functionality<commit_after># Create your views here.
from django.http import HttpResponse, HttpResponseRedirect
from django.template import RequestContext
from django.shortcuts import render_to_response
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required

from rango.models import Category, Page
from rango.forms import CategoryForm, UserForm, UserProfileForm, PageForm

def encode_url(str):
  return str.replace(' ', '_')

def decode_url(str):
  return str.replace('_', ' ')

def index(request):
  request.session.set_test_cookie()
  context = RequestContext(request)

  category_list = Category.objects.order_by('-name')[:5]
  context_dict = {'categories': category_list}

  for category in category_list:
    category.url = category.name.replace(' ', '_')

  return render_to_response('rango/index.html', context_dict, context)

def category(request, category_name_url):
  context = RequestContext(request)

  category_name = category_name_url.replace('_', ' ')

  context_dict = {'category_name': category_name}

  try:
    category = Category.objects.get(name=category_name)
    pages = Page.objects.filter(category=category)
    context_dict['pages'] = pages
    context_dict['category'] = category
  except Category.DoesNotExist:
    pass

  return render_to_response('rango/category.html', context_dict, context)

def add_category(request):
  context = RequestContext(request)

  if request.method == 'POST':
    form = CategoryForm(request.POST)

    if form.is_valid():
      form.save(commit=True)

      return index(request)
    else:
      print form.errors

  else:
    form = CategoryForm()

  return render_to_response('rango/add_category.html', {'form': form}, context)

def register(request):
  if request.session.test_cookie_worked():
    print ">>>>> TEST COOKIE WORKED!"
    request.session.delete_test_cookie()
  context = RequestContext(request)
  registered = False

  if request.method == 'POST':
    user_form = UserForm(data=request.POST)
    profile_form = UserProfileForm(data=request.POST)

    if user_form.is_valid() and profile_form.is_valid():
      user = user_form.save()
      user.set_password(<PASSWORD>)
{'fraction_non_alphanumeric': 0.11015370284117373, 'fraction_numerical': 0.0009315323707498836, 'mean_word_length': 4.8276797829036635, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 9, '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': '7137402', 'n_tokens_mistral': 1290, 'n_tokens_neox': 1250, 'n_words': 287}