Fix styling of friend codes list

This commit is contained in:
badblocks 2025-03-14 11:08:01 -07:00
parent bc181b12d9
commit 36f97be9d0
2 changed files with 23 additions and 11 deletions

View file

@ -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):
"""