from django.apps import AppConfig
from django.contrib import admin


class ScrobblesConfig(AppConfig):
    name = "scrobbles"

    def ready(self):
        import scrobbles.signals  # noqa
        from scrobbles.models import Scrobble

        original_index = admin.site.index

        def custom_index(request, extra_context=None):
            extra_context = extra_context or {}
            extra_context["recent_scrobbles"] = Scrobble.objects.filter(
                timestamp__isnull=False
            ).order_by("-timestamp")[:20]
            return original_index(request, extra_context)

        admin.site.index = custom_index
