upload/views.py
author Madhusudan.C.S <madhusudancs@gmail.com>
Wed, 02 Sep 2009 23:34:38 +0530
changeset 13 b0175fed9d20
parent 12 0a2b4e85a4ab
child 15 2dbb5e797989
permissions -rw-r--r--
Fixed indentation issue in models.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
12
0a2b4e85a4ab Modified views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 3
diff changeset
    17
def upload_file(request, template_name='index.html'):
1
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    18
    if request.method == 'POST':
12
0a2b4e85a4ab Modified views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 3
diff changeset
    19
        form = ParticipantForm(request.POST, request.FILES)		
1
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    20
        if form.is_valid():
12
0a2b4e85a4ab Modified views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 3
diff changeset
    21
            #handle_uploaded_file(request.FILES['file'])
0a2b4e85a4ab Modified views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 3
diff changeset
    22
            form.save()	   
0a2b4e85a4ab Modified views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 3
diff changeset
    23
        return render_to_response(template_name, {'form': ParticipantForm(), 'value': False,})
0a2b4e85a4ab Modified views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 3
diff changeset
    24
    c = Context()
1
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    25
    return render_to_response(template_name,
12
0a2b4e85a4ab Modified views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 3
diff changeset
    26
			  {'form': ParticipantForm(), 'value': True,})