Skip to content

Commit

Permalink
Merge pull request #767 from sapuri/optimize-level
Browse files Browse the repository at this point in the history
main: optimize level view
  • Loading branch information
sapuri authored Mar 10, 2024
2 parents 9f27f27 + d94b3b7 commit 620e873
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 36 deletions.
16 changes: 2 additions & 14 deletions main/templates/main/difflist.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,8 @@ <h3 id="lv{{ sran_level }}" class="level-folder {{ folder_status }}">Lv{{ sran_l
-
{% 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>
<td class="bad_count">{{ data.bad_count|default:'-' }}</td>
<td class="updated_at">{{ data.updated_at|default:'-'|date:'Y/m/d H:i' }}</td>
</tr>
{% endfor %}
</tbody>
Expand Down
35 changes: 16 additions & 19 deletions main/templates/main/level/level.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<meta property="og:title" content="公式難易度表(Lv{{ level }}) - スパランドットコム" />
<meta property="og:type" content="article" />
<meta name="description" content="スパランドットコムの公式難易度表ページです。(Lv{{ level }})">
<script src="{% static 'js/music_list.js' %}"></script>
{% endblock %}

{% block content %}
Expand Down Expand Up @@ -38,24 +37,22 @@ <h3 id="lv{{ level }}" class="level-folder {{ folder_status }}">Lv{{ level }}</h
<th>更新日時</th>
</thead>
<tbody>
{% for music in music_list %}
<tr id="music-{{ music.id }}">
<script>$(function() { getClearStatus({{ music.id }}); });</script>
<td class="sran_level">{{ music.sran_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="sran_level">{{ data.sran_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">{{ data.bad_count|default:'-' }}</td>
<td class="updated_at">{{ data.updated_at|default:'-'|date:'Y/m/d H:i' }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
Expand Down
51 changes: 48 additions & 3 deletions main/views/level.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 Level, Medal, Music
from main.models import Bad_Count, Extra_Option, Level, Medal, Music
from main.services import get_folder_status


Expand All @@ -14,14 +15,58 @@ def level(request: HttpRequest, level: int) -> HttpResponse:

level_id = Level.MAX - level + 1

music_query = Music.objects.filter(level=level_id).order_by('-sran_level', 'title')
music_query = Music.objects.filter(level=level_id).order_by('-sran_level', 'title').select_related('difficulty',
'sran_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,
'sran_level': music.sran_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

0 comments on commit 620e873

Please sign in to comment.