dynamicweb/utils/forms.py

217 lines
7.3 KiB
Python
Raw Normal View History

from django import forms
2018-07-07 19:29:44 +00:00
from django.contrib.auth import authenticate
from django.core.mail import EmailMultiAlternatives
2018-07-07 19:29:44 +00:00
from django.template.loader import render_to_string
from django.utils.translation import ugettext_lazy as _
2018-07-07 19:29:44 +00:00
from membership.models import CustomUser
2018-07-07 19:29:44 +00:00
from .models import ContactMessage, BillingAddress, UserBillingAddress
2017-07-16 11:05:55 +00:00
# from utils.fields import CountryField
class SignupFormMixin(forms.ModelForm):
confirm_password = forms.CharField(widget=forms.PasswordInput())
password = forms.CharField(widget=forms.PasswordInput())
class Meta:
model = CustomUser
fields = ['name', 'email', 'password']
widgets = {
'name': forms.TextInput(
attrs={'placeholder': _('Enter your name or company name')}),
}
def clean_confirm_password(self):
password = self.cleaned_data.get('password')
confirm_password = self.cleaned_data.get('confirm_password')
if not confirm_password == password:
raise forms.ValidationError("Passwords don't match")
return confirm_password
class LoginFormMixin(forms.Form):
email = forms.CharField(widget=forms.EmailInput())
password = forms.CharField(widget=forms.PasswordInput())
class Meta:
fields = ['email', 'password']
def clean(self):
email = self.cleaned_data.get('email')
password = self.cleaned_data.get('password')
is_auth = authenticate(email=email, password=password)
if not is_auth:
2017-08-29 12:18:41 +00:00
raise forms.ValidationError(
_("Your username and/or password were incorrect."))
return self.cleaned_data
def clean_email(self):
email = self.cleaned_data.get('email')
try:
CustomUser.objects.get(email=email)
return email
except CustomUser.DoesNotExist:
raise forms.ValidationError(_("User does not exist"))
class ResendActivationEmailForm(forms.Form):
email = forms.CharField(widget=forms.EmailInput())
class Meta:
fields = ['email']
def clean_email(self):
email = self.cleaned_data.get('email')
try:
c = CustomUser.objects.get(email=email)
if c.validated == 1:
2018-07-07 19:30:40 +00:00
raise forms.ValidationError(
_("The account is already active."))
return email
except CustomUser.DoesNotExist:
raise forms.ValidationError(_("User does not exist"))
class PasswordResetRequestForm(forms.Form):
email = forms.CharField(widget=forms.EmailInput())
class Meta:
fields = ['email']
def clean_email(self):
email = self.cleaned_data.get('email')
try:
CustomUser.objects.get(email=email)
return email
except CustomUser.DoesNotExist:
raise forms.ValidationError(_("User does not exist"))
class SetPasswordForm(forms.Form):
"""
A form that lets a user change set their password without entering the old
password
"""
error_messages = {
'password_mismatch': _("The two password fields didn't match."),
}
new_password1 = forms.CharField(label=_("New password"),
widget=forms.PasswordInput)
new_password2 = forms.CharField(label=_("New password confirmation"),
widget=forms.PasswordInput)
def clean_new_password2(self):
password1 = self.cleaned_data.get('new_password1')
password2 = self.cleaned_data.get('new_password2')
if password1 and password2:
if password1 != password2:
raise forms.ValidationError(
self.error_messages['password_mismatch'],
2017-07-16 11:05:55 +00:00
code='password_mismatch', )
return password2
class EditCreditCardForm(forms.Form):
token = forms.CharField(widget=forms.HiddenInput())
class BillingAddressForm(forms.ModelForm):
token = forms.CharField(widget=forms.HiddenInput(), required=False)
card = forms.CharField(widget=forms.HiddenInput(), required=False)
class Meta:
model = BillingAddress
2017-08-29 12:18:41 +00:00
fields = ['cardholder_name', 'street_address',
'city', 'postal_code', 'country', 'vat_number']
labels = {
'cardholder_name': _('Cardholder Name'),
'street_address': _('Street Address'),
'city': _('City'),
'postal_code': _('Postal Code'),
'Country': _('Country'),
'VAT Number': _('VAT Number')
}
2017-09-08 18:16:29 +00:00
class BillingAddressFormSignup(BillingAddressForm):
name = forms.CharField(label=_('Name'))
email = forms.EmailField(label=_('Email Address'))
field_order = ['name', 'email']
class Meta:
model = BillingAddress
fields = ['name', 'email', 'cardholder_name', 'street_address',
'city', 'postal_code', 'country', 'vat_number']
labels = {
'name': 'Name',
'email': _('Email'),
'cardholder_name': _('Cardholder Name'),
'street_address': _('Street Address'),
'city': _('City'),
'postal_code': _('Postal Code'),
'Country': _('Country'),
'vat_number': _('VAT Number')
}
def clean_email(self):
email = self.cleaned_data.get('email')
try:
CustomUser.objects.get(email=email)
raise forms.ValidationError(
2018-07-07 19:30:40 +00:00
_("The email %(email)s is already registered with us. "
"Please reset your password and access your account.") %
{'email': email}
)
except CustomUser.DoesNotExist:
return email
2017-09-08 18:16:29 +00:00
class UserBillingAddressForm(forms.ModelForm):
user = forms.ModelChoiceField(queryset=CustomUser.objects.all(),
widget=forms.HiddenInput())
class Meta:
model = UserBillingAddress
2017-08-29 12:18:41 +00:00
fields = ['cardholder_name', 'street_address',
'city', 'postal_code', 'country', 'user']
labels = {
2017-08-29 12:18:41 +00:00
'cardholder_name': _('Cardholder Name'),
'street_address': _('Street Building'),
'city': _('City'),
'postal_code': _('Postal Code'),
'Country': _('Country'),
'vat_number': _('VAT Number'),
}
class ContactUsForm(forms.ModelForm):
error_css_class = 'autofocus'
class Meta:
model = ContactMessage
fields = ['name', 'email', 'phone_number', 'message']
widgets = {
'name': forms.TextInput(attrs={'class': u'form-control'}),
'email': forms.TextInput(attrs={'class': u'form-control'}),
'phone_number': forms.TextInput(attrs={'class': u'form-control'}),
'message': forms.Textarea(attrs={'class': u'form-control'}),
}
labels = {
'name': _('Name'),
'email': _('Email'),
'phone_number': _('Phone number'),
'message': _('Message'),
}
def send_email(self, email_to='info@digitalglarus.ch'):
2017-08-29 12:18:41 +00:00
text_content = render_to_string(
'emails/contact.txt', {'data': self.cleaned_data})
html_content = render_to_string(
'emails/contact.html', {'data': self.cleaned_data})
email = EmailMultiAlternatives('Subject', text_content)
email.attach_alternative(html_content, "text/html")
email.to = [email_to]
email.send()