gremmie@1: """ gremmie@1: Forms for the downloads application. gremmie@1: """ gremmie@1: import os gremmie@1: gremmie@1: from django import forms gremmie@1: bgneal@1023: from core.html import ImageCheckError bgneal@1023: from core.html import image_check bgneal@1023: from core.markup import site_markup bgneal@204: from downloads.models import PendingDownload gremmie@1: from downloads.models import AllowedExtension gremmie@1: gremmie@1: gremmie@1: class AddDownloadForm(forms.ModelForm): gremmie@1: """Form to allow adding downloads.""" bgneal@312: title = forms.CharField(required=True, bgneal@133: widget=forms.TextInput(attrs={'size': 64, 'maxlength': 64})) bgneal@312: description = forms.CharField(required=False, bgneal@133: widget=forms.Textarea(attrs={'class': 'markItUp smileyTarget'})) gremmie@1: bgneal@1023: def clean_description(self): bgneal@1023: description = self.cleaned_data['description'].strip() bgneal@1023: self.html = None bgneal@1023: if not description: bgneal@1023: raise forms.ValidationError("Please enter a description") bgneal@1023: bgneal@1023: self.html = site_markup(description) bgneal@1023: try: bgneal@1023: image_check(self.html) bgneal@1023: except ImageCheckError as ex: bgneal@1023: raise forms.ValidationError(str(ex)) bgneal@1023: bgneal@1023: return description bgneal@1023: gremmie@1: def clean_file(self): gremmie@1: file = self.cleaned_data['file'] gremmie@1: ext = os.path.splitext(file.name)[1] gremmie@1: allowed_exts = AllowedExtension.objects.get_extension_list() gremmie@1: if ext in allowed_exts: gremmie@1: return file gremmie@1: raise forms.ValidationError('The file extension "%s" is not allowed.' % ext) gremmie@1: gremmie@1: class Meta: bgneal@204: model = PendingDownload gremmie@1: fields = ('title', 'category', 'description', 'file')