本文整理匯總了Python中server.models.User.save方法的典型用法代碼示例。如果您正苦於以下問題:Python User.save方法的具體用法?Python User.save怎麽用?Python User.save使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類server.models.User
的用法示例。
在下文中一共展示了User.save方法的7個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: register
# 需要導入模塊: from server.models import User [as 別名]
# 或者: from server.models.User import save [as 別名]
def register(request):
if request.method == 'POST':
username = request.POST.get('username')
email = request.POST.get('email')
password = request.POST.get('password')
if isEmpty(username) or isEmpty(email) or isEmpty(password):
string = '參數錯誤'
if isEmpty(username):
string = '用戶名不能為空'
if isEmpty(email):
string = '郵箱不能為空'
if isEmpty(password):
string = '密碼不能為空'
json = {}
json[code_name] = 0
json[msg_name] = string
return JsonResponse(json)
else:
users = User.objects.filter(username=username)
if len(users) > 0:
return JsonResponse({code_name:0,msg_name:'用戶已經存在'})
else:
info = User()
info.username = username
info.nickname = request.POST.get('nickname')
info.email = request.POST.get('email')
info.password = request.POST.get('password')
info.save()
return JsonResponse({code_name:1,msg_name:'注冊成功'})
else:
return render(request,"register.html")
示例2: setUp
# 需要導入模塊: from server.models import User [as 別名]
# 或者: from server.models.User import save [as 別名]
def setUp(self):
t = Tool(id=1, name='test_tool', status=1, status_message='working ok')
t.save()
u = User(id=1, name='Test user', subscribed=True)
u.card_set.add(Card(card_id='12345678'))
u.save()
示例3: dummy_users
# 需要導入模塊: from server.models import User [as 別名]
# 或者: from server.models.User import save [as 別名]
def dummy_users():
dummy_user_one = User(username=generate_username())
dummy_user_one.save()
dummy_user_two = User(username=generate_username())
dummy_user_two.save()
return [dummy_user_one, dummy_user_two]
示例4: post
# 需要導入模塊: from server.models import User [as 別名]
# 或者: from server.models.User import save [as 別名]
def post(self):
try:
username = generate_username()
user = User(username=username)
user.save()
self.write(json.dumps(user.to_representation()))
except Chat.DoesNotExist:
self.clear()
self.set_status(404)
self.finish({'error': 'not found'})
示例5: authenticate
# 需要導入模塊: from server.models import User [as 別名]
# 或者: from server.models.User import save [as 別名]
def authenticate(self, username=None, password=None):
login_valid = (settings.ADMIN_LOGIN == username)
pwd_valid = check_password(password, settings.ADMIN_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='get from settings.py')
user.is_staff = True
user.is_superuser = True
user.save()
return user
return None
示例6: post
# 需要導入模塊: from server.models import User [as 別名]
# 或者: from server.models.User import save [as 別名]
def post(self):
data = request.form
if len(set(['name', 'email', 'phone', 'username']) &
set([x for x in data])) == 4:
user = User(name=data['name'], email=data[
'email'], username=data['username'], phone=data['phone'])
user.save()
return jsonify({'res': True,
'user': {
'name': user.name,
'email': user.email,
'phone': user.phone,
'username': user.username
}}
)
else:
return jsonify({'res': False,
'message': 'No name or email'}
)
示例7: handle
# 需要導入模塊: from server.models import User [as 別名]
# 或者: from server.models.User import save [as 別名]
def handle(self, *args, **options):
if len(args) != 1:
raise CommandError('need the path to the carddb.json file')
path = args[0]
if not os.path.exists(args[0]):
raise CommandError('Can\'t find %s' % (path))
fh = open(path, 'r')
for u in json.load(fh):
# {u'perms': [], u'subscribed': False, u'gladosfile': u'zz', u'nick': u'notsubscribed', u'cards': [u'44444444'], u'id': u'4'}
eu = None
try:
eu = User.objects.get(pk=u['id'])
except ObjectDoesNotExist:
pass
if eu:
# update nick and subscription
eu.name = u['nick']
eu.subscribed=u['subscribed']
ecs = eu.card_set.all()
for ec in ecs:
# delete cards that have gone away
if ec.card_id not in u['cards']:
ec.delete()
for c in u['cards']:
# add new cards
if c not in [x.card_id for x in ecs]:
try:
eu.card_set.add(Card(card_id=c))
except DataError as e:
print "error adding new card for %s, card too long? %s" % (eu, c)
print u
print e
eu.save()
else:
nu = User(id=int(u['id']), name=u['nick'], subscribed=u['subscribed'])
try:
nu.save()
except Exception as e:
print "blah."
print e
print u
for c in u['cards']:
try:
# the card already exist?
ec = Card.objects.get(card_id=c)
# bother, it does.
# Which of the 2 users is subscribed?
if not u['subscribed']:
print "skipping card %s, it's already in the db and this user is not subscribed" % (c,)
continue
else:
# darn, maybe the other user is not subscribed?
if not ec.user.subscribed:
print "card %s is already in the db for %s, but they are not subscribed so i'm deleting the card" % (c, ec.user)
ec.delete()
else:
print "panic: card id %s is in use by 2 users: %s and %s" % (c, ec.user, nu)
except ObjectDoesNotExist:
pass
except Exception as e:
print u
print "lol wtf", e
try:
nu.card_set.add(Card(card_id=c))
except Exception as e:
print e
print u
print nu
try:
nu.save()
except Exception as e:
print e
print u
print nu
fh.close()