start: rewrite the database layer using a freaking ORM

+ start ditching in-mem stores
+ move main db table to a new name
+ experiments!
This commit is contained in:
cwilvx
2024-06-24 00:26:47 +03:00
parent c3472a865a
commit c42ec4dcde
27 changed files with 1399 additions and 397 deletions
+45 -30
View File
@@ -8,6 +8,7 @@ from app.settings import SUPPORTED_FILES
from app.utils.wintools import win_replace_slash
from app.store.tracks import TrackStore
from app.db import TrackTable as TrackDB
def create_folder(path: str, trackcount=0, foldercount=0) -> Folder:
@@ -37,44 +38,52 @@ def get_first_child_from_path(root: str, maybe_child: str):
return os.path.join(root, first)
def get_folders(paths: list[str]):
"""
Filters out folders that don't have any tracks and
returns a list of folder objects.
"""
count_dict = {
"tracks": {path: 0 for path in paths},
# folders are immediate children of the root folder
"folders": {path: set() for path in paths},
}
for track in TrackStore.tracks:
for path in paths:
# a child path should be longer than the root path
if len(track.folder) >= len(path) and track.folder.startswith(path):
count_dict["tracks"][path] += 1
# counting subfolders
p = get_first_child_from_path(path, track.folder)
if p:
count_dict["folders"][path].add(p)
folders = [
{
"path": path,
"trackcount": count_dict["tracks"][path],
"foldercount": len(count_dict["folders"][path]),
}
for path in paths
]
folders = TrackDB.count_tracks_containing_paths(paths)
return [
create_folder(f["path"], f["trackcount"], f["foldercount"])
create_folder(f["path"], f["trackcount"], foldercount=0)
for f in folders
if f["trackcount"] > 0
]
# count_dict = {
# "tracks": {path: 0 for path in paths},
# # folders are immediate children of the root folder
# "folders": {path: set() for path in paths},
# }
# for track in TrackStore.tracks:
# for path in paths:
# # a child path should be longer than the root path
# if len(track.folder) >= len(path) and track.folder.startswith(path):
# count_dict["tracks"][path] += 1
# # counting subfolders
# p = get_first_child_from_path(path, track.folder)
# if p:
# count_dict["folders"][path].add(p)
# folders = [
# {
# "path": path,
# "trackcount": count_dict["tracks"][path],
# "foldercount": len(count_dict["folders"][path]),
# }
# for path in paths
# ]
# return [
# create_folder(f["path"], f["trackcount"], f["foldercount"])
# for f in folders
# if f["trackcount"] > 0
# ]
class GetFilesAndDirs:
@@ -131,7 +140,13 @@ class GetFilesAndDirs:
files_.sort(key=lambda f: f["time"])
files = [f["path"] for f in files_]
tracks = TrackStore.get_tracks_by_filepaths(files)
tracks = []
if files:
tracks = TrackDB.get_tracks_by_filepaths(files)
print("printing files")
print(tracks)
# tracks = TrackStore.get_tracks_by_filepaths(files)
folders = []
if not self.tracks_only:
@@ -145,7 +160,7 @@ class GetFilesAndDirs:
return {
"path": path,
"tracks": serialize_tracks(tracks),
"tracks": tracks,
"folders": folders,
}