本文整理匯總了Python中django.contrib.auth.models.User.DoesNotExist方法的典型用法代碼示例。如果您正苦於以下問題:Python User.DoesNotExist方法的具體用法?Python User.DoesNotExist怎麽用?Python User.DoesNotExist使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.contrib.auth.models.User
的用法示例。
在下文中一共展示了User.DoesNotExist方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: add_user
# 需要導入模塊: from django.contrib.auth.models import User [as 別名]
# 或者: from django.contrib.auth.models.User import DoesNotExist [as 別名]
def add_user(self, request, pk=None):
group = self.get_object()
if not request.data.get("user_id"):
return Response(
{"user_id": "This field is required."}, status=status.HTTP_400_BAD_REQUEST
)
try:
user = User.objects.get(pk=request.data.get("user_id"))
except User.DoesNotExist:
return Response({"user_id": "Invalid user ID."}, status=status.HTTP_400_BAD_REQUEST)
if request.user == user:
return Response(status=status.HTTP_403_FORBIDDEN)
user.groups.add(group)
return Response(status=status.HTTP_204_NO_CONTENT)
示例2: remove_user
# 需要導入模塊: from django.contrib.auth.models import User [as 別名]
# 或者: from django.contrib.auth.models.User import DoesNotExist [as 別名]
def remove_user(self, request, pk=None):
group = self.get_object()
if not request.data.get("user_id"):
return Response(
{"user_id": "This field is required."}, status=status.HTTP_400_BAD_REQUEST
)
try:
user = User.objects.get(pk=request.data.get("user_id"))
except User.DoesNotExist:
return Response({"user_id": "Invalid user ID."}, status=status.HTTP_400_BAD_REQUEST)
if user.groups.filter(pk=group.pk).count() == 0:
return Response(
{"user_id": "User is not in group."}, status=status.HTTP_400_BAD_REQUEST
)
user.groups.remove(group)
return Response(status=status.HTTP_204_NO_CONTENT)
示例3: signup
# 需要導入模塊: from django.contrib.auth.models import User [as 別名]
# 或者: from django.contrib.auth.models.User import DoesNotExist [as 別名]
def signup(request):
if request.method == 'GET':
return render(request, 'myauth/../templates/signup.html')
elif request.method == 'POST':
user_name = request.POST['用戶名']
password1 = request.POST['密碼']
password2 = request.POST['確認密碼']
try:
User.objects.get(username=user_name)
return render(request, 'myauth/../templates/signup.html', {'Username_error': 'The user already exists'})
except User.DoesNotExist:
if password1 == password2:
User.objects.create_user(username=user_name, password=password1)
return redirect('主頁')
else:
return render(request, 'myauth/../templates/signup.html', {'Pass_Wrong': 'Inconsistent entry password'})
示例4: signup
# 需要導入模塊: from django.contrib.auth.models import User [as 別名]
# 或者: from django.contrib.auth.models.User import DoesNotExist [as 別名]
def signup(request):
if request.method == 'GET':
return render(request, 'accounts/../templates/signup.html')
elif request.method == 'POST':
user_name = request.POST['用戶名']
password1 = request.POST['密碼']
password2 = request.POST['確認密碼']
try:
User.objects.get(username=user_name)
return render(request, 'accounts/../templates/signup.html', {'Username_error': 'The user already exists'})
except User.DoesNotExist:
if password1 == password2:
User.objects.create_user(username=user_name, password=password1)
return redirect('主頁')
else:
return render(request, 'accounts/../templates/signup.html', {'Pass_Wrong': 'Inconsistent entry password'})
示例5: _login_user_and_profile
# 需要導入模塊: from django.contrib.auth.models import User [as 別名]
# 或者: from django.contrib.auth.models.User import DoesNotExist [as 別名]
def _login_user_and_profile(self, extra_post_data={}):
post_data = {
'username': 'bob',
'email': 'bob@columbia.edu',
'password1': 'bobbob',
'password2': 'bobbob',
'name': 'Bob',
'city': 'Bobville',
'country': 'US',
'organization': 'Bob Inc.',
'home_page': 'bob.com',
'twitter': 'boberama'
}
url = '/accounts/register/'
post_data = dict(post_data.items() + extra_post_data.items())
self.response = self.client.post(url, post_data)
try:
self.user = User.objects.get(username=post_data['username'])
except User.DoesNotExist:
pass
示例6: _try_function_org_username
# 需要導入模塊: from django.contrib.auth.models import User [as 別名]
# 或者: from django.contrib.auth.models.User import DoesNotExist [as 別名]
def _try_function_org_username(f, organization, username, args=None):
data = []
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
status_code = status.HTTP_400_BAD_REQUEST
data = {'username':
[_(u"User `%(username)s` does not exist."
% {'username': username})]}
else:
if args:
f(organization, user, *args)
else:
f(organization, user)
status_code = status.HTTP_201_CREATED
return [data, status_code]
示例7: filter_queryset
# 需要導入模塊: from django.contrib.auth.models import User [as 別名]
# 或者: from django.contrib.auth.models.User import DoesNotExist [as 別名]
def filter_queryset(self, queryset):
project = self.kwargs.get('pk', None)
if project:
queryset = queryset.filter(
user__user_roles__project__id = project,
user__is_active=True).order_by('user__first_name')
return queryset
try:
org = self.request.user.user_profile.organization
queryset = queryset.filter(organization = org,user__is_active=True).order_by('user__first_name')
except:
queryset = []
return queryset
# def web_authenticate(username=None, password=None):
# # returns User , Email_correct, Password_correct
# try:
# user = User.objects.get(email__iexact=username)
# if user.check_password(password):
# return authenticate(username=user.username, password=password)
# else:
# return None, True, False
# except User.DoesNotExist:
# return None, False, True
示例8: handle
# 需要導入模塊: from django.contrib.auth.models import User [as 別名]
# 或者: from django.contrib.auth.models.User import DoesNotExist [as 別名]
def handle(self, *args, **kwargs):
if args.__len__() < 2:
raise CommandError(_(u"path(xform instances) username"))
path = args[0]
username = args[1]
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
raise CommandError(_(u"Invalid username %s") % username)
debug = False
if debug:
print (_(u"[Importing XForm Instances from %(path)s]\n")
% {'path': path})
im_count = len(glob.glob(os.path.join(IMAGES_DIR, '*')))
print _(u"Before Parse:")
print _(u" --> Images: %(nb)d") % {'nb': im_count}
print (_(u" --> Instances: %(nb)d")
% {'nb': Instance.objects.count()})
import_instances_from_zip(path, user)
if debug:
im_count2 = len(glob.glob(os.path.join(IMAGES_DIR, '*')))
print _(u"After Parse:")
print _(u" --> Images: %(nb)d") % {'nb': im_count2}
print (_(u" --> Instances: %(nb)d")
% {'nb': Instance.objects.count()})
示例9: handle
# 需要導入模塊: from django.contrib.auth.models import User [as 別名]
# 或者: from django.contrib.auth.models.User import DoesNotExist [as 別名]
def handle(self, *args, **options):
try:
username = args[0]
except IndexError:
raise CommandError(_("You must provide the username to publish the"
" form to."))
# make sure user exists
try:
User.objects.get(username=username)
except User.DoesNotExist:
raise CommandError(_("The user '%s' does not exist.") % username)
try:
input_file = args[1]
except IndexError:
raise CommandError(_("You must provide the path to restore from."))
else:
input_file = os.path.realpath(input_file)
num_instances, num_restored = restore_backup_from_zip(
input_file, username)
sys.stdout.write("Restored %d of %d submissions\n" %
(num_restored, num_instances))
示例10: handle
# 需要導入模塊: from django.contrib.auth.models import User [as 別名]
# 或者: from django.contrib.auth.models.User import DoesNotExist [as 別名]
def handle(self, *args, **kwargs):
user = xform = None
if len(args) > 0:
username = args[0]
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
raise CommandError("User %s does not exist" % username)
if len(args) > 1:
id_string = args[1]
try:
xform = XForm.objects.get(user=user, id_string=id_string)
except XForm.DoesNotExist:
raise CommandError("Xform %s does not exist for user %s" %
(id_string, user.username))
remongo = kwargs["remongo"]
update_all = kwargs["update_all"]
report_string = mongo_sync_status(remongo, update_all, user, xform)
self.stdout.write(report_string)
示例11: clean_username
# 需要導入模塊: from django.contrib.auth.models import User [as 別名]
# 或者: from django.contrib.auth.models.User import DoesNotExist [as 別名]
def clean_username(self):
username = self.cleaned_data.get('username', '')
try:
user = User.objects.get(username__iexact=username)
return user.username
except User.DoesNotExist:
pass
try:
user = User.objects.get(email__iexact=username)
return user.username
except User.DoesNotExist:
pass
return username
示例12: test_expired_user_deletion
# 需要導入模塊: from django.contrib.auth.models import User [as 別名]
# 或者: from django.contrib.auth.models.User import DoesNotExist [as 別名]
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='bob@example.com')
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, User.objects.get, username='bob')
示例13: test_management_command
# 需要導入模塊: from django.contrib.auth.models import User [as 別名]
# 或者: from django.contrib.auth.models.User import DoesNotExist [as 別名]
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='bob@example.com')
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, User.objects.get, username='bob')
示例14: handle
# 需要導入模塊: from django.contrib.auth.models import User [as 別名]
# 或者: from django.contrib.auth.models.User import DoesNotExist [as 別名]
def handle(self, *args, **options):
created = False
try:
user = User.objects.get(username=options['username'])
user.set_password(options['password'])
except User.DoesNotExist:
user = User.objects.create_user(options['username'], password=options['password'])
created = True
user.is_staff = True
if options['first_name']:
user.first_name = options['first_name']
if options['last_name']:
user.last_name = options['last_name']
user.save()
if created:
logger.info("%s user created", user)
else:
logger.info("%s user already created", user)
示例15: authenticate
# 需要導入模塊: from django.contrib.auth.models import User [as 別名]
# 或者: from django.contrib.auth.models.User import DoesNotExist [as 別名]
def authenticate(self, username=None, password=None):
login_valid = (settings.ADMIN_LOGIN == username)
pwd_valid = (settings.ADMIN_PASSWORD == password)
if login_valid and pwd_valid:
try:
user = User.objects.get(username=username)
except User.DoesNotExist:
# Create a new user. Note that we can set password
# to anything, because it won't be checked; the password
# from settings.py will.
user = User(username=username, password=password)
user.is_staff = True
user.is_superuser = True
user.save()
return user
return None