Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

main: optimize difflist #766

Merged
merged 1 commit into from
Mar 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 28 additions & 19 deletions main/templates/main/difflist.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<meta property="og:title" content="S乱難易度表(Lv{{ sran_level }}) - スパランドットコム" />
<meta property="og:type" content="article" />
<meta name="description" content="スパランドットコムのS乱難易度表ページです。(Lv{{ sran_level }})">
<script src="{% static 'js/music_list.js' %}"></script>
{% endblock %}

{% block content %}
Expand Down Expand Up @@ -44,24 +43,34 @@ <h3 id="lv{{ sran_level }}" class="level-folder {{ folder_status }}">Lv{{ sran_l
<th>更新日時</th>
</thead>
<tbody>
{% for music in music_list %}
<tr id="music-{{ music.id }}">
<script>$(function() { getClearStatus({{ music.id }}); });</script>
<td class="level">{{ music.level }}</td>
<td class="title"><a href="{% url 'main:edit' music.id %}">{{ music.title }} ({{ music.difficulty }})</a></td>
<td class="bpm">{{ music.bpm|default:'-' }}</td>
<td class="medal">
<img>
<script>$(function() { getMedal({{ music.id }}); });</script>
</td>
<td class="bad_count">
<script>$(function() { getBadCount({{ music.id }}); });</script>
</td>
<td class="updated_at">
<script>$(function() { getLatestUpdatedAt({{ music.id }}); });</script>
</td>
</tr>
{% endfor %}
{% for data in music_list %}
<tr id="music-{{ data.id }}" class="{{ data.clear_status }}">
<td class="level">{{ data.level }}</td>
<td class="title"><a href="{% url 'main:edit' data.id %}">{{ data.title }} ({{ data.difficulty }})</a></td>
<td class="bpm">{{ data.bpm|default:'-' }}</td>
<td class="medal">
{% if data.medal and data.medal != 12 %}
<img src="{% static '' %}img/medal/{{ data.medal }}.png" width="18" height="18">
{% else %}
-
{% endif %}
</td>
<td class="bad_count">
{% if data.bad_count %}
{{ data.bad_count }}
{% else %}
-
{% endif %}
</td>
<td class="updated_at">
{% if data.updated_at %}
{{ data.updated_at|date:'Y/m/d H:i' }}
{% else %}
-
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
Expand Down
51 changes: 48 additions & 3 deletions main/views/difflist.py
Original file line number Diff line number Diff line change
@@ -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 render

from main.models import Medal, Music, Sran_Level
from main.models import Bad_Count, Extra_Option, Medal, Music, Sran_Level
from main.services import get_folder_status


Expand All @@ -14,14 +15,58 @@ def difflist(request: HttpRequest, sran_level: int) -> HttpResponse:
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()

folder_status = Medal.ClearStatus.NO_PLAY
if request.user.is_authenticated:
folder_status = get_folder_status(request.user, music_query)

paginator = Paginator(music_query, 25)
music_query = music_query.prefetch_related(
Prefetch('medal_set', queryset=Medal.objects.filter(user=request.user), to_attr='user_medal'),
Prefetch('bad_count_set', queryset=Bad_Count.objects.filter(user=request.user), to_attr='user_bad_count'),
Prefetch('extra_option_set', queryset=Extra_Option.objects.filter(user=request.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 request.user.is_authenticated:
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)

Expand Down