22 lines
No EOL
921 B
Python
22 lines
No EOL
921 B
Python
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 |