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})
|
---|
64 |
|
---|
65 |
|
---|
66 | def avg_price_per_artist(request):
|
---|
67 | with connection.cursor() as cursor:
|
---|
68 | cursor.execute("SELECT * FROM avg_price_per_artist;")
|
---|
69 | rows = cursor.fetchall()
|
---|
70 |
|
---|
71 | data = [{'name': row[0], 'avg_price_per_track': row[1]} for row in rows]
|
---|
72 |
|
---|
73 | return render(request, 'avg_price_per_artist.html', {'data': data})
|
---|
74 |
|
---|
75 |
|
---|
76 | def rank_list_artists(request):
|
---|
77 | with connection.cursor() as cursor:
|
---|
78 | cursor.execute("SELECT * FROM rank_list_artists;")
|
---|
79 | rows = cursor.fetchall()
|
---|
80 |
|
---|
81 | data = [{'name': row[0], 'num_invoices': row[1]} for row in rows]
|
---|
82 |
|
---|
83 | return render(request, 'rank_list_artists.html', {'data': data})
|
---|
84 |
|
---|
85 |
|
---|
86 | def media_type_percentage(request):
|
---|
87 | with connection.cursor() as cursor:
|
---|
88 | cursor.execute("SELECT * FROM media_type_percentage;")
|
---|
89 | rows = cursor.fetchall()
|
---|
90 |
|
---|
91 | data = [{'name': row[0], 'num_of_tracks': row[1], 'percentage': row[2]} for row in rows]
|
---|
92 |
|
---|
93 | return render(request, 'media_type_percentage.html', {'data': data})
|
---|
94 |
|
---|
95 |
|
---|
96 | def most_listened_genre_per_customer(request):
|
---|
97 | with connection.cursor() as cursor:
|
---|
98 | cursor.execute("SELECT * FROM most_listened_genre_per_customer;")
|
---|
99 | rows = cursor.fetchall()
|
---|
100 |
|
---|
101 | data = [{'first_name': row[0], 'last_name': row[1], 'most_listened_genre': row[2]} for row in rows]
|
---|
102 |
|
---|
103 | return render(request, 'most_listened_genre_per_customer.html', {'data': data})
|
---|
104 |
|
---|
105 |
|
---|
106 | def genres_per_customer(request):
|
---|
107 | customers = Customer.objects.all()
|
---|
108 | selected_customer_id = request.GET.get('customer_id')
|
---|
109 | data = []
|
---|
110 |
|
---|
111 | if selected_customer_id:
|
---|
112 | query = """
|
---|
113 | SELECT c.first_name as first_name, c.last_name as last_name, g.name as genre, COUNT(tr.track_id) as track_count
|
---|
114 | FROM customer c
|
---|
115 | LEFT JOIN invoice i ON c.customer_id = i.customer_id
|
---|
116 | LEFT JOIN invoice_line il ON i.invoice_id = il.invoice_id
|
---|
117 | LEFT JOIN track tr ON il.track_id = tr.track_id
|
---|
118 | LEFT JOIN genre g ON tr.genre_id = g.genre_id
|
---|
119 | WHERE c.customer_id = %s
|
---|
120 | GROUP BY c.customer_id, c.first_name, c.last_name, g.genre_id, g.name
|
---|
121 | ORDER BY c.first_name
|
---|
122 | """
|
---|
123 |
|
---|
124 | with connection.cursor() as cursor:
|
---|
125 | cursor.execute(query, [selected_customer_id])
|
---|
126 | rows = cursor.fetchall()
|
---|
127 | data = [{'first_name': row[0], 'last_name': row[1], 'genre': row[2], 'track_count': row[3]} for row in rows]
|
---|
128 |
|
---|
129 | return render(request, 'genres_per_customer.html', {
|
---|
130 | 'customers': customers,
|
---|
131 | 'data': data,
|
---|
132 | 'selected_customer_id': selected_customer_id,
|
---|
133 | })
|
---|
134 |
|
---|
135 |
|
---|
136 | def invoice_per_customer_after_date(request):
|
---|
137 | customers = Customer.objects.all()
|
---|
138 | selected_customer_id = request.GET.get('customer_id')
|
---|
139 | selected_date = request.GET.get('invoice_date', '2000-01-01')
|
---|
140 | data = []
|
---|
141 | sum = 0
|
---|
142 |
|
---|
143 | if selected_customer_id:
|
---|
144 | if selected_date:
|
---|
145 | query = """
|
---|
146 | SELECT c.first_name, c.last_name, i.invoice_date::date, i.total
|
---|
147 | FROM customer c
|
---|
148 | JOIN invoice i ON c.customer_id = i.customer_id
|
---|
149 | WHERE c.customer_id = %s AND i.invoice_date > %s
|
---|
150 | ORDER BY i.invoice_date
|
---|
151 | """
|
---|
152 | with connection.cursor() as cursor:
|
---|
153 | cursor.execute(query, [selected_customer_id, selected_date])
|
---|
154 | rows = cursor.fetchall()
|
---|
155 | for r in rows:
|
---|
156 | sum += r[3]
|
---|
157 |
|
---|
158 | data = [{'first_name': row[0], 'last_name': row[1], 'invoice_date': row[2], 'total': row[3]} for row in
|
---|
159 | rows]
|
---|
160 | else:
|
---|
161 | query = """
|
---|
162 | SELECT c.first_name, c.last_name, i.invoice_date::date, i.total
|
---|
163 | FROM customer c
|
---|
164 | JOIN invoice i ON c.customer_id = i.customer_id
|
---|
165 | WHERE c.customer_id = %s
|
---|
166 | ORDER BY i.invoice_date
|
---|
167 | """
|
---|
168 | with connection.cursor() as cursor:
|
---|
169 | cursor.execute(query, [selected_customer_id])
|
---|
170 | rows = cursor.fetchall()
|
---|
171 | for r in rows:
|
---|
172 | sum += r[3]
|
---|
173 |
|
---|
174 | data = [{'first_name': row[0], 'last_name': row[1], 'invoice_date': row[2], 'total': row[3]}
|
---|
175 | for row in
|
---|
176 | rows]
|
---|
177 |
|
---|
178 | return render(request, 'invoices_per_customer_after_date.html', {
|
---|
179 | 'customers': customers,
|
---|
180 | 'total_sum': sum,
|
---|
181 | 'data': data,
|
---|
182 | 'selected_customer_id': selected_customer_id,
|
---|
183 | })
|
---|