Changeset 59b2e9c for music/views.py
- Timestamp:
- 05/01/25 21:17:02 (2 weeks ago)
- Branches:
- master
- Children:
- ac66823
- Parents:
- 4abe330
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
music/views.py
r4abe330 r59b2e9c 3 3 import django 4 4 from django.db import connection 5 from django.shortcuts import redirect 5 6 6 7 os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'MuiscOrganizationSystem.settings') … … 11 12 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') 13 20 14 21 def album_list(request): … … 27 34 heading = request.GET.get('model', 'All Artists') 28 35 data = Artist.objects.values_list('name', flat=True) 29 print(data) 36 30 37 return render(request, 'list.html', {'data': data, 'heading': heading}) 31 38 … … 38 45 39 46 data = [{'genre': row[0], 'count': row[1]} for row in rows] 40 print(data)41 47 42 48 return render(request, 'track_count_per_genre.html', {'data': data}) … … 49 55 50 56 data = [{'artist': row[0], 'avg': row[1]} for row in rows] 51 print(data)52 57 53 58 return render(request, 'avg_track_duration.html', {'data': data}) … … 108 113 selected_customer_id = request.GET.get('customer_id') 109 114 data = [] 115 customer = None 110 116 111 117 if selected_customer_id: 118 customer = Customer.objects.filter(customer_id=selected_customer_id).first() 112 119 query = """ 113 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 … … 130 137 'customers': customers, 131 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, 132 185 'selected_customer_id': selected_customer_id, 133 186 }) … … 140 193 data = [] 141 194 sum = 0 195 customer = None 142 196 143 197 if selected_customer_id: 198 customer = Customer.objects.filter(customer_id=selected_customer_id).first() 144 199 if selected_date: 145 200 query = """ … … 180 235 'total_sum': sum, 181 236 'data': data, 237 'customer': customer, 182 238 'selected_customer_id': selected_customer_id, 183 239 })
Note:
See TracChangeset
for help on using the changeset viewer.