# Path: src/librement/profile/emails/forms.py
# class EmailForm(forms.Form):
#     email = forms.EmailField()
# 
#     def __init__(self, user, *args, **kwargs):
#         self.user = user
# 
#         super(EmailForm, self).__init__(*args, **kwargs)
# 
#     def clean_email(self):
#         val = self.cleaned_data['email']
# 
#         if Email.objects.filter(email=val).exists():
#             raise forms.ValidationError("Email address already in use.")
# 
#         return val
# 
#     def send_email(self):
#         email = self.cleaned_data['email']
# 
#         send_mail(
#             (email,),
#             'profile/emails/confirm.email', {
#                 'user': self.user,
#                 'signed': Signer().sign(email),
#             },
#         )
# 
# class ConfirmForm(forms.Form):
#     signed = forms.CharField()
# 
#     def __init__(self, user, *args, **kwargs):
#         self.user = user
# 
#         super(ConfirmForm, self).__init__(*args, **kwargs)
# 
#     def clean_signed(self):
#         val = self.cleaned_data['signed']
# 
#         try:
#             email = Signer().unsign(val)
#         except BadSignature:
#             raise forms.ValidationError("")
# 
#         if Email.objects.filter(email=email).exists():
#             # Race condition or linked clicked twice
#             raise forms.ValidationError("")
# 
#         self.cleaned_data['email'] = email
# 
#         return val
# 
#     def save(self):
#         self.user.emails.create(
#             email=self.cleaned_data['email'],
#         )


from django.contrib import messages
from django.shortcuts import render, get_object_or_404, redirect
from django.views.decorators.http import require_POST
from django.contrib.auth.decorators import login_required
from .forms import EmailForm, ConfirmForm
# according to the terms contained in the LICENSE file.



@login_required
def view(request):
    return render(request, 'profile/emails/view.html')

@login_required
def confirm(request):
    form = ConfirmForm(request.user, request.GET)

    if form.is_valid():
        form.save()

        messages.success(
            request,
            "Your email adddress was successfully confirmed.",
        )
    else:
        messages.error(
            request,
            "There was an error confirming your email address.",
        )

    return redirect('profile:emails:view')

@login_required
def add(request):
    if request.method == 'POST':

# Path: src/librement/profile/emails/forms.py
# class EmailForm(forms.Form):
#     email = forms.EmailField()
# 
#     def __init__(self, user, *args, **kwargs):
#         self.user = user
# 
#         super(EmailForm, self).__init__(*args, **kwargs)
# 
#     def clean_email(self):
#         val = self.cleaned_data['email']
# 
#         if Email.objects.filter(email=val).exists():
#             raise forms.ValidationError("Email address already in use.")
# 
#         return val
# 
#     def send_email(self):
#         email = self.cleaned_data['email']
# 
#         send_mail(
#             (email,),
#             'profile/emails/confirm.email', {
#                 'user': self.user,
#                 'signed': Signer().sign(email),
#             },
#         )
# 
# class ConfirmForm(forms.Form):
#     signed = forms.CharField()
# 
#     def __init__(self, user, *args, **kwargs):
#         self.user = user
# 
#         super(ConfirmForm, self).__init__(*args, **kwargs)
# 
#     def clean_signed(self):
#         val = self.cleaned_data['signed']
# 
#         try:
#             email = Signer().unsign(val)
#         except BadSignature:
#             raise forms.ValidationError("")
# 
#         if Email.objects.filter(email=email).exists():
#             # Race condition or linked clicked twice
#             raise forms.ValidationError("")
# 
#         self.cleaned_data['email'] = email
# 
#         return val
# 
#     def save(self):
#         self.user.emails.create(
#             email=self.cleaned_data['email'],
#         )


# Path: src/librement/profile/emails/views.py
from django.contrib import messages
from django.shortcuts import render, get_object_or_404, redirect
from django.views.decorators.http import require_POST
from django.contrib.auth.decorators import login_required
from .forms import EmailForm, ConfirmForm

# according to the terms contained in the LICENSE file.



@login_required
def view(request):
    return render(request, 'profile/emails/view.html')

@login_required
def confirm(request):
    form = ConfirmForm(request.user, request.GET)

    if form.is_valid():
        form.save()

        messages.success(
            request,
            "Your email adddress was successfully confirmed.",
        )
    else:
        messages.error(
            request,
            "There was an error confirming your email address.",
        )

    return redirect('profile:emails:view')

@login_required
def add(request):
    if request.method == 'POST':

        form = EmailForm(request.user, request.POST)