fix friend_code editing

This commit is contained in:
badblocks 2025-03-15 15:22:41 -07:00
parent 36f97be9d0
commit 0ac8ac8d5c
5 changed files with 88 additions and 9 deletions

View file

@ -2,7 +2,7 @@ from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy
from django.shortcuts import redirect, get_object_or_404
from django.views.generic import ListView, CreateView, DeleteView, View, TemplateView
from django.views.generic import ListView, CreateView, DeleteView, View, TemplateView, UpdateView
from accounts.models import FriendCode
from accounts.forms import FriendCodeForm
from django.db.models import Case, When, Value, BooleanField
@ -127,4 +127,25 @@ class SettingsView(LoginRequiredMixin, TemplateView):
"""
Display the user's settings.
"""
template_name = "account/settings.html"
template_name = "account/settings.html"
class EditFriendCodeView(LoginRequiredMixin, UpdateView):
"""
Edit the in-game name for a friend code.
The friend code itself is displayed as plain text.
Also includes "Set Default" and "Delete" buttons in the template.
"""
model = FriendCode
# Only the in_game_name field is editable
fields = ['in_game_name']
template_name = "friend_codes/edit_friend_code.html"
context_object_name = "friend_code"
success_url = reverse_lazy("list_friend_codes")
def get_queryset(self):
# Ensure the user can only edit their own friend codes
return FriendCode.objects.filter(user=self.request.user)
def form_valid(self, form):
messages.success(self.request, "Friend code updated successfully.")
return super().form_valid(form)