From 372da2fd60060586714918758c62bf55ea62a09b Mon Sep 17 00:00:00 2001 From: sapuri Date: Mon, 11 Mar 2024 00:21:51 +0900 Subject: [PATCH] users: optimize cleardata view --- static/js/user_music_list.js | 103 --------------------------- users/templates/users/cleardata.html | 35 +++++---- users/views/cleardata.py | 50 ++++++++++++- 3 files changed, 63 insertions(+), 125 deletions(-) delete mode 100644 static/js/user_music_list.js diff --git a/static/js/user_music_list.js b/static/js/user_music_list.js deleted file mode 100644 index 336b5d4c..00000000 --- a/static/js/user_music_list.js +++ /dev/null @@ -1,103 +0,0 @@ -/* クリア状況を取得 */ -function getClearStatus(music_id, user_id) { - const selector = 'tr#music-'+music_id; - $.ajax({ - url: SERVER_URL+'api/get_clear_status/'+music_id+'/', - type: 'GET', - data: { - user_id: user_id - } - }) - .then( - function(response) { - $(selector).addClass(response.clear_status); - $(selector+' script').remove(); - }, - function(err) { - console.log(err); - } - ); -} - -/* BAD数を取得 */ -function getBadCount(music_id, user_id) { - const selector = 'tr#music-'+music_id+' .bad_count'; - $.ajax({ - url: SERVER_URL+'api/get_bad_count/'+music_id+'/', - type: 'GET', - data: { - user_id: user_id - } - }) - .then( - function(response) { - let bad_count = '-'; - if (response.bad_count != null) { - bad_count = response.bad_count; - } - // BAD数を描画 - $(selector).text(bad_count); - }, - function(err) { - console.log(err); - } - ); -} - -/* メダルを取得 */ -function getMedal(music_id, user_id) { - const selector = 'tr#music-'+music_id+' .medal'; - $.ajax({ - url: SERVER_URL+'api/get_medal/'+music_id+'/', - type: 'GET', - data: { - user_id: user_id - } - }) - .then( - function(response) { - if (response.medal && response.medal !== 12) { - // 未プレイ以外ならメダルを描画 - const medal = response.medal; - $(selector+' .loading').remove(); - $(selector+' img').attr('src', `${STATIC_URL}img/medal/${medal}.png`); - $(selector+' img').attr('width', '18'); - $(selector+' img').attr('height', '18'); - $(selector+' script').remove(); - } else { - $(selector).text('-'); - } - }, - function(err) { - console.log(err); - } - ); -} - -/* 最新の更新日時を取得 */ -function getLatestUpdatedAt(music_id, user_id) { - const selector = 'tr#music-'+music_id+' .updated_at'; - $.ajax({ - url: SERVER_URL+'api/get_latest_updated_at/'+music_id+'/', - type: 'GET', - data: { - user_id: user_id - } - }) - .then( - function(response) { - let updated_at = '-'; - if (response.is_active) { - // ゼロパディング - response.day = ("0" + response.day).slice(-2); - response.minute = ("0" + response.minute).slice(-2); - updated_at = response.year+'/'+response.month+'/'+response.day+' '+response.hour+':'+response.minute; - } - // 更新日時を描画 - $(selector).text(updated_at); - }, - function(err) { - console.log(err); - } - ); -} diff --git a/users/templates/users/cleardata.html b/users/templates/users/cleardata.html index f6e3407d..6ce4bb5c 100644 --- a/users/templates/users/cleardata.html +++ b/users/templates/users/cleardata.html @@ -8,7 +8,6 @@ - {% endblock %} {% block content %} @@ -29,24 +28,22 @@

Lv{{ sran_l 更新日時 - {% for music in music_list %} - - - {{ music.level }} - {{ music.title }} ({{ music.difficulty }}) - {{ music.bpm|default:'-' }} - - - - - - - - - - - - {% endfor %} + {% for data in music_list %} + + {{ data.level }} + {{ data.title }} ({{ data.difficulty }}) + {{ data.bpm|default:'-' }} + + {% if data.medal and data.medal != 12 %} + + {% else %} + - + {% endif %} + + {{ data.bad_count|default:'-' }} + {{ data.updated_at|default:'-'|date:'Y/m/d H:i' }} + + {% endfor %} diff --git a/users/views/cleardata.py b/users/views/cleardata.py index 45de23f5..231240ad 100644 --- a/users/views/cleardata.py +++ b/users/views/cleardata.py @@ -1,8 +1,9 @@ from django.core.paginator import Paginator +from django.db.models import Prefetch from django.http import Http404, HttpRequest, HttpResponse from django.shortcuts import get_object_or_404, render -from main.models import Music, Sran_Level +from main.models import Bad_Count, Extra_Option, Medal, Music, Sran_Level from main.services import get_folder_status from users.models import CustomUser @@ -16,10 +17,53 @@ def cleardata(request: HttpRequest, username: str, sran_level: int) -> HttpRespo if sran_level <= 0 or sran_level > Sran_Level.MAX: raise Http404 - music_query = Music.objects.filter(sran_level=sran_level).order_by('level', 'title') + music_query = Music.objects.filter(sran_level=sran_level).order_by('level', 'title').select_related('difficulty', + 'level') music_count = music_query.count() - paginator = Paginator(music_query, 25) + music_query = music_query.prefetch_related( + Prefetch('medal_set', queryset=Medal.objects.filter(user=selected_user), to_attr='user_medal'), + Prefetch('bad_count_set', queryset=Bad_Count.objects.filter(user=selected_user), to_attr='user_bad_count'), + Prefetch('extra_option_set', queryset=Extra_Option.objects.filter(user=selected_user, hard=True), + to_attr='user_extra_option') + ) + + music_data = [] + for music in music_query: + medal = None + bad_count = None + extra_option = None + clear_status = Medal.ClearStatus.NO_PLAY.value + updated_at = None + + if hasattr(music, 'user_medal') and music.user_medal: + medal = music.user_medal[0] + updated_at = medal.updated_at + + if hasattr(music, 'user_bad_count') and music.user_bad_count: + bad_count = music.user_bad_count[0] + if not updated_at or bad_count.updated_at > updated_at: + updated_at = bad_count.updated_at + + if hasattr(music, 'user_extra_option') and music.user_extra_option: + extra_option = music.user_extra_option[0] + + if medal: + clear_status = medal.get_clear_status(bad_count=bad_count, extra_option=extra_option).value + + music_data.append({ + 'id': music.id, + 'level': music.level.level, + 'title': music.title, + 'difficulty': music.difficulty.difficulty_short(), + 'bpm': music.bpm, + 'medal': medal.int() if medal else None, + 'bad_count': bad_count.int() if bad_count else None, + 'clear_status': clear_status, + 'updated_at': updated_at, + }) + + paginator = Paginator(music_data, 25) page = request.GET.get('page') music_list = paginator.get_page(page)