migrated playlist reference storage to object references as opposed to name strings
This commit is contained in:
parent
1ac189ca7a
commit
3bb7ac663a
@ -34,10 +34,16 @@ def get_playlists():
|
||||
|
||||
pulled_user = database.get_user_doc_ref(session['username'])
|
||||
|
||||
playlists = database.get_user_playlists_collection(pulled_user.id)
|
||||
playlists = pulled_user.collection(u'playlists')
|
||||
|
||||
playlist_docs = [i.to_dict() for i in playlists.stream()]
|
||||
|
||||
for j in playlist_docs:
|
||||
j['playlist_references'] = [i.get().to_dict().get('name', 'n/a')
|
||||
for i in j['playlist_references']]
|
||||
|
||||
response = {
|
||||
'playlists': [i.to_dict() for i in playlists.stream()]
|
||||
'playlists': playlist_docs
|
||||
}
|
||||
|
||||
return jsonify(response), 200
|
||||
@ -53,7 +59,7 @@ def playlist():
|
||||
if 'username' in session:
|
||||
|
||||
user_ref = database.get_user_doc_ref(session['username'])
|
||||
playlists = database.get_user_playlists_collection(user_ref.id)
|
||||
playlists = user_ref.collection(u'playlists')
|
||||
|
||||
if request.method == 'GET' or request.method == 'DELETE':
|
||||
playlist_name = request.args.get('name', None)
|
||||
@ -69,12 +75,17 @@ def playlist():
|
||||
|
||||
if request.method == "GET":
|
||||
|
||||
return jsonify(queried_playlist[0].to_dict()), 200
|
||||
playlist_doc = queried_playlist[0].to_dict()
|
||||
|
||||
playlist_doc['playlist_references'] = [i.get().to_dict().get('name', 'n/a')
|
||||
for i in playlist_doc['playlist_references']]
|
||||
|
||||
return jsonify(playlist_doc), 200
|
||||
|
||||
elif request.method == 'DELETE':
|
||||
|
||||
logger.info(f'deleted {session["username"]} / {queried_playlist[0].to_dict()["name"]}')
|
||||
playlists.document(queried_playlist[0].id).delete()
|
||||
queried_playlist[0].reference.delete()
|
||||
|
||||
return jsonify({"message": 'playlist deleted', "status": "success"}), 200
|
||||
|
||||
@ -91,7 +102,19 @@ def playlist():
|
||||
playlist_name = request_json['name']
|
||||
|
||||
playlist_parts = request_json.get('parts', None)
|
||||
playlist_references = request_json.get('playlist_references', None)
|
||||
|
||||
playlist_references = []
|
||||
|
||||
if request_json.get('playlist_references', None):
|
||||
for i in request_json['playlist_references']:
|
||||
retrieved_ref = database.get_user_playlist_ref_by_user_ref(user_ref, i)
|
||||
if retrieved_ref:
|
||||
playlist_references.append(retrieved_ref)
|
||||
else:
|
||||
return jsonify({"message": f'managed playlist {i} not found', "status": "error"}), 400
|
||||
|
||||
if len(playlist_references) == 0:
|
||||
playlist_references = None
|
||||
|
||||
playlist_id = request_json.get('id', None)
|
||||
playlist_shuffle = request_json.get('shuffle', None)
|
||||
|
@ -23,7 +23,7 @@ def get_user_doc_ref(user):
|
||||
|
||||
if len(users) > 0:
|
||||
if len(users) == 1:
|
||||
return db.collection(u'spotify_users').document(u'{}'.format(users[0].id))
|
||||
return users[0].reference
|
||||
|
||||
else:
|
||||
logger.error(f"multiple {user}'s found")
|
||||
@ -41,33 +41,45 @@ def get_user_playlists_collection(user_id):
|
||||
return playlists
|
||||
|
||||
|
||||
def get_user_playlist_ref(user, playlist):
|
||||
def get_user_playlist_ref_by_username(user, playlist):
|
||||
|
||||
user_ref = get_user_doc_ref(user)
|
||||
|
||||
if user_ref:
|
||||
|
||||
playlist_collection = get_user_playlists_collection(user_ref.id)
|
||||
|
||||
if playlist_collection:
|
||||
query = [i for i in playlist_collection.where(u'name', u'==', playlist).stream()]
|
||||
|
||||
if len(query) > 0:
|
||||
if len(query) > 1:
|
||||
return playlist_collection.document(u'{}'.format(query[0].id))
|
||||
|
||||
else:
|
||||
logger.error(f'{user} multiple response playlists found for {playlist}')
|
||||
return query
|
||||
|
||||
else:
|
||||
logger.error(f'{user} no playlist found for {playlist}')
|
||||
return None
|
||||
|
||||
else:
|
||||
logger.error(f'{user} playlist collection not found, looking up {playlist}')
|
||||
return None
|
||||
return get_user_playlist_ref_by_user_ref(user_ref, playlist)
|
||||
|
||||
else:
|
||||
logger.error(f'{user} not found, looking up {playlist}')
|
||||
return None
|
||||
|
||||
|
||||
def get_user_playlist_ref_by_user_ref(user_ref, playlist):
|
||||
|
||||
playlist_collection = get_user_playlists_collection(user_ref.id)
|
||||
|
||||
username = user_ref.get().to_dict()['username']
|
||||
|
||||
if playlist_collection:
|
||||
query = [i for i in playlist_collection.where(u'name', u'==', playlist).stream()]
|
||||
|
||||
if len(query) > 0:
|
||||
if len(query) == 1:
|
||||
if query[0].exists:
|
||||
return query[0].reference
|
||||
|
||||
else:
|
||||
logger.error(f'{playlist} for {username} does not exist')
|
||||
return query[0]
|
||||
|
||||
else:
|
||||
logger.error(f'{username} multiple response playlists found for {playlist}')
|
||||
return query
|
||||
|
||||
else:
|
||||
logger.error(f'{username} no playlist found for {playlist}')
|
||||
return None
|
||||
|
||||
else:
|
||||
logger.error(f'{username} playlist collection not found, looking up {playlist}')
|
||||
return None
|
||||
|
@ -23,33 +23,52 @@ class PartGenerator:
|
||||
else:
|
||||
raise NameError('no user info provided')
|
||||
|
||||
def reset(self):
|
||||
self.queried_playlists = []
|
||||
self.parts = []
|
||||
|
||||
def get_recursive_parts(self, name):
|
||||
logger.info(f'getting part from {name} for {self.user_id}')
|
||||
|
||||
self.queried_playlists = []
|
||||
self.parts = []
|
||||
self._generate_parts(name)
|
||||
self.reset()
|
||||
self.process_reference_by_name(name)
|
||||
|
||||
return [i for i in {i for i in self.parts}]
|
||||
|
||||
def _generate_parts(self, name):
|
||||
self.queried_playlists.append(name)
|
||||
def process_reference_by_name(self, name):
|
||||
|
||||
playlist_query = [i.to_dict() for i in
|
||||
database.get_user_playlists_collection(self.user_id).where(u'name', '==', name).stream()]
|
||||
playlist_query = [i for i in
|
||||
database.get_user_playlists_collection(self.user_id).where(u'name', u'==', name).stream()]
|
||||
|
||||
if len(playlist_query) > 0:
|
||||
if len(playlist_query) == 1:
|
||||
|
||||
playlist_doc = playlist_query[0]
|
||||
self.parts += playlist_doc['parts']
|
||||
if playlist_query[0].id not in self.queried_playlists:
|
||||
|
||||
for i in playlist_doc['playlist_references']:
|
||||
if i not in self.queried_playlists:
|
||||
self._generate_parts(i)
|
||||
playlist_doc = playlist_query[0].to_dict()
|
||||
self.parts += playlist_doc['parts']
|
||||
|
||||
for i in playlist_doc['playlist_references']:
|
||||
if i.id not in self.queried_playlists:
|
||||
self.process_reference_by_reference(i)
|
||||
|
||||
else:
|
||||
logger.warning(f'playlist reference {name} already queried')
|
||||
|
||||
else:
|
||||
logger.warning(f"multiple {name}'s found")
|
||||
|
||||
else:
|
||||
logger.warning(f'playlist {name} not found')
|
||||
logger.warning(f'playlist reference {name} not found')
|
||||
|
||||
def process_reference_by_reference(self, ref):
|
||||
|
||||
if ref.id not in self.queried_playlists:
|
||||
playlist_reference_object = ref.get().to_dict()
|
||||
self.parts += playlist_reference_object['parts']
|
||||
|
||||
for i in playlist_reference_object['playlist_references']:
|
||||
self.process_reference_by_reference(i)
|
||||
|
||||
else:
|
||||
logger.warning(f'playlist reference {ref.get().to_dict()["name"]} already queried')
|
||||
|
Loading…
Reference in New Issue
Block a user