# HG changeset patch # User dellsystem # Date 1348330824 14400 # Node ID ba51d3b7740ba53db968c61f99067d1d95110f11 # Parent 4f69775150485c067038cd1e2c4178ebf3e2fb32 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. diff -r 4f6977515048 -r ba51d3b7740b apps/snippet/forms.py --- a/apps/snippet/forms.py Sat Sep 22 12:17:03 2012 -0400 +++ b/apps/snippet/forms.py Sat Sep 22 12:20:24 2012 -0400 @@ -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 diff -r 4f6977515048 -r ba51d3b7740b apps/snippet/views.py --- a/apps/snippet/views.py Sat Sep 22 12:17:03 2012 -0400 +++ b/apps/snippet/views.py Sat Sep 22 12:20:24 2012 -0400 @@ -1,7 +1,7 @@ import difflib from django.shortcuts import render_to_response, \ - get_object_or_404, get_list_or_404, render + get_object_or_404, get_list_or_404, render, redirect from django.template.context \ import RequestContext from django.http \ @@ -28,40 +28,36 @@ return render(request, 'snippet/explore.html', context) -def snippet_new(request, template_name='snippet/snippet_new.djhtml'): - +def snippet_new(request): if request.method == "POST": snippet = Snippet() - try: + + if request.user.is_authenticated(): snippet.author = request.user - except: - pass - snippet_form = SnippetForm(data=request.POST, request=request, + + snippet_form = SnippetForm(request.POST, + request.FILES, + request=request, instance=snippet) + if snippet_form.is_valid(): request, new_snippet = snippet_form.save() - return HttpResponseRedirect(new_snippet.get_absolute_url()) + return redirect(new_snippet) else: snippet_form = SnippetForm(request=request) recent = Snippet.objects.all()[:10] - template_context = { + context = { 'snippet_form': snippet_form, 'recent_snippets' : recent, } - return render_to_response( - template_name, - template_context, - RequestContext(request) - ) + return render(request, 'snippet/snippet_new.djhtml', context) def snippet_details(request, snippet_id, - template_name='snippet/snippet_details.djhtml', is_raw=False): - snippet = get_object_or_404(Snippet, secret_id=snippet_id) snippet.num_views += 1 snippet.save() @@ -76,12 +72,13 @@ } if request.method == "POST": - snippet_form = SnippetForm(data=request.POST, + snippet_form = SnippetForm(request.POST, + request.FILES, request=request, initial=new_snippet_initial) if snippet_form.is_valid(): request, new_snippet = snippet_form.save(parent=snippet) - return HttpResponseRedirect(new_snippet.get_absolute_url()) + return redirect(new_snippet) else: snippet_form = SnippetForm(initial=new_snippet_initial, request=request) @@ -91,7 +88,7 @@ else: default_pygments_style = PygmentsStyle.objects.get(pk=1) - template_context = { + context = { 'snippet_form': snippet_form, 'snippet': snippet, 'lines': range(snippet.get_linecount()), @@ -102,11 +99,7 @@ 'no_descendants': len(tree) == 1, } - response = render_to_response( - template_name, - template_context, - RequestContext(request) - ) + response = render(request, 'snippet/snippet_details.djhtml', context) if is_raw: response['Content-Type'] = 'text/plain' diff -r 4f6977515048 -r ba51d3b7740b templates/simple_field.djhtml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/templates/simple_field.djhtml Sat Sep 22 12:20:24 2012 -0400 @@ -0,0 +1,10 @@ +{{ field.label_tag }} +{{ field }} + +{% if field.errors %} +
{{ field.errors }}
+{% endif %} + +{% if field.help_text %} +

{{ field.help_text }}

+{% endif %} diff -r 4f6977515048 -r ba51d3b7740b templates/snippet/snippet_form.djhtml --- a/templates/snippet/snippet_form.djhtml Sat Sep 22 12:17:03 2012 -0400 +++ b/templates/snippet/snippet_form.djhtml Sat Sep 22 12:20:24 2012 -0400 @@ -1,18 +1,40 @@ {% load i18n %} -
-{% csrf_token %} - {% for field in snippet_form %} - {{ field.errors }} - {{ field.label_tag }} - {{ field }} - {% if request.session.userprefs.display_all_lexer %} - {% ifequal field.name "lexer" %} - - {% endifequal %} - {% endif %} - {% endfor %} + + {% if snippet_form.non_field_errors %} +
+ {{ snippet_form.non_field_errors }} +
+ {% endif %} + + {% csrf_token %} + + {% with field=snippet_form.title %} + {% include "simple_field.djhtml" %} + {% endwith %} + + {% with field=snippet_form.content %} + {% include "simple_field.djhtml" %} + {% endwith %} + + {% with field=snippet_form.file %} + {% include "simple_field.djhtml" %} +
+ {% endwith %} + + {% with field=snippet_form.lexer %} + {% include "simple_field.djhtml" %} + {% if request.session.userprefs.display_all_lexer %} + + {% endif %} + {% endwith %} + + {% with field=snippet_form.expire_options %} + {% include "simple_field.djhtml" %} + {% endwith %} +