Skip to content

Commit 88ca0ce

Browse files
authored
Plugins/web: fix endpoints /…/values/… (#6158)
Following #4709 and #5447, the web plugin used single-quotes (ie. string litteral) in the SQL query for table columns. Thus, for instance, the query `GET /item/values/albumartist` would return the litteral "albumartist" instead of a list of unique album artists. This prevents the Mopidy beets integration from working, returning the single artist "albumartist".
2 parents 07445fd + 189fedb commit 88ca0ce

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

beetsplug/web/__init__.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
import base64
1818
import json
1919
import os
20+
import typing as t
2021

2122
import flask
22-
from flask import g, jsonify
23+
from flask import jsonify
2324
from unidecode import unidecode
2425
from werkzeug.routing import BaseConverter, PathConverter
2526

@@ -28,6 +29,17 @@
2829
from beets.dbcore.query import PathQuery
2930
from beets.plugins import BeetsPlugin
3031

32+
# Type checking hacks
33+
34+
if t.TYPE_CHECKING:
35+
36+
class LibraryCtx(flask.ctx._AppCtxGlobals):
37+
lib: beets.library.Library
38+
39+
g = LibraryCtx()
40+
else:
41+
from flask import g
42+
3143
# Utilities.
3244

3345

@@ -232,7 +244,7 @@ def _get_unique_table_field_values(model, field, sort_field):
232244
raise KeyError
233245
with g.lib.transaction() as tx:
234246
rows = tx.query(
235-
f"SELECT DISTINCT '{field}' FROM '{model._table}' ORDER BY '{sort_field}'"
247+
f"SELECT DISTINCT {field} FROM {model._table} ORDER BY {sort_field}"
236248
)
237249
return [row[0] for row in rows]
238250

docs/changelog.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ Bug fixes:
4343
accepted a list of strings). :bug:`5962`
4444
- Fix a bug introduced in release 2.4.0 where import from any valid
4545
import-log-file always threw a "none of the paths are importable" error.
46+
- :doc:`/plugins/web`: repair broken `/item/values/…` and `/albums/values/…`
47+
endpoints. Previously, due to single-quotes (ie. string literal) in the SQL
48+
query, the query eg. `GET /item/values/albumartist` would return the literal
49+
"albumartist" instead of a list of unique album artists.
4650

4751
For plugin developers:
4852

test/plugins/test_web.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,13 @@ def test_get_all_items(self):
118118
assert response.status_code == 200
119119
assert len(res_json["items"]) == 3
120120

121+
def test_get_unique_item_artist(self):
122+
response = self.client.get("/item/values/artist")
123+
res_json = json.loads(response.data.decode("utf-8"))
124+
125+
assert response.status_code == 200
126+
assert res_json["values"] == ["", "AAA Singers"]
127+
121128
def test_get_single_item_by_id(self):
122129
response = self.client.get("/item/1")
123130
res_json = json.loads(response.data.decode("utf-8"))

0 commit comments

Comments
 (0)