Graphs and tables for your Spotify account.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.3 KiB

  1. from django.db import models
  2. from login.models import User
  3. # id's are 22 in length in examples but set to 30 for buffer
  4. MAX_ID = 30
  5. # Genre {{{ #
  6. class Genre(models.Model):
  7. class Meta:
  8. verbose_name = "Genre"
  9. verbose_name_plural = "Genres"
  10. name = models.CharField(primary_key=True, max_length=50)
  11. num_songs = models.PositiveIntegerField()
  12. def __str__(self):
  13. return self.name
  14. # }}} Genre #
  15. # Artist {{{ #
  16. class Artist(models.Model):
  17. class Meta:
  18. verbose_name = "Artist"
  19. verbose_name_plural = "Artists"
  20. id = models.CharField(primary_key=True, max_length=MAX_ID)
  21. name = models.CharField(max_length=50)
  22. genres = models.ManyToManyField(Genre, blank=True)
  23. def __str__(self):
  24. return self.name
  25. # }}} Artist #
  26. # Track {{{ #
  27. class Track(models.Model):
  28. class Meta:
  29. verbose_name = "Track"
  30. verbose_name_plural = "Tracks"
  31. id = models.CharField(primary_key=True, max_length=MAX_ID)
  32. artists = models.ManyToManyField(Artist, blank=True)
  33. year = models.PositiveSmallIntegerField(blank=True, null=True)
  34. popularity = models.PositiveSmallIntegerField()
  35. runtime = models.PositiveSmallIntegerField()
  36. name = models.CharField(max_length=200)
  37. users = models.ManyToManyField(User, blank=True)
  38. genre = models.ForeignKey(Genre, on_delete=models.CASCADE, blank=True,
  39. null=True)
  40. def __str__(self):
  41. track_str = "{}, genre: {}, artists: [".format(self.name, self.genre)
  42. for artist in self.artists.all():
  43. track_str += "{}, ".format(artist.name)
  44. track_str += "]"
  45. return track_str
  46. # }}} Track #
  47. # AudioFeatures {{{ #
  48. class AudioFeatures(models.Model):
  49. class Meta:
  50. verbose_name = "AudioFeatures"
  51. verbose_name_plural = "AudioFeatures"
  52. track = models.OneToOneField(Track, on_delete=models.CASCADE, primary_key=True,)
  53. acousticness = models.DecimalField(decimal_places=3, max_digits=3)
  54. danceability = models.DecimalField(decimal_places=3, max_digits=3)
  55. energy = models.DecimalField(decimal_places=3, max_digits=3)
  56. instrumentalness = models.DecimalField(decimal_places=3, max_digits=3)
  57. loudness = models.DecimalField(decimal_places=3, max_digits=6)
  58. speechiness = models.DecimalField(decimal_places=3, max_digits=3)
  59. tempo = models.DecimalField(decimal_places=3, max_digits=6)
  60. valence = models.DecimalField(decimal_places=3, max_digits=3)
  61. def __str__(self):
  62. return super(AudioFeatures, self).__str__()
  63. # }}} AudioFeatures #
  64. # History {{{ #
  65. class History(models.Model):
  66. class Meta:
  67. verbose_name = "History"
  68. verbose_name_plural = "History"
  69. unique_together = (("user", "timestamp"),)
  70. id = models.AutoField(primary_key=True)
  71. user = models.ForeignKey(User, on_delete=models.CASCADE)
  72. timestamp = models.DateTimeField()
  73. track = models.ForeignKey(Track, on_delete=models.CASCADE)
  74. def __str__(self):
  75. return " - ".join((str(self.user), str(self.timestamp), str(self.track)))
  76. def get_artists(self):
  77. artist_names = [artist.name for artist in self.track.artists.all()]
  78. return ', '.join(artist_names)
  79. def get_iso_timestamp(self):
  80. return self.timestamp.isoformat()
  81. # }}} #