upload/views.py
author Shantanu <shantanu@fossee.in>
Wed, 02 Sep 2009 19:43:43 +0530
changeset 3 eb11f0116216
parent 2 4d2bbb2f3c4e
child 12 0a2b4e85a4ab
permissions -rw-r--r--
Removed typo in forms.py.

#can add features like: supporting files with specified extensions, making edit box of content better etc.
from django.http import HttpResponse
from django.template import Context, Template
from django.shortcuts import render_to_response
from form import FileForm, Uploaded_fileForm
from forms import ParticipantForm
import tarfile

#function to read the upoaded file and store it
def handle_uploaded_file(f):
    destination = open(f.name, 'wb+')
    for chunk in f.chunks():
        destination.write(chunk)
    destination.close()

#view to handle uploaded file, showing content of file, and option of uploading a file
def upload_file(request,template_name='index.html'):
    if request.method == 'POST':
        form = FileForm(request.POST, request.FILES)		
        if form.is_valid():
	    #if else for checking the size of uploaded file
            handle_uploaded_file(request.FILES['file'])
	    uploaded_form = Uploaded_fileForm(initial={'content': open(request.FILES['file'].name).read()})
	    c = Context({'form': uploaded_form,
			'value': False,
			}) 	    
	    #display a page with textbox and all the content of file	   
            return render_to_response(template_name,
			context_instance = c)
    c = Context({'form': ParticipantForm(),
		'value': True,
		})
    return render_to_response(template_name,
			context_instance = c)