# Path: apps/teams/forms.py
# class TokenAuthForm(forms.Form):
#     token = forms.CharField()
# 
# Path: apps/teams/models.py
# class Team(AbstractBaseUser, PermissionsMixin):
#     name = models.CharField(max_length=60, help_text=_("team name"), blank=False, null=False, unique=True, db_index=True)
#     is_staff = models.BooleanField(help_text=_('staff status'), blank=False, null=False, default=False, db_index=True)
#     is_active = models.BooleanField(help_text=_('User can log in'), blank=False, null=False, default=True, db_index=True)
#     token = models.CharField(max_length=64, unique=True, blank=False, null=True, db_index=True)
# 
#     USERNAME_FIELD = 'name'
# 
#     objects = TeamManager()
# 
#     @property
#     def get_full_name(self):
#         return self.name
# 
#     @property
#     def get_short_name(self):
#         return self.name
# 
#     def get_absolute_url(self):
#         from django.core.urlresolvers import reverse
#         return reverse('teams.views.show', args=[self.name])


from django.conf import settings
from django.shortcuts import render, get_object_or_404, redirect
from django.contrib.auth import authenticate, login, logout
from .forms import TokenAuthForm
from .models import Team
from quests.models import Quest
    from django.utils import translation
from __future__ import print_function, unicode_literals, absolute_import


# Create your views here.


def show(req, name):
    team = get_object_or_404(Team, name=name)
    solved_tasks = Quest.objects.filter(questvariant__team_id=team.id, questvariant__questanswer__is_checked=True,
                                        questvariant__questanswer__is_success=True).select_related('category')
    return render(req, 'teams/show.html', {"team": team, "solved_tasks": solved_tasks})


def do_login(request):
    if request.method == "POST":
        form = TokenAuthForm(data=request.POST)
        if form.is_valid():
            team = authenticate(token=form.cleaned_data['token'])

            # Don't know why, but don't allow to login staff and admins via main form
            if team is not None and not team.is_staff and not team.is_superuser:
                login(request, team)
                return redirect("home")
            else:
                if team is None:
                    form.errors['token'] = form.error_class(["Wrong token"])
                else:
                    form.errors['token'] = form.error_class(["Special users can't login by token"])
        return render(request, "teams/login.html", {"form": form})
    else:

# Path: apps/teams/forms.py
# class TokenAuthForm(forms.Form):
#     token = forms.CharField()
# 
# Path: apps/teams/models.py
# class Team(AbstractBaseUser, PermissionsMixin):
#     name = models.CharField(max_length=60, help_text=_("team name"), blank=False, null=False, unique=True, db_index=True)
#     is_staff = models.BooleanField(help_text=_('staff status'), blank=False, null=False, default=False, db_index=True)
#     is_active = models.BooleanField(help_text=_('User can log in'), blank=False, null=False, default=True, db_index=True)
#     token = models.CharField(max_length=64, unique=True, blank=False, null=True, db_index=True)
# 
#     USERNAME_FIELD = 'name'
# 
#     objects = TeamManager()
# 
#     @property
#     def get_full_name(self):
#         return self.name
# 
#     @property
#     def get_short_name(self):
#         return self.name
# 
#     def get_absolute_url(self):
#         from django.core.urlresolvers import reverse
#         return reverse('teams.views.show', args=[self.name])


# Path: apps/teams/views.py
from django.conf import settings
from django.shortcuts import render, get_object_or_404, redirect
from django.contrib.auth import authenticate, login, logout
from .forms import TokenAuthForm
from .models import Team
from quests.models import Quest
    from django.utils import translation

from __future__ import print_function, unicode_literals, absolute_import


# Create your views here.


def show(req, name):
    team = get_object_or_404(Team, name=name)
    solved_tasks = Quest.objects.filter(questvariant__team_id=team.id, questvariant__questanswer__is_checked=True,
                                        questvariant__questanswer__is_success=True).select_related('category')
    return render(req, 'teams/show.html', {"team": team, "solved_tasks": solved_tasks})


def do_login(request):
    if request.method == "POST":
        form = TokenAuthForm(data=request.POST)
        if form.is_valid():
            team = authenticate(token=form.cleaned_data['token'])

            # Don't know why, but don't allow to login staff and admins via main form
            if team is not None and not team.is_staff and not team.is_superuser:
                login(request, team)
                return redirect("home")
            else:
                if team is None:
                    form.errors['token'] = form.error_class(["Wrong token"])
                else:
                    form.errors['token'] = form.error_class(["Special users can't login by token"])
        return render(request, "teams/login.html", {"form": form})
    else:

        return render(request, "teams/login.html", {"form": TokenAuthForm()})