本文整理汇总了Python中registration.compat.get_user_model函数的典型用法代码示例。如果您正苦于以下问题:Python get_user_model函数的具体用法?Python get_user_model怎么用?Python get_user_model使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_user_model函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: register
def register(self, request, **cleaned_data):
username, email, password = cleaned_data['username'], cleaned_data['email'], cleaned_data['password1']
get_user_model().objects.create_user(username, email, password)
new_user = authenticate(username=username, password=password)
login(request, new_user)
signals.user_registered.send(sender=self.__class__,
user=new_user,
request=request)
return new_user
示例2: test_activation_form
def test_activation_form(self):
"""
Test that ``ActivationForm`` enforces username constraints
and matching passwords.
"""
User = get_user_model()
# Create a user so we can verify that duplicate usernames aren't
# permitted.
User.objects.create_user('alice', '[email protected]', 'secret')
invalid_data_dicts = [
# Mismatched passwords.
{'data': {'password1': 'foo',
'password2': 'bar'},
'error': ('__all__', [u"The two password fields didn't match."])},
]
for invalid_dict in invalid_data_dicts:
form = forms.ActivationForm(data=invalid_dict['data'])
self.failIf(form.is_valid())
self.assertEqual(form.errors[invalid_dict['error'][0]],
invalid_dict['error'][1])
form = forms.ActivationForm(data={'password1': 'foo',
'password2': 'foo'})
self.failUnless(form.is_valid())
示例3: register
def register(self, username, email, site, send_email=True):
"""register new user with ``username`` and ``email``
Create a new, inactive ``User``, generate a ``RegistrationProfile``
and email notification to the ``User``, returning the new ``User``.
By default, a registration email will be sent to the new user. To
disable this, pass ``send_email=False``. A registration email will be
generated by ``registration/registration_email.txt`` and
``registration/registration_email_subject.txt``.
The user created by this method has no usable password and it will
be set after activation.
This method is transactional. Thus if some exception has occur in this
method, the newly created user will be rollbacked.
"""
User = get_user_model()
new_user = User.objects.create_user(username, email, 'password')
new_user.set_unusable_password()
new_user.is_active = False
new_user.save()
profile = self.create(user=new_user)
if send_email:
profile.send_registration_email(site)
return new_user
示例4: test_activation_form
def test_activation_form(self):
"""
Test that ``ActivationForm`` enforces username constraints
and matching passwords.
"""
User = get_user_model()
# Create a user so we can verify that duplicate usernames aren't
# permitted.
User.objects.create_user("alice", "[email protected]", "secret")
invalid_data_dicts = [
# Mismatched passwords.
{
"data": {"password1": "foo", "password2": "bar"},
"error": ("__all__", ["The two password fields didn't match."]),
}
]
for invalid_dict in invalid_data_dicts:
form = forms.ActivationForm(data=invalid_dict["data"])
self.failIf(form.is_valid())
self.assertEqual(form.errors[invalid_dict["error"][0]], invalid_dict["error"][1])
form = forms.ActivationForm(data={"password1": "foo", "password2": "foo"})
self.failUnless(form.is_valid())
示例5: clean_email1
def clean_email1(self):
"""Validate that the supplied email address is unique for the site."""
User = get_user_model()
if User.objects.filter(email__iexact=self.cleaned_data['email1']):
raise forms.ValidationError(_(
"This email address is already in use. "
"Please supply a different email address."))
return self.cleaned_data['email1']
示例6: setUp
def setUp(self):
User = get_user_model()
self.backend = DefaultRegistrationBackend()
self.mock_request = mock_request()
self.admin = User.objects.create_superuser(
username='mark', email='[email protected]',
password='password')
self.client.login(username='mark', password='password')
self.admin_url = reverse('admin:index')
示例7: test_registration_form
def test_registration_form(self):
"""
Test that ``RegistrationForm`` enforces username constraints
and matching passwords.
"""
# Create a user so we can verify that duplicate usernames aren't
# permitted.
get_user_model().objects.create_user('alice', '[email protected]', 'secret')
invalid_data_dicts = [
# Non-alphanumeric username.
{'data': {'username': 'foo/bar',
'email': '[email protected]',
'password1': 'foo',
'password2': 'foo'},
'error': ('username', [u"This value may contain only letters, numbers and @/./+/-/_ characters."])},
# Already-existing username.
{'data': {'username': 'alice',
'email': '[email protected]',
'password1': 'secret',
'password2': 'secret'},
'error': ('username', [u"A user with that username already exists."])},
# Mismatched passwords.
{'data': {'username': 'foo',
'email': '[email protected]',
'password1': 'foo',
'password2': 'bar'},
'error': ('__all__', [u"The two password fields didn't match."])},
]
for invalid_dict in invalid_data_dicts:
form = forms.RegistrationForm(data=invalid_dict['data'])
self.failIf(form.is_valid())
self.assertEqual(form.errors[invalid_dict['error'][0]],
invalid_dict['error'][1])
form = forms.RegistrationForm(data={'username': 'foo',
'email': '[email protected]',
'password1': 'foo',
'password2': 'foo'})
self.failUnless(form.is_valid())
示例8: clean_username
def clean_username(self):
"""
Validate that the username is alphanumeric and is not already in use.
"""
User = get_user_model()
try:
User.objects.get(username__iexact=self.cleaned_data['username'])
except User.DoesNotExist:
return self.cleaned_data['username']
raise forms.ValidationError(_(
"A user with that username already exists."))
示例9: test_activation_email
def test_activation_email(self):
"""
``RegistrationProfile.send_activation_email`` sends an
email.
"""
new_user = get_user_model().objects.create_user(**self.user_info)
profile = RegistrationProfile.objects.create_profile(new_user)
profile.send_activation_email(Site.objects.get_current())
self.assertEqual(len(mail.outbox), 1)
self.assertEqual(mail.outbox[0].to, [self.user_info['email']])
示例10: test_registration_form_unique_email
def test_registration_form_unique_email(self):
"""
Test that ``RegistrationFormUniqueEmail`` validates uniqueness
of email addresses.
"""
# Create a user so we can verify that duplicate addresses
# aren't permitted.
get_user_model().objects.create_user('alice', '[email protected]', 'secret')
form = forms.RegistrationFormUniqueEmail(data={'username': 'foo',
'email': '[email protected]',
'password1': 'foo',
'password2': 'foo'})
self.failIf(form.is_valid())
self.assertEqual(form.errors['email'],
[u"This email address is already in use. Please supply a different email address."])
form = forms.RegistrationFormUniqueEmail(data={'username': 'foo',
'email': '[email protected]',
'password1': 'foo',
'password2': 'foo'})
self.failUnless(form.is_valid())
示例11: delete_expired_users
def delete_expired_users(self):
"""
Remove expired instances of ``RegistrationProfile`` and their
associated ``User``s.
Accounts to be deleted are identified by searching for
instances of ``RegistrationProfile`` with expired activation
keys, and then checking to see if their associated ``User``
instances have the field ``is_active`` set to ``False``; any
``User`` who is both inactive and has an expired activation
key will be deleted.
It is recommended that this method be executed regularly as
part of your routine site maintenance; this application
provides a custom management command which will call this
method, accessible as ``manage.py cleanupregistration``.
Regularly clearing out accounts which have never been
activated serves two useful purposes:
1. It alleviates the ocasional need to reset a
``RegistrationProfile`` and/or re-send an activation email
when a user does not receive or does not act upon the
initial activation email; since the account will be
deleted, the user will be able to simply re-register and
receive a new activation key.
2. It prevents the possibility of a malicious user registering
one or more accounts and never activating them (thus
denying the use of those usernames to anyone else); since
those accounts will be deleted, the usernames will become
available for use again.
If you have a troublesome ``User`` and wish to disable their
account while keeping it in the database, simply delete the
associated ``RegistrationProfile``; an inactive ``User`` which
does not have an associated ``RegistrationProfile`` will not
be deleted.
"""
for profile in self.all():
try:
if profile.activation_key_expired():
user = profile.user
if not user.is_active:
user.delete()
profile.delete()
except get_user_model().DoesNotExist:
profile.delete()
示例12: test_profile_creation
def test_profile_creation(self):
"""
Creating a registration profile for a user populates the
profile with the correct user and a SHA1 hash to use as
activation key.
"""
new_user = get_user_model().objects.create_user(**self.user_info)
profile = RegistrationProfile.objects.create_profile(new_user)
self.assertEqual(RegistrationProfile.objects.count(), 1)
self.assertEqual(profile.user.id, new_user.id)
self.failUnless(re.match('^[a-f0-9]{40}$', profile.activation_key))
self.assertEqual(unicode(profile),
"Registration information for alice")
示例13: test_untreated_activation
def test_untreated_activation(self):
User = get_user_model()
new_user = self.backend.register(
username='bob', email='[email protected]',
request=self.mock_request)
profile = new_user.registration_profile
activated_user = self.backend.activate(
activation_key=profile.activation_key,
request=self.mock_request,
password='swardfish')
self.failIf(activated_user)
new_user = User.objects.get(pk=new_user.pk)
self.failIf(new_user.is_active)
self.failIf(new_user.has_usable_password())
示例14: test_expired_user_deletion
def test_expired_user_deletion(self):
"""
``RegistrationProfile.objects.delete_expired_users()`` only
deletes inactive users whose activation window has expired.
"""
new_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(),
**self.user_info)
expired_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(),
username='bob',
password='secret',
email='[email protected]')
expired_user.date_joined -= datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS + 1)
expired_user.save()
RegistrationProfile.objects.delete_expired_users()
self.assertEqual(RegistrationProfile.objects.count(), 1)
self.assertRaises(User.DoesNotExist, get_user_model().objects.get, username='bob')
示例15: test_management_command
def test_management_command(self):
"""
The ``cleanupregistration`` management command properly
deletes expired accounts.
"""
new_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(),
**self.user_info)
expired_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(),
username='bob',
password='secret',
email='[email protected]')
expired_user.date_joined -= datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS + 1)
expired_user.save()
management.call_command('cleanupregistration')
self.assertEqual(RegistrationProfile.objects.count(), 1)
self.assertRaises(User.DoesNotExist, get_user_model().objects.get, username='bob')