gcs-django/gurps_character/forms.py
2025-03-09 17:58:06 +11:00

28 lines
729 B
Python

import json
from django import forms
from django.core.exceptions import ValidationError
def validate_file(value):
if value.size > 10**6:
raise ValidationError("File too large")
try:
data = json.loads(value.read())
except json.JSONDecodeError:
raise ValidationError("Not a GCS file - json decode")
try:
version = data["version"]
except KeyError:
raise ValidationError("Not a GCS file - key error")
if version < 4:
raise ValidationError(
f"The file version ({version}) is too old. Please use a newer "
"version (5.20.3) of GCS."
)
class UploadFileForm(forms.Form):
file = forms.FileField(validators=[validate_file])