~hristoast/mousikofidi

f7a3838fc3c38bed617a5ddb620a21fe7e3aa378 — Hristos N. Triantafillou 9 months ago bbbd440 0.4
Add dir path "breadcrumb" links (#2)
3 files changed, 81 insertions(+), 35 deletions(-)

M mousikofidi.py
M templates/dir_detail.html
M test_mousikofidi.py
M mousikofidi.py => mousikofidi.py +30 -1
@@ 138,9 138,14 @@ def browse_dir(context: dict, unescaped_path: str) -> dict:

    item_list = make_unique_slugs(_item_list)

    # TODO: Maybe don't stuff raw and encoded paths in context..
    music_dirs = []
    for d in context["music_dirs"]:
        music_dirs.append(d["raw"])

    context["file_list"] = file_list
    context["page_name"] = unescaped_path
    context["page_path"] = unescaped_path
    context["page_path"] = breadcrumb_links_from_path(unescaped_path, music_dirs)
    context["playlist_add"] = True
    context["playlist_rm"] = False
    context["item_type"] = item_type


@@ 399,6 404,30 @@ def video_dict(path: str) -> dict:
    }


def breadcrumb_links_from_path(path: str, music_dirs: list) -> str:
    link_string = ""
    path_string = ""

    for d in music_dirs:
        if path.startswith(d):
            link_string += '<a href="/browse/{escaped}">{unescaped}</a>'.format(
                escaped=quote(d), unescaped=d
            )
            path_string += d

            new_path = path.replace(d, "").strip("/")

            dir_list = new_path.split("/")

            for dd in dir_list:
                if dd:
                    path_string = os.path.join(path_string, dd)
                    link_string += '/<a href="/browse/{escaped}">{dir_name}</a>'.format(
                        escaped=quote(path_string), dir_name=dd
                    )
    return link_string


def handle_playlist_cmd(cmd: str, path: str, context: dict) -> dict:
    # This is only used in one view but was created in order to keep
    # all of this business out of the main view function.

M templates/dir_detail.html => templates/dir_detail.html +1 -1
@@ 1,7 1,7 @@
{% extends 'base.html' %}
{% block content %}

  <h4 class="center">{{ page_path }}</h4>
  <h4 class="center">{{ page_path|safe }}</h4>

  {% if item_type == "dir" %}
    {% include "directory_details.html" %}

M test_mousikofidi.py => test_mousikofidi.py +50 -33
@@ 27,6 27,7 @@ from mousikofidi import (
    app,
    audio_dict,
    audio_metadata,
    breadcrumb_links_from_path,
    browse_dir,
    browse_file,
    config_to_string,


@@ 133,47 134,51 @@ def test_audio_metadata_real_ogg():
    }


def test_breadcrumb_links_from_path():
    music_dirs = ["/home/user/music/flac", "/home/user/music/mp3"]
    path = "/home/user/music/flac/SomeArtist/SomeAlbum/SomeDisc"

    link_string = breadcrumb_links_from_path(path, music_dirs)

    print(link_string)

    assert (
        link_string
        == '<a href="/browse/%2Fhome%2Fuser%2Fmusic%2Fflac">/home/user/music/flac</a>/<a href="/browse/%2Fhome%2Fuser%2Fmusic%2Fflac%2FSomeArtist">SomeArtist</a>/<a href="/browse/%2Fhome%2Fuser%2Fmusic%2Fflac%2FSomeArtist%2FSomeAlbum">SomeAlbum</a>/<a href="/browse/%2Fhome%2Fuser%2Fmusic%2Fflac%2FSomeArtist%2FSomeAlbum%2FSomeDisc">SomeDisc</a>'
    )


def test_browse_dir():
    example_dir_unescaped = os.path.join(THIS_DIR, "example")
    example_dir_escaped = quote(example_dir_unescaped)
    config_string = """config:
  favicon_path: /fidi.png
  logo_path: /fidi.png
  music_dirs:
    - {}
  playlist_dir: /home/username/music/playlists
  site_name: TestSite
  theme: dark
""".format(
        example_dir_unescaped
    )
    config_file = tempfile.mkstemp(suffix=".yml")[1]
    with open(config_file, "w") as f:
        for line in config_string:
            f.write(line)
    c = init(use_config=config_file)
    with app.test_request_context("/browse/" + example_dir_escaped):
        c = request_context(
            init(use_config=os.path.join(THIS_DIR, "example", "fidi.yml"))
        )
        dir_dict = browse_dir(c, example_dir_unescaped)
    os.remove(config_file)
    unescaped_sourcedir = sys.argv[-1]
    escaped_sourcedir = quote(unescaped_sourcedir)

    expected_dict = {
        "config": {
            "favicon_path": "/fidi.png",
            "logo_path": "/fidi.png",
            "music_dirs": ["{}/example".format(unescaped_sourcedir)],
            "playlist_dir": "/home/username/music/playlists",
            "secret_key": None,
            "site_name": "TestSite",
            "theme": "dark",
        },
        "page_name": "{}/example".format(unescaped_sourcedir),
        "page_path": "{}/example".format(unescaped_sourcedir),
        "playlist_add": True,
        "playlist_rm": False,
        "item_type": "dir",
        "dir_list": [],
        "autoplay": False,
        "debug": False,
        "favicon_path": "/fidi.png",
        "logo_path": "/fidi.png",
        "music_dirs": [
            {
                "raw": "/home/username/music/flac",
                "encoded": "%2Fhome%2Fusername%2Fmusic%2Fflac",
            },
            {
                "raw": "/home/username/music/ogg",
                "encoded": "%2Fhome%2Fusername%2Fmusic%2Fogg",
            },
        ],
        "secret_key": None,
        "site_name": "MousikóFídi - Your Music Cloud",
        "user_playlist": [],
        "theme": "/css/water/light.standalone",
        "username": None,
        "file_list": [
            "{}/example/fake.flac".format(unescaped_sourcedir),
            "{}/example/fake.mp3".format(unescaped_sourcedir),


@@ 184,6 189,12 @@ def test_browse_dir():
            "{}/example/real.mp3".format(unescaped_sourcedir),
            "{}/example/real.ogg".format(unescaped_sourcedir),
        ],
        "page_name": "{}/example".format(unescaped_sourcedir),
        "page_path": "",
        "playlist_add": True,
        "playlist_rm": False,
        "item_type": "dir",
        "dir_list": [],
        "item_list": [
            {
                "album": None,


@@ 946,8 957,14 @@ def test_dir_detail_found(client):
        )
        in rv.data
    )

    assert (
        bytes('<h4 class="center">{}</h4>'.format(example_dir_unescaped), "utf8")
        bytes(
            '<h4 class="center"><a href="/browse/{escaped}">{unescaped}</a></h4>'.format(
                escaped=quote(example_dir_unescaped), unescaped=example_dir_unescaped
            ),
            "utf8",
        )
        in rv.data
    )
    assert (