本文整理汇总了Python中registration.compat.get_user_model函数的典型用法代码示例。如果您正苦于以下问题:Python get_user_model函数的具体用法?Python get_user_model怎么用?Python get_user_model使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_user_model函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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
开发者ID:deemoowoor,项目名称:django-registration,代码行数:10,代码来源:views.py
示例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())
开发者ID:david--wright,项目名称:django-inspectional-registration,代码行数:27,代码来源:test_forms.py
示例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
开发者ID:sogimu,项目名称:ariadna_services,代码行数:30,代码来源:models.py
示例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())
开发者ID:sogimu,项目名称:ariadna_services,代码行数:26,代码来源:test_forms.py
示例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']
开发者ID:sbgrid,项目名称:django-inspectional-registration,代码行数:8,代码来源:forms.py
示例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')
开发者ID:david--wright,项目名称:django-inspectional-registration,代码行数:10,代码来源:test_admin.py
示例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())
开发者ID:deemoowoor,项目名称:django-registration,代码行数:42,代码来源:forms.py
示例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."))
开发者ID:sbgrid,项目名称:django-inspectional-registration,代码行数:11,代码来源:forms.py
示例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']])
开发者ID:deemoowoor,项目名称:django-registration,代码行数:11,代码来源:models.py
示例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())
开发者ID:deemoowoor,项目名称:django-registration,代码行数:23,代码来源:forms.py
示例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()
开发者ID:torchbox,项目名称:django-registration,代码行数:49,代码来源:models.py
示例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")
开发者ID:deemoowoor,项目名称:django-registration,代码行数:15,代码来源:models.py
示例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())
开发者ID:david--wright,项目名称:django-inspectional-registration,代码行数:16,代码来源:test_backends.py
示例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')
开发者ID:deemoowoor,项目名称:django-registration,代码行数:18,代码来源:models.py
示例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')
开发者ID:deemoowoor,项目名称:django-registration,代码行数:18,代码来源:models.py
示例16: create_inactive_user
def create_inactive_user(self, username, email, password,
site, send_email=True):
"""
Create a new, inactive ``User``, generate a
``RegistrationProfile`` and email its activation key to the
``User``, returning the new ``User``.
By default, an activation email will be sent to the new
user. To disable this, pass ``send_email=False``.
"""
new_user = get_user_model().objects.create_user(username, email, password)
new_user.is_active = False
new_user.save()
registration_profile = self.create_profile(new_user)
if send_email:
registration_profile.send_activation_email(site)
return new_user
开发者ID:torchbox,项目名称:django-registration,代码行数:21,代码来源:models.py
示例17: test_expired_activation
def test_expired_activation(self):
User = get_user_model()
expired_user = self.backend.register(
username='bob', email='[email protected]',
request=self.mock_request)
profile = expired_user.registration_profile
self.backend.accept(profile, request=self.mock_request)
expired_user.date_joined -= datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS+1)
expired_user.save()
activated_user = self.backend.activate(
activation_key=profile.activation_key,
request=self.mock_request,
password='swardfish')
self.failIf(activated_user)
expired_user = User.objects.get(pk=expired_user.pk)
self.failIf(expired_user.is_active)
self.failIf(expired_user.has_usable_password())
开发者ID:david--wright,项目名称:django-inspectional-registration,代码行数:21,代码来源:test_backends.py
示例18: test_expired_activation
def test_expired_activation(self):
"""
Attempting to activate outside the permitted window does not
activate the account.
"""
new_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(),
**self.user_info)
new_user.date_joined -= datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS + 1)
new_user.save()
profile = RegistrationProfile.objects.get(user=new_user)
activated = RegistrationProfile.objects.activate_user(profile.activation_key)
self.failIf(isinstance(activated, User))
self.failIf(activated)
new_user = get_user_model().objects.get(username='alice')
self.failIf(new_user.is_active)
profile = RegistrationProfile.objects.get(user=new_user)
self.assertNotEqual(profile.activation_key, RegistrationProfile.ACTIVATED)
开发者ID:deemoowoor,项目名称:django-registration,代码行数:22,代码来源:models.py
示例19: test_registration_form
def test_registration_form(self):
"""
Test that ``RegistrationForm`` 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 = [
# Non-alphanumeric username.
{
"data": {"username": "foo/bar", "email1": "[email protected]", "email2": "[email protected]"},
"error": ("username", ["This value must contain only letters, numbers and underscores."]),
},
# Already-existing username.
{
"data": {"username": "alice", "email1": "[email protected]", "email2": "[email protected]"},
"error": ("username", ["A user with that username already exists."]),
},
# Mismatched email.
{
"data": {"username": "foo", "email1": "[email protected]", "email2": "[email protected]"},
"error": ("__all__", ["The two email 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": "foofoohogehoge", "email1": "[email protected]", "email2": "[email protected]"}
)
self.failUnless(form.is_valid())
开发者ID:sogimu,项目名称:ariadna_services,代码行数:38,代码来源:test_forms.py
示例20: test_registration_form_unique_email
def test_registration_form_unique_email(self):
"""
Test that ``RegistrationFormUniqueEmail`` validates uniqueness
of email addresses.
"""
User = get_user_model()
# Create a user so we can verify that duplicate addresses
# aren't permitted.
User.objects.create_user("alice", "[email protected]", "secret")
form = forms.RegistrationFormUniqueEmail(
data={"username": "foo", "email1": "[email protected]", "email2": "[email protected]"}
)
self.failIf(form.is_valid())
self.assertEqual(
form.errors["email1"], ["This email address is already in use. Please supply a different email address."]
)
form = forms.RegistrationFormUniqueEmail(
data={"username": "foofoohogehoge", "email1": "[email protected]", "email2": "[email protected]"}
)
self.failUnless(form.is_valid())
开发者ID:sogimu,项目名称:ariadna_services,代码行数:23,代码来源:test_forms.py
注:本文中的registration.compat.get_user_model函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论