1 | from django.shortcuts import render
|
---|
2 | import os
|
---|
3 | import django
|
---|
4 | from django.db import connection
|
---|
5 |
|
---|
6 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'MuiscOrganizationSystem.settings')
|
---|
7 | django.setup()
|
---|
8 |
|
---|
9 | from music.models import *
|
---|
10 |
|
---|
11 |
|
---|
12 | # Create your views here.
|
---|
13 |
|
---|
14 | def album_list(request):
|
---|
15 | heading = request.GET.get('model', 'All Albums')
|
---|
16 | data = Album.objects.values_list('title', flat=True)
|
---|
17 | return render(request, 'list.html', {'data': data, 'heading': heading})
|
---|
18 |
|
---|
19 |
|
---|
20 | def track_list(request):
|
---|
21 | heading = request.GET.get('model', 'All Tracks')
|
---|
22 | data = Track.objects.values_list('name', flat=True)
|
---|
23 | return render(request, 'list.html', {'data': data, 'heading': heading})
|
---|
24 |
|
---|
25 |
|
---|
26 | def artist_list(request):
|
---|
27 | heading = request.GET.get('model', 'All Artists')
|
---|
28 | data = Artist.objects.values_list('name', flat=True)
|
---|
29 | print(data)
|
---|
30 | return render(request, 'list.html', {'data': data, 'heading': heading})
|
---|
31 |
|
---|
32 |
|
---|
33 | # VIEWS
|
---|
34 | def tracks_count_per_genre(request):
|
---|
35 | with connection.cursor() as cursor:
|
---|
36 | cursor.execute("SELECT * FROM track_count_per_genre;")
|
---|
37 | rows = cursor.fetchall()
|
---|
38 |
|
---|
39 | data = [{'genre': row[0], 'count': row[1]} for row in rows]
|
---|
40 | print(data)
|
---|
41 |
|
---|
42 | return render(request, 'track_count_per_genre.html', {'data': data})
|
---|
43 |
|
---|
44 |
|
---|
45 | def avg_track_duration(request):
|
---|
46 | with connection.cursor() as cursor:
|
---|
47 | cursor.execute("SELECT * FROM avg_track_duration_per_artist;")
|
---|
48 | rows = cursor.fetchall()
|
---|
49 |
|
---|
50 | data = [{'artist': row[0], 'avg': row[1]} for row in rows]
|
---|
51 | print(data)
|
---|
52 |
|
---|
53 | return render(request, 'avg_track_duration.html', {'data': data})
|
---|
54 |
|
---|
55 |
|
---|
56 | def rank_list_most_active_customers(request):
|
---|
57 | with connection.cursor() as cursor:
|
---|
58 | cursor.execute("SELECT * FROM rank_list_most_active_customers_view;")
|
---|
59 | rows = cursor.fetchall()
|
---|
60 |
|
---|
61 | data = [{'name': row[0], 'total_orders': row[1], 'total_money_spent': row[2]} for row in rows]
|
---|
62 |
|
---|
63 | return render(request, 'rank_list_most_active_customers.html', {'data': data})
|
---|