Skip to content

Commit

Permalink
Merge pull request #72 from Syriiin/improve-admin
Browse files Browse the repository at this point in the history
Improve admin list pages
  • Loading branch information
Syriiin authored Jun 10, 2024
2 parents af18121 + c289f0e commit 9228281
Show file tree
Hide file tree
Showing 10 changed files with 220 additions and 17 deletions.
41 changes: 39 additions & 2 deletions leaderboards/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from django.contrib import admin

from common.osu.enums import Gamemode
from leaderboards.enums import LeaderboardAccessType
from leaderboards.models import Invite, Leaderboard, Membership


Expand All @@ -10,11 +12,39 @@ class LeaderboardAdmin(admin.ModelAdmin):
"score_filter",
)

list_display = [
"__str__",
"gamemode_display",
"access_type_display",
"member_count",
"archived",
"owner",
"creation_time",
]
list_select_related = ["owner"]
list_filter = ["gamemode", "access_type", "archived"]

@admin.display(description="Gamemode")
def gamemode_display(self, obj: Leaderboard):
return Gamemode(obj.gamemode).name

@admin.display(description="Access Type")
def access_type_display(self, obj: Leaderboard):
return LeaderboardAccessType(obj.access_type).name


class MembershipAdmin(admin.ModelAdmin):
model = Membership
filter_horizontal = ("scores",)
raw_id_fields = ("leaderboard", "user", "scores")
raw_id_fields = ("leaderboard", "user")

list_display = [
"id",
"leaderboard",
"user",
"pp",
"rank",
"score_count",
]


class InviteAdmin(admin.ModelAdmin):
Expand All @@ -24,6 +54,13 @@ class InviteAdmin(admin.ModelAdmin):
"user",
)

list_display = [
"id",
"leaderboard",
"user",
"invite_date",
]


admin.site.register(Leaderboard, LeaderboardAdmin)
admin.site.register(Membership, MembershipAdmin)
Expand Down
2 changes: 1 addition & 1 deletion leaderboards/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def update_member_count(self):
self.save()

def __str__(self):
return f"[{Gamemode(self.gamemode).name}] {self.name}"
return f"{self.name}"

class Meta:
indexes = [models.Index(fields=["gamemode"])]
Expand Down
7 changes: 3 additions & 4 deletions leaderboards/test_models.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import pytest

from leaderboards.models import Invite, Leaderboard, Membership
from profiles.models import OsuUser


@pytest.mark.django_db
class TestLeaderboard:
def test_magic_str(self, leaderboard: Leaderboard):
assert str(leaderboard) == "[STANDARD] test leaderboard"
assert str(leaderboard) == "test leaderboard"

def test_get_pp_record(self):
# TODO: this
Expand All @@ -29,7 +28,7 @@ def test_update_member_count(self):
@pytest.mark.django_db
class TestMembership:
def test_magic_str(self, membership: Membership):
assert str(membership) == "[STANDARD] test leaderboard: TestOsuUser"
assert str(membership) == "test leaderboard: TestOsuUser"

def test_recalculate(self):
# TODO: this
Expand All @@ -39,4 +38,4 @@ def test_recalculate(self):
@pytest.mark.django_db
class TestInvite:
def test_magic_str(self, invite: Invite):
assert str(invite) == "[STANDARD] test leaderboard: TestOsuUser"
assert str(invite) == "test leaderboard: TestOsuUser"
10 changes: 9 additions & 1 deletion osuauth/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@ class UserAdmin(BaseUserAdmin):
Custom user admin
"""

list_display = ("username", "osu_user", "is_staff")
list_display = (
"username",
"osu_user",
"is_staff",
"last_login",
"date_joined",
"last_active",
)
list_select_related = ("osu_user",)
fieldsets = (
(None, {"fields": ("username", "password")}),
(
Expand Down
162 changes: 159 additions & 3 deletions profiles/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from django.contrib import admin

from common.osu import utils
from common.osu.enums import BeatmapStatus, Gamemode, Mods
from profiles.enums import ScoreMutation, ScoreResult
from profiles.models import (
Beatmap,
DifficultyCalculation,
Expand All @@ -13,25 +16,115 @@
)


class OsuUserAdmin(admin.ModelAdmin):
model = OsuUser

list_display = [
"__str__",
"id",
"country",
"join_date",
]


class UserStatsAdmin(admin.ModelAdmin):
model = UserStats
raw_id_fields = ("user",)

list_display = [
"id",
"gamemode_display",
"user",
"rank",
"pp",
]
list_filter = ["gamemode"]

@admin.display(description="Gamemode")
def gamemode_display(self, obj: UserStats):
return Gamemode(obj.gamemode).name


class BeatmapAdmin(admin.ModelAdmin):
model = Beatmap
raw_id_fields = ("creator",)

list_display = [
"__str__",
"gamemode_display",
"status_display",
"submission_date",
"last_updated",
"approval_date",
]
list_filter = ["gamemode", "status"]

@admin.display(description="Gamemode")
def gamemode_display(self, obj: Beatmap):
return Gamemode(obj.gamemode).name

@admin.display(description="Status")
def status_display(self, obj: Beatmap):
return BeatmapStatus(obj.status).name


class DifficultyCalculationAdmin(admin.ModelAdmin):
model = DifficultyCalculation
raw_id_fields = ("beatmap",)

list_display = [
"id",
"beatmap",
"mods_display",
"calculator_engine",
"calculator_version",
]
list_filter = ["calculator_engine"]
# NOTE: for some reason it's automatically joining on beatmap__creator unless we explicitly list_select_related
list_select_related = ["beatmap"]

@admin.display(description="Mods")
def mods_display(self, obj: DifficultyCalculation):
if obj.mods == Mods.NONE:
return None
return utils.get_mods_string(obj.mods)


class DifficultyValueAdmin(admin.ModelAdmin):
model = DifficultyCalculation
model = DifficultyValue
raw_id_fields = ("calculation",)

list_display = [
"id",
"beatmap_display",
"mods_display",
"calculator_engine_display",
"calculator_version_display",
"name",
"value",
]
list_filter = ["calculation__calculator_engine", "name"]
# NOTE: for some reason it's automatically joining on beatmap__creator unless we explicitly list_select_related
list_select_related = ["calculation__beatmap"]

@admin.display(description="Mods")
def mods_display(self, obj: DifficultyValue):
if obj.calculation.mods == Mods.NONE:
return None
return utils.get_mods_string(obj.calculation.mods)

@admin.display(description="Beatmap")
def beatmap_display(self, obj: DifficultyValue):
return obj.calculation.beatmap

@admin.display(description="Calculator Engine")
def calculator_engine_display(self, obj: DifficultyValue):
return obj.calculation.calculator_engine

@admin.display(description="Calculator Version")
def calculator_version_display(self, obj: DifficultyValue):
return obj.calculation.calculator_version


class ScoreAdmin(admin.ModelAdmin):
model = Score
Expand All @@ -40,6 +133,39 @@ class ScoreAdmin(admin.ModelAdmin):
"user_stats",
)

list_display = [
"__str__",
"user",
"mods_display",
"accuracy_display",
"beatmap",
"result_display",
"mutation_display",
]
list_select_related = ["user_stats__user", "beatmap"]

@admin.display(description="User")
def user(self, obj: Score):
return obj.user_stats.user

@admin.display(description="Mods")
def mods_display(self, obj: Score):
if obj.mods == Mods.NONE:
return None
return utils.get_mods_string(obj.mods)

@admin.display(description="Accuracy")
def accuracy_display(self, obj: Score):
return f"{obj.accuracy:.2f}%"

@admin.display(description="Result")
def result_display(self, obj: Score):
return ScoreResult(obj.result).name

@admin.display(description="Mutation")
def mutation_display(self, obj: Score):
return ScoreMutation(obj.mutation).name


class PerformanceCalculationAdmin(admin.ModelAdmin):
model = PerformanceCalculation
Expand All @@ -48,13 +174,43 @@ class PerformanceCalculationAdmin(admin.ModelAdmin):
"difficulty_calculation",
)

# no joins because it gets really slow, especially on high page numbers
list_display = [
"id",
"score_id",
"calculator_engine",
"calculator_version",
]


class PerformanceValueAdmin(admin.ModelAdmin):
model = PerformanceCalculation
model = PerformanceValue
raw_id_fields = ("calculation",)

list_display = [
"id",
"score_id_display",
"calculator_engine_display",
"calculator_version_display",
"name",
"value",
]
list_select_related = ["calculation"]

@admin.display(description="Score ID")
def score_id_display(self, obj: PerformanceValue):
return obj.calculation.score_id

@admin.display(description="Calculator Engine")
def calculator_engine_display(self, obj: PerformanceValue):
return obj.calculation.calculator_engine

@admin.display(description="Calculator Version")
def calculator_version_display(self, obj: PerformanceValue):
return obj.calculation.calculator_version


admin.site.register(OsuUser)
admin.site.register(OsuUser, OsuUserAdmin)
admin.site.register(UserStats, UserStatsAdmin)
admin.site.register(Beatmap, BeatmapAdmin)
admin.site.register(DifficultyCalculation, DifficultyCalculationAdmin)
Expand Down
2 changes: 1 addition & 1 deletion profiles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ def update_performance_values(
error_reporter.report_error(e)

def __str__(self):
return f"{self.beatmap_id}: {self.performance_total:.0f}pp"
return f"{Gamemode(self.gamemode).name} {self.id}"

class Meta:
constraints = [
Expand Down
3 changes: 0 additions & 3 deletions profiles/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ def test_update_difficulty_values(self, beatmap: Beatmap):

@pytest.mark.django_db
class TestScore:
def test_magic_str(self, score: Score):
assert str(score) == "1: 395pp"

def test_process(self, score: Score):
score.process()
assert score.result == ScoreResult.END_CHOKE
Expand Down
6 changes: 6 additions & 0 deletions users/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,11 @@ class ScoreFilterPresetAdmin(admin.ModelAdmin):
"score_filter",
)

list_display = [
"__str__",
"user",
]
list_select_related = ["user__osu_user"]


admin.site.register(ScoreFilterPreset, ScoreFilterPresetAdmin)
2 changes: 1 addition & 1 deletion users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ class ScoreFilterPreset(models.Model):
score_filter = models.ForeignKey(ScoreFilter, on_delete=models.CASCADE)

def __str__(self):
return f"{self.user_id}: {self.name}"
return self.name
2 changes: 1 addition & 1 deletion users/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ def score_filter_preset(self, user: User, score_filter: ScoreFilter):
)

def test_magic_str(self, score_filter_preset):
assert str(score_filter_preset) == f"{score_filter_preset.user_id}: Hidden"
assert str(score_filter_preset) == f"Hidden"

0 comments on commit 9228281

Please sign in to comment.