From 36f97be9d0a4f66d0a3f03d67f4825dc5fa81a92 Mon Sep 17 00:00:00 2001 From: badbl0cks <4161747+badbl0cks@users.noreply.github.com> Date: Fri, 14 Mar 2025 11:08:01 -0700 Subject: [PATCH] Fix styling of friend codes list --- accounts/views.py | 13 +++++++++++- .../friend_codes/list_friend_codes.html | 21 ++++++++++--------- 2 files changed, 23 insertions(+), 11 deletions(-) diff --git a/accounts/views.py b/accounts/views.py index 20a1f4d..e3a4e6a 100644 --- a/accounts/views.py +++ b/accounts/views.py @@ -5,6 +5,7 @@ from django.shortcuts import redirect, get_object_or_404 from django.views.generic import ListView, CreateView, DeleteView, View, TemplateView from accounts.models import FriendCode from accounts.forms import FriendCodeForm +from django.db.models import Case, When, Value, BooleanField class ListFriendCodesView(LoginRequiredMixin, ListView): """ @@ -15,7 +16,17 @@ class ListFriendCodesView(LoginRequiredMixin, ListView): context_object_name = "friend_codes" def get_queryset(self): - return self.request.user.friend_codes.all() + # Get the default friend code's primary key if it exists. + default_pk = getattr(self.request.user.default_friend_code, "pk", None) + + # Annotate each friend code with is_default=True if its pk matches. + return self.request.user.friend_codes.all().annotate( + is_default=Case( + When(pk=default_pk, then=Value(True)), + default=Value(False), + output_field=BooleanField() + ) + ) class AddFriendCodeView(LoginRequiredMixin, CreateView): """ diff --git a/theme/templates/friend_codes/list_friend_codes.html b/theme/templates/friend_codes/list_friend_codes.html index e48217e..bdd0747 100644 --- a/theme/templates/friend_codes/list_friend_codes.html +++ b/theme/templates/friend_codes/list_friend_codes.html @@ -3,27 +3,28 @@ {% block title %}My Friend Codes{% endblock %} {% block content %} -