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.

116 lines
3.4 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. # genre = models.ForeignKey(Genre, on_delete=models.CASCADE, blank=True,
  24. # null=True)
  25. def __str__(self):
  26. return self.name
  27. # }}} Artist #
  28. # Track {{{ #
  29. class Track(models.Model):
  30. class Meta:
  31. verbose_name = "Track"
  32. verbose_name_plural = "Tracks"
  33. id = models.CharField(primary_key=True, max_length=MAX_ID)
  34. artists = models.ManyToManyField(Artist, blank=True)
  35. year = models.PositiveSmallIntegerField(blank=True, null=True)
  36. popularity = models.PositiveSmallIntegerField()
  37. runtime = models.PositiveSmallIntegerField()
  38. name = models.CharField(max_length=200)
  39. users = models.ManyToManyField(User, blank=True)
  40. genre = models.ForeignKey(Genre, on_delete=models.CASCADE, blank=True,
  41. null=True)
  42. def __str__(self):
  43. track_str = "{}, genre: {}, artists: [".format(self.name, self.genre)
  44. for artist in self.artists.all():
  45. track_str += "{}, ".format(artist.name)
  46. track_str += "]"
  47. return track_str
  48. # }}} Track #
  49. # AudioFeatures {{{ #
  50. class AudioFeatures(models.Model):
  51. class Meta:
  52. verbose_name = "AudioFeatures"
  53. verbose_name_plural = "AudioFeatures"
  54. track = models.OneToOneField(Track, on_delete=models.CASCADE, primary_key=True,)
  55. acousticness = models.DecimalField(decimal_places=3, max_digits=3)
  56. danceability = models.DecimalField(decimal_places=3, max_digits=3)
  57. energy = models.DecimalField(decimal_places=3, max_digits=3)
  58. instrumentalness = models.DecimalField(decimal_places=3, max_digits=3)
  59. loudness = models.DecimalField(decimal_places=3, max_digits=6)
  60. speechiness = models.DecimalField(decimal_places=3, max_digits=3)
  61. tempo = models.DecimalField(decimal_places=3, max_digits=6)
  62. valence = models.DecimalField(decimal_places=3, max_digits=3)
  63. def __str__(self):
  64. return super(AudioFeatures, self).__str__()
  65. # }}} AudioFeatures #
  66. # History {{{ #
  67. class History(models.Model):
  68. class Meta:
  69. verbose_name = "History"
  70. verbose_name_plural = "History"
  71. unique_together = (("user", "timestamp"),)
  72. id = models.AutoField(primary_key=True)
  73. user = models.ForeignKey(User, on_delete=models.CASCADE)
  74. timestamp = models.DateTimeField()
  75. track = models.ForeignKey(Track, on_delete=models.CASCADE)
  76. def __str__(self):
  77. return " - ".join((str(self.user), str(self.timestamp), str(self.track)))
  78. def get_artists(self):
  79. artist_names = [artist.name for artist in self.track.artists.all()]
  80. return ', '.join(artist_names)
  81. def get_iso_timestamp(self):
  82. return self.timestamp.isoformat()
  83. # }}} #