views.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from django.db.models import Count
  2. from django.views import generic
  3. from music.models import Album, Artist, Track
  4. from scrobbles.models import ChartRecord
  5. from scrobbles.stats import get_scrobble_count_qs
  6. from scrobbles.views import ScrobbleableListView, ScrobbleableDetailView
  7. class TrackListView(ScrobbleableListView):
  8. model = Track
  9. class TrackDetailView(ScrobbleableDetailView):
  10. model = Track
  11. def get_context_data(self, **kwargs):
  12. context_data = super().get_context_data(**kwargs)
  13. context_data["charts"] = ChartRecord.objects.filter(
  14. track=self.object, rank__in=[1, 2, 3]
  15. )
  16. return context_data
  17. class ArtistListView(generic.ListView):
  18. model = Artist
  19. paginate_by = 100
  20. def get_queryset(self):
  21. return (
  22. super()
  23. .get_queryset()
  24. .annotate(scrobble_count=Count("track__scrobble"))
  25. .order_by("-scrobble_count")
  26. )
  27. def get_context_data(self, *, object_list=None, **kwargs):
  28. context_data = super().get_context_data(
  29. object_list=object_list, **kwargs
  30. )
  31. context_data["view"] = self.request.GET.get("view")
  32. return context_data
  33. class ArtistDetailView(generic.DetailView):
  34. model = Artist
  35. slug_field = "uuid"
  36. def get_context_data(self, **kwargs):
  37. context_data = super().get_context_data(**kwargs)
  38. artist = context_data["object"]
  39. rank = 1
  40. tracks_ranked = []
  41. scrobbles = artist.tracks.first().scrobble_count
  42. for track in artist.tracks:
  43. if scrobbles > track.scrobble_count:
  44. rank += 1
  45. tracks_ranked.append((rank, track))
  46. scrobbles = track.scrobble_count
  47. context_data["tracks_ranked"] = tracks_ranked
  48. context_data["charts"] = ChartRecord.objects.filter(
  49. artist=self.object, rank__in=[1, 2, 3]
  50. )
  51. return context_data
  52. class AlbumListView(generic.ListView):
  53. model = Album
  54. def get_queryset(self):
  55. return (
  56. super()
  57. .get_queryset()
  58. .annotate(scrobble_count=Count("track__scrobble"))
  59. .order_by("-scrobble_count")
  60. )
  61. class AlbumDetailView(generic.DetailView):
  62. model = Album
  63. slug_field = "uuid"
  64. def get_context_data(self, **kwargs):
  65. context_data = super().get_context_data(**kwargs)
  66. # context_data['charts'] = ChartRecord.objects.filter(
  67. # track__album=self.object, rank__in=[1, 2, 3]
  68. # )
  69. return context_data