Refactor saving History obj
From import_history and parse_history. Also refactor getting CSV information from import_history.
This commit is contained in:
51
api/utils.py
51
api/utils.py
@@ -236,12 +236,14 @@ def get_artists_in_genre(user, genre, max_songs):
|
|||||||
|
|
||||||
# }}} get_artists_in_genre #
|
# }}} get_artists_in_genre #
|
||||||
|
|
||||||
|
# save_track_artists {{{ #
|
||||||
|
|
||||||
def save_track_artists(track_dict, artist_genre_queue, user_headers):
|
def save_track_artists(track_dict, artist_genre_queue, user_headers):
|
||||||
""" Update artist info before creating Track so that Track object can
|
""" Update artist info before creating Track so that Track object can
|
||||||
reference Artist object.
|
reference Artist object.
|
||||||
|
|
||||||
:track_dict: TODO
|
:track_dict: response from Spotify API for track
|
||||||
:returns: None
|
:returns: list of Artist objects in Track
|
||||||
|
|
||||||
"""
|
"""
|
||||||
track_artists = []
|
track_artists = []
|
||||||
@@ -259,6 +261,10 @@ def save_track_artists(track_dict, artist_genre_queue, user_headers):
|
|||||||
|
|
||||||
return track_artists
|
return track_artists
|
||||||
|
|
||||||
|
# }}} save_track_artists #
|
||||||
|
|
||||||
|
# get_user_header {{{ #
|
||||||
|
|
||||||
def get_user_header(user_obj):
|
def get_user_header(user_obj):
|
||||||
"""Returns the authorization string needed to make an API call.
|
"""Returns the authorization string needed to make an API call.
|
||||||
|
|
||||||
@@ -284,3 +290,44 @@ def get_user_header(user_obj):
|
|||||||
user_obj.save()
|
user_obj.save()
|
||||||
|
|
||||||
return {'Authorization': "Bearer " + user_obj.access_token}
|
return {'Authorization': "Bearer " + user_obj.access_token}
|
||||||
|
|
||||||
|
# }}} get_user_header #
|
||||||
|
|
||||||
|
def save_history_obj (user, timestamp, track):
|
||||||
|
"""Return (get/create) a History object with the specified parameters. Can't
|
||||||
|
use built-in get_or_create since don't know auto PK.
|
||||||
|
|
||||||
|
:user: User object History should be associated with
|
||||||
|
:timestamp: time at which song was listened to
|
||||||
|
:track: Track object for song
|
||||||
|
:returns: History object
|
||||||
|
|
||||||
|
"""
|
||||||
|
history_query = History.objects.filter(user__exact=user,
|
||||||
|
timestamp__exact=timestamp)
|
||||||
|
if len(history_query) == 0:
|
||||||
|
history_obj = History.objects.create(user=user, timestamp=timestamp,
|
||||||
|
track=track)
|
||||||
|
else:
|
||||||
|
history_obj = history_query[0]
|
||||||
|
|
||||||
|
return history_obj
|
||||||
|
|
||||||
|
def get_next_history_row(csv_reader, headers, prev_info):
|
||||||
|
"""Return formatted information from next row in history CSV file.
|
||||||
|
|
||||||
|
:csv_reader: TODO
|
||||||
|
:headers:
|
||||||
|
:prev_info: history_obj_info of last row in case no more rows
|
||||||
|
:returns: (boolean of if last row, dict with information of next row)
|
||||||
|
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
row = next(csv_reader)
|
||||||
|
# if Track.objects.filter(id__exact=row[1]).exists():
|
||||||
|
history_obj_info = {}
|
||||||
|
for i in range(len(headers)):
|
||||||
|
history_obj_info[headers[i]] = row[i]
|
||||||
|
return False, history_obj_info
|
||||||
|
except StopIteration:
|
||||||
|
return True, prev_info
|
||||||
|
|||||||
46
api/views.py
46
api/views.py
@@ -150,10 +150,8 @@ def parse_history(request, user_secret):
|
|||||||
user_headers)
|
user_headers)
|
||||||
track_obj, track_created = save_track_obj(track_dict['track'],
|
track_obj, track_created = save_track_obj(track_dict['track'],
|
||||||
track_artists, None)
|
track_artists, None)
|
||||||
history_obj, history_created = History.objects.get_or_create(
|
history_obj = save_history_obj(user_obj, parse(track_dict['played_at']),
|
||||||
user=user_obj,
|
track_obj)
|
||||||
timestamp=parse(track_dict['played_at']),
|
|
||||||
track=track_obj,)
|
|
||||||
|
|
||||||
if console_logging:
|
if console_logging:
|
||||||
tracks_processed += 1
|
tracks_processed += 1
|
||||||
@@ -257,19 +255,12 @@ def import_history(request, upload_id):
|
|||||||
history_obj_info_lst = []
|
history_obj_info_lst = []
|
||||||
artist_genre_queue = []
|
artist_genre_queue = []
|
||||||
|
|
||||||
next(csv_reader)
|
# skip header row
|
||||||
row = next(csv_reader)
|
last_row, history_obj_info = get_next_history_row(csv_reader, headers,
|
||||||
last_row = False
|
{})
|
||||||
while not last_row:
|
while not last_row:
|
||||||
|
last_row, history_obj_info = get_next_history_row(csv_reader,
|
||||||
# if Track.objects.filter(id__exact=row[1]).exists():
|
headers, history_obj_info)
|
||||||
history_obj_info = {}
|
|
||||||
for i in range(len(headers)):
|
|
||||||
history_obj_info[headers[i]] = row[i]
|
|
||||||
try:
|
|
||||||
row = next(csv_reader)
|
|
||||||
except StopIteration:
|
|
||||||
last_row = True
|
|
||||||
|
|
||||||
# }}} setup #
|
# }}} setup #
|
||||||
|
|
||||||
@@ -290,33 +281,22 @@ def import_history(request, upload_id):
|
|||||||
# }}} get tracks_response #
|
# }}} get tracks_response #
|
||||||
|
|
||||||
for track_dict in tracks_response:
|
for track_dict in tracks_response:
|
||||||
# create History obj {{{ #
|
|
||||||
|
|
||||||
# don't associate history track with User, not necessarily in their
|
# don't associate history track with User, not necessarily in their
|
||||||
# library
|
# library
|
||||||
track_artists = save_track_artists(track_dict, artist_genre_queue,
|
track_artists = save_track_artists(track_dict, artist_genre_queue,
|
||||||
user_headers)
|
user_headers)
|
||||||
track_obj, track_created = save_track_obj(track_dict, track_artists, None)
|
track_obj, track_created = save_track_obj(track_dict,
|
||||||
|
track_artists, None)
|
||||||
|
|
||||||
timestamp = parse(history_obj_info_lst[responses_processed]['timestamp'])
|
timestamp = \
|
||||||
# missing PK to do get_or_create
|
parse(history_obj_info_lst[responses_processed]['timestamp'])
|
||||||
history_query = \
|
history_obj = save_history_obj(upload_obj.user, timestamp,
|
||||||
History.objects.filter(user__exact=upload_obj.user,
|
track_obj)
|
||||||
timestamp__exact=timestamp)
|
|
||||||
if len(history_query) == 0:
|
|
||||||
history_obj = \
|
|
||||||
History.objects.create(user=upload_obj.user,
|
|
||||||
timestamp=timestamp,
|
|
||||||
track=track_obj,)
|
|
||||||
else:
|
|
||||||
history_obj = history_query[0]
|
|
||||||
|
|
||||||
if console_logging:
|
if console_logging:
|
||||||
print("Processed row #{}: {}".format(
|
print("Processed row #{}: {}".format(
|
||||||
(rows_read - TRACKS_LIMIT) + responses_processed, history_obj,))
|
(rows_read - TRACKS_LIMIT) + responses_processed, history_obj,))
|
||||||
responses_processed += 1
|
responses_processed += 1
|
||||||
|
|
||||||
# }}} create History obj #
|
|
||||||
|
|
||||||
history_obj_info_lst = []
|
history_obj_info_lst = []
|
||||||
|
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h1>{{ user_id }}'s Listening History</h1>
|
<h1>{{ user_id }}'s Listening History</h1>
|
||||||
|
<p> Found {{ total_history }} songs. </p>
|
||||||
<a class="btn btn-primary " href="{% export_url 'csv' %}" role="button">Export</a>
|
<a class="btn btn-primary " href="{% export_url 'csv' %}" role="button">Export</a>
|
||||||
{% render_table user_history_table %}
|
{% render_table user_history_table %}
|
||||||
</body>
|
</body>
|
||||||
|
|||||||
@@ -65,6 +65,7 @@ class HistoryList(ExportMixin, SingleTableView):
|
|||||||
def get_context_data(self, **kwargs):
|
def get_context_data(self, **kwargs):
|
||||||
context = super().get_context_data(**kwargs)
|
context = super().get_context_data(**kwargs)
|
||||||
context['user_id'] = self.request.session['user_id']
|
context['user_id'] = self.request.session['user_id']
|
||||||
|
context['total_history'] = self.get_table_data().count()
|
||||||
return context
|
return context
|
||||||
|
|
||||||
def get_export_filename(self, export_format):
|
def get_export_filename(self, export_format):
|
||||||
|
|||||||
Reference in New Issue
Block a user