當前位置: 首頁>>代碼示例>>Python>>正文


Python DBManager.create_session_scope方法代碼示例

本文整理匯總了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!'})
開發者ID:savageboy74,項目名稱:tyggbot,代碼行數:33,代碼來源:api.py

示例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})
開發者ID:savageboy74,項目名稱:tyggbot,代碼行數:34,代碼來源:api.py

示例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)
開發者ID:savageboy74,項目名稱:tyggbot,代碼行數:11,代碼來源:api.py

示例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)
開發者ID:savageboy74,項目名稱:tyggbot,代碼行數:12,代碼來源:api.py

示例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
開發者ID:savageboy74,項目名稱:tyggbot,代碼行數:13,代碼來源:app.py

示例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)
開發者ID:savageboy74,項目名稱:tyggbot,代碼行數:15,代碼來源:app.py

示例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)
開發者ID:savageboy74,項目名稱:tyggbot,代碼行數:15,代碼來源:api.py

示例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
            })
開發者ID:savageboy74,項目名稱:tyggbot,代碼行數:46,代碼來源:api.py


注:本文中的tyggbot.models.db.DBManager.create_session_scope方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。