comparison downloads/forms.py @ 1023:a5ebc74dc3f3

Perform image_check on downloads.
author Brian Neal <bgneal@gmail.com>
date Thu, 10 Dec 2015 20:57:50 -0600
parents 21c592cac71c
children
comparison
equal deleted inserted replaced
1022:82f1f6f905eb 1023:a5ebc74dc3f3
3 """ 3 """
4 import os 4 import os
5 5
6 from django import forms 6 from django import forms
7 7
8 from core.html import ImageCheckError
9 from core.html import image_check
10 from core.markup import site_markup
8 from downloads.models import PendingDownload 11 from downloads.models import PendingDownload
9 from downloads.models import AllowedExtension 12 from downloads.models import AllowedExtension
10 13
11 14
12 class AddDownloadForm(forms.ModelForm): 15 class AddDownloadForm(forms.ModelForm):
13 """Form to allow adding downloads.""" 16 """Form to allow adding downloads."""
14 title = forms.CharField(required=True, 17 title = forms.CharField(required=True,
15 widget=forms.TextInput(attrs={'size': 64, 'maxlength': 64})) 18 widget=forms.TextInput(attrs={'size': 64, 'maxlength': 64}))
16 description = forms.CharField(required=False, 19 description = forms.CharField(required=False,
17 widget=forms.Textarea(attrs={'class': 'markItUp smileyTarget'})) 20 widget=forms.Textarea(attrs={'class': 'markItUp smileyTarget'}))
21
22 def clean_description(self):
23 description = self.cleaned_data['description'].strip()
24 self.html = None
25 if not description:
26 raise forms.ValidationError("Please enter a description")
27
28 self.html = site_markup(description)
29 try:
30 image_check(self.html)
31 except ImageCheckError as ex:
32 raise forms.ValidationError(str(ex))
33
34 return description
18 35
19 def clean_file(self): 36 def clean_file(self):
20 file = self.cleaned_data['file'] 37 file = self.cleaned_data['file']
21 ext = os.path.splitext(file.name)[1] 38 ext = os.path.splitext(file.name)[1]
22 allowed_exts = AllowedExtension.objects.get_extension_list() 39 allowed_exts = AllowedExtension.objects.get_extension_list()