From f8dd338f07c5196e02cded1050cb51157348be27 Mon Sep 17 00:00:00 2001 From: Kartik Ohri Date: Wed, 11 Sep 2024 10:10:23 +0530 Subject: [PATCH 1/2] Fetch musicbrainz_row_id from MB for non-supporters A supporter account is not required for donations, hence fallback to checking MB db for musicbrainz_row_id if the specified editor_name is not associated with a supporter. --- config.py.example | 1 + consul_config.py.ctmpl | 1 + metabrainz/__init__.py | 3 +++ metabrainz/db/__init__.py | 7 +++++++ metabrainz/model/payment.py | 40 +++++++++++++++++++++++++------------ 5 files changed, 39 insertions(+), 13 deletions(-) diff --git a/config.py.example b/config.py.example index 38907e87..40fc70e7 100644 --- a/config.py.example +++ b/config.py.example @@ -7,6 +7,7 @@ SECRET_KEY = "CHANGE_THIS" # DATABASE SQLALCHEMY_DATABASE_URI = "postgresql://metabrainz:metabrainz@db:5432/metabrainz" +SQLALCHEMY_DATABASE_URI = "" SQLALCHEMY_TRACK_MODIFICATIONS = False POSTGRES_ADMIN_URI = "postgresql://postgres:postgres@db/postgres" diff --git a/consul_config.py.ctmpl b/consul_config.py.ctmpl index 5248f1c2..dce27ffa 100644 --- a/consul_config.py.ctmpl +++ b/consul_config.py.ctmpl @@ -16,6 +16,7 @@ DEBUG = False {{if service "pgbouncer-master"}} {{with index (service "pgbouncer-master") 0}} SQLALCHEMY_DATABASE_URI = "postgresql://{{template "KEY" "postgresql/username"}}:{{template "KEY" "postgresql/password"}}@{{.Address}}:{{.Port}}/{{template "KEY" "postgresql/db_name"}}" +SQLALCHEMY_MUSICBRAINZ_URI = 'postgresql://musicbrainz_ro@{{.Address}}:{{.Port}}/musicbrainz_db' {{end}} {{end}} diff --git a/metabrainz/__init__.py b/metabrainz/__init__.py index b4e13e32..45c0d9f8 100644 --- a/metabrainz/__init__.py +++ b/metabrainz/__init__.py @@ -92,6 +92,9 @@ def create_app(debug=None, config_path=None): # Database from metabrainz import db db.init_db_engine(app.config["SQLALCHEMY_DATABASE_URI"]) + if app.config.get("SQLALCHEMY_MUSICBRAINZ_URI", None): + db.init_mb_db_engine(app.config["SQLALCHEMY_MUSICBRAINZ_URI"]) + from metabrainz import model model.db.init_app(app) diff --git a/metabrainz/db/__init__.py b/metabrainz/db/__init__.py index 948a314d..4d6f003b 100644 --- a/metabrainz/db/__init__.py +++ b/metabrainz/db/__init__.py @@ -4,12 +4,19 @@ engine: sqlalchemy.engine.Engine = None +mb_engine: sqlalchemy.engine.Engine = None + def init_db_engine(connect_str): global engine engine = create_engine(connect_str, poolclass=NullPool) +def init_mb_db_engine(connect_str): + global mb_engine + mb_engine = create_engine(connect_str, poolclass=NullPool) + + def run_sql_script(sql_file_path): with open(sql_file_path) as sql: with engine.connect() as connection: diff --git a/metabrainz/model/payment.py b/metabrainz/model/payment.py index 22457552..9dce9d29 100644 --- a/metabrainz/model/payment.py +++ b/metabrainz/model/payment.py @@ -1,6 +1,6 @@ from __future__ import division -from sqlalchemy import exists +from sqlalchemy import exists, text from metabrainz.model import db, Supporter from metabrainz.payments import Currency, SUPPORTED_CURRENCIES @@ -155,6 +155,30 @@ def get_biggest_donations(cls, limit=None, offset=None): query = query.offset(offset) return count, query.all() + @staticmethod + def get_musicbrainz_row_id(editor_name): + """ Get the musicbrainz row id by given editor's name + + First try to retrieve the row id from the supporters table and then from MB database. + """ + supporter = Supporter.get(musicbrainz_id=editor_name) + if supporter is not None and supporter.musicbrainz_row_id is not None: + return supporter.musicbrainz_row_id + + from metabrainz.db import mb_engine + if mb_engine is not None: + with mb_engine.connect() as mb_conn: + result = mb_conn.execute( + text("SELECT id FROM editor WHERE lower(name) = lower(:editor_name)"), + {"editor_name": editor_name} + ) + row = result.fetchone() + if row is not None: + return row.id + + return None + + @classmethod def process_paypal_ipn(cls, form): """Processor for PayPal IPNs (Instant Payment Notifications). @@ -242,12 +266,7 @@ def process_paypal_ipn(cls, form): if is_donation: new_payment.editor_name = form.get('custom') - - supporter = Supporter.get(musicbrainz_id=new_payment.editor_name) - if supporter is None: - new_payment.editor_id = None - else: - new_payment.editor_id = supporter.musicbrainz_row_id + new_payment.editor_id = cls.get_musicbrainz_row_id(new_payment.editor_name) anonymous_opt = options.get("anonymous") if anonymous_opt is None: @@ -387,12 +406,7 @@ def _log_stripe_charge(cls, charge, metadata): if "editor" in metadata: new_donation.editor_name = metadata["editor"] - - supporter = Supporter.get(musicbrainz_id=new_donation.editor_name) - if supporter is None: - new_donation.editor_id = None - else: - new_donation.editor_id = supporter.musicbrainz_row_id + new_donation.editor_id = cls.get_musicbrainz_row_id(new_donation.editor_name) else: # Organization payment new_donation.invoice_number = metadata["invoice_number"] From 6761d75ec6e2e7916f1c7b244ef22d47331d5eb6 Mon Sep 17 00:00:00 2001 From: Kartik Ohri Date: Wed, 11 Sep 2024 19:08:39 +0530 Subject: [PATCH 2/2] fix typo --- config.py.example | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config.py.example b/config.py.example index 40fc70e7..93ced9cb 100644 --- a/config.py.example +++ b/config.py.example @@ -7,7 +7,7 @@ SECRET_KEY = "CHANGE_THIS" # DATABASE SQLALCHEMY_DATABASE_URI = "postgresql://metabrainz:metabrainz@db:5432/metabrainz" -SQLALCHEMY_DATABASE_URI = "" +SQLALCHEMY_MUSICBRAINZ_URI = "" SQLALCHEMY_TRACK_MODIFICATIONS = False POSTGRES_ADMIN_URI = "postgresql://postgres:postgres@db/postgres"