本文整理汇总了Python中api.models.User类的典型用法代码示例。如果您正苦于以下问题:Python User类的具体用法?Python User怎么用?Python User使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了User类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestUserApiRetrieve
class TestUserApiRetrieve(APITestCase):
def setUp(self):
self.user = User(first_name='Etienne',last_name='Chabert',email='[email protected]',password='password',date_of_birth='1990-11-18')
self.user.save()
def test_user_valid_token(self):
token = self.user.get_valid_token()
data = {
'token' : token.token.__str__()
}
response = self.client.post('/api/users/retrieve',data)
self.assertEqual(response.status_code, 200)
def test_user_expired_token(self):
token = self.user.get_valid_token()
token.creation_dateTime -= timedelta(days=42)
token.save()
data = {
'token' : token.token.__str__()
}
response = self.client.post('/api/users/retrieve',data)
self.assertEqual(response.status_code, 400)
def test_user_unknown_token(self):
data = {
'token' : uuid.uuid4()
}
response = self.client.post('/api/users/retrieve',data)
self.assertEqual(response.status_code, 400)
示例2: add_user
def add_user(name, birthday, phone, pushtoken, photo=None, phone_model=None):
user = User(name, birthday, phone, pushtoken, phone_model)
db.session.add(user)
db.session.commit()
if photo is not None:
user.photo = func.get_user_photo_path(user.id)
db.session.commit()
示例3: get
def get(self, username=None):
response = {}
if username:
user = User.objects(username__iexact=username).first()
if not user:
abort(404, message="Username does not exist.")
response = {
'username': user.username,
'email': user.email,
'type': user.user_type,
'created_at': str(user.created_at),
'updated_at': str(user.updated_at)
}
else:
users = User.objects()
for index, user in enumerate(users):
response[index + 1] = {
'username': user.username,
'email': user.email,
'type': user.user_type,
'created_at': str(user.created_at),
'updated_at': str(user.updated_at)
}
return response
示例4: register_user
def register_user(request):
try:
username = request.GET["username"]
except KeyError:
return HttpResponseBadRequest("username required but not provided")
u = User(username=username)
u.save()
return HttpResponse(user_json(registered=True))
示例5: setUp
def setUp(self):
self.app = create_app("test_config")
self.ctx = self.app.app_context()
self.ctx.push()
db.drop_all()
db.create_all()
u = User(username=self.default_username, password=self.default_password)
db.session.add(u)
db.session.commit()
self.client = TestClient(self.app, u.generate_auth_token(), "")
示例6: post
def post(self):
args = parser.parse_args()
user = User.objects(username__iexact=args['username']).first()
if user:
abort(409, message="Username already exists.")
user = User(username=args['username'])
user.password = bcrypt.generate_password_hash(args['password'])
if args['type'] == 'individual':
user.user_type = args['type']
user.user_profile = Individual()
elif args['type'] == 'group':
user.user_type = args['type']
user.user_profile = Group()
user.save()
response = {
'username': user.username,
'email': user.email,
'type': user.user_type,
'created_at': str(user.created_at),
'updated_at': str(user.updated_at)
}
return response
示例7: adduser
def adduser(username):
u"""Adds user to the REST API"""
from getpass import getpass
password = getpass()
password2 = getpass(prompt='Confirm: ')
if password != password2:
sys.exit('Sorry, passwords are not matching.')
db.set(username, username)
user = User(username=username)
user.password = password
print("User {} is correctly created.".format(username))
示例8: post
def post(self):
req_kwargs = self.reqparse.parse_args()
user = User(email=req_kwargs['email'])
user.hash_password(req_kwargs['password'])
try:
user.save()
except ValidationError as e:
return {'message': e.message}, status.HTTP_400_BAD_REQUEST
except NotUniqueError:
return {'message': 'This email already registered'}, status.HTTP_409_CONFLICT
return {'message': 'Success'}, status.HTTP_200_OK
示例9: users_post
def users_post():
email = request.form['email']
password = request.form['password']
info = request.form['info']
if email is None or password is None:
abort(400)
if User.query.filter_by(email=email).first() is not None:
abort(400)
user = User(email=email, info=info)
user.hash_password(password)
user.registered_on = datetime.utcnow()
db.session.add(user)
db.session.commit()
return jsonify({'status': 'OK', 'user': user.email, 'pass': password})
示例10: create_user
def create_user(self, author, user, passhash, user_root):
u = User()
u.author = author
u.user_root = user_root
u.active = True
u.name = user
u.password = passhash
u.save()
示例11: create_user
def create_user():
j = request.get_json()
u = User(first_name=j['first_name'],
last_name=j['last_name'],
username=j['username'],
role=j['role'])
# Hash password
# This is not a secure way at all to do this...
u.password_hash = hashlib.sha256(j['password'].encode()).hexdigest()
db_session.add(u)
db_session.commit()
return jsonify(UserSchema().dump(u).data)
示例12: delete
def delete(self, project_id, collaborator_id):
collaborator = Collaborator.get(collaborator_id)
current_user = User.current_user()
if current_user.key() != collaborator.user.key():
collaborator.remove()
else:
self.error(404)
示例13: get_token_validity_status
def get_token_validity_status():
"""check if the token is still valid. If not return 400. If valid, return new token"""
token = request.authorization.username
user = User.verify_auth_token(token)
if not user:
return get_error("token not valid"), 400
else:
return jsonify(user_info(user)), 200
示例14: get
def get(self, request, *args, **kwargs):
csrf_token = get_token(request)
id = kwargs.get('id')
user = User.get_by_pk(id)
if user is None:
data = {'user': {}, 'csrf_token': csrf_token}
else:
data = {'user': user.to_dict(), 'csrf_token': csrf_token}
return render_json_response(request, data)
示例15: user_add
def user_add(request):
name = request.GET.get('name', '')
email = request.GET.get('email', '')
context={}
""" check if email is correct and name not empty """
if not re.match(r"[^@][email protected][^@]+\.[^@]+", email) or name == '':
return render(request, 'webapp/user_form.html', context)
""" check if user already exists """
try:
exists = User.objects.get(email=email)
except User.DoesNotExist:
exists = None
user = User(email = email, name = name)
user.save()
user_list = User.objects.all().order_by('email')
context={"user_list":user_list}
return render(request, 'webapp/user.html', context)
return render(request, 'webapp/user_form.html', context)