diff options
Diffstat (limited to 'mach2.py')
-rw-r--r-- | mach2.py | 58 |
1 files changed, 35 insertions, 23 deletions
@@ -20,7 +20,6 @@ from models.artist import Artist from models.track import Track from models.user import User - _CONFIG = configparser.ConfigParser() _CONFIG.read("mach2.ini") @@ -81,8 +80,8 @@ def load_user_from_request(req): if api_key: user = None - result = query_db("SELECT * FROM user WHERE api_key = ?", - [api_key], one=True) + result = query_db("SELECT * FROM user WHERE api_key = ?", [api_key], + one=True) if result: user = User(id=result[0], @@ -195,8 +194,10 @@ def album_search(album_name): result_albums = [] for returned_album in Album.search(current_app.config["LIBRARY"], - name={"data": album_name, - "operator": "LIKE"}): + name={ + "data": album_name, + "operator": "LIKE" + }): result_albums.append(returned_album.as_dict()) return jsonify(result_albums) @@ -227,10 +228,12 @@ def artists(): if order_by: returned_artists = Artist.all(current_app.config["LIBRARY"], order=order_by, - direction=order_direction, limit=lim, + direction=order_direction, + limit=lim, offset=off) else: - returned_artists = Artist.all(current_app.config["LIBRARY"], limit=lim, + returned_artists = Artist.all(current_app.config["LIBRARY"], + limit=lim, offset=off) for returned_artist in returned_artists: @@ -279,7 +282,7 @@ def artist_search(artist_name): name={ "data": artist_name, "operator": "LIKE" - }): + }): result_artists.append(artist.as_dict()) return jsonify(artists) @@ -309,11 +312,14 @@ def tracks(): if order_by: returned_tracks = Track.all(current_app.config["LIBRARY"], - order=order_by, direction=order_direction, - limit=lim, offset=off) + order=order_by, + direction=order_direction, + limit=lim, + offset=off) else: returned_tracks = Track.all(current_app.config["LIBRARY"], - limit=lim, offset=off) + limit=lim, + offset=off) for returned_track in returned_tracks: result_tracks.append(returned_track.as_dict()) @@ -381,14 +387,17 @@ def track(track_id): def track_search(track_name): result_tracks = [] for returned_track in Track.search(current_app.config["LIBRARY"], - name={"data": track_name, - "operator": "LIKE"}): + name={ + "data": track_name, + "operator": "LIKE" + }): result_tracks.append(returned_track.as_dict()) return jsonify(result_tracks) -@MACH2.route("/user", defaults={"user_id": None}, +@MACH2.route("/user", + defaults={"user_id": None}, methods=["GET", "POST", "PUT"]) @MACH2.route("/user/<int:user_id>", methods=["DELETE", "GET", "PUT"]) @login_required @@ -401,6 +410,7 @@ def users(user_id): on the currently logged in user. """ + def update_user(user, user_data): """Update the user with the supplied data. @@ -411,8 +421,7 @@ def users(user_id): """ db_conn = get_db() if "password" in user_data: - password_hash, api_key = user.new_password( - user_data["password"]) + password_hash, api_key = user.new_password(user_data["password"]) update_query = ("UPDATE user SET password_hash = ?, " "api_key = ? WHERE id = ?") @@ -441,8 +450,7 @@ def users(user_id): update_query, (user_data["transcode_command"], user.id)) if rows_updated > 0: - user.transcode_command = user_data[ - "transcode_command"] + user.transcode_command = user_data["transcode_command"] else: error = dict(message="Unable to update user") return jsonify(error), 500 @@ -499,10 +507,13 @@ def load_user(user_id): result = query_db("SELECT * FROM user WHERE id = ?", [user_id], one=True) if result: - local_user = User( - id=result[0], username=result[1],password_hash=result[2], - authenticated=1, active=result[4], anonymous=0, - transcode_command=result[7]) + local_user = User(id=result[0], + username=result[1], + password_hash=result[2], + authenticated=1, + active=result[4], + anonymous=0, + transcode_command=result[7]) return local_user @@ -512,7 +523,8 @@ def login(): if request.method == "POST": user = None result = query_db("SELECT * FROM user WHERE username = ?", - [request.form["username"]], one=True) + [request.form["username"]], + one=True) if result: user = User(id=result[0], username=result[1], |