Skip to content

Commit

Permalink
Fetch musicbrainz_row_id from MB for non-supporters (#482)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
amCap1712 authored Sep 11, 2024
1 parent 65fc502 commit ebf5710
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 13 deletions.
1 change: 1 addition & 0 deletions config.py.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ SECRET_KEY = "CHANGE_THIS"

# DATABASE
SQLALCHEMY_DATABASE_URI = "postgresql://metabrainz:metabrainz@db:5432/metabrainz"
SQLALCHEMY_MUSICBRAINZ_URI = ""
SQLALCHEMY_TRACK_MODIFICATIONS = False

POSTGRES_ADMIN_URI = "postgresql://postgres:postgres@db/postgres"
Expand Down
1 change: 1 addition & 0 deletions consul_config.py.ctmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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}}

Expand Down
3 changes: 3 additions & 0 deletions metabrainz/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
7 changes: 7 additions & 0 deletions metabrainz/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
40 changes: 27 additions & 13 deletions metabrainz/model/payment.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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"]
Expand Down

0 comments on commit ebf5710

Please sign in to comment.