<commit_before>import datetime
from random import shuffle
from functools import wraps

from django.conf import settings
from django.shortcuts import render
from django.views.decorators.csrf import ensure_csrf_cookie
from django.utils.timezone import utc

from .models import Category, Option, Ballot, BallotCategory, BallotOption, AlreadyVoted
from .forms import LoginForm
from .helpers import json_rest, throttle_post

DEMO_MODE = getattr(settings, 'DEMO_MODE', False)

def _fake_perform_checks(func):
  @wraps(func)
  def inner(request, data, *args, **kwargs):
    if data['username'] == 'onjo' and data['password'] == '<PASSWORD>':
      return dict(result='already_voted')

    elif data['username'] == 'desu' and data['password'] == '<PASSWORD>':
      user_row = dict(user_id=1)
      return func(request, data, user_row, *args, **kwargs)

    else:
      return dict(result='login_failed')

  return inner

def _actual_perform_checks(func):
  @wraps(func)
  def inner(request, data, *args, **kwargs):
    result, user_row = login_db(data['username'], data['password'])
    if result != 'LOGIN_SUCCESS':
      return dict(result='login_failed')

    if AlreadyVoted.objects.filter(user_id=user_row['user_id']).exists():
      return dict(result='already_voted')

    return func(request, data, user_row, *args, **kwargs)

  return inner

if DEMO_MODE:
  perform_checks = _fake_perform_checks
else:
  from phpbb.auth.auth_db import login_db
  from phpbb.auth.backends import connect_to_database
  perform_checks = _actual_perform_checks
  connect_to_database()

def handle_errors(func):
  # XXX stub
  return func

@ensure_csrf_cookie
def vote_page(request):
    categories = Category.objects.all()
    options = []
    for category in categories:
      categoryOptions = list(category.option_set.all())
      shuffle(categoryOptions)
      options.append(categoryOptions)

    vars = dict(
      categories_options=zip(categories, options),
      login_form=LoginForm(),
      DEMO_MODE=DEMO_MODE,
      GOOGLE_ANALYTICS_TOKEN=getattr(settings, 'GOOGLE_ANALYTICS_TOKEN', '')
    )

    return render(request, 'vote.jade', vars)

@throttle_post
@json_rest
@perform_checks
def login_api(request, data, user_row):<commit_msg>hack to recover on failed mysql connection<commit_after>import datetime
from random import shuffle
from functools import wraps

from django.conf import settings
from django.shortcuts import render
from django.views.decorators.csrf import ensure_csrf_cookie
from django.utils.timezone import utc

from .models import Category, Option, Ballot, BallotCategory, BallotOption, AlreadyVoted
from .forms import LoginForm
from .helpers import json_rest, throttle_post

DEMO_MODE = getattr(settings, 'DEMO_MODE', False)

def _fake_perform_checks(func):
  @wraps(func)
  def inner(request, data, *args, **kwargs):
    if data['username'] == 'onjo' and data['password'] == '<PASSWORD>':
      return dict(result='already_voted')

    elif data['username'] == 'desu' and data['password'] == '<PASSWORD>':
      user_row = dict(user_id=1)
      return func(request, data, user_row, *args, **kwargs)

    else:
      return dict(result='login_failed')

  return inner

def login_db_with_retry(username, password):
  try:
    return login_db(username, password)
  except OperationalError:
    connect_to_database()
    return login_db(username, password)

def _actual_perform_checks(func):
  @wraps(func)
  def inner(request, data, *args, **kwargs):
    result, user_row = login_db_with_retry(data['username'], data['password'])
    if result != 'LOGIN_SUCCESS':
      return dict(result='login_failed')

    if AlreadyVoted.objects.filter(user_id=user_row['user_id']).exists():
      return dict(result='already_voted')

    return func(request, data, user_row, *args, **kwargs)

  return inner

if DEMO_MODE:
  perform_checks = _fake_perform_checks
else:
  from phpbb.auth.auth_db import login_db
  from phpbb.auth.backends import connect_to_database
  from MySQLdb import OperationalError # XXX
  perform_checks = _actual_perform_checks
  connect_to_database()

def handle_errors(func):
  # XXX stub
  return func

@ensure_csrf_cookie
def vote_page(request):
    categories = Category.objects.all()
    options = []
    for category in categories:
      categoryOptions = list(category.option_set.all())
      shuffle(categoryOptions)
      options.append(categoryOptions)

    vars = dict(
      categories_options=zip(categories, options),
      login_form=LoginForm(),
      DEMO_MODE=DEMO_MODE,
      GOOGLE_ANALYTICS_TOKEN=getattr(settings, 'GOOGLE_ANALYTICS_TOKEN', '')
    )

    return render(request, 'vote.jade', vars)

@throttle_post
@json_rest
@perform_checks
def login_api(request, data, user_row):
{'fraction_non_alphanumeric': 0.10683308652422255, 'fraction_numerical': 0.0004231013327691982, 'mean_word_length': 4.873291925465838, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '5271621', 'n_tokens_mistral': 1528, 'n_tokens_neox': 1478, 'n_words': 357}