Initial working version with minor bugs

This commit is contained in:
badblocks 2025-02-26 00:06:42 -08:00
parent f946e4933a
commit 71b3993326
83 changed files with 34485 additions and 173 deletions

0
friend_codes/__init__.py Normal file
View file

5
friend_codes/admin.py Normal file
View file

@ -0,0 +1,5 @@
from django.contrib import admin
from .models import FriendCode
# Register your models here.
admin.site.register(FriendCode)

6
friend_codes/apps.py Normal file
View file

@ -0,0 +1,6 @@
from django.apps import AppConfig
class FriendCodesConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'friend_codes'

22
friend_codes/forms.py Normal file
View file

@ -0,0 +1,22 @@
from django import forms
from .models import FriendCode
class FriendCodeForm(forms.ModelForm):
class Meta:
model = FriendCode
fields = ["friend_code"]
def clean_friend_code(self):
friend_code = self.cleaned_data.get("friend_code", "").strip()
# Remove any dashes from the input so we can validate the digits only.
friend_code_clean = friend_code.replace("-", "")
# Ensure that the cleaned friend code is exactly 16 digits.
if len(friend_code_clean) != 16 or not friend_code_clean.isdigit():
raise forms.ValidationError("Friend code must be exactly 16 digits long.")
# Format the friend code with dashes: XXXX-XXXX-XXXX-XXXX.
friend_code_formatted = f"{friend_code_clean[:4]}-{friend_code_clean[4:8]}-{friend_code_clean[8:12]}-{friend_code_clean[12:16]}"
return friend_code_formatted

View file

@ -0,0 +1,27 @@
# Generated by Django 5.1.2 on 2025-02-20 02:54
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]
operations = [
migrations.CreateModel(
name='FriendCode',
fields=[
('id', models.AutoField(primary_key=True, serialize=False)),
('friend_code', models.CharField(max_length=16)),
('created_at', models.DateTimeField(auto_now_add=True)),
('updated_at', models.DateTimeField(auto_now=True)),
('user', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='friend_codes', to=settings.AUTH_USER_MODEL)),
],
),
]

View file

@ -0,0 +1,18 @@
# Generated by Django 5.1.2 on 2025-02-20 03:06
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('friend_codes', '0001_initial'),
]
operations = [
migrations.AlterField(
model_name='friendcode',
name='friend_code',
field=models.CharField(max_length=19),
),
]

View file

13
friend_codes/models.py Normal file
View file

@ -0,0 +1,13 @@
from django.db import models
from django.conf import settings
from accounts.models import CustomUser
class FriendCode(models.Model):
id = models.AutoField(primary_key=True)
friend_code = models.CharField(max_length=19)
user = models.ForeignKey("accounts.CustomUser", on_delete=models.PROTECT, related_name='friend_codes')
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
def __str__(self):
return self.friend_code

3
friend_codes/tests.py Normal file
View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

8
friend_codes/urls.py Normal file
View file

@ -0,0 +1,8 @@
from django.urls import path
from .views import ListFriendCodesView, AddFriendCodeView, DeleteFriendCodeView
urlpatterns = [
path('', ListFriendCodesView.as_view(), name='list_friend_codes'),
path('add/', AddFriendCodeView.as_view(), name='add_friend_code'),
path('delete/<int:pk>/', DeleteFriendCodeView.as_view(), name='delete_friend_code'),
]

67
friend_codes/views.py Normal file
View file

@ -0,0 +1,67 @@
from django.shortcuts import render, redirect
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy
from django.views.generic import ListView, CreateView, DeleteView
from .models import FriendCode
from .forms import FriendCodeForm
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):
# Only display friend codes that belong to the current user.
return self.request.user.friend_codes.all()
class AddFriendCodeView(LoginRequiredMixin, CreateView):
"""
Add a new friend code for the current user.
"""
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):
# Set the friend code's user to the current user before saving.
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.
The friend code will not be removed if it is referenced by trade offers via
either the initiated_by or accepted_by relationships.
"""
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):
# Ensure the friend code belongs to the current user.
return FriendCode.objects.filter(user=self.request.user)
def post(self, request, *args, **kwargs):
self.object = self.get_object()
# Check if there are any trade offers associated with this friend code.
if self.object.initiated_by.exists() or self.object.accepted_by.exists():
messages.error(
request,
"Cannot remove this friend code because there are existing trade offers associated with it."
)
return redirect(self.success_url)
else:
self.object.delete()
messages.success(request, "Friend code removed successfully.")
return redirect(self.success_url)