Skip to content

Commit

Permalink
Merge pull request #768 from sapuri/optimize-cleardata
Browse files Browse the repository at this point in the history
users: optimize cleardata view
  • Loading branch information
sapuri authored Mar 10, 2024
2 parents 620e873 + 372da2f commit 154f20e
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 125 deletions.
103 changes: 0 additions & 103 deletions static/js/user_music_list.js

This file was deleted.

35 changes: 16 additions & 19 deletions users/templates/users/cleardata.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
<meta property="og:title" content="{{ selected_user.player_name|default:selected_user.username }}のクリア状況(Lv{{ sran_level }}) - スパランドットコム" />
<meta property="og:type" content="article" />
<meta name="description" content="スパランドットコムのクリア状況確認ページです。(Lv{{ sran_level }})">
<script src="{% static 'js/user_music_list.js' %}"></script>
{% endblock %}

{% block content %}
Expand All @@ -29,24 +28,22 @@ <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 }}, {{ selected_user.id }}); });</script>
<td class="level">{{ music.level }}</td>
<td class="title"><a href="{% url 'main:ranking_detail' music.id %}">{{ music.title }} ({{ music.difficulty }})</a></td>
<td class="bpm">{{ music.bpm|default:'-' }}</td>
<td class="medal">
<img>
<script>$(function() { getMedal({{ music.id }}, {{ selected_user.id }}); });</script>
</td>
<td class="bad_count">
<script>$(function() { getBadCount({{ music.id }}, {{ selected_user.id }}); });</script>
</td>
<td class="updated_at">
<script>$(function() { getLatestUpdatedAt({{ music.id }}, {{ selected_user.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:ranking_detail' 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
50 changes: 47 additions & 3 deletions users/views/cleardata.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 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
Expand All @@ -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)

Expand Down

0 comments on commit 154f20e

Please sign in to comment.