# Path: game/models.py
# class Game(models.Model):
# class GameSquare(models.Model):
# class GameLog(models.Model):
#     def __unicode__(self):
#     def get_available_games():
#     def created_count(user):
#     def get_games_for_player(user):
#     def get_by_id(id):
#     def create_new(user):
#     def add_log(self, text, user=None):
#     def get_all_game_squares(self):
#     def get_game_square(row, col):
#     def get_square_by_coords(self, coords):
#     def get_game_log(self):
#     def send_game_update(self):
#     def next_player_turn(self):
#     def mark_complete(self, winner):
#     def __unicode__(self):
#     def get_by_id(id):
#     def get_surrounding(self):
#     def claim(self, status_type, user):
#     def __unicode__(self):
#     STATUS_TYPES = (
#         ('Free', 'Free'),
#         ('Selected', 'Selected'),
#         ('Surrounding', 'Surrounding')
#     )


from django.contrib.auth.forms import PasswordResetForm
from django.shortcuts import redirect
from django.views.generic import CreateView, TemplateView, View, FormView
from django.contrib.auth import authenticate, login
from game.forms import *
from game.models import User, Game
from django.contrib.auth.forms import AuthenticationForm, UserCreationForm
from django.contrib.auth import REDIRECT_FIELD_NAME, login as auth_login, logout as auth_logout
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.contrib import messages
from django.contrib.auth import get_user
from django.shortcuts import get_object_or_404
import json
        if request.user.is_authenticated:
            return redirect('/lobby/')
        super(HomeView, self).dispatch(request, *args, **kwargs)


class CreateUserView(CreateView):
    template_name = 'register.html'
    form_class = UserCreationForm
    success_url = '/lobby/'

    def form_valid(self, form):
        valid = super(CreateUserView, self).form_valid(form)
        username, password = form.cleaned_data.get('username'), form.cleaned_data.get('password1')
        new_user = authenticate(username=username, password=password)
        login(self.request, new_user)
        return valid


class LobbyView(TemplateView):
    template_name = 'components/lobby/lobby.html'

    @method_decorator(login_required)
    def dispatch(self, request, *args, **kwargs):
        return super(LobbyView, self).dispatch(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(LobbyView, self).get_context_data(**kwargs)
        # get current open games to prepopulate the list

        # we're creating a list of games that contains just the id (for the link) and the creator

# Path: game/models.py
# class Game(models.Model):
# class GameSquare(models.Model):
# class GameLog(models.Model):
#     def __unicode__(self):
#     def get_available_games():
#     def created_count(user):
#     def get_games_for_player(user):
#     def get_by_id(id):
#     def create_new(user):
#     def add_log(self, text, user=None):
#     def get_all_game_squares(self):
#     def get_game_square(row, col):
#     def get_square_by_coords(self, coords):
#     def get_game_log(self):
#     def send_game_update(self):
#     def next_player_turn(self):
#     def mark_complete(self, winner):
#     def __unicode__(self):
#     def get_by_id(id):
#     def get_surrounding(self):
#     def claim(self, status_type, user):
#     def __unicode__(self):
#     STATUS_TYPES = (
#         ('Free', 'Free'),
#         ('Selected', 'Selected'),
#         ('Surrounding', 'Surrounding')
#     )


# Path: game/views/views.py
from django.contrib.auth.forms import PasswordResetForm
from django.shortcuts import redirect
from django.views.generic import CreateView, TemplateView, View, FormView
from django.contrib.auth import authenticate, login
from game.forms import *
from game.models import User, Game
from django.contrib.auth.forms import AuthenticationForm, UserCreationForm
from django.contrib.auth import REDIRECT_FIELD_NAME, login as auth_login, logout as auth_logout
from django.contrib.auth.decorators import login_required
from django.utils.decorators import method_decorator
from django.contrib import messages
from django.contrib.auth import get_user
from django.shortcuts import get_object_or_404
import json

        if request.user.is_authenticated:
            return redirect('/lobby/')
        super(HomeView, self).dispatch(request, *args, **kwargs)


class CreateUserView(CreateView):
    template_name = 'register.html'
    form_class = UserCreationForm
    success_url = '/lobby/'

    def form_valid(self, form):
        valid = super(CreateUserView, self).form_valid(form)
        username, password = form.cleaned_data.get('username'), form.cleaned_data.get('password1')
        new_user = authenticate(username=username, password=password)
        login(self.request, new_user)
        return valid


class LobbyView(TemplateView):
    template_name = 'components/lobby/lobby.html'

    @method_decorator(login_required)
    def dispatch(self, request, *args, **kwargs):
        return super(LobbyView, self).dispatch(request, *args, **kwargs)

    def get_context_data(self, **kwargs):
        context = super(LobbyView, self).get_context_data(**kwargs)
        # get current open games to prepopulate the list

        # we're creating a list of games that contains just the id (for the link) and the creator

        available_games = [{'creator': game.creator.username, 'id': game.pk} for game in Game.get_available_games()]