Commit 6b11c1c775d3675662be40db6634cb557693cbf9
1 parent
89d98f7b
Exists in
master
and in
5 other branches
Form to app files #180
Showing
1 changed file
with
26 additions
and
0 deletions
Show diff stats
files/forms.py
| 1 | +from django.conf import settings | |
| 1 | 2 | from django import forms |
| 2 | 3 | from .models import TopicFile |
| 4 | +from django.core.exceptions import ValidationError, FieldError | |
| 3 | 5 | from django.utils.translation import ugettext_lazy as _ |
| 4 | 6 | |
| 5 | 7 | class FileForm(forms.ModelForm): |
| 6 | 8 | |
| 9 | + def clean_file_url(self): | |
| 10 | + file_url = self.cleaned_data['file_url'] | |
| 11 | + if file_url._size > settings.MAX_UPLOAD_SIZE: | |
| 12 | + raise forms.ValidationError(_('File too large (Max 10MB)')) | |
| 13 | + return file_url | |
| 14 | + | |
| 15 | + | |
| 16 | + class Meta: | |
| 17 | + model = TopicFile | |
| 18 | + fields = ['name', 'file_url'] | |
| 19 | + | |
| 20 | +class UpdateFileForm(forms.ModelForm): | |
| 21 | + file_url = forms.FileField(required=False) | |
| 22 | + | |
| 23 | + def clean_file_url(self): | |
| 24 | + file_url = self.cleaned_data['file_url'] | |
| 25 | + print(file_url) | |
| 26 | + if file_url: | |
| 27 | + if hasattr(file_url, '_size'): | |
| 28 | + if file_url._size > settings.MAX_UPLOAD_SIZE: | |
| 29 | + raise forms.ValidationError(_('File too large (Max 10MB)')) | |
| 30 | + return file_url | |
| 31 | + | |
| 32 | + | |
| 7 | 33 | class Meta: |
| 8 | 34 | model = TopicFile |
| 9 | 35 | fields = ['name', 'file_url'] |
| 10 | 36 | \ No newline at end of file | ... | ... |