Browse Source

Create History relation and add entries

Logs User, time and Track.
master
Kevin Mok 6 years ago
parent
commit
d06e5912cc
  1. 20
      api/models.py
  2. 12
      api/views.py
  3. 1
      requirements.txt

20
api/models.py

@ -86,3 +86,23 @@ class AudioFeatures(models.Model):
return super(AudioFeatures, self).__str__()
# }}} AudioFeatures #
# History {{{ #
class History(models.Model):
class Meta:
verbose_name = "History"
verbose_name_plural = "History"
unique_together = (("user", "time"),)
history_id = models.AutoField(primary_key=True)
user = models.ForeignKey(User, on_delete=models.CASCADE)
time = models.DateTimeField()
track = models.ForeignKey(Track, on_delete=models.CASCADE)
def __str__(self):
return (self.user, self.time, self.track)
# }}} #

12
api/views.py

@ -15,6 +15,7 @@ from .utils import *
from .models import *
from login.models import User
from login.utils import get_user_context
from dateutil.parser import parse
# }}} imports #
@ -168,13 +169,18 @@ def parse_history(request, user_secret):
# library
track_obj, track_created = save_track_obj(track_dict['track'],
track_artists, None)
history_obj, history_created = History.objects.get_or_create(
user=user_obj,
time=parse(track_dict['played_at']),
track=track_obj,)
if console_logging:
tracks_processed += 1
print("Added track #{}: {} - {}".format(
print("Added track #{} for user {}: {} - {}".format(
tracks_processed,
track_obj.artists.first(),
track_obj.name,
history_obj.user,
history_obj.time,
history_obj.track,
))
if len(artist_genre_queue) > 0:

1
requirements.txt

@ -14,3 +14,4 @@ requests==2.18.4
six==1.11.0
urllib3==1.22
wrapt==1.10.11
python-dateutil==2.7.5
Loading…
Cancel
Save