Advertisement
Guest User

Untitled

a guest
Jul 9th, 2017
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. class AuthenticationForm(forms.Form):
  2. """
  3. Base class for authenticating users. Extend this to get a form that accepts
  4. username/password logins.
  5. """
  6. username = UsernameField(
  7. max_length=254,
  8. widget=forms.TextInput(attrs={'autofocus': True}),
  9. )
  10. password = forms.CharField(
  11. label=_("Password"),
  12. strip=False,
  13. widget=forms.PasswordInput,
  14. )
  15.  
  16. error_messages = {
  17. 'invalid_login': _(
  18. "Please enter a correct %(username)s and password. Note that both "
  19. "fields may be case-sensitive."
  20. ),
  21. 'inactive': _("This account is inactive."),
  22. 'IP': _("You tried to login from to much different IP address in last 24 hours. Contact administrator.")
  23. }
  24.  
  25. def __init__(self, request=None, *args, **kwargs):
  26. """
  27. The 'request' parameter is set for custom auth use by subclasses.
  28. The form data comes in via the standard 'data' kwarg.
  29. """
  30. self.request = request
  31. self.user_cache = None
  32. super(AuthenticationForm, self).__init__(*args, **kwargs)
  33.  
  34. # Set the label for the "username" field.
  35. self.username_field = UserModel._meta.get_field(UserModel.USERNAME_FIELD)
  36. if self.fields['username'].label is None:
  37. self.fields['username'].label = capfirst(self.username_field.verbose_name)
  38.  
  39. def clean(self):
  40. username = self.cleaned_data.get('username')
  41. password = self.cleaned_data.get('password')
  42.  
  43. if username is not None and password:
  44.  
  45. self.user_cache = authenticate(self.request, username=username, password=password)
  46.  
  47. # self.ip_remote= self.request.META['REMOTE_ADDR']
  48. if self.user_cache is None:
  49. raise forms.ValidationError(
  50. self.error_messages['invalid_login'],
  51. code='invalid_login',
  52. params={'username': self.username_field.verbose_name},
  53. )
  54. else:
  55. self.confirm_login_allowed(self.user_cache)
  56.  
  57. return self.cleaned_data
  58.  
  59. def confirm_login_allowed(self, user):
  60. """
  61. Controls whether the given User may log in. This is a policy setting,
  62. independent of end-user authentication. This default behavior is to
  63. allow login by active users, and reject login by inactive users.
  64.  
  65. If the given user cannot log in, this method should raise a
  66. ``forms.ValidationError``.
  67.  
  68. If the given user may log in, this method should return None.
  69. """
  70.  
  71. from mysql_queries import user_ips
  72. #check user status, how many ip's he had over last 24 hours
  73. user_status = user_ips(user)
  74.  
  75. if user_status == 400:
  76. raise forms.ValidationError(
  77. self.error_messages['IP'],
  78. code='IP',
  79. )
  80.  
  81. if not user.is_active:
  82. raise forms.ValidationError(
  83. self.error_messages['inactive'],
  84. code='inactive',
  85. )
  86.  
  87. def get_user_id(self):
  88. if self.user_cache:
  89. return self.user_cache.id
  90. return None
  91.  
  92. def get_user(self):
  93. return self.user_cache
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement