breakdown models.py into db module and object models

This commit is contained in:
geoffrey45
2022-03-21 01:00:13 +03:00
parent fcfd0c7936
commit cf14610509
7 changed files with 391 additions and 320 deletions
+39
View File
@@ -0,0 +1,39 @@
import pymongo
import json
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]
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