本文整理汇总了Python中models.Session.to_json方法的典型用法代码示例。如果您正苦于以下问题:Python Session.to_json方法的具体用法?Python Session.to_json怎么用?Python Session.to_json使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类models.Session
的用法示例。
在下文中一共展示了Session.to_json方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: session
# 需要导入模块: from models import Session [as 别名]
# 或者: from models.Session import to_json [as 别名]
def session():
type = request.json.get('type')
if type == 'register':
try:
login = request.json.get('login')
password = request.json.get('password')
role = request.json.get('role')
role_id = request.json.get('role_id')
if login is None or login == '':
return jsonify({'error': 'argumet missing'}), 403
if password is None or password == '':
return jsonify({'error': 'argumet missing'}), 403
if role != 'patient' and role != 'doctor':
return jsonify({'error': 'argumet missing'}), 403
if role_id is None or role_id == '':
return jsonify({'error': 'argumet missing'}), 403
user = User.query.filter_by(login=login).first()
if user is not None:
return jsonify({'error': 'Такой пользователь уже существует!'}), 403
user = User(login=login, password=sha256(password.encode('UTF-8')).digest(), role=role, role_id=role_id)
db.session.add(user)
db.session.commit()
return jsonify(user.to_json()), 201
except:
return '', 500
if type == 'login':
try:
password = request.json.get('password')
login = request.json.get('login')
if password is None or password == '':
return jsonify({'error': 'argumet missing'}), 403
if login is None or login == '':
return jsonify({'error': 'argumet missing'}), 403
pass_hash = sha256(password.encode('UTF-8')).digest()
user = User.query.filter_by(login=login, pass_hash=pass_hash).first()
if user is not None:
ses = Session.query.filter_by(user_id=user.to_json().get('user_id')).first()
if ses is None:
token = sha256(str(uuid4()).encode('UTF-8')).hexdigest()
ses = Session(user_id=user.to_json().get('user_id'), token=token)
db.session.add(ses)
db.session.commit()
return jsonify(ses.to_json()), 200
else:
return jsonify(ses.to_json()), 200
return '', 404
except:
return '', 500
if type == 'token':
try:
token = request.json.get('token')
ses = Session.query.filter_by(token=token).first()
if ses is not None:
user = User.query.filter_by(id=ses.to_json().get('user_id')).first()
return jsonify(user.to_json()), 200
return '', 404
except:
return '', 500
return '', 403
示例2: post
# 需要导入模块: from models import Session [as 别名]
# 或者: from models.Session import to_json [as 别名]
def post(self, id = None):
request = self.wsgi_request
response = self.wsgi_response
c = self.context
param_dict = dict(
title = request.params.title,
conf_id = request.params.conf_id,
desc = request.params.desc,
venue = request.params.venue,
talk_type = request.params.talk_type,
start_date = parse(request.params.start_date),
end_date = parse(request.params.end_date),
duration = request.params.duration,
speaker_title = request.params.speaker_title)
content = param_dict
session = Session(**param_dict)
session = session.save()
conf = Conference.get(session.conf_id)
if conf:
conf.add_session(session)
self.set_body(session.to_json())
response.headers['content-type'] = 'application/json'
return self.render()
示例3: session
# 需要导入模块: from models import Session [as 别名]
# 或者: from models.Session import to_json [as 别名]
def session():
type = request.json.get('type')
if type == 'register':
try:
username = request.json.get('username')
password = request.json.get('password')
email = request.json.get('email')
if username is None or username == '' or password is None or password == '' or email is None or email == '':
return jsonify({'error': 'argumet missing'}), 403
user = User.query.filter_by(username=username).first()
if user is not None:
return jsonify({'error': 'Same user exists'}), 403
user = User.query.filter_by(email=email).first()
if user is not None:
return jsonify({'error': 'Same email exists'}), 403
user = User(email=email, username=username, password=password)
db.session.add(user)
db.session.commit()
return '', 201
except:
return '', 500
if type == 'login':
try:
password = request.json.get('password')
email = request.json.get('email')
if password is None or password == '' or email is None or email == '':
return jsonify({'error': 'argumet missing'}), 403
user = User.query.filter_by(email=email).first()
if user is not None:
ses = Session.query.filter_by(user_id=user.to_json().get('user_id')).first()
if ses is None:
token = sha256(str(uuid4()).encode('UTF-8')).hexdigest()
expire_time = datetime.now() + timedelta(days=30)
ses = Session(user_id=user.to_json().get('user_id'), token=token, expire_time=expire_time)
db.session.add(ses)
db.session.commit()
return jsonify(ses.to_json()), 200
else:
if ses.expire_time > datetime.now():
ses.expire_time = (datetime.now() + timedelta(days=30))
db.session.add(ses)
db.session.commit()
return jsonify(ses.to_json()), 200
else:
db.session.delete(ses)
db.session.commit()
return '', 404
return '', 404
except:
return '', 500
if type == 'token':
try:
token = request.json.get('token')
ses = Session.query.filter_by(token=token).first()
if ses is not None and ses.expire_time > datetime.now():
user = User.query.filter_by(id=ses.to_json().get('user_id')).first()
return jsonify(user.to_json()), 200
return '', 404
except:
return '', 500
return '', 403