def playlists(user_name: str): """ Show user playlists """ if not current_app.config.get("FEATURE_PLAYLIST", False): raise NotFound() offset = request.args.get('offset', 0) try: offset = int(offset) except ValueError: raise BadRequest("Incorrect int argument offset: %s" % request.args.get("offset")) count = request.args.get("count", DEFAULT_NUMBER_OF_PLAYLISTS_PER_CALL) try: count = int(count) except ValueError: raise BadRequest("Incorrect int argument count: %s" % request.args.get("count")) user = _get_user(user_name) user_data = { "name": user.musicbrainz_id, "id": user.id, } current_user_data = {} if current_user.is_authenticated: current_user_data = { "id": current_user.id, "name": current_user.musicbrainz_id, "auth_token": current_user.auth_token, } include_private = current_user.is_authenticated and current_user.id == user.id playlists = [] user_playlists, playlist_count = get_playlists_for_user(user.id, include_private=include_private, load_recordings=False, count=count, offset=offset) for playlist in user_playlists: playlists.append(serialize_jspf(playlist)) props = { "current_user": current_user_data, "api_url": current_app.config["API_URL"], "playlists": playlists, "user": user_data, "active_section": "playlists", "playlist_count": playlist_count, "pagination_offset": offset, "playlists_per_page": count, } return render_template( "playlists/playlists.html", active_section="playlists", props=ujson.dumps(props), user=user )
def get_playlists_for_user(playlist_user_name): """ Fetch playlist metadata in JSPF format without recordings for the given user. If a user token is provided in the Authorization header, return private playlists as well as public playlists for that user. :params count: The number of playlists to return (for pagination). Default :data:`~webserver.views.api.DEFAULT_NUMBER_OF_PLAYLISTS_PER_CALL` :params offset: The offset of into the list of playlists to return (for pagination) :statuscode 200: Yay, you have data! :statuscode 404: User not found :resheader Content-Type: *application/json* """ user = validate_auth_header(True) count = get_non_negative_param('count', DEFAULT_NUMBER_OF_PLAYLISTS_PER_CALL) offset = get_non_negative_param('offset', 0) playlist_user = db_user.get_by_mb_id(playlist_user_name) if playlist_user is None: raise APINotFound("Cannot find user: %s" % playlist_user_name) include_private = True if user and user["id"] == playlist_user[ "id"] else False playlists, playlist_count = db_playlist.get_playlists_for_user( playlist_user["id"], include_private=include_private, load_recordings=False, count=count, offset=offset) return jsonify( serialize_playlists(playlists, playlist_count, count, offset))