Mercurial > hg > agora-ahsan
diff apps/snippet/forms.py @ 133:ba51d3b7740b
Add ability to upload a file to create a snippet
If a file is specified, the contents of that file are used in preference
to the contents of the 'content' text box.
author | dellsystem <ilostwaldo@gmail.com> |
---|---|
date | Sat, 22 Sep 2012 12:20:24 -0400 |
parents | d858aae811d0 |
children | f299232c82e8 |
line wrap: on
line diff
--- a/apps/snippet/forms.py +++ b/apps/snippet/forms.py @@ -19,6 +19,12 @@ EXPIRE_DEFAULT = 3600*24*30 class SnippetForm(forms.ModelForm): + file = forms.FileField(help_text=_("If the snippet you want to post is \ + saved as a file on your computer, you can upload it directly rather \ + than having to copy and paste it into the box above. If a file \ + is specified, the text in the content field above will be \ + ignored."), + required=False) expire_options = forms.ChoiceField( choices=EXPIRE_CHOICES, @@ -26,7 +32,8 @@ label=_(u'Expires'), ) - def __init__(self, request, *args, **kwargs): + def __init__(self, *args, **kwargs): + request = kwargs.pop('request') super(SnippetForm, self).__init__(*args, **kwargs) self.request = request @@ -43,6 +50,26 @@ except KeyError: pass + # Make the content field not required (validated in clean()) + self.fields['content'].required = False + self.fields['title'].required = True + + def clean(self): + cleaned_data = super(SnippetForm, self).clean() + file_data = cleaned_data.get('file') + content = cleaned_data.get('content') + + if file_data: + file_data.open() + cleaned_data['content'] = file_data.read() + elif not content: + # No snippet data specified + raise forms.ValidationError(_("Please specify some content for \ + the snippet, either in the content field or by uploading \ + a file.")) + + return cleaned_data + def save(self, parent=None, *args, **kwargs): # Set parent snippet