# Path: src/signup/forms.py
# class SignUpForm(UserCreationForm):
#     email = forms.EmailField(max_length=254, help_text='Required. Inform a valid email address.')
# 
#     class Meta:
#         model = User
#         fields = ('username', 'email', 'password1', 'password2', )
# 
# Path: src/signup/tokens.py
# class AccountActivationTokenGenerator(PasswordResetTokenGenerator):
#     def _make_hash_value(self, user, timestamp):


from django.contrib.auth import login
from django.contrib.auth.models import User
from django.contrib.sites.shortcuts import get_current_site
from django.shortcuts import render, redirect
from django.utils.encoding import force_bytes, force_text
from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode
from django.template.loader import render_to_string
from .forms import SignUpForm
from .tokens import account_activation_token

Copyright (c) 2016 Simple is Better Than Complex
Released under MIT license, https://github.com/sibtc/simple-signup/blob/master/LICENSE

Modified:
  - use application configuration
  - detect if server is http/https

"""


def signup(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            user = form.save(commit=False)
            user.is_active = False
            user.save()
            current_site = get_current_site(request)
            subject = 'Activate your botw-tracker account'
            message = render_to_string('signup/account_activation_email.html', {
                'user': user,
                'domain': current_site.domain,
                'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                'token': account_activation_token.make_token(user),
                'protocol': 'https' if request.is_secure() else 'http',
            })
            user.email_user(subject, message)
            return redirect('app-views-signup-activation-sent')
    else:

# Path: src/signup/forms.py
# class SignUpForm(UserCreationForm):
#     email = forms.EmailField(max_length=254, help_text='Required. Inform a valid email address.')
# 
#     class Meta:
#         model = User
#         fields = ('username', 'email', 'password1', 'password2', )
# 
# Path: src/signup/tokens.py
# class AccountActivationTokenGenerator(PasswordResetTokenGenerator):
#     def _make_hash_value(self, user, timestamp):


# Path: src/signup/views.py
from django.contrib.auth import login
from django.contrib.auth.models import User
from django.contrib.sites.shortcuts import get_current_site
from django.shortcuts import render, redirect
from django.utils.encoding import force_bytes, force_text
from django.utils.http import urlsafe_base64_encode, urlsafe_base64_decode
from django.template.loader import render_to_string
from .forms import SignUpForm
from .tokens import account_activation_token


Copyright (c) 2016 Simple is Better Than Complex
Released under MIT license, https://github.com/sibtc/simple-signup/blob/master/LICENSE

Modified:
  - use application configuration
  - detect if server is http/https

"""


def signup(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            user = form.save(commit=False)
            user.is_active = False
            user.save()
            current_site = get_current_site(request)
            subject = 'Activate your botw-tracker account'
            message = render_to_string('signup/account_activation_email.html', {
                'user': user,
                'domain': current_site.domain,
                'uid': urlsafe_base64_encode(force_bytes(user.pk)),
                'token': account_activation_token.make_token(user),
                'protocol': 'https' if request.is_secure() else 'http',
            })
            user.email_user(subject, message)
            return redirect('app-views-signup-activation-sent')
    else:

        form = SignUpForm()