Initial working version with minor bugs
This commit is contained in:
parent
f946e4933a
commit
71b3993326
83 changed files with 34485 additions and 173 deletions
22
friend_codes/forms.py
Normal file
22
friend_codes/forms.py
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue