Changeset 59b2e9c
- Timestamp:
- 05/01/25 21:17:02 (2 weeks ago)
- Branches:
- master
- Children:
- ac66823
- Parents:
- 4abe330
- Files:
-
- 4 added
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
MuiscOrganizationSystem/settings.py
r4abe330 r59b2e9c 16 16 BASE_DIR = Path(__file__).resolve().parent.parent 17 17 18 19 18 # Quick-start development settings - unsuitable for production 20 19 # See https://docs.djangoproject.com/en/5.1/howto/deployment/checklist/ … … 27 26 28 27 ALLOWED_HOSTS = [] 29 30 28 31 29 # Application definition … … 49 47 "django.contrib.messages.middleware.MessageMiddleware", 50 48 "django.middleware.clickjacking.XFrameOptionsMiddleware", 49 'music.middleware.Redirect404ToHomeMiddleware', 51 50 ] 52 51 … … 72 71 WSGI_APPLICATION = "MuiscOrganizationSystem.wsgi.application" 73 72 74 75 73 # Database 76 74 # https://docs.djangoproject.com/en/5.1/ref/settings/#databases … … 94 92 } 95 93 96 97 94 # Password validation 98 95 # https://docs.djangoproject.com/en/5.1/ref/settings/#auth-password-validators … … 113 110 ] 114 111 115 116 112 # Internationalization 117 113 # https://docs.djangoproject.com/en/5.1/topics/i18n/ … … 125 121 USE_TZ = True 126 122 127 128 123 # Static files (CSS, JavaScript, Images) 129 124 # https://docs.djangoproject.com/en/5.1/howto/static-files/ -
music/urls.py
r4abe330 r59b2e9c 1 1 from django.urls import path 2 from django.views.generic import RedirectView 3 2 4 from . import views 3 5 4 6 urlpatterns = [ 7 path('', views.home_page, name='homepage'), 5 8 path('album/', views.album_list, name='album_list'), 6 9 path('artist/', views.artist_list, name='artist_list'), … … 10 13 path('track/', views.track_list, name='track_list'), 11 14 path('customer/genres-per-customer', views.genres_per_customer, name='genres_per_customer'), 12 path('customer/invoices-per-customer', views.invoice_per_customer_after_date, name='invoice_per_customer_after_date'), 13 path('customer/most-popular-genre-per-customer', views.most_listened_genre_per_customer, name='most_listened_genre_per_customer'), 15 path('customer/invoices-per-customer', views.invoice_per_customer_after_date, 16 name='invoice_per_customer_after_date'), 17 path('customer/artist-per-genre', views.most_popular_artist_per_customer_per_genre, 18 name='most_popular_artist_per_customer_per_genre'), 19 path('customer/most-popular-genre-per-customer', views.most_listened_genre_per_customer, 20 name='most_listened_genre_per_customer'), 14 21 path('media-type/percentage', views.media_type_percentage, name='media_type_percentage'), 15 22 path('track/per-genre', views.tracks_count_per_genre, name='track_count_per_genre'), -
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 }) -
templates/avg_price_per_artist.html
r4abe330 r59b2e9c 29 29 <li class="list-group-item d-flex justify-content-between align-items-center"> 30 30 <div class="col-6"> 31 <strong>{{ row.name }}</strong>31 {{ row.name }} 32 32 </div> 33 33 <div class="col-6 text-end"> -
templates/avg_track_duration.html
r4abe330 r59b2e9c 19 19 <a href="#" class="list-group-item list-group-item-action d-flex justify-content-between align-items-center"> 20 20 <div> 21 <strong>{{ row.artist }}</strong>21 {{ row.artist }} 22 22 </div> 23 23 <span class="badge bg-primary rounded-pill">{{ row.avg }}</span> -
templates/genres_per_customer.html
r4abe330 r59b2e9c 32 32 </div> 33 33 </form> 34 34 {% if customer %} 35 <div class="row display-5"> 36 <div class="col-3 bg-light-subtle m-1 pb-3 border border-secondary-subtle rounded-3"> 37 {{ customer }} 38 </div> 39 </div> 40 {% endif %} 35 41 {% if not data %} 36 42 <div class="alert alert-warning" role="alert"> … … 38 44 </div> 39 45 {% else %} 40 <div class="table-responsive"> 41 <table class="table table-striped"> 42 <thead> 43 <tr> 44 <th scope="col">First Name</th> 45 <th scope="col">Last Name</th> 46 <th scope="col">Genre</th> 47 <th scope="col">Number of Tracks</th> 48 </tr> 49 </thead> 50 <tbody> 46 <div class="container"> 47 <div class="row col-6 justify-content-between mb-3"> 48 <div class="col-6 font-weight-bold"> 49 <strong>Genre</strong> 50 </div> 51 <div class="col-6 font-weight-bold px-4"> 52 <strong>Number of tracks</strong> 53 </div> 54 </div> 55 <ul class="row list-group col-6"> 51 56 {% for row in data %} 52 <tr> 53 <td>{{ row.first_name }}</td> 54 <td>{{ row.last_name }}</td> 55 <td>{{ row.genre }}</td> 56 <td>{{ row.track_count }}</td> 57 </tr> 57 <li class="list-group-item d-flex justify-content-between align-items-center"> 58 <div class="col-6"> 59 <span>{{ row.genre }}</span> 60 </div> 61 <div class="col-6"> 62 <span>Number of Tracks: {{ row.track_count }}</span> 63 </div> 64 </li> 65 {% empty %} 66 <li class="list-group-item">No data available for this customer.</li> 58 67 {% endfor %} 59 </tbody> 60 </table> 68 </ul> 61 69 </div> 62 70 {% endif %} -
templates/invoices_per_customer_after_date.html
r4abe330 r59b2e9c 27 27 </select> 28 28 </div> 29 30 29 <div class="col-md-4"> 31 30 <label for="invoice_date" class="form-label">Select Date:</label> … … 38 37 </div> 39 38 </form> 39 40 {% if customer %} 41 <div class="row display-5"> 42 <div class="col-3 bg-light-subtle m-1 pb-3 border border-secondary-subtle rounded-3"> 43 {{ customer }} 44 </div> 45 </div> 46 {% endif %} 40 47 41 48 {% if not selected_customer_id %} -
templates/most_listened_genre_per_customer.html
r4abe330 r59b2e9c 10 10 </head> 11 11 <body class="d-flex bg-light"> 12 13 14 12 {% include 'sidebar.html' %} 13 <div class="container mt-5"> 14 <h1 class="text-center mb-4">Most Listened Genre per Customer</h1> 15 15 16 <div class="list-group"> 17 <h4 class="mb-3">First Name   Last Name   -   Genre</h4> 18 {% for row in data %} 19 <a href="#" class="list-group-item list-group-item-action"> 20 {{ row.first_name }}  -  {{ row.last_name }}  -  {{ row.most_listened_genre }} 21 </a> 22 {% empty %} 23 <div class="alert alert-warning" role="alert"> 24 No data found. 16 <div class="list-group"> 17 <div class="row"> 18 <div class="col-4 font-weight-bold"><strong>First Name</strong></div> 19 <div class="col-4 font-weight-bold"><strong>Last Name</strong></div> 20 <div class="col-4 font-weight-bold"><strong>Genre</strong></div> 21 </div> 22 {% for row in data %} 23 <a href="#" class="list-group-item list-group-item-action"> 24 <div class="row"> 25 <div class="col-4 font-weight-bold">{{ row.first_name }}</div> 26 <div class="col-4 font-weight-bold">{{ row.last_name }}</div> 27 <div class="col-4 font-weight-bold">{{ row.most_listened_genre }}</div> 25 28 </div> 26 {% endfor %} 27 </div> 29 </a> 30 {% empty %} 31 <div class="alert alert-warning" role="alert"> 32 No data found. 33 </div> 34 {% endfor %} 28 35 </div> 36 </div> 29 37 </body> 30 38 </html> -
templates/sidebar.html
r4abe330 r59b2e9c 1 <div class=" d-flex flex-column flex-shrink-0 p-3 bg-light" style="width: 250px;">2 <h4 class="mb-4"> Navigation</h4>1 <div class="h-100 d-flex flex-column flex-shrink-0 p-3 bg-light-subtle border border-2 border-secondary-subtle rounded-end-5" style="width: 250px;"> 2 <h4 class="mb-4">Menu</h4> 3 3 <ul class="nav nav-pills flex-column mb-auto"> 4 4 <li class="nav-item"> … … 62 62 </a> 63 63 </li> 64 <li class="nav-item"> 65 <a href="{% url 'most_popular_artist_per_customer_per_genre' %}" class="nav-link {% if request.path == '/customer/artist-per-genre' %}active{% endif %}"> 66 Most Popular Artists Per Customer 67 </a> 68 </li> 64 69 </ul> 65 70 </div>
Note:
See TracChangeset
for help on using the changeset viewer.