Commit 983e0ff3 authored by Lukáš Lalinský's avatar Lukáš Lalinský

Fix web DB handling

parent 2a204f78
Pipeline #20526 passed with stages
in 3 minutes and 59 seconds
from typing import Dict, Any, NewType, TYPE_CHECKING
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm import Session
from sqlalchemy.engine import Engine, Connection
from acoustid.tables import metadata
if TYPE_CHECKING:
from acoustid.script import Script
Session = sessionmaker()
AppDB = NewType('AppDB', Connection)
FingerprintDB = NewType('FingerprintDB', Connection)
IngestDB = NewType('IngestDB', Connection)
......
# Copyright (C) 2014 Lukas Lalinsky
# Distributed under the MIT license, see the LICENSE file for details.
from sqlalchemy.orm import sessionmaker
from typing import Dict, Callable, Any
from sqlalchemy.engine import Engine, Connection
from sqlalchemy.orm import scoped_session, sessionmaker
from acoustid.script import Script
from acoustid.db import (
AppDB,
FingerprintDB,
IngestDB,
MusicBrainzDB,
Session,
get_session_args,
)
class Database(object):
def __init__(self):
self.engines = {} # type: Dict[str, Engine]
self.session_factory = sessionmaker()
self.session = None
self.session = scoped_session(Session)
def configure(self, script, scopefunc):
# type: (Script, Callable[[], Any]) -> None
self.engines = script.db_engines
self.session_factory.configure(**get_session_args(script))
self.session = scoped_session(self.session_factory, scopefunc)
def connection(self, bind_key):
# type: (str) -> Connection
return self.session.connection(bind=self.engines[bind_key])
def get_app_db(self):
# type: () -> AppDB
return AppDB(self.connection('app'))
def get_fingerprint_db(self):
# type: () -> FingerprintDB
return FingerprintDB(self.connection('fingerprint'))
def get_ingest_db(self):
# type: () -> IngestDB
return IngestDB(self.connection('ingest'))
def get_musicbrainz_db(self):
# type: () -> MusicBrainzDB
return MusicBrainzDB(self.connection('musicbrainz'))
db = Database()
......@@ -6,7 +6,6 @@ from sentry_sdk.integrations.flask import FlaskIntegration
from flask import Flask, request, session
from flask.sessions import SecureCookieSessionInterface
from werkzeug.middleware.proxy_fix import ProxyFix
from sqlalchemy.orm import scoped_session
from acoustid.script import Script
from acoustid.web import db
from acoustid.web.views.general import general_page
......@@ -88,8 +87,7 @@ def make_application(config_filename=None, tests=False):
from acoustid.api import get_health_response
return get_health_response(script, request)
db.session_factory.configure(bind=config.databases.databases['app'].create_engine())
db.session = scoped_session(db.session_factory, scopefunc=get_flask_request_scope)
db.configure(script, get_flask_request_scope)
app.register_blueprint(general_page)
app.register_blueprint(user_page)
......
......@@ -16,20 +16,23 @@ logger = logging.getLogger(__name__)
metadata_page = Blueprint('metadata', __name__)
@metadata_page.route('/track/<track_id>')
def track(track_id):
conn = db.session.connection()
@metadata_page.route('/track/<track_id_or_gid>')
def track(track_id_or_gid):
# xtype: (str) -> str
fingerprint_db = db.get_fingerprint_db()
musicbrainz_db = db.get_musicbrainz_db()
if is_uuid(track_id):
track_gid = track_id
track_id = resolve_track_gid(conn, track_id)
if is_uuid(track_id_or_gid):
track_gid = track_id_or_gid
track_id = resolve_track_gid(fingerprint_db, track_gid)
else:
try:
track_id = int(track_id)
track_id = int(track_id_or_gid)
except ValueError:
track_id = None
query = sql.select([schema.track.c.gid], schema.track.c.id == track_id)
track_gid = conn.execute(query).scalar()
track_gid = fingerprint_db.execute(query).scalar()
if track_id is None or track_gid is None:
abort(404)
......@@ -44,7 +47,7 @@ def track(track_id):
schema.fingerprint.c.length,
schema.fingerprint.c.submission_count],
schema.fingerprint.c.track_id == track_id).order_by(schema.fingerprint.c.length)
fingerprints = conn.execute(query).fetchall()
fingerprints = fingerprint_db.execute(query).fetchall()
query = sql.select(
[schema.track_mbid.c.id,
......@@ -52,9 +55,9 @@ def track(track_id):
schema.track_mbid.c.submission_count,
schema.track_mbid.c.disabled],
schema.track_mbid.c.track_id == track_id)
mbids = conn.execute(query).fetchall()
mbids = fingerprint_db.execute(query).fetchall()
metadata = lookup_recording_metadata(conn, [r['mbid'] for r in mbids])
metadata = lookup_recording_metadata(musicbrainz_db, [r['mbid'] for r in mbids])
recordings = []
for mbid in mbids:
......@@ -80,7 +83,7 @@ def track(track_id):
filter(TrackMBIDChange.track_mbid_id.in_(m.id for m in mbids)).\
order_by(TrackMBIDChange.created.desc()).all()
moderator = is_moderator(conn, session.get('id'))
moderator = is_moderator(db.get_app_db(), session.get('id'))
return render_template('track.html', title=title,
fingerprints=fingerprints, recordings=recordings,
......@@ -91,7 +94,7 @@ def track(track_id):
@metadata_page.route('/fingerprint/<int:fingerprint_id>')
def fingerprint(fingerprint_id):
conn = db.session.connection()
finerprint_db = db.get_fingerprint_db()
title = 'Fingerprint #%s' % (fingerprint_id,)
query = sql.select(
[schema.fingerprint.c.id,
......@@ -100,16 +103,16 @@ def fingerprint(fingerprint_id):
schema.fingerprint.c.track_id,
schema.fingerprint.c.submission_count],
schema.fingerprint.c.id == fingerprint_id)
fingerprint = conn.execute(query).first()
fingerprint = finerprint_db.execute(query).first()
query = sql.select([schema.track.c.gid], schema.track.c.id == fingerprint['track_id'])
track_gid = conn.execute(query).scalar()
track_gid = finerprint_db.execute(query).scalar()
return render_template('fingerprint.html', title=title,
fingerprint=fingerprint, track_gid=track_gid)
@metadata_page.route('/fingerprint/<int:fingerprint_id_1>/compare/<int:fingerprint_id_2>')
def compare_fingerprints(fingerprint_id_1, fingerprint_id_2):
conn = db.session.connection()
finerprint_db = db.get_fingerprint_db()
title = 'Compare fingerprints #%s and #%s' % (fingerprint_id_1, fingerprint_id_2)
query = sql.select(
[schema.fingerprint.c.id,
......@@ -120,7 +123,7 @@ def compare_fingerprints(fingerprint_id_1, fingerprint_id_2):
schema.fingerprint.c.id.in_((fingerprint_id_1, fingerprint_id_2)))
fingerprint_1 = None
fingerprint_2 = None
for fingerprint in conn.execute(query):
for fingerprint in finerprint_db.execute(query):
if fingerprint['id'] == fingerprint_id_1:
fingerprint_1 = fingerprint
elif fingerprint['id'] == fingerprint_id_2:
......@@ -135,14 +138,13 @@ def compare_fingerprints(fingerprint_id_1, fingerprint_id_2):
def mbid(mbid):
from acoustid.data.track import lookup_tracks
from acoustid.data.musicbrainz import lookup_recording_metadata
conn = db.session.connection()
metadata = lookup_recording_metadata(conn, [mbid])
metadata = lookup_recording_metadata(db.get_musicbrainz_db(), [mbid])
if mbid not in metadata:
title = 'Incorrect Recording'
return render_template('mbid-not-found.html', title=title, mbid=mbid)
metadata = metadata[mbid]
title = 'Recording "%s" by %s' % (metadata['name'], metadata['artist_name'])
tracks = lookup_tracks(conn, [mbid]).get(mbid, [])
tracks = lookup_tracks(db.get_fingerprint_db(), [mbid]).get(mbid, [])
return render_template('mbid.html', title=title, tracks=tracks, mbid=mbid)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment