Restyle Remove global lists and read everything from the database. (#71)

This commit is contained in:
restyled-io[bot]
2022-07-06 17:41:01 +03:00
committed by GitHub
parent 73891aa5cf
commit e77f2b9f2b
11 changed files with 97 additions and 70 deletions
+13 -5
View File
@@ -1,10 +1,10 @@
"""
This file contains the Playlists class for interacting with the playlist documents in MongoDB.
"""
from app import helpers
from app.db.mongodb import convert_many
from app.db.mongodb import convert_one
from app.db.mongodb import MongoPlaylists
from app import helpers
from bson import ObjectId
@@ -18,8 +18,12 @@ class Playlists(MongoPlaylists):
Inserts a new playlist object into the database.
"""
return self.collection.update_one(
{"name": playlist["name"]},
{"$set": playlist},
{
"name": playlist["name"]
},
{
"$set": playlist
},
upsert=True,
).upserted_id
@@ -45,7 +49,9 @@ class Playlists(MongoPlaylists):
return self.collection.update_one(
{"_id": ObjectId(playlistid)},
{"$set": {"lastUpdated": date}},
{"$set": {
"lastUpdated": date
}},
)
def add_track_to_playlist(self, playlistid: str, track: dict) -> None:
@@ -56,7 +62,9 @@ class Playlists(MongoPlaylists):
{
"_id": ObjectId(playlistid),
},
{"$push": {"pre_tracks": track}},
{"$push": {
"pre_tracks": track
}},
)
self.set_last_updated(playlistid)
+42 -18
View File
@@ -2,7 +2,9 @@
This file contains the AllSongs class for interacting with track documents in MongoDB.
"""
import pymongo
from app.db.mongodb import MongoTracks, convert_many, convert_one
from app.db.mongodb import convert_many
from app.db.mongodb import convert_one
from app.db.mongodb import MongoTracks
from bson import ObjectId
@@ -18,9 +20,12 @@ class Tracks(MongoTracks):
"""
Inserts a new track object into the database.
"""
return self.collection.update_one(
{"filepath": song_obj["filepath"]}, {"$set": song_obj}, upsert=True
).upserted_id
return self.collection.update_one({
"filepath": song_obj["filepath"]
}, {
"$set": song_obj
},
upsert=True).upserted_id
def insert_many(self, songs: list):
"""
@@ -52,21 +57,33 @@ class Tracks(MongoTracks):
"""
Returns all the songs matching the albums in the query params (using regex).
"""
songs = self.collection.find({"album": {"$regex": query, "$options": "i"}})
songs = self.collection.find(
{"album": {
"$regex": query,
"$options": "i"
}})
return convert_many(songs)
def search_songs_by_artist(self, query: str) -> list:
"""
Returns all the songs matching the artists in the query params.
"""
songs = self.collection.find({"artists": {"$regex": query, "$options": "i"}})
songs = self.collection.find(
{"artists": {
"$regex": query,
"$options": "i"
}})
return convert_many(songs)
def find_song_by_title(self, query: str) -> list:
"""
Finds all the tracks matching the title in the query params.
"""
song = self.collection.find({"title": {"$regex": query, "$options": "i"}})
song = self.collection.find(
{"title": {
"$regex": query,
"$options": "i"
}})
return convert_many(song)
def find_songs_by_album(self, name: str, artist: str) -> list:
@@ -80,7 +97,9 @@ class Tracks(MongoTracks):
"""
Returns a sorted list of all the tracks exactly matching the folder in the query params
"""
songs = self.collection.find({"folder": query}).sort("title", pymongo.ASCENDING)
songs = self.collection.find({
"folder": query
}).sort("title", pymongo.ASCENDING)
return convert_many(songs)
def find_songs_by_filenames(self, filenames: list) -> list:
@@ -102,8 +121,10 @@ class Tracks(MongoTracks):
Returns a list of all the tracks matching the path in the query params.
"""
return self.collection.count_documents(
{"filepath": {"$regex": f"^{path}", "$options": "i"}}
)
{"filepath": {
"$regex": f"^{path}",
"$options": "i"
}})
def find_songs_by_artist(self, query: str) -> list:
"""
@@ -117,8 +138,10 @@ class Tracks(MongoTracks):
Returns a list of all the tracks containing the albumartist in the query params.
"""
songs = self.collection.find(
{"albumartist": {"$regex": query, "$options": "i"}}
)
{"albumartist": {
"$regex": query,
"$options": "i"
}})
return convert_many(songs)
def get_song_by_path(self, path: str) -> dict:
@@ -155,13 +178,14 @@ class Tracks(MongoTracks):
songs = self.collection.find({"albumhash": hash})
return convert_many(songs)
def find_track_by_title_artists_album(
self, title: str, artist: str, album: str
) -> dict:
def find_track_by_title_artists_album(self, title: str, artist: str,
album: str) -> dict:
"""
Returns a single track matching the title, artist, and album in the query params.
"""
song = self.collection.find_one(
{"title": title, "artists": artist, "album": album}
)
song = self.collection.find_one({
"title": title,
"artists": artist,
"album": album
})
return convert_one(song)