# 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

@registration_open_required
@transaction.atomic
def register(request):

    if request.method == 'POST':
        user_form = forms.UserForm(request.POST, prefix='user')
        team_form = forms.TeamForm(request.POST, request.FILES, prefix='team')

        if user_form.is_valid() and team_form.is_valid():
            user = user_form.save()
            team_form.save(user)
            user_form.send_confirmation_mail(request)

            messages.success(request,
                             mark_safe(_('Successful registration! A confirmation mail has been sent to '
                                         'your formal email address. <strong>You must click the link inside '
                                         'that email to complete your sign-up, otherwise you will not be '
                                         'able to participate.</strong>')))

            return redirect(settings.HOME_URL)
    else:
        user_form = forms.UserForm(prefix='user')
        team_form = forms.TeamForm(prefix='team')

    return render(request, 'register.html', {'user_form': user_form, 'team_form': team_form})


@login_required
@registration_open_required

# 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


@registration_open_required
@transaction.atomic
def register(request):

    if request.method == 'POST':
        user_form = forms.UserForm(request.POST, prefix='user')
        team_form = forms.TeamForm(request.POST, request.FILES, prefix='team')

        if user_form.is_valid() and team_form.is_valid():
            user = user_form.save()
            team_form.save(user)
            user_form.send_confirmation_mail(request)

            messages.success(request,
                             mark_safe(_('Successful registration! A confirmation mail has been sent to '
                                         'your formal email address. <strong>You must click the link inside '
                                         'that email to complete your sign-up, otherwise you will not be '
                                         'able to participate.</strong>')))

            return redirect(settings.HOME_URL)
    else:
        user_form = forms.UserForm(prefix='user')
        team_form = forms.TeamForm(prefix='team')

    return render(request, 'register.html', {'user_form': user_form, 'team_form': team_form})


@login_required
@registration_open_required

@transaction.atomic