dynamicweb/membership/forms.py

67 lines
2.7 KiB
Python
Raw Permalink Normal View History

2016-03-07 16:49:02 +00:00
__author__ = 'tomislav'
from django import forms
2016-03-11 18:42:45 +00:00
from django.utils.translation import ugettext_lazy as _
2016-04-28 17:42:01 +00:00
from django.contrib.auth import authenticate,login
2016-03-11 18:42:45 +00:00
from .models import CreditCards
2016-03-07 16:49:02 +00:00
from utils.forms import SignupFormMixin
2016-03-07 16:49:02 +00:00
class LoginForm(forms.Form):
email = forms.EmailField(label="Email address", max_length=50,
2016-03-11 18:42:45 +00:00
widget=forms.TextInput(
attrs={'class': 'form-control', 'placeholder': 'Enter email'}))
2016-03-07 16:49:02 +00:00
password = forms.CharField(label='Password', max_length=50,
2016-03-11 18:42:45 +00:00
widget=forms.TextInput(
attrs={'class': 'form-control', 'placeholder': 'Password',
'type': 'password'}))
2016-03-07 16:49:02 +00:00
2016-04-28 17:42:01 +00:00
def clean(self):
email = self.cleaned_data.get('email')
password = self.cleaned_data.get('password')
user = authenticate(email=email, password=password)
if not user:
raise forms.ValidationError("Sorry, that login was invalid. Please try again.")
return self.cleaned_data
def login(self,request):
username = self.cleaned_data.get('email')
password = self.cleaned_data.get('password')
user = authenticate(email=username, password=password)
2016-04-28 17:42:01 +00:00
return user
class RegisterForm(SignupFormMixin):
password = forms.CharField(widget=forms.PasswordInput())
confirm_password = forms.CharField(widget=forms.PasswordInput())
2016-03-11 18:42:45 +00:00
class PaymentForm(forms.ModelForm):
class Meta:
model = CreditCards
fields = ('name', 'card_number', 'expiry_date', 'ccv', 'user_id')
labels = {'name': _('Name'), 'card_number': _('Card number'), 'expiry_date': _('Expiry date'),
'ccv': _('CCV')}
2016-04-28 17:42:01 +00:00
exclude = ('user_id', 'payment_type')
2016-03-11 18:42:45 +00:00
widgets = {
'name': forms.TextInput(
attrs={'class': 'form-control', "placeholder": "Enter name on card",
'placeholder': 'Enter name on card'}),
2016-04-28 17:42:01 +00:00
'card_number': forms.TextInput(
attrs={'class': 'form-control', 'placeholder': 'Card Number', 'data-stripe': 'number'}),
2016-03-11 18:42:45 +00:00
'expiry_date': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'MM/YYYY'}),
2016-04-28 17:42:01 +00:00
'ccv': forms.TextInput(
attrs={'class': 'form-control', 'placeholder': 'CCV', 'data-stripe': 'cvc'})}
2016-03-11 18:42:45 +00:00
def clean(self):
data = self.cleaned_data
# if CreditCards.objects.filter(card_number=data.get("card_number")):
# raise forms.ValidationError({'card_number': _('Credit card is used before.')})
return self.cleaned_data
def save(self, user_id):
self.instance.user_id = user_id
self.instance.user_id_id = user_id.id
super(PaymentForm, self).save()