2010-07-05 17:52:52 -04:00
|
|
|
import datetime
|
2010-04-22 21:17:00 -04:00
|
|
|
from django import forms
|
2020-11-06 14:46:40 +07:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2010-04-22 21:17:00 -04:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.contrib.auth import authenticate
|
2010-09-17 13:55:15 -04:00
|
|
|
from django.db.models import Q
|
2013-07-16 11:16:25 -07:00
|
|
|
from django.conf import settings
|
2010-06-12 21:20:06 -04:00
|
|
|
from apps.reader.models import Feature
|
2012-07-05 22:20:49 -07:00
|
|
|
from apps.profile.tasks import EmailNewUser
|
2012-08-15 18:35:55 -07:00
|
|
|
from apps.social.models import MActivity
|
2013-05-13 18:03:17 -07:00
|
|
|
from apps.profile.models import blank_authenticate, RNewUserQueue
|
2010-08-23 18:37:59 -04:00
|
|
|
from utils import log as logging
|
2020-05-04 09:46:36 -04:00
|
|
|
from dns.resolver import query, NXDOMAIN, NoNameservers, NoAnswer
|
2010-04-22 21:17:00 -04:00
|
|
|
|
|
|
|
class LoginForm(forms.Form):
|
2010-09-12 13:55:04 -04:00
|
|
|
username = forms.CharField(label=_("Username or Email"), max_length=30,
|
2012-09-12 18:47:37 -07:00
|
|
|
widget=forms.TextInput(attrs={'tabindex': 1, 'class': 'NB-input'}),
|
2010-04-22 21:17:00 -04:00
|
|
|
error_messages={'required': 'Please enter a username.'})
|
2010-11-04 19:38:28 -04:00
|
|
|
password = forms.CharField(label=_("Password"),
|
2012-09-12 18:47:37 -07:00
|
|
|
widget=forms.PasswordInput(attrs={'tabindex': 2, 'class': 'NB-input'}),
|
2010-07-05 16:05:54 -04:00
|
|
|
required=False)
|
|
|
|
# error_messages={'required': 'Please enter a password.'})
|
2020-06-08 20:40:45 -04:00
|
|
|
add = forms.CharField(required=False, widget=forms.HiddenInput())
|
2010-04-22 21:17:00 -04:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
self.user_cache = None
|
|
|
|
super(LoginForm, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
def clean(self):
|
2010-11-04 19:24:41 -04:00
|
|
|
username = self.cleaned_data.get('username', '').lower()
|
2010-08-02 09:57:53 -04:00
|
|
|
password = self.cleaned_data.get('password', '')
|
2010-09-16 14:35:20 -04:00
|
|
|
|
2015-07-21 15:53:37 -07:00
|
|
|
if '@' in username:
|
|
|
|
user = User.objects.filter(email=username)
|
|
|
|
if not user:
|
|
|
|
user = User.objects.filter(email__iexact=username)
|
|
|
|
else:
|
|
|
|
user = User.objects.filter(username=username)
|
|
|
|
if not user:
|
|
|
|
user = User.objects.filter(username__iexact=username)
|
2013-03-20 18:36:15 -07:00
|
|
|
if user:
|
|
|
|
user = user[0]
|
2010-09-16 14:35:20 -04:00
|
|
|
if username and user:
|
2013-03-20 18:36:15 -07:00
|
|
|
self.user_cache = authenticate(username=user.username, password=password)
|
2012-02-27 16:36:39 -08:00
|
|
|
if self.user_cache is None:
|
2013-03-20 18:36:15 -07:00
|
|
|
blank = blank_authenticate(user.username)
|
|
|
|
if blank:
|
|
|
|
user.set_password(user.username)
|
|
|
|
user.save()
|
|
|
|
self.user_cache = authenticate(username=user.username, password=user.username)
|
2010-04-22 21:17:00 -04:00
|
|
|
if self.user_cache is None:
|
2014-09-03 17:12:16 -07:00
|
|
|
email_user = User.objects.filter(email__iexact=username)
|
2013-03-20 18:36:15 -07:00
|
|
|
if email_user:
|
|
|
|
email_user = email_user[0]
|
|
|
|
self.user_cache = authenticate(username=email_user.username, password=password)
|
2012-02-28 11:29:34 -08:00
|
|
|
if self.user_cache is None:
|
2013-03-20 18:36:15 -07:00
|
|
|
blank = blank_authenticate(email_user.username)
|
|
|
|
if blank:
|
|
|
|
email_user.set_password(email_user.username)
|
|
|
|
email_user.save()
|
|
|
|
self.user_cache = authenticate(username=email_user.username, password=email_user.username)
|
2012-02-27 16:36:39 -08:00
|
|
|
if self.user_cache is None:
|
2013-03-20 18:36:15 -07:00
|
|
|
logging.info(" ***> [%s] Bad Login" % username)
|
2014-04-29 15:24:33 -07:00
|
|
|
raise forms.ValidationError(_("Whoopsy-daisy, wrong password. Try again."))
|
2010-09-16 14:35:20 -04:00
|
|
|
elif username and not user:
|
2012-08-19 14:19:18 -07:00
|
|
|
raise forms.ValidationError(_("That username is not registered. Please try again."))
|
2010-09-16 14:35:20 -04:00
|
|
|
|
2010-04-22 21:17:00 -04:00
|
|
|
return self.cleaned_data
|
|
|
|
|
|
|
|
def get_user_id(self):
|
|
|
|
if self.user_cache:
|
|
|
|
return self.user_cache.id
|
|
|
|
return None
|
|
|
|
|
|
|
|
def get_user(self):
|
|
|
|
return self.user_cache
|
|
|
|
|
|
|
|
|
|
|
|
class SignupForm(forms.Form):
|
2020-06-11 11:45:50 -04:00
|
|
|
use_required_attribute = False
|
|
|
|
|
2011-09-14 20:08:40 -07:00
|
|
|
username = forms.RegexField(regex=r'^\w+$',
|
|
|
|
max_length=30,
|
2012-09-12 18:47:37 -07:00
|
|
|
widget=forms.TextInput(attrs={'class': 'NB-input'}),
|
2020-06-15 05:15:36 -04:00
|
|
|
label=_('Username'),
|
2011-09-14 20:08:40 -07:00
|
|
|
error_messages={
|
|
|
|
'required': 'Please enter a username.',
|
|
|
|
'invalid': "Your username may only contain letters and numbers."
|
|
|
|
})
|
2012-09-12 18:47:37 -07:00
|
|
|
email = forms.EmailField(widget=forms.TextInput(attrs={'maxlength': 75, 'class': 'NB-input'}),
|
2020-06-15 05:15:36 -04:00
|
|
|
label=_('Email'),
|
2013-05-13 18:03:17 -07:00
|
|
|
required=True,
|
|
|
|
error_messages={'required': 'Please enter an email.'})
|
2017-12-08 16:43:30 -08:00
|
|
|
password = forms.CharField(widget=forms.PasswordInput(attrs={'class': 'NB-input'},
|
|
|
|
render_value=True,),
|
2020-06-15 05:15:36 -04:00
|
|
|
label=_('Password'),
|
2011-09-14 20:08:40 -07:00
|
|
|
required=False)
|
|
|
|
# error_messages={'required': 'Please enter a password.'})
|
2010-04-22 21:17:00 -04:00
|
|
|
|
2011-09-14 20:08:40 -07:00
|
|
|
def clean_username(self):
|
|
|
|
username = self.cleaned_data['username']
|
2010-09-16 14:35:20 -04:00
|
|
|
return username
|
2010-04-22 21:17:00 -04:00
|
|
|
|
2011-09-14 20:08:40 -07:00
|
|
|
def clean_password(self):
|
|
|
|
if not self.cleaned_data['password']:
|
2010-07-05 16:05:54 -04:00
|
|
|
return ""
|
2011-09-14 20:08:40 -07:00
|
|
|
return self.cleaned_data['password']
|
2010-07-05 16:05:54 -04:00
|
|
|
|
|
|
|
def clean_email(self):
|
2013-03-21 01:15:28 +05:30
|
|
|
email = self.cleaned_data.get('email', None)
|
|
|
|
if email:
|
|
|
|
email_exists = User.objects.filter(email__iexact=email).count()
|
|
|
|
if email_exists:
|
2020-06-15 05:15:36 -04:00
|
|
|
raise forms.ValidationError(_('Someone is already using that email address.'))
|
2015-12-09 17:21:42 -08:00
|
|
|
if any([banned in email for banned in ['mailwire24', 'mailbox9', 'scintillamail', 'bluemailboxes', 'devmailing']]):
|
2016-11-20 13:23:53 -08:00
|
|
|
logging.info(" ***> [%s] Spammer signup banned: %s/%s" % (self.cleaned_data.get('username', None), self.cleaned_data.get('password', None), email))
|
2015-06-26 11:09:06 -07:00
|
|
|
raise forms.ValidationError('Seriously, fuck off spammer.')
|
2016-01-21 13:37:38 -08:00
|
|
|
try:
|
|
|
|
domain = email.rsplit('@', 1)[-1]
|
|
|
|
if not query(domain, 'MX'):
|
|
|
|
raise forms.ValidationError('Sorry, that email is invalid.')
|
2020-05-04 09:46:36 -04:00
|
|
|
except (NXDOMAIN, NoNameservers, NoAnswer):
|
2016-01-21 13:37:38 -08:00
|
|
|
raise forms.ValidationError('Sorry, that email is invalid.')
|
2016-11-20 13:23:53 -08:00
|
|
|
return self.cleaned_data['email']
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
username = self.cleaned_data.get('username', '')
|
|
|
|
password = self.cleaned_data.get('password', '')
|
|
|
|
email = self.cleaned_data.get('email', None)
|
2017-12-08 16:43:30 -08:00
|
|
|
|
2012-02-27 13:11:55 -08:00
|
|
|
exists = User.objects.filter(username__iexact=username).count()
|
|
|
|
if exists:
|
|
|
|
user_auth = authenticate(username=username, password=password)
|
|
|
|
if not user_auth:
|
2020-06-15 05:15:36 -04:00
|
|
|
raise forms.ValidationError(_('Someone is already using that username.'))
|
2017-12-08 16:43:30 -08:00
|
|
|
|
2012-02-27 13:11:55 -08:00
|
|
|
return self.cleaned_data
|
|
|
|
|
2010-04-22 21:17:00 -04:00
|
|
|
def save(self, profile_callback=None):
|
2012-02-27 13:11:55 -08:00
|
|
|
username = self.cleaned_data['username']
|
|
|
|
password = self.cleaned_data['password']
|
2016-11-20 13:23:53 -08:00
|
|
|
email = self.cleaned_data['email']
|
2013-03-21 01:15:28 +05:30
|
|
|
|
2012-02-27 13:11:55 -08:00
|
|
|
exists = User.objects.filter(username__iexact=username).count()
|
|
|
|
if exists:
|
|
|
|
user_auth = authenticate(username=username, password=password)
|
|
|
|
if not user_auth:
|
2020-06-15 05:15:36 -04:00
|
|
|
raise forms.ValidationError(_('Someone is already using that username.'))
|
2012-02-27 13:11:55 -08:00
|
|
|
else:
|
|
|
|
return user_auth
|
2013-03-20 18:40:03 -07:00
|
|
|
|
|
|
|
if not password:
|
|
|
|
password = username
|
2012-02-27 13:11:55 -08:00
|
|
|
|
|
|
|
new_user = User(username=username)
|
|
|
|
new_user.set_password(password)
|
2020-11-13 20:41:23 -05:00
|
|
|
if not getattr(settings, 'AUTO_ENABLE_NEW_USERS', True):
|
|
|
|
new_user.is_active = False
|
2013-03-21 01:15:28 +05:30
|
|
|
new_user.email = email
|
2020-07-03 02:12:59 -04:00
|
|
|
new_user.last_login = datetime.datetime.now()
|
2010-04-22 21:17:00 -04:00
|
|
|
new_user.save()
|
2012-02-27 13:11:55 -08:00
|
|
|
new_user = authenticate(username=username,
|
|
|
|
password=password)
|
2020-07-03 02:12:59 -04:00
|
|
|
new_user = User.objects.get(username=username)
|
2012-08-15 18:35:55 -07:00
|
|
|
MActivity.new_signup(user_id=new_user.pk)
|
|
|
|
|
2013-05-13 18:03:17 -07:00
|
|
|
RNewUserQueue.add_user(new_user.pk)
|
|
|
|
|
2012-07-05 22:20:49 -07:00
|
|
|
if new_user.email:
|
2020-10-05 00:45:20 +07:00
|
|
|
EmailNewUser().delay(user_id=new_user.pk)
|
2010-04-22 21:17:00 -04:00
|
|
|
|
2013-07-16 11:16:25 -07:00
|
|
|
if getattr(settings, 'AUTO_PREMIUM_NEW_USERS', False):
|
|
|
|
new_user.profile.activate_premium()
|
|
|
|
elif getattr(settings, 'AUTO_ENABLE_NEW_USERS', False):
|
|
|
|
new_user.profile.activate_free()
|
|
|
|
|
2010-04-22 21:17:00 -04:00
|
|
|
return new_user
|
2010-06-12 21:20:06 -04:00
|
|
|
|
|
|
|
class FeatureForm(forms.Form):
|
2020-06-11 11:45:50 -04:00
|
|
|
use_required_attribute = False
|
|
|
|
|
2010-06-12 21:20:06 -04:00
|
|
|
description = forms.CharField(required=True)
|
|
|
|
|
|
|
|
def save(self):
|
2010-07-05 17:52:52 -04:00
|
|
|
feature = Feature(description=self.cleaned_data['description'],
|
2010-10-10 23:55:00 -04:00
|
|
|
date=datetime.datetime.utcnow() + datetime.timedelta(minutes=1))
|
2010-06-12 21:20:06 -04:00
|
|
|
feature.save()
|
2020-11-13 20:41:23 -05:00
|
|
|
return feature
|