upload/views.py
author Madhusudan.C.S <madhusudancs@gmail.com>
Wed, 02 Sep 2009 20:27:41 +0530
changeset 7 e49daa7af035
parent 3 eb11f0116216
child 12 0a2b4e85a4ab
permissions -rw-r--r--
Added imports to urls.py.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
     1
#can add features like: supporting files with specified extensions, making edit box of content better etc.
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
     2
from django.http import HttpResponse
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
     3
from django.template import Context, Template
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
     4
from django.shortcuts import render_to_response
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
     5
from form import FileForm, Uploaded_fileForm
2
4d2bbb2f3c4e Added models and views.
Shantanu <shantanu@fossee.in>
parents: 1
diff changeset
     6
from forms import ParticipantForm
1
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
     7
import tarfile
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
     8
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
     9
#function to read the upoaded file and store it
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    10
def handle_uploaded_file(f):
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    11
    destination = open(f.name, 'wb+')
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    12
    for chunk in f.chunks():
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    13
        destination.write(chunk)
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    14
    destination.close()
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    15
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    16
#view to handle uploaded file, showing content of file, and option of uploading a file
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    17
def upload_file(request,template_name='index.html'):
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    18
    if request.method == 'POST':
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    19
        form = FileForm(request.POST, request.FILES)		
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    20
        if form.is_valid():
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    21
	    #if else for checking the size of uploaded file
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    22
            handle_uploaded_file(request.FILES['file'])
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    23
	    uploaded_form = Uploaded_fileForm(initial={'content': open(request.FILES['file'].name).read()})
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    24
	    c = Context({'form': uploaded_form,
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    25
			'value': False,
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    26
			}) 	    
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    27
	    #display a page with textbox and all the content of file	   
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    28
            return render_to_response(template_name,
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    29
			context_instance = c)
2
4d2bbb2f3c4e Added models and views.
Shantanu <shantanu@fossee.in>
parents: 1
diff changeset
    30
    c = Context({'form': ParticipantForm(),
1
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    31
		'value': True,
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    32
		})
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    33
    return render_to_response(template_name,
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    34
			context_instance = c)