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