1
|
1 |
#can add features like: supporting files with specified extensions, making edit box of content better etc.
|
|
2 |
from django.http import HttpResponse
|
|
3 |
from django.template import Context, Template
|
|
4 |
from django.shortcuts import render_to_response
|
|
5 |
from form import FileForm, Uploaded_fileForm
|
2
|
6 |
from forms import ParticipantForm
|
1
|
7 |
import tarfile
|
|
8 |
|
|
9 |
#function to read the upoaded file and store it
|
|
10 |
def handle_uploaded_file(f):
|
|
11 |
destination = open(f.name, 'wb+')
|
|
12 |
for chunk in f.chunks():
|
|
13 |
destination.write(chunk)
|
|
14 |
destination.close()
|
|
15 |
|
|
16 |
#view to handle uploaded file, showing content of file, and option of uploading a file
|
|
17 |
def upload_file(request,template_name='index.html'):
|
|
18 |
if request.method == 'POST':
|
|
19 |
form = FileForm(request.POST, request.FILES)
|
|
20 |
if form.is_valid():
|
|
21 |
#if else for checking the size of uploaded file
|
|
22 |
handle_uploaded_file(request.FILES['file'])
|
|
23 |
uploaded_form = Uploaded_fileForm(initial={'content': open(request.FILES['file'].name).read()})
|
|
24 |
c = Context({'form': uploaded_form,
|
|
25 |
'value': False,
|
|
26 |
})
|
|
27 |
#display a page with textbox and all the content of file
|
|
28 |
return render_to_response(template_name,
|
|
29 |
context_instance = c)
|
2
|
30 |
c = Context({'form': ParticipantForm(),
|
1
|
31 |
'value': True,
|
|
32 |
})
|
|
33 |
return render_to_response(template_name,
|
|
34 |
context_instance = c)
|