# Path: src/ctf_gameserver/web/registration/models.py
# class Team(models.Model):
#     """
#     Database representation of a team participating in the competition.
#     This enhances the user model, where the team name, password, formal email address etc. are stored. It is
#     particularly attuned to django.contrib.auth.models.User, but should work with other user models as well.
#     """
# 
#     user = models.OneToOneField(settings.AUTH_USER_MODEL, primary_key=True, on_delete=models.CASCADE)
# 
#     net_number = models.PositiveSmallIntegerField(null=True, unique=True)
#     informal_email = models.EmailField(_('Informal email address'))
#     image = ThumbnailImageField(upload_to=_gen_image_name, blank=True)
#     affiliation = models.CharField(max_length=100, blank=True)
#     country = models.CharField(max_length=100)
#     # NOP teams don't get included in the scoring
#     nop_team = models.BooleanField(default=False, db_index=True)
# 
#     class ActiveObjectsManager(models.Manager):
#         def get_queryset(self):
#             return super().get_queryset().filter(user__is_active=True)
# 
#     class ActiveNotNopObjectsManager(ActiveObjectsManager):
#         def get_queryset(self):
#             return super().get_queryset().filter(nop_team=False)
# 
#     # The first Manager in a class is used as default
#     objects = models.Manager()
#     # QuerySet that only returns Teams whose associated user object is not marked as inactive
#     active_objects = ActiveObjectsManager()
#     # QuerySet that only returns active Teams that are not marked as NOP team
#     active_not_nop_objects = ActiveNotNopObjectsManager()
# 
#     def __str__(self):
#         # pylint: disable=no-member
#         return self.user.username
# 
# Path: src/ctf_gameserver/web/registration/util.py
# class EmailConfirmationTokenGenerator(PasswordResetTokenGenerator):
#     def _make_hash_value(self, user, timestamp):
# def get_country_names():


import logging
import random
import ctf_gameserver.web.scoring.models as scoring_models
from django.db import transaction, IntegrityError
from django.views.generic import ListView
from django.shortcuts import render, redirect
from django.conf import settings
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext as _
from django.contrib import messages
from django.contrib.auth import logout, get_user_model
from django.contrib.auth.decorators import login_required
from django.contrib.admin.views.decorators import staff_member_required
from ctf_gameserver.web.scoring.decorators import before_competition_required, registration_open_required
from . import forms
from .models import Team
from .util import email_token_generator
    })


@transaction.atomic
def confirm_email(request):

    try:
        user_pk = request.GET['user']
        token = request.GET['token']
    except KeyError:
        messages.error(request, _('Missing parameters, email address could not be confirmed.'))
        return render(request, '400.html', status=400)

    error_message = _('Invalid user or token, email address could not be confirmed.')

    # pylint: disable=protected-access
    try:
        user = User._default_manager.get(pk=user_pk)
    except User.DoesNotExist:
        messages.error(request, error_message)
        return render(request, '400.html', status=400)

    if not email_token_generator.check_token(user, token):
        messages.error(request, error_message)
        return render(request, '400.html', status=400)

    if user.is_active:
        # Nothing to do, don't generate net number again!
        return redirect(settings.HOME_URL)


# Path: src/ctf_gameserver/web/registration/models.py
# class Team(models.Model):
#     """
#     Database representation of a team participating in the competition.
#     This enhances the user model, where the team name, password, formal email address etc. are stored. It is
#     particularly attuned to django.contrib.auth.models.User, but should work with other user models as well.
#     """
# 
#     user = models.OneToOneField(settings.AUTH_USER_MODEL, primary_key=True, on_delete=models.CASCADE)
# 
#     net_number = models.PositiveSmallIntegerField(null=True, unique=True)
#     informal_email = models.EmailField(_('Informal email address'))
#     image = ThumbnailImageField(upload_to=_gen_image_name, blank=True)
#     affiliation = models.CharField(max_length=100, blank=True)
#     country = models.CharField(max_length=100)
#     # NOP teams don't get included in the scoring
#     nop_team = models.BooleanField(default=False, db_index=True)
# 
#     class ActiveObjectsManager(models.Manager):
#         def get_queryset(self):
#             return super().get_queryset().filter(user__is_active=True)
# 
#     class ActiveNotNopObjectsManager(ActiveObjectsManager):
#         def get_queryset(self):
#             return super().get_queryset().filter(nop_team=False)
# 
#     # The first Manager in a class is used as default
#     objects = models.Manager()
#     # QuerySet that only returns Teams whose associated user object is not marked as inactive
#     active_objects = ActiveObjectsManager()
#     # QuerySet that only returns active Teams that are not marked as NOP team
#     active_not_nop_objects = ActiveNotNopObjectsManager()
# 
#     def __str__(self):
#         # pylint: disable=no-member
#         return self.user.username
# 
# Path: src/ctf_gameserver/web/registration/util.py
# class EmailConfirmationTokenGenerator(PasswordResetTokenGenerator):
#     def _make_hash_value(self, user, timestamp):
# def get_country_names():


# Path: src/ctf_gameserver/web/registration/views.py
import logging
import random
import ctf_gameserver.web.scoring.models as scoring_models
from django.db import transaction, IntegrityError
from django.views.generic import ListView
from django.shortcuts import render, redirect
from django.conf import settings
from django.utils.safestring import mark_safe
from django.utils.translation import ugettext as _
from django.contrib import messages
from django.contrib.auth import logout, get_user_model
from django.contrib.auth.decorators import login_required
from django.contrib.admin.views.decorators import staff_member_required
from ctf_gameserver.web.scoring.decorators import before_competition_required, registration_open_required
from . import forms
from .models import Team
from .util import email_token_generator

    })


@transaction.atomic
def confirm_email(request):

    try:
        user_pk = request.GET['user']
        token = request.GET['token']
    except KeyError:
        messages.error(request, _('Missing parameters, email address could not be confirmed.'))
        return render(request, '400.html', status=400)

    error_message = _('Invalid user or token, email address could not be confirmed.')

    # pylint: disable=protected-access
    try:
        user = User._default_manager.get(pk=user_pk)
    except User.DoesNotExist:
        messages.error(request, error_message)
        return render(request, '400.html', status=400)

    if not email_token_generator.check_token(user, token):
        messages.error(request, error_message)
        return render(request, '400.html', status=400)

    if user.is_active:
        # Nothing to do, don't generate net number again!
        return redirect(settings.HOME_URL)


    team = Team.objects.get(user__pk=user_pk)