from django import forms
from django.utils.translation import ugettext_lazy as _
from django.core.urlresolvers import reverse

from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
from crispy_forms.layout import Layout, Fieldset, ButtonHolder, Field
from .models import WaitingListEntry

class WaitingListEntryForm(forms.ModelForm):
    class Meta:
        model = WaitingListEntry
        exclude = ['invited',]

    def clean_email(self):
        value = self.cleaned_data["email"]
        try:
            entry = WaitingListEntry.objects.get(email=value)
        except WaitingListEntry.DoesNotExist:
            return value
        else:
            raise forms.ValidationError(
                "The email address %(email)s already registered on %(date)s." % {
                    "email": value,
                    "date": entry.created.strftime("%m/%d/%y"),
                }
            )

    def __init__(self, *args, **kwargs):
        super(WaitingListEntryForm, self).__init__(*args, **kwargs)
        self.fields["email"].widget.attrs["placeholder"] = "your@email.com"
        self.fields["email"].label = ""

        self.helper = FormHelper()
        self.helper.form_id = 'waitlist-signup-form'
        self.helper.form_class = 'validate form-inline'
        self.helper.form_method = 'post'
        self.helper.form_action =  reverse('waitlist_signup')

        self.helper.layout = Layout(
            Fieldset(
                '',
                 Field('email', css_class='input-large')
            ),
            Submit('submit', 'Subscribe', css_class='btn btn-success btn-large')
        )


class InvitationKeyForm(forms.Form):
    email = forms.EmailField()

@@ -310,16 +310,190 @@
 tEntry%0A%0A
+try:%0A    from django.contrib.auth import get_user_model%0Aexcept ImportError: # django %3C 1.5%0A    from django.contrib.auth.models import User%0Aelse:%0A    User = get_user_model()%0A%0A
 class Wa
@@ -680,16 +680,202 @@
 email%22%5D%0A
+%0A        user_exists = User.objects.filter(email=value).exists()%0A        if user_exists:%0A            raise forms.ValidationError(%0A                _(%22User already exists%22)%0A            )%0A%0A
         
@@ -1755,16 +1755,106 @@
 ignup')%0A
+        self.helper.help_text_inline = False%0A        self.helper.error_text_inline = False
 %0A       

Check for existing user during waitlist signup