Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions beetsplug/web/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
import base64
import json
import os
import typing as t

import flask
from flask import g, jsonify
from flask import jsonify
from unidecode import unidecode
from werkzeug.routing import BaseConverter, PathConverter

Expand All @@ -28,6 +29,17 @@
from beets.dbcore.query import PathQuery
from beets.plugins import BeetsPlugin

# Type checking hacks

if t.TYPE_CHECKING:

class LibraryCtx(flask.ctx._AppCtxGlobals):
lib: beets.library.Library

g = LibraryCtx()
else:
from flask import g

# Utilities.


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

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

For plugin developers:

Expand Down
7 changes: 7 additions & 0 deletions test/plugins/test_web.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ def test_get_all_items(self):
assert response.status_code == 200
assert len(res_json["items"]) == 3

def test_get_unique_item_artist(self):
response = self.client.get("/item/values/artist")
res_json = json.loads(response.data.decode("utf-8"))

assert response.status_code == 200
assert res_json["values"] == ["", "AAA Singers"]

def test_get_single_item_by_id(self):
response = self.client.get("/item/1")
res_json = json.loads(response.data.decode("utf-8"))
Expand Down
Loading