gcs-django/gurps_character/forms.py
2024-06-26 14:29:06 +10:00

28 lines
768 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:
import bpdb;bpdb.set_trace()
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])