1 | from django.shortcuts import render
|
---|
2 | import os
|
---|
3 | import django
|
---|
4 | from django.db import connection
|
---|
5 | from django.shortcuts import redirect
|
---|
6 |
|
---|
7 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'MuiscOrganizationSystem.settings')
|
---|
8 | django.setup()
|
---|
9 |
|
---|
10 | from music.models import *
|
---|
11 |
|
---|
12 |
|
---|
13 | # Create your views here.
|
---|
14 |
|
---|
15 | def home_page(request):
|
---|
16 | return render(request, 'home.html')
|
---|
17 |
|
---|
18 | def redirect_to_home(request, exception):
|
---|
19 | return redirect('home_page')
|
---|
20 |
|
---|
21 | def album_list(request):
|
---|
22 | heading = request.GET.get('model', 'All Albums')
|
---|
23 | data = Album.objects.values_list('title', flat=True)
|
---|
24 | return render(request, 'list.html', {'data': data, 'heading': heading})
|
---|
25 |
|
---|
26 |
|
---|
27 | def track_list(request):
|
---|
28 | heading = request.GET.get('model', 'All Tracks')
|
---|
29 | data = Track.objects.values_list('name', flat=True)
|
---|
30 | return render(request, 'list.html', {'data': data, 'heading': heading})
|
---|
31 |
|
---|
32 |
|
---|
33 | def artist_list(request):
|
---|
34 | heading = request.GET.get('model', 'All Artists')
|
---|
35 | data = Artist.objects.values_list('name', flat=True)
|
---|
36 |
|
---|
37 | return render(request, 'list.html', {'data': data, 'heading': heading})
|
---|
38 |
|
---|
39 |
|
---|
40 | # VIEWS
|
---|
41 | def tracks_count_per_genre(request):
|
---|
42 | with connection.cursor() as cursor:
|
---|
43 | cursor.execute("SELECT * FROM track_count_per_genre;")
|
---|
44 | rows = cursor.fetchall()
|
---|
45 |
|
---|
46 | data = [{'genre': row[0], 'count': row[1]} for row in rows]
|
---|
47 |
|
---|
48 | return render(request, 'track_count_per_genre.html', {'data': data})
|
---|
49 |
|
---|
50 |
|
---|
51 | def avg_track_duration(request):
|
---|
52 | with connection.cursor() as cursor:
|
---|
53 | cursor.execute("SELECT * FROM avg_track_duration_per_artist;")
|
---|
54 | rows = cursor.fetchall()
|
---|
55 |
|
---|
56 | data = [{'artist': row[0], 'avg': row[1]} for row in rows]
|
---|
57 |
|
---|
58 | return render(request, 'avg_track_duration.html', {'data': data})
|
---|
59 |
|
---|
60 |
|
---|
61 | def rank_list_most_active_customers(request):
|
---|
62 | with connection.cursor() as cursor:
|
---|
63 | cursor.execute("SELECT * FROM rank_list_most_active_customers_view;")
|
---|
64 | rows = cursor.fetchall()
|
---|
65 |
|
---|
66 | data = [{'name': row[0], 'total_orders': row[1], 'total_money_spent': row[2]} for row in rows]
|
---|
67 |
|
---|
68 | return render(request, 'rank_list_most_active_customers.html', {'data': data})
|
---|
69 |
|
---|
70 |
|
---|
71 | def avg_price_per_artist(request):
|
---|
72 | with connection.cursor() as cursor:
|
---|
73 | cursor.execute("SELECT * FROM avg_price_per_artist;")
|
---|
74 | rows = cursor.fetchall()
|
---|
75 |
|
---|
76 | data = [{'name': row[0], 'avg_price_per_track': row[1]} for row in rows]
|
---|
77 |
|
---|
78 | return render(request, 'avg_price_per_artist.html', {'data': data})
|
---|
79 |
|
---|
80 |
|
---|
81 | def rank_list_artists(request):
|
---|
82 | with connection.cursor() as cursor:
|
---|
83 | cursor.execute("SELECT * FROM rank_list_artists;")
|
---|
84 | rows = cursor.fetchall()
|
---|
85 |
|
---|
86 | data = [{'name': row[0], 'num_invoices': row[1], 'money_earned': row[2]} for row in rows]
|
---|
87 |
|
---|
88 | return render(request, 'rank_list_artists.html', {'data': data})
|
---|
89 |
|
---|
90 |
|
---|
91 | def media_type_percentage(request):
|
---|
92 | with connection.cursor() as cursor:
|
---|
93 | cursor.execute("SELECT * FROM media_type_percentage;")
|
---|
94 | rows = cursor.fetchall()
|
---|
95 |
|
---|
96 | data = [{'name': row[0], 'num_of_tracks': row[1], 'percentage': row[2]} for row in rows]
|
---|
97 |
|
---|
98 | return render(request, 'media_type_percentage.html', {'data': data})
|
---|
99 |
|
---|
100 |
|
---|
101 | def most_listened_genre_per_customer(request):
|
---|
102 | with connection.cursor() as cursor:
|
---|
103 | cursor.execute("SELECT * FROM most_listened_genre_per_customer;")
|
---|
104 | rows = cursor.fetchall()
|
---|
105 |
|
---|
106 | data = [{'first_name': row[0], 'last_name': row[1], 'most_listened_genre': row[2]} for row in rows]
|
---|
107 |
|
---|
108 | return render(request, 'most_listened_genre_per_customer.html', {'data': data})
|
---|
109 |
|
---|
110 |
|
---|
111 | def genres_per_customer(request):
|
---|
112 | customers = Customer.objects.all()
|
---|
113 | selected_customer_id = request.GET.get('customer_id')
|
---|
114 | data = []
|
---|
115 | customer = None
|
---|
116 |
|
---|
117 | if selected_customer_id:
|
---|
118 | customer = Customer.objects.filter(customer_id=selected_customer_id).first()
|
---|
119 | query = """
|
---|
120 | SELECT c.first_name as first_name, c.last_name as last_name, g.name as genre, COUNT(tr.track_id) as track_count
|
---|
121 | FROM customer c
|
---|
122 | LEFT JOIN invoice i ON c.customer_id = i.customer_id
|
---|
123 | LEFT JOIN invoice_line il ON i.invoice_id = il.invoice_id
|
---|
124 | LEFT JOIN track tr ON il.track_id = tr.track_id
|
---|
125 | LEFT JOIN genre g ON tr.genre_id = g.genre_id
|
---|
126 | WHERE c.customer_id = %s
|
---|
127 | GROUP BY c.customer_id, c.first_name, c.last_name, g.genre_id, g.name
|
---|
128 | ORDER BY c.first_name
|
---|
129 | """
|
---|
130 |
|
---|
131 | with connection.cursor() as cursor:
|
---|
132 | cursor.execute(query, [selected_customer_id])
|
---|
133 | rows = cursor.fetchall()
|
---|
134 | data = [{'first_name': row[0], 'last_name': row[1], 'genre': row[2], 'track_count': row[3]} for row in rows]
|
---|
135 |
|
---|
136 | return render(request, 'genres_per_customer.html', {
|
---|
137 | 'customers': customers,
|
---|
138 | 'data': data,
|
---|
139 | 'customer': customer,
|
---|
140 | 'selected_customer_id': selected_customer_id,
|
---|
141 | })
|
---|
142 |
|
---|
143 | def most_popular_artist_per_customer_per_genre(request):
|
---|
144 | customers = Customer.objects.all()
|
---|
145 | selected_customer_id = request.GET.get('customer_id')
|
---|
146 | data = []
|
---|
147 | customer = []
|
---|
148 | if selected_customer_id:
|
---|
149 | query = """WITH PlayCounts AS (
|
---|
150 | SELECT
|
---|
151 | g.genre_id,
|
---|
152 | g.name AS genre_name,
|
---|
153 | ar.name AS artist_name,
|
---|
154 | COUNT(*) AS play_count
|
---|
155 | FROM customer c
|
---|
156 | JOIN invoice i ON c.customer_id = i.customer_id
|
---|
157 | JOIN invoice_line il ON i.invoice_id = il.invoice_id
|
---|
158 | JOIN track tr ON il.track_id = tr.track_id
|
---|
159 | JOIN genre g ON tr.genre_id = g.genre_id
|
---|
160 | JOIN album a ON tr.album_id = a.album_id
|
---|
161 | JOIN artist ar ON a.artist_id = ar.artist_id
|
---|
162 | WHERE c.customer_id = %s
|
---|
163 | GROUP BY g.genre_id, g.name, ar.name
|
---|
164 | ),
|
---|
165 | MaxPlayCounts AS (
|
---|
166 | SELECT genre_id, MAX(play_count) AS max_count
|
---|
167 | FROM PlayCounts
|
---|
168 | GROUP BY genre_id
|
---|
169 | )
|
---|
170 | SELECT pc.genre_name, pc.artist_name, pc.play_count
|
---|
171 | FROM PlayCounts pc
|
---|
172 | JOIN MaxPlayCounts mpc ON pc.genre_id = mpc.genre_id AND pc.play_count = mpc.max_count;
|
---|
173 | """
|
---|
174 | customer = Customer.objects.filter(customer_id=selected_customer_id).first()
|
---|
175 |
|
---|
176 | with connection.cursor() as cursor:
|
---|
177 | cursor.execute(query, [selected_customer_id])
|
---|
178 | rows = cursor.fetchall()
|
---|
179 | data = [{'genre': row[0], 'arist': row[1]} for row in rows]
|
---|
180 |
|
---|
181 | return render(request, 'most_popular_artist_per_customer_per_genre.html', {
|
---|
182 | 'customers': customers,
|
---|
183 | 'customer': customer,
|
---|
184 | 'data': data,
|
---|
185 | 'selected_customer_id': selected_customer_id,
|
---|
186 | })
|
---|
187 |
|
---|
188 |
|
---|
189 | def invoice_per_customer_after_date(request):
|
---|
190 | customers = Customer.objects.all()
|
---|
191 | selected_customer_id = request.GET.get('customer_id')
|
---|
192 | selected_date = request.GET.get('invoice_date', '2000-01-01')
|
---|
193 | data = []
|
---|
194 | sum = 0
|
---|
195 | customer = None
|
---|
196 |
|
---|
197 | if selected_customer_id:
|
---|
198 | customer = Customer.objects.filter(customer_id=selected_customer_id).first()
|
---|
199 | if selected_date:
|
---|
200 | query = """
|
---|
201 | SELECT c.first_name, c.last_name, i.invoice_date::date, i.total
|
---|
202 | FROM customer c
|
---|
203 | JOIN invoice i ON c.customer_id = i.customer_id
|
---|
204 | WHERE c.customer_id = %s AND i.invoice_date > %s
|
---|
205 | ORDER BY i.invoice_date
|
---|
206 | """
|
---|
207 | with connection.cursor() as cursor:
|
---|
208 | cursor.execute(query, [selected_customer_id, selected_date])
|
---|
209 | rows = cursor.fetchall()
|
---|
210 | for r in rows:
|
---|
211 | sum += r[3]
|
---|
212 |
|
---|
213 | data = [{'first_name': row[0], 'last_name': row[1], 'invoice_date': row[2], 'total': row[3]} for row in
|
---|
214 | rows]
|
---|
215 | else:
|
---|
216 | query = """
|
---|
217 | SELECT c.first_name, c.last_name, i.invoice_date::date, i.total
|
---|
218 | FROM customer c
|
---|
219 | JOIN invoice i ON c.customer_id = i.customer_id
|
---|
220 | WHERE c.customer_id = %s
|
---|
221 | ORDER BY i.invoice_date
|
---|
222 | """
|
---|
223 | with connection.cursor() as cursor:
|
---|
224 | cursor.execute(query, [selected_customer_id])
|
---|
225 | rows = cursor.fetchall()
|
---|
226 | for r in rows:
|
---|
227 | sum += r[3]
|
---|
228 |
|
---|
229 | data = [{'first_name': row[0], 'last_name': row[1], 'invoice_date': row[2], 'total': row[3]}
|
---|
230 | for row in
|
---|
231 | rows]
|
---|
232 |
|
---|
233 | return render(request, 'invoices_per_customer_after_date.html', {
|
---|
234 | 'customers': customers,
|
---|
235 | 'total_sum': sum,
|
---|
236 | 'data': data,
|
---|
237 | 'customer': customer,
|
---|
238 | 'selected_customer_id': selected_customer_id,
|
---|
239 | })
|
---|