optionally limit number of new registrations (bug 698488)

This commit is contained in:
Andy McKay 2011-11-01 14:38:14 -07:00
Родитель 67e9edcf64
Коммит d0ccf7da22
4 изменённых файлов: 53 добавлений и 16 удалений

Просмотреть файл

@ -29,9 +29,9 @@
</div>
</div>{# .secondary #}
<div class="primary c">
<section class="island">
<h1>{{ _('Register') }}</h1>
{% if form %}
{% if form %}
<section class="island">
<h1>{{ _('Register') }}</h1>
<form method="post" action="" class="prettyform grid user-input">
{{ csrf() }}
<fieldset>
@ -90,8 +90,8 @@
<button type="submit">{{ _('Register') }}</button>
</p>
</form>
{% endif %}
</section>
</section>
{% endif %}
</div>{# .primary #}

Просмотреть файл

@ -6,7 +6,7 @@ from django.core import mail
from django.core.validators import validate_slug
from django.utils.http import int_to_base36
from manage import settings
from django.conf import settings
from mock import Mock, patch
from nose.tools import eq_
from pyquery import PyQuery as pq
@ -428,16 +428,20 @@ class TestUserRegisterForm(UserFormBase):
self.assertContains(r, "You are already logged in")
self.assertNotContains(r, '<button type="submit">Register</button>')
def good_data(self):
return {
'email': 'john.connor@sky.net',
'password': 'carebears',
'password2': 'carebears',
'username': 'BigJC',
'homepage': ''
}
@patch('captcha.fields.ReCaptchaField.clean')
def test_success(self, clean):
clean.return_value = ''
data = {'email': 'john.connor@sky.net',
'password': 'carebears',
'password2': 'carebears',
'username': 'BigJC',
'homepage': ''}
self.client.post('/en-US/firefox/users/register', data,
self.client.post('/en-US/firefox/users/register', self.good_data(),
follow=True)
# TODO XXX POSTREMORA: uncomment when remora goes away
#self.assertContains(r, "Congratulations!")
@ -462,6 +466,29 @@ class TestUserRegisterForm(UserFormBase):
err = u'Ensure this value has at most %s characters (it has %s).'
self.assertFormError(r, 'form', field, err % (length, length + 1))
@patch.object(settings, 'REGISTER_USER_LIMIT', 1)
def test_hit_limit_get(self):
res = self.client.get(reverse('users.register'))
doc = pq(res.content)
eq_(len(doc('.error')), 1)
@patch.object(settings, 'REGISTER_USER_LIMIT', 1)
@patch('captcha.fields.ReCaptchaField.clean')
def test_hit_limit_post(self, clean):
clean.return_value = ''
res = self.client.get(reverse('users.register'),
self.good_data())
doc = pq(res.content)
eq_(len(doc('.error')), 1)
eq_(UserProfile.objects.count(), 3) # No user was created.
@patch.object(settings, 'REGISTER_USER_LIMIT', 0)
@patch('captcha.fields.ReCaptchaField.clean')
def test_no_limit_post(self, clean):
clean.return_value = ''
self.client.post(reverse('users.register'), self.good_data())
eq_(UserProfile.objects.count(), 4) # One user was created.
class TestBlacklistedUsernameAdminAddForm(UserFormBase):

Просмотреть файл

@ -451,9 +451,15 @@ def profile(request, user_id):
@anonymous_csrf
@no_login_required
def register(request):
if request.user.is_authenticated():
messages.info(request, _("You are already logged in to an account."))
if (settings.REGISTER_USER_LIMIT and
UserProfile.objects.count() > settings.REGISTER_USER_LIMIT):
messages.error(request, 'Sorry, no more registrations are allowed.')
form = None
elif request.user.is_authenticated():
messages.info(request, _('You are already logged in to an account.'))
form = None
elif request.method == 'POST':
form = forms.UserRegisterForm(request.POST)
@ -465,7 +471,7 @@ def register(request):
u.generate_confirmationcode()
u.save()
u.create_django_user()
log.info(u"Registered new account for user (%s)", u)
log.info(u'Registered new account for user (%s)', u)
u.email_confirmation_code()
@ -484,7 +490,7 @@ def register(request):
# new info yet (total guess). Anyway, I'm assuming the
# first one worked properly, so this is still a success
# case to tne end user so we just log it...
log.error("Failed to register new user (%s): %s" % (u, e))
log.error('Failed to register new user (%s): %s' % (u, e))
return http.HttpResponseRedirect(reverse('users.login'))

Просмотреть файл

@ -1285,3 +1285,7 @@ NO_LOGIN_REQUIRED_MODULES = (
'django.contrib.auth.views.password_reset',
'django.contrib.auth.views.password_reset_done'
)
# Sets an upper limit on the number of users. If 0, it's ignored. If the
# number of users meets or exceeds this, they can't register.
REGISTER_USER_LIMIT = 0