| 
									
										
										
										
											2016-03-07 17:49:02 +01:00
										 |  |  | __author__ = 'tomislav' | 
					
						
							|  |  |  | from django import forms | 
					
						
							| 
									
										
										
										
											2016-03-11 19:42:45 +01:00
										 |  |  | from django.utils.translation import ugettext_lazy as _ | 
					
						
							| 
									
										
										
										
											2017-06-29 17:34:40 +03:00
										 |  |  | from django.contrib.auth import authenticate | 
					
						
							| 
									
										
										
										
											2016-03-11 19:42:45 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | from .models import CreditCards | 
					
						
							| 
									
										
										
										
											2016-03-07 17:49:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-11 01:07:12 -05:00
										 |  |  | from utils.forms import SignupFormMixin | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-07 17:49:02 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | class LoginForm(forms.Form): | 
					
						
							|  |  |  |     email = forms.EmailField(label="Email address", max_length=50, | 
					
						
							| 
									
										
										
										
											2016-03-11 19:42:45 +01:00
										 |  |  |                              widget=forms.TextInput( | 
					
						
							|  |  |  |                                  attrs={'class': 'form-control', 'placeholder': 'Enter email'})) | 
					
						
							| 
									
										
										
										
											2016-03-07 17:49:02 +01:00
										 |  |  |     password = forms.CharField(label='Password', max_length=50, | 
					
						
							| 
									
										
										
										
											2016-03-11 19:42:45 +01:00
										 |  |  |                                widget=forms.TextInput( | 
					
						
							|  |  |  |                                    attrs={'class': 'form-control', 'placeholder': 'Password', | 
					
						
							|  |  |  |                                           'type': 'password'})) | 
					
						
							| 
									
										
										
										
											2016-03-07 17:49:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-04-28 19:42:01 +02: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 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-29 17:34:40 +03:00
										 |  |  |     def login(self, request): | 
					
						
							| 
									
										
										
										
											2016-04-28 19:42:01 +02:00
										 |  |  |         username = self.cleaned_data.get('email') | 
					
						
							|  |  |  |         password = self.cleaned_data.get('password') | 
					
						
							| 
									
										
										
										
											2016-08-03 00:50:33 -05:00
										 |  |  |         user = authenticate(email=username, password=password) | 
					
						
							| 
									
										
										
										
											2016-04-28 19:42:01 +02:00
										 |  |  |         return user | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-11 01:07:12 -05:00
										 |  |  | class RegisterForm(SignupFormMixin): | 
					
						
							|  |  |  |     password = forms.CharField(widget=forms.PasswordInput()) | 
					
						
							|  |  |  |     confirm_password = forms.CharField(widget=forms.PasswordInput()) | 
					
						
							| 
									
										
										
										
											2016-03-11 19:42:45 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-06-29 17:34:40 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-11 19:42:45 +01: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 19:42:01 +02:00
										 |  |  |         exclude = ('user_id', 'payment_type') | 
					
						
							| 
									
										
										
										
											2016-03-11 19:42:45 +01:00
										 |  |  |         widgets = { | 
					
						
							|  |  |  |             'name': forms.TextInput( | 
					
						
							|  |  |  |                 attrs={'class': 'form-control', "placeholder": "Enter name on card", | 
					
						
							|  |  |  |                        'placeholder': 'Enter name on card'}), | 
					
						
							| 
									
										
										
										
											2016-04-28 19:42:01 +02:00
										 |  |  |             'card_number': forms.TextInput( | 
					
						
							|  |  |  |                 attrs={'class': 'form-control', 'placeholder': 'Card Number', 'data-stripe': 'number'}), | 
					
						
							| 
									
										
										
										
											2016-03-11 19:42:45 +01:00
										 |  |  |             'expiry_date': forms.TextInput(attrs={'class': 'form-control', 'placeholder': 'MM/YYYY'}), | 
					
						
							| 
									
										
										
										
											2016-04-28 19:42:01 +02:00
										 |  |  |             'ccv': forms.TextInput( | 
					
						
							|  |  |  |                 attrs={'class': 'form-control', 'placeholder': 'CCV', 'data-stripe': 'cvc'})} | 
					
						
							| 
									
										
										
										
											2016-03-11 19:42:45 +01: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.')}) | 
					
						
							| 
									
										
										
										
											2017-06-29 17:34:40 +03:00
										 |  |  |         return data | 
					
						
							| 
									
										
										
										
											2016-03-11 19:42:45 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def save(self, user_id): | 
					
						
							|  |  |  |         self.instance.user_id = user_id | 
					
						
							|  |  |  |         self.instance.user_id_id = user_id.id | 
					
						
							|  |  |  |         super(PaymentForm, self).save() |