gremmie@1
|
1 """
|
gremmie@1
|
2 Forms for the downloads application.
|
gremmie@1
|
3 """
|
gremmie@1
|
4 import os
|
gremmie@1
|
5
|
gremmie@1
|
6 from django import forms
|
gremmie@1
|
7
|
bgneal@1023
|
8 from core.html import ImageCheckError
|
bgneal@1023
|
9 from core.html import image_check
|
bgneal@1023
|
10 from core.markup import site_markup
|
bgneal@204
|
11 from downloads.models import PendingDownload
|
gremmie@1
|
12 from downloads.models import AllowedExtension
|
gremmie@1
|
13
|
gremmie@1
|
14
|
gremmie@1
|
15 class AddDownloadForm(forms.ModelForm):
|
gremmie@1
|
16 """Form to allow adding downloads."""
|
bgneal@312
|
17 title = forms.CharField(required=True,
|
bgneal@133
|
18 widget=forms.TextInput(attrs={'size': 64, 'maxlength': 64}))
|
bgneal@312
|
19 description = forms.CharField(required=False,
|
bgneal@133
|
20 widget=forms.Textarea(attrs={'class': 'markItUp smileyTarget'}))
|
gremmie@1
|
21
|
bgneal@1023
|
22 def clean_description(self):
|
bgneal@1023
|
23 description = self.cleaned_data['description'].strip()
|
bgneal@1023
|
24 self.html = None
|
bgneal@1023
|
25 if not description:
|
bgneal@1023
|
26 raise forms.ValidationError("Please enter a description")
|
bgneal@1023
|
27
|
bgneal@1023
|
28 self.html = site_markup(description)
|
bgneal@1023
|
29 try:
|
bgneal@1023
|
30 image_check(self.html)
|
bgneal@1023
|
31 except ImageCheckError as ex:
|
bgneal@1023
|
32 raise forms.ValidationError(str(ex))
|
bgneal@1023
|
33
|
bgneal@1023
|
34 return description
|
bgneal@1023
|
35
|
gremmie@1
|
36 def clean_file(self):
|
gremmie@1
|
37 file = self.cleaned_data['file']
|
gremmie@1
|
38 ext = os.path.splitext(file.name)[1]
|
gremmie@1
|
39 allowed_exts = AllowedExtension.objects.get_extension_list()
|
gremmie@1
|
40 if ext in allowed_exts:
|
gremmie@1
|
41 return file
|
gremmie@1
|
42 raise forms.ValidationError('The file extension "%s" is not allowed.' % ext)
|
gremmie@1
|
43
|
gremmie@1
|
44 class Meta:
|
bgneal@204
|
45 model = PendingDownload
|
gremmie@1
|
46 fields = ('title', 'category', 'description', 'file')
|