|
|
@ -145,12 +145,15 @@ def user_data(request): |
|
|
|
request.session['user_id'] = user_data_response['id'] # store the user_id so it may be used to create model |
|
|
|
# request.session['user_name'] = user_data_response['display_name'] |
|
|
|
|
|
|
|
# get_or_create() returns a tuple (obj, created) |
|
|
|
user = User.objects.get_or_create(user_id=user_data_response['id'])[0] |
|
|
|
try: |
|
|
|
user = User.objects.get(user_id=user_data_response['id']) |
|
|
|
except User.DoesNotExist: |
|
|
|
user = User(user_id=user_data_response['id'], user_secret=generate_random_string(30)) |
|
|
|
user.save() |
|
|
|
|
|
|
|
context = { |
|
|
|
'user_name': user_data_response['display_name'], |
|
|
|
'id': user_data_response['id'], |
|
|
|
'user_secret': user.user_secret, |
|
|
|
} |
|
|
|
|
|
|
|
parse_library(headers, TRACKS_TO_QUERY, user) |
|
|
@ -178,3 +181,23 @@ def get_artist_data(request, user_id): |
|
|
|
processed_artist_data = [{'name': artist.name, 'num_songs': artist.num_songs} for artist in artist_counts] |
|
|
|
|
|
|
|
return JsonResponse(data=processed_artist_data, safe=False) |
|
|
|
|
|
|
|
|
|
|
|
def get_audio_feature_data(request, audio_feature, client_secret): |
|
|
|
"""Returns all data points for a given audio feature |
|
|
|
|
|
|
|
Args: |
|
|
|
request: the HTTP request |
|
|
|
audio_feature: The audio feature to be queried |
|
|
|
client_secret: client secret, used to identify the user |
|
|
|
""" |
|
|
|
user = User.objects.get(user_secret=client_secret) |
|
|
|
user_tracks = Track.objects.filter(users=user) |
|
|
|
response_payload = { |
|
|
|
'data_points': [], |
|
|
|
} |
|
|
|
for track in user_tracks: |
|
|
|
audio_feature_obj = AudioFeatures.objects.get(track=track) |
|
|
|
response_payload['data_points'].append(getattr(audio_feature_obj, audio_feature)) |
|
|
|
return JsonResponse(response_payload) |
|
|
|
|