Guest User

Untitled

a guest
Jun 6th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. from django.contrib.auth.forms import AuthenticationForm as BaseAuthenticationForm
  2. from django.contrib.auth.models import User
  3. from inc.models import Account, UserProfile, Counter
  4.  
  5. class AuthenticationForm(BaseAuthenticationForm):
  6.  
  7. def clean(self):
  8. username = self.cleaned_data.get('username')
  9. user = User.objects.filter(username = username).first()
  10. if user != None:
  11. print(user)
  12. if not user.is_superuser and not user.is_staff:
  13. account = Account.objects.filter(num_account = UserProfile.objects.filter(user__username = username).first().num_account).first()
  14. have_counter = Counter.objects.filter(num_account = account).all()
  15. if not have_counter:
  16. raise forms.ValidationError('Some text...')
  17. return self.cleaned_data
  18.  
  19. from django.contrib.auth import login, authenticate
  20. from .forms import AuthenticationForm
  21.  
  22. def LogIn(request):
  23. if request.method == 'POST':
  24. form = AuthenticationForm(data=request.POST)
  25. if form.is_valid():
  26. username = form.cleaned_data.get('username')
  27. password = form.cleaned_data.get('password')
  28. user = authenticate(username=username, password=password)
  29. print(user)
  30. login(request, user)
  31. return redirect('/')
  32. else:
  33. print(form.errors)
  34. else:
  35. form = AuthenticationForm()
  36. return render(request, 'userprocessing/login.html', {'form': form})
  37.  
  38. AnonymousUser' object has no attribute '_meta'
Add Comment
Please, Sign In to add comment