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, UpdateView from accounts.models import FriendCode, CustomUser from accounts.forms import FriendCodeForm, UserSettingsForm from django.db.models import Case, When, Value, BooleanField from trades.models import TradeOffer, TradeAcceptance class ListFriendCodesView(LoginRequiredMixin, ListView): """ Display the current user's friend codes. """ model = FriendCode template_name = "friend_codes/list_friend_codes.html" context_object_name = "friend_codes" def get_queryset(self): # 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): """ Add a new friend code for the current user. If the user does not yet have a default, the newly added code will automatically become the default. """ model = FriendCode form_class = FriendCodeForm template_name = "friend_codes/add_friend_code.html" success_url = reverse_lazy("list_friend_codes") def form_valid(self, form): form.instance.user = self.request.user messages.success(self.request, "Friend code added successfully.") return super().form_valid(form) class DeleteFriendCodeView(LoginRequiredMixin, DeleteView): """ Remove an existing friend code. Prevent deletion if the friend code is bound to any trade offers. Also, prevent deletion if the friend code is either the only one or is set as the default friend code. """ model = FriendCode template_name = "friend_codes/confirm_delete_friend_code.html" context_object_name = "friend_code" success_url = reverse_lazy("list_friend_codes") def get_queryset(self): # Only allow deletion of friend codes owned by the current user. return FriendCode.objects.filter(user=self.request.user) def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) friend_code = self.get_object() user = self.request.user # Determine if the deletion should be disabled. disable_delete = False error_message = None if user.friend_codes.count() == 1: disable_delete = True error_message = "Cannot delete your only friend code." elif user.default_friend_code == friend_code: disable_delete = True error_message = ( "Cannot delete your default friend code. " "Please set a different default first." ) context["disable_delete"] = disable_delete context["error_message"] = error_message return context def post(self, request, *args, **kwargs): self.object = self.get_object() user = self.object.user # Check if the friend code is the only one; prevent deletion. if user.friend_codes.count() == 1: messages.error(request, "Cannot remove your only friend code.") return redirect(self.success_url) # Check if the friend code is set as default; prevent deletion. if user.default_friend_code == self.object: messages.error( request, "Cannot delete your default friend code. Please set a different default first." ) return redirect(self.success_url) # Use the unfiltered manager and filter by the friend code's primary key trade_offer_exists = TradeOffer.all_offers.filter(initiated_by_id=self.object.pk).exists() trade_acceptance_exists = TradeAcceptance.objects.filter(accepted_by_id=self.object.pk).exists() if trade_offer_exists or trade_acceptance_exists: messages.error( request, "Cannot remove this friend code because there are existing trade offers associated with it." ) return redirect(self.success_url) # Proceed to safe deletion. self.object.delete() messages.success(request, "Friend code removed successfully.") return redirect(self.success_url) class ChangeDefaultFriendCodeView(LoginRequiredMixin, View): """ Change the default friend code for the current user. """ def post(self, request, *args, **kwargs): friend_code_id = kwargs.get("pk") friend_code = get_object_or_404(FriendCode, pk=friend_code_id, user=request.user) request.user.set_default_friend_code(friend_code) messages.success(request, "Default friend code updated successfully.") return redirect("list_friend_codes") # Updated SettingsView to update the new user setting. class SettingsView(LoginRequiredMixin, UpdateView): """ Display account navigation links and allow the user to update their friend code visibility setting. """ model = CustomUser form_class = UserSettingsForm template_name = "account/settings.html" success_url = reverse_lazy("settings") def get_object(self): return self.request.user def form_valid(self, form): messages.success(self.request, "Settings updated successfully.") return super().form_valid(form) class ProfileView(LoginRequiredMixin, TemplateView): """ Display the user's profile. """ template_name = "account/profile.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)