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