diff --git a/maloja/apis/native_v1.py b/maloja/apis/native_v1.py index 099df87..6c4542a 100644 --- a/maloja/apis/native_v1.py +++ b/maloja/apis/native_v1.py @@ -515,4 +515,13 @@ def get_export(**keys): @authenticated_function(api=True) def delete_scrobble(timestamp): """Internal Use Only""" - database.remove_scrobble(timestamp) + return database.remove_scrobble(timestamp) + + +@api.post("edit_artist") +@authenticated_function(api=True) +def edit_artist(oldname,newname): + # we probably wanna pass the id to the web interface at some point + # but for now we just use the old name as identifer as it's always unique + """Internal Use Only""" + return database.change_artist_name(oldname,newname) diff --git a/maloja/database/__init__.py b/maloja/database/__init__.py index 1526f20..67462a9 100644 --- a/maloja/database/__init__.py +++ b/maloja/database/__init__.py @@ -137,6 +137,13 @@ def remove_scrobble(timestamp): result = sqldb.delete_scrobble(timestamp) dbcache.invalidate_caches(timestamp) +@waitfordb +def change_artist_name(oldname,newname): + log(f"Renaming {oldname} to {newname}") + id = sqldb.get_artist_id(oldname) + sqldb.edit_artist(id,newname) + dbcache.invalidate_entity_cache() + dbcache.invalidate_caches() diff --git a/maloja/database/dbcache.py b/maloja/database/dbcache.py index c31d130..c9e53e0 100644 --- a/maloja/database/dbcache.py +++ b/maloja/database/dbcache.py @@ -94,12 +94,12 @@ def cached_wrapper_individual(inner_func): return outer_func -def invalidate_caches(scrobbletime): +def invalidate_caches(scrobbletime=None): if malojaconfig['USE_GLOBAL_CACHE']: cleared, kept = 0, 0 for k in cache.keys(): # VERY BIG TODO: differentiate between None as in 'unlimited timerange' and None as in 'time doesnt matter here'! - if (k[3] is None or scrobbletime >= k[3]) and (k[4] is None or scrobbletime <= k[4]): + if scrobbletime is None or (k[3] is None or scrobbletime >= k[3]) and (k[4] is None or scrobbletime <= k[4]): cleared += 1 del cache[k] else: diff --git a/maloja/database/sqldb.py b/maloja/database/sqldb.py index 7a2a7ac..3b33afb 100644 --- a/maloja/database/sqldb.py +++ b/maloja/database/sqldb.py @@ -353,6 +353,19 @@ def get_artist_id(artistname,create_new=True,dbconn=None): return result.inserted_primary_key[0] +### Edit existing + +@connection_provider +def edit_artist(id,artistdict,dbconn=None): + dbentry = artist_dict_to_db(artistdict) + + op = DB['artists'].update().where( + DB['artists'].c.id==id + ).values( + **dbentry + ) + result = dbconn.execute(op) + diff --git a/maloja/web/jinja/artist.jinja b/maloja/web/jinja/artist.jinja index 244b9c9..a023f84 100644 --- a/maloja/web/jinja/artist.jinja +++ b/maloja/web/jinja/artist.jinja @@ -29,7 +29,9 @@ {% block content %} - + @@ -73,6 +75,7 @@ +