本文整理汇总了Python中tyggbot.models.db.DBManager.create_session_scope方法的典型用法代码示例。如果您正苦于以下问题:Python DBManager.create_session_scope方法的具体用法?Python DBManager.create_session_scope怎么用?Python DBManager.create_session_scope使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tyggbot.models.db.DBManager
的用法示例。
在下文中一共展示了DBManager.create_session_scope方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: pleblist_next
# 需要导入模块: from tyggbot.models.db import DBManager [as 别名]
# 或者: from tyggbot.models.db.DBManager import create_session_scope [as 别名]
def pleblist_next():
if not request.method == 'POST':
return make_response(jsonify({'error': 'Invalid request method'}), 405)
if 'song_id' not in request.form:
return make_response(jsonify({'error': 'Missing data song_id'}), 400)
if 'password' not in request.form:
return make_response(jsonify({'error': 'Missing data password'}), 400)
salted_password = generate_password_hash(config['web']['pleblist_password'], config['web']['pleblist_password_salt'])
try:
user_password = base64.b64decode(request.form['password'])
except binascii.Error:
return make_response(jsonify({'error': 'Invalid data password'}), 400)
if not user_password == salted_password:
return make_response(jsonify({'error': 'Invalid password'}), 401)
with DBManager.create_session_scope() as session:
try:
current_song = session.query(PleblistSong).filter(PleblistSong.id == int(request.form['song_id'])).order_by(PleblistSong.date_added.asc()).first()
except ValueError:
return make_response(jsonify({'error': 'Invalid data song_id'}), 400)
if current_song is None:
return make_response(jsonify({'error': 'No song active in the pleblist'}), 404)
current_song.date_played = datetime.datetime.now()
session.commit()
# TODO: Add more data.
# Was this song forcefully skipped? Or did it end naturally.
return jsonify({'message': 'Success!'})
示例2: pleblist_add
# 需要导入模块: from tyggbot.models.db import DBManager [as 别名]
# 或者: from tyggbot.models.db.DBManager import create_session_scope [as 别名]
def pleblist_add():
if not request.method == 'POST':
return make_response(jsonify({'error': 'Invalid request method'}), 405)
if 'youtube_id' not in request.form:
return make_response(jsonify({'error': 'Missing data youtube_id'}), 400)
if 'password' not in request.form:
return make_response(jsonify({'error': 'Missing data password'}), 400)
salted_password = generate_password_hash(config['web']['pleblist_password'], config['web']['pleblist_password_salt'])
try:
user_password = base64.b64decode(request.form['password'])
except binascii.Error:
return make_response(jsonify({'error': 'Invalid data password'}), 400)
if not user_password == salted_password:
return make_response(jsonify({'error': 'Invalid password'}), 401)
with DBManager.create_session_scope() as session:
youtube_id = request.form['youtube_id']
current_stream = session.query(Stream).filter_by(ended=False).order_by(Stream.stream_start).first()
if current_stream is None:
return make_response(jsonify({'error': 'Stream offline'}), 400)
song_requested = PleblistSong(current_stream.id, youtube_id)
session.add(song_requested)
song_info = session.query(PleblistSongInfo).filter_by(pleblist_song_youtube_id=youtube_id).first()
if song_info is None and song_requested.song_info is None:
PleblistManager.init(config['youtube']['developer_key'])
song_info = PleblistManager.create_pleblist_song_info(song_requested.youtube_id)
if song_info is not False:
session.add(song_info)
session.commit()
return jsonify({'success': True})
示例3: pleblist_blacklist
# 需要导入模块: from tyggbot.models.db import DBManager [as 别名]
# 或者: from tyggbot.models.db.DBManager import create_session_scope [as 别名]
def pleblist_blacklist():
with DBManager.create_session_scope() as session:
current_stream = session.query(Stream).filter_by(ended=False).order_by(Stream.stream_start).first()
if current_stream is None:
return make_response(jsonify({'error': 'Stream offline'}), 400)
# TODO: implement this
return make_response(jsonify({'error': 'NOT IMPLEMENTED'}), 400)
示例4: pleblist_list_by_stream
# 需要导入模块: from tyggbot.models.db import DBManager [as 别名]
# 或者: from tyggbot.models.db.DBManager import create_session_scope [as 别名]
def pleblist_list_by_stream(stream_id):
with DBManager.create_session_scope() as session:
songs = session.query(PleblistSong).filter_by(stream_id=stream_id).all()
payload = {
'_total': len(songs),
'songs': [song.jsonify() for song in songs],
}
return jsonify(payload)
示例5: pleblist_history_redirect
# 需要导入模块: from tyggbot.models.db import DBManager [as 别名]
# 或者: from tyggbot.models.db.DBManager import create_session_scope [as 别名]
def pleblist_history_redirect():
with DBManager.create_session_scope() as session:
current_stream = session.query(Stream).filter_by(ended=False).order_by(Stream.stream_start.desc()).first()
if current_stream is not None:
return redirect('/pleblist/history/{}/'.format(current_stream.id), 303)
last_stream = session.query(Stream).filter_by(ended=True).order_by(Stream.stream_start.desc()).first()
if last_stream is not None:
return redirect('/pleblist/history/{}/'.format(last_stream.id), 303)
return render_template('pleblist_history_no_stream.html'), 404
示例6: pleblist_history_stream
# 需要导入模块: from tyggbot.models.db import DBManager [as 别名]
# 或者: from tyggbot.models.db.DBManager import create_session_scope [as 别名]
def pleblist_history_stream(stream_id):
with DBManager.create_session_scope() as session:
stream = session.query(Stream).filter_by(id=stream_id).one_or_none()
if stream is None:
return render_template('pleblist_history_404.html'), 404
songs = session.query(PleblistSong).filter(PleblistSong.stream_id == stream.id).order_by(PleblistSong.date_added.asc(), PleblistSong.date_played.asc()).all()
total_length_left = sum([song.song_info.duration if song.date_played is None and song.song_info is not None else 0 for song in songs])
return render_template('pleblist_history.html',
stream=stream,
songs=songs,
total_length_left=total_length_left)
示例7: pleblist_list
# 需要导入模块: from tyggbot.models.db import DBManager [as 别名]
# 或者: from tyggbot.models.db.DBManager import create_session_scope [as 别名]
def pleblist_list():
with DBManager.create_session_scope() as session:
current_stream = session.query(Stream).filter_by(ended=False).order_by(Stream.stream_start).first()
if current_stream is None:
return make_response(jsonify({'error': 'Stream offline'}), 400)
songs = session.query(PleblistSong).filter(PleblistSong.stream_id == current_stream.id, PleblistSong.date_played.is_(None)).all()
payload = {
'_total': len(songs),
'songs': [song.jsonify() for song in songs],
}
return jsonify(payload)
示例8: pleblist_validate
# 需要导入模块: from tyggbot.models.db import DBManager [as 别名]
# 或者: from tyggbot.models.db.DBManager import create_session_scope [as 别名]
def pleblist_validate():
if not request.method == 'POST':
return make_response(jsonify({'error': 'Invalid request method'}), 405)
if 'youtube_id' not in request.form:
return make_response(jsonify({'error': 'Missing data youtube_id'}), 400)
with DBManager.create_session_scope() as session:
youtube_id = request.form['youtube_id']
print(youtube_id)
song_info = session.query(PleblistSongInfo).filter_by(pleblist_song_youtube_id=youtube_id).first()
if song_info is not None:
return jsonify({
'message': 'success',
'song_info': song_info.jsonify()
})
PleblistManager.init(config['youtube']['developer_key'])
song_info = PleblistManager.create_pleblist_song_info(youtube_id)
if not song_info and len(youtube_id) > 11:
youtube_id = youtube_id[:11]
song_info = session.query(PleblistSongInfo).filter_by(pleblist_song_youtube_id=youtube_id).first()
if song_info is not None:
return jsonify({
'message': 'success',
'new_youtube_id': youtube_id,
'song_info': song_info.jsonify()
})
else:
song_info = PleblistManager.create_pleblist_song_info(youtube_id)
if song_info:
print(song_info)
session.add(song_info)
session.commit()
return jsonify({
'message': 'success',
'new_youtube_id': youtube_id,
'song_info': song_info.jsonify()
})
return jsonify({
'message': 'invalid youtube id',
'song_info': None
})