2016-04-07 05:26:50 +00:00
|
|
|
from django import forms
|
2016-04-26 06:16:03 +00:00
|
|
|
from .models import ContactMessage, BillingAddress
|
2016-04-07 05:26:50 +00:00
|
|
|
from django.template.loader import render_to_string
|
|
|
|
from django.core.mail import EmailMultiAlternatives
|
2016-04-09 18:00:16 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2016-06-21 05:10:38 +00:00
|
|
|
from membership.models import CustomUser
|
2016-04-26 06:16:03 +00:00
|
|
|
# from utils.fields import CountryField
|
2016-04-23 07:22:44 +00:00
|
|
|
|
|
|
|
|
2016-06-21 05:10:38 +00:00
|
|
|
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")
|
|
|
|
else:
|
|
|
|
return email
|
|
|
|
|
|
|
|
|
|
|
|
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'],
|
|
|
|
code='password_mismatch',)
|
|
|
|
return password2
|
|
|
|
|
|
|
|
|
2016-04-26 06:16:03 +00:00
|
|
|
class BillingAddressForm(forms.ModelForm):
|
2016-04-23 07:22:44 +00:00
|
|
|
token = forms.CharField(widget=forms.HiddenInput())
|
|
|
|
|
|
|
|
class Meta:
|
2016-04-26 06:16:03 +00:00
|
|
|
model = BillingAddress
|
|
|
|
fields = ['street_address', 'city', 'postal_code', 'country']
|
2016-04-23 07:22:44 +00:00
|
|
|
labels = {
|
|
|
|
'street_address': _('Street Address'),
|
|
|
|
'city': _('City'),
|
|
|
|
'postal_code': _('Postal Code'),
|
|
|
|
'Country': _('Country'),
|
|
|
|
}
|
2016-04-07 05:26:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ContactUsForm(forms.ModelForm):
|
|
|
|
error_css_class = 'autofocus'
|
|
|
|
|
|
|
|
class Meta:
|
2016-04-10 21:12:43 +00:00
|
|
|
model = ContactMessage
|
2016-04-07 05:26:50 +00:00
|
|
|
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'}),
|
|
|
|
}
|
2016-04-09 18:00:16 +00:00
|
|
|
labels = {
|
|
|
|
'name': _('Name'),
|
|
|
|
'email': _('Email'),
|
|
|
|
'phone_number': _('Phone number'),
|
|
|
|
'message': _('Message'),
|
|
|
|
}
|
2016-04-07 05:26:50 +00:00
|
|
|
|
|
|
|
def send_email(self):
|
|
|
|
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")
|
2016-04-09 01:11:22 +00:00
|
|
|
email.to = ['info@digitalglarus.ch']
|
2016-04-07 05:26:50 +00:00
|
|
|
email.send()
|