diff --git a/dal/forms.py b/dal/forms.py new file mode 100644 index 0000000..ecbf9bc --- /dev/null +++ b/dal/forms.py @@ -0,0 +1,40 @@ +from django import forms +from django.contrib.auth import authenticate +from django.shortcuts import render +from django.utils.translation import ugettext_lazy as _ + + +class LoginForm(forms.Form): + email = forms.CharField(widget=forms.TextInput()) + 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') + if self.errors: + return self.cleaned_data + is_auth = authenticate(username=email, password=password) + if not is_auth: + raise forms.ValidationError( + _("Your username and/or password were incorrect.")) + elif is_auth.validated == 0: + raise forms.ValidationError( + _("Your account is not activated yet.")) + return self.cleaned_data + + def clean_email(self): + email = self.cleaned_data.get('email') + return email + # try: + # CustomUser.objects.get(email=email) + # return email + # except CustomUser.DoesNotExist: + # raise forms.ValidationError(_("User does not exist")) + + def form_valid(self, form): + form.save() + return render(self.request, 'useroptions.html', {'user': self.get_context_data()}) + diff --git a/dal/views.py b/dal/views.py index 0e8cd19..5ef21ad 100644 --- a/dal/views.py +++ b/dal/views.py @@ -1,6 +1,6 @@ # Imports from django from django.shortcuts import render -from django.views.generic import View +from django.views.generic import View, FormView from django.contrib.auth import authenticate, login, logout from django.contrib.auth.models import User from django.http import HttpResponse, HttpResponseRedirect @@ -9,6 +9,7 @@ from django.urls import reverse_lazy from django.contrib.auth.tokens import PasswordResetTokenGenerator from django.core.mail import EmailMessage from .models import ResetToken +from .forms import LoginForm # Imports for the extra stuff not in django @@ -96,21 +97,9 @@ class LDAP(object): return sorted(uidlist)[-1] + 1 -class Index(View): - def get(self, request): - if request.user.is_authenticated: - return render(request, 'useroptions.html', { 'user': request.user } ) - return render(request, 'landing.html') - - def post(self, request): - username = request.POST.get('username') - password = request.POST.get('password') - pwd = r'%s' % password - user = authenticate(request, username=username, password=pwd) - if user is not None: - login(request, user) - return render(request, 'useroptions.html', { 'user': user } ) - return render(request, 'loginfailed.html') +class Index(FormView): + template_name = "landing.html" + form_class = LoginForm class Register(View): def get(self, request):