upload/views.py
author Madhusudan.C.S <madhusudancs@gmail.com>
Thu, 03 Sep 2009 00:17:30 +0530
changeset 16 a5bbfbe5feb1
parent 15 2dbb5e797989
child 17 08a47999f316
permissions -rw-r--r--
Fixed the HTML title.
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()	   
15
2dbb5e797989 Fixed views.py to accommodate form validation.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
    23
            return render_to_response(template_name, {'form': form, 'value': False,})
2dbb5e797989 Fixed views.py to accommodate form validation.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
    24
        else:
2dbb5e797989 Fixed views.py to accommodate form validation.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
    25
            return render_to_response(template_name, {'form': form, 'value': True,})
12
0a2b4e85a4ab Modified views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 3
diff changeset
    26
    c = Context()
1
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    27
    return render_to_response(template_name,
12
0a2b4e85a4ab Modified views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 3
diff changeset
    28
			  {'form': ParticipantForm(), 'value': True,})