From 6b11c1c775d3675662be40db6634cb557693cbf9 Mon Sep 17 00:00:00 2001 From: ailsoncgt Date: Mon, 17 Oct 2016 22:46:42 -0300 Subject: [PATCH] Form to app files #180 --- files/forms.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+), 0 deletions(-) diff --git a/files/forms.py b/files/forms.py index a5300c1..e93f89c 100644 --- a/files/forms.py +++ b/files/forms.py @@ -1,9 +1,35 @@ +from django.conf import settings from django import forms from .models import TopicFile +from django.core.exceptions import ValidationError, FieldError from django.utils.translation import ugettext_lazy as _ class FileForm(forms.ModelForm): + def clean_file_url(self): + file_url = self.cleaned_data['file_url'] + if file_url._size > settings.MAX_UPLOAD_SIZE: + raise forms.ValidationError(_('File too large (Max 10MB)')) + return file_url + + + class Meta: + model = TopicFile + fields = ['name', 'file_url'] + +class UpdateFileForm(forms.ModelForm): + file_url = forms.FileField(required=False) + + def clean_file_url(self): + file_url = self.cleaned_data['file_url'] + print(file_url) + if file_url: + if hasattr(file_url, '_size'): + if file_url._size > settings.MAX_UPLOAD_SIZE: + raise forms.ValidationError(_('File too large (Max 10MB)')) + return file_url + + class Meta: model = TopicFile fields = ['name', 'file_url'] \ No newline at end of file -- libgit2 0.21.2