mirror of
https://github.com/Dvorinka/swingmusic-extended.git
synced 2026-06-03 20:13:02 +00:00
Move MongoDB classes into a seperate db module (#62)
* move album class into mongodb dir * inherit AlbumMethods at DB Initialization level * move album -> db/mongodb * move mongodb classes into separate package
This commit is contained in:
+191
-33
@@ -1,44 +1,202 @@
|
||||
"""
|
||||
This module creates and initiliazes a MongoDB instance. It also contains the
|
||||
`convert_one()` and `conver_many()` methods for converting MongoDB cursors to Python dicts.
|
||||
"""
|
||||
|
||||
import pymongo
|
||||
import json
|
||||
from bson import json_util
|
||||
|
||||
|
||||
class Mongo:
|
||||
class AlbumMethods:
|
||||
"""
|
||||
The base class for all mongodb classes.
|
||||
Lists all the methods that can be found in the Albums class.
|
||||
"""
|
||||
|
||||
def __init__(self, database):
|
||||
mongo_uri = pymongo.MongoClient()
|
||||
self.db = mongo_uri[database]
|
||||
def insert_album():
|
||||
"""
|
||||
Inserts a new album object into the database.
|
||||
"""
|
||||
pass
|
||||
|
||||
def get_all_albums():
|
||||
"""
|
||||
Returns all the albums in the database.
|
||||
"""
|
||||
pass
|
||||
|
||||
def get_album_by_id():
|
||||
"""
|
||||
Returns a single album matching the passed id.
|
||||
"""
|
||||
pass
|
||||
|
||||
def get_album_by_name():
|
||||
"""
|
||||
Returns a single album matching the passed name.
|
||||
"""
|
||||
pass
|
||||
|
||||
def get_album_by_artist():
|
||||
"""
|
||||
Returns a single album matching the passed artist name.
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
def convert_one(song):
|
||||
class ArtistMethods:
|
||||
"""
|
||||
Converts a single mongodb cursor to a json object.
|
||||
"""
|
||||
json_song = json.dumps(song, default=json_util.default)
|
||||
loaded_song = json.loads(json_song)
|
||||
|
||||
return loaded_song
|
||||
|
||||
|
||||
def convert_many(array):
|
||||
"""
|
||||
Converts a list of mongodb cursors to a list of json objects.
|
||||
Lists all the methods that can be found in the Artists class.
|
||||
"""
|
||||
|
||||
songs = []
|
||||
def insert_artist():
|
||||
"""
|
||||
Inserts a new artist object into the database.
|
||||
"""
|
||||
pass
|
||||
|
||||
for song in array:
|
||||
json_song = json.dumps(song, default=json_util.default)
|
||||
loaded_song = json.loads(json_song)
|
||||
def get_all_artists():
|
||||
"""
|
||||
Returns all the artists in the database.
|
||||
"""
|
||||
pass
|
||||
|
||||
songs.append(loaded_song)
|
||||
def get_artist_by_id():
|
||||
"""
|
||||
Returns an artist matching the mongo Id.
|
||||
"""
|
||||
pass
|
||||
|
||||
return songs
|
||||
def get_artists_by_name():
|
||||
"""
|
||||
Returns all the artists matching the query.
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class PlaylistMethods:
|
||||
"""
|
||||
Lists all the methods that can be found in the Playlists class.
|
||||
"""
|
||||
|
||||
def insert_playlist():
|
||||
"""
|
||||
Inserts a new playlist object into the database.
|
||||
"""
|
||||
pass
|
||||
|
||||
def get_all_playlists():
|
||||
"""
|
||||
Returns all the playlists in the database.
|
||||
"""
|
||||
pass
|
||||
|
||||
def get_playlist_by_id():
|
||||
"""
|
||||
Returns a single playlist matching the id in the query params.
|
||||
"""
|
||||
pass
|
||||
|
||||
def add_track_to_playlist():
|
||||
"""
|
||||
Adds a track to a playlist.
|
||||
"""
|
||||
pass
|
||||
|
||||
def get_playlist_by_name():
|
||||
"""
|
||||
Returns a single playlist matching the name in the query params.
|
||||
"""
|
||||
pass
|
||||
|
||||
def update_playlist():
|
||||
"""
|
||||
Updates a playlist.
|
||||
"""
|
||||
pass
|
||||
|
||||
|
||||
class TrackMethods:
|
||||
"""
|
||||
Lists all the methods that can be found in the Tracks class.
|
||||
"""
|
||||
|
||||
def insert_track():
|
||||
"""
|
||||
Inserts a new track object into the database.
|
||||
"""
|
||||
pass
|
||||
|
||||
def drop_db():
|
||||
"""
|
||||
Drops the entire database.
|
||||
"""
|
||||
pass
|
||||
|
||||
def get_all_tracks():
|
||||
"""
|
||||
Returns all the tracks in the database.
|
||||
"""
|
||||
pass
|
||||
|
||||
def get_track_by_id():
|
||||
"""
|
||||
Returns a single track matching the id in the query params.
|
||||
"""
|
||||
pass
|
||||
|
||||
def get_track_by_album():
|
||||
"""
|
||||
Returns a single track matching the album in the query params.
|
||||
"""
|
||||
pass
|
||||
|
||||
def search_tracks_by_album():
|
||||
"""
|
||||
Returns all the tracks matching the albums in the query params (using regex).
|
||||
"""
|
||||
pass
|
||||
|
||||
def search_tracks_by_artist():
|
||||
"""
|
||||
Returns all the tracks matching the artists in the query params.
|
||||
"""
|
||||
pass
|
||||
|
||||
def find_track_by_title():
|
||||
"""
|
||||
Finds all the tracks matching the title in the query params.
|
||||
"""
|
||||
pass
|
||||
|
||||
def find_tracks_by_album():
|
||||
"""
|
||||
Finds all the tracks matching the album in the query params.
|
||||
"""
|
||||
pass
|
||||
|
||||
def find_tracks_by_folder():
|
||||
"""
|
||||
Finds all the tracks matching the folder in the query params.
|
||||
"""
|
||||
pass
|
||||
|
||||
def find_tracks_by_artist():
|
||||
"""
|
||||
Finds all the tracks matching the artist in the query params.
|
||||
"""
|
||||
pass
|
||||
|
||||
def find_tracks_by_albumartist():
|
||||
"""
|
||||
Finds all the tracks matching the album artist in the query params.
|
||||
"""
|
||||
pass
|
||||
|
||||
def get_track_by_path():
|
||||
"""
|
||||
Returns a single track matching the path in the query params.
|
||||
"""
|
||||
pass
|
||||
|
||||
def remove_track_by_path():
|
||||
"""
|
||||
Removes a track from the database. Returns a boolean indicating success or failure of the operation.
|
||||
"""
|
||||
pass
|
||||
|
||||
def remove_track_by_id():
|
||||
"""
|
||||
Removes a track from the database. Returns a boolean indicating success or failure of the operation.
|
||||
"""
|
||||
pass
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
"""
|
||||
This module creates and initiliazes a MongoDB instance. It also contains the
|
||||
`convert_one()` and `conver_many()` methods for converting MongoDB cursors to Python dicts.
|
||||
"""
|
||||
import json
|
||||
|
||||
import pymongo
|
||||
from app.db import AlbumMethods
|
||||
from app.db import ArtistMethods
|
||||
from app.db import PlaylistMethods
|
||||
from app.db import TrackMethods
|
||||
from bson import json_util
|
||||
|
||||
|
||||
class Mongo:
|
||||
"""
|
||||
The base class for all mongodb classes.
|
||||
"""
|
||||
|
||||
def __init__(self, database):
|
||||
mongo_uri = pymongo.MongoClient()
|
||||
self.db = mongo_uri[database]
|
||||
|
||||
|
||||
class MongoAlbums(Mongo, AlbumMethods):
|
||||
|
||||
def __init__(self):
|
||||
super(MongoAlbums, self).__init__("ALICE_ALBUMS")
|
||||
self.collection = self.db["ALL_ALBUMS"]
|
||||
|
||||
|
||||
class MongoArtists(Mongo, ArtistMethods):
|
||||
|
||||
def __init__(self):
|
||||
super(MongoArtists, self).__init__("ALICE_ARTISTS")
|
||||
self.collection = self.db["ALL_ARTISTS"]
|
||||
|
||||
|
||||
class MongoPlaylists(Mongo, PlaylistMethods):
|
||||
|
||||
def __init__(self):
|
||||
super(MongoPlaylists, self).__init__("ALICE_PLAYLISTS")
|
||||
self.collection = self.db["ALL_PLAYLISTS"]
|
||||
|
||||
|
||||
class MongoTracks(Mongo, TrackMethods):
|
||||
|
||||
def __init__(self):
|
||||
super(MongoTracks, self).__init__("ALICE_MUSIC_TRACKS")
|
||||
self.collection = self.db["ALL_TRACKS"]
|
||||
|
||||
|
||||
# ====================================================================== #
|
||||
# cursor convertion methods
|
||||
|
||||
|
||||
def convert_one(song):
|
||||
"""
|
||||
Converts a single mongodb cursor to a json object.
|
||||
"""
|
||||
json_song = json.dumps(song, default=json_util.default)
|
||||
loaded_song = json.loads(json_song)
|
||||
|
||||
return loaded_song
|
||||
|
||||
|
||||
def convert_many(array):
|
||||
"""
|
||||
Converts a list of mongodb cursors to a list of json objects.
|
||||
"""
|
||||
|
||||
songs = []
|
||||
|
||||
for song in array:
|
||||
json_song = json.dumps(song, default=json_util.default)
|
||||
loaded_song = json.loads(json_song)
|
||||
|
||||
songs.append(loaded_song)
|
||||
|
||||
return songs
|
||||
@@ -3,22 +3,18 @@ This file contains the Album class for interacting with
|
||||
album documents in MongoDB.
|
||||
"""
|
||||
from app import db
|
||||
from app.db.mongodb import convert_many
|
||||
from app.db.mongodb import convert_one
|
||||
from app.db.mongodb import MongoAlbums
|
||||
from app.models import Album
|
||||
from bson import ObjectId
|
||||
|
||||
convert_many = db.convert_many
|
||||
convert_one = db.convert_one
|
||||
|
||||
|
||||
class Albums(db.Mongo):
|
||||
class Albums(MongoAlbums):
|
||||
"""
|
||||
The class for all album-related database operations.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
super(Albums, self).__init__("ALICE_ALBUMS")
|
||||
self.collection = self.db["ALL_ALBUMS"]
|
||||
|
||||
def insert_album(self, album: Album) -> None:
|
||||
"""
|
||||
Inserts a new album object into the database.
|
||||
@@ -1,19 +1,15 @@
|
||||
"""
|
||||
This file contains the Artists class for interacting with artist documents in MongoDB.
|
||||
"""
|
||||
from app import db
|
||||
from app.db.mongodb import MongoArtists
|
||||
from bson import ObjectId
|
||||
|
||||
|
||||
class Artists(db.Mongo):
|
||||
class Artists(MongoArtists):
|
||||
"""
|
||||
The artist class for all artist related database operations.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
super(Artists, self).__init__("ALICE_ARTISTS")
|
||||
self.collection = self.db["ALL_ARTISTS"]
|
||||
|
||||
def insert_artist(self, artist_obj: dict) -> None:
|
||||
"""
|
||||
Inserts an artist into the database.
|
||||
@@ -1,32 +1,29 @@
|
||||
"""
|
||||
This file contains the Playlists class for interacting with the playlist documents in MongoDB.
|
||||
"""
|
||||
from app import db
|
||||
from app import models
|
||||
from app.db.mongodb import convert_many
|
||||
from app.db.mongodb import convert_one
|
||||
from app.db.mongodb import MongoPlaylists
|
||||
from app.helpers import create_new_date
|
||||
from bson import ObjectId
|
||||
|
||||
from app.helpers import create_new_date
|
||||
|
||||
convert_many = db.convert_many
|
||||
convert_one = db.convert_one
|
||||
|
||||
|
||||
class Playlists(db.Mongo):
|
||||
class Playlists(MongoPlaylists):
|
||||
"""
|
||||
The class for all playlist-related database operations.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
super(Playlists, self).__init__("ALICE_PLAYLISTS")
|
||||
self.collection = self.db["ALL_PLAYLISTS"]
|
||||
|
||||
def insert_playlist(self, playlist: dict) -> None:
|
||||
"""
|
||||
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
|
||||
|
||||
@@ -54,7 +51,14 @@ class Playlists(db.Mongo):
|
||||
{
|
||||
"_id": ObjectId(playlistid),
|
||||
},
|
||||
{"$push": {"pre_tracks": track}, "$set": {"lastUpdated": date}},
|
||||
{
|
||||
"$push": {
|
||||
"pre_tracks": track
|
||||
},
|
||||
"$set": {
|
||||
"lastUpdated": date
|
||||
}
|
||||
},
|
||||
)
|
||||
|
||||
def get_playlist_by_name(self, name: str) -> dict:
|
||||
@@ -0,0 +1,30 @@
|
||||
# """
|
||||
# This file contains the TrackColors class for interacting with Track colors documents in MongoDB.
|
||||
# """
|
||||
# from app import db
|
||||
# class TrackColors(db.Mongo):
|
||||
# """
|
||||
# The class for all track-related database operations.
|
||||
# """
|
||||
# def __init__(self):
|
||||
# super(TrackColors, self).__init__("ALICE_TRACK_COLORS")
|
||||
# self.collection = self.db["TRACK_COLORS"]
|
||||
# def insert_track_color(self, track_color: dict) -> None:
|
||||
# """
|
||||
# Inserts a new track object into the database.
|
||||
# """
|
||||
# return self.collection.update_one(
|
||||
# {
|
||||
# "filepath": track_color["filepath"]
|
||||
# },
|
||||
# {
|
||||
# "$set": track_color
|
||||
# },
|
||||
# upsert=True,
|
||||
# ).upserted_id
|
||||
# def get_track_color_by_track(self, filepath: str) -> dict:
|
||||
# """
|
||||
# Returns a track color object by its filepath.
|
||||
# """
|
||||
# track_color = self.collection.find_one({"filepath": filepath})
|
||||
# return db.convert_one(track_color)
|
||||
@@ -1,22 +1,17 @@
|
||||
"""
|
||||
This file contains the AllSongs class for interacting with track documents in MongoDB.
|
||||
"""
|
||||
from app import db
|
||||
from app.db.mongodb import convert_many
|
||||
from app.db.mongodb import convert_one
|
||||
from app.db.mongodb import MongoTracks
|
||||
from bson import ObjectId
|
||||
|
||||
convert_many = db.convert_many
|
||||
convert_one = db.convert_one
|
||||
|
||||
|
||||
class AllSongs(db.Mongo):
|
||||
class Tracks(MongoTracks):
|
||||
"""
|
||||
The class for all track-related database operations.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
super(AllSongs, self).__init__("ALICE_MUSIC_TRACKS")
|
||||
self.collection = self.db["ALL_TRACKS"]
|
||||
|
||||
# def drop_db(self):
|
||||
# self.collection.drop()
|
||||
|
||||
@@ -1,35 +0,0 @@
|
||||
"""
|
||||
This file contains the TrackColors class for interacting with Track colors documents in MongoDB.
|
||||
"""
|
||||
from app import db
|
||||
|
||||
|
||||
class TrackColors(db.Mongo):
|
||||
"""
|
||||
The class for all track-related database operations.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
super(TrackColors, self).__init__("ALICE_TRACK_COLORS")
|
||||
self.collection = self.db["TRACK_COLORS"]
|
||||
|
||||
def insert_track_color(self, track_color: dict) -> None:
|
||||
"""
|
||||
Inserts a new track object into the database.
|
||||
"""
|
||||
return self.collection.update_one(
|
||||
{
|
||||
"filepath": track_color["filepath"]
|
||||
},
|
||||
{
|
||||
"$set": track_color
|
||||
},
|
||||
upsert=True,
|
||||
).upserted_id
|
||||
|
||||
def get_track_color_by_track(self, filepath: str) -> dict:
|
||||
"""
|
||||
Returns a track color object by its filepath.
|
||||
"""
|
||||
track_color = self.collection.find_one({"filepath": filepath})
|
||||
return db.convert_one(track_color)
|
||||
@@ -1,14 +1,12 @@
|
||||
"""
|
||||
All the MongoDB instances are created here.
|
||||
"""
|
||||
from app.db import albums
|
||||
from app.db import artists
|
||||
from app.db import playlists
|
||||
from app.db import trackcolors
|
||||
from app.db import tracks
|
||||
from app.db.mongodb import albums
|
||||
from app.db.mongodb import artists
|
||||
from app.db.mongodb import playlists
|
||||
from app.db.mongodb import tracks
|
||||
|
||||
tracks_instance = tracks.AllSongs()
|
||||
tracks_instance = tracks.Tracks()
|
||||
artist_instance = artists.Artists()
|
||||
track_color_instance = trackcolors.TrackColors()
|
||||
album_instance = albums.Albums()
|
||||
playlist_instance = playlists.Playlists()
|
||||
|
||||
Reference in New Issue
Block a user