Added upload file handler.
authorShantanu <shantanu@fossee.in>
Wed, 02 Sep 2009 17:19:11 +0530
changeset 1 6d3b60546075
parent 0 5fb12cd6d64d
child 2 4d2bbb2f3c4e
Added upload file handler.
settings.py
upload/form.py
upload/views.py
--- a/settings.py	Wed Sep 02 16:59:27 2009 +0530
+++ b/settings.py	Wed Sep 02 17:19:11 2009 +0530
@@ -9,8 +9,8 @@
 
 MANAGERS = ADMINS
 
-DATABASE_ENGINE = ''           # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
-DATABASE_NAME = ''             # Or path to database file if using sqlite3.
+DATABASE_ENGINE = 'sqlite3'           # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
+DATABASE_NAME = 'user_record'             # Or path to database file if using sqlite3.
 DATABASE_USER = ''             # Not used with sqlite3.
 DATABASE_PASSWORD = ''         # Not used with sqlite3.
 DATABASE_HOST = ''             # Set to empty string for localhost. Not used with sqlite3.
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/upload/form.py	Wed Sep 02 17:19:11 2009 +0530
@@ -0,0 +1,12 @@
+from django import forms
+from django.utils.translation import ugettext_lazy as _
+
+attrs_dict = { 'class': 'required' }
+
+#form for file field, lets you upload file
+class FileForm(forms.Form):	
+	file = forms.FileField()
+
+#form for creating a editable box, out of the content of the file
+class Uploaded_fileForm(forms.Form):
+	content = forms.CharField(widget=forms.Textarea(attrs=attrs_dict),label=_(u'content'))
--- a/upload/views.py	Wed Sep 02 16:59:27 2009 +0530
+++ b/upload/views.py	Wed Sep 02 17:19:11 2009 +0530
@@ -1,1 +1,51 @@
-# Create your views here.
+#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
+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': FileForm(),
+		'value': True,
+		})
+    return render_to_response(template_name,
+			context_instance = c)
+
+#to create the dump of content shown in text box and then making it downloadable
+def file_archive(request):
+	if request.method == 'POST':
+		form = Uploaded_fileForm(request.POST, request.FILES)
+		if form.is_valid():
+			response = HttpResponse(mimetype='application/tar')
+    			response['Content-Disposition'] = 'attachment; filename=content.tar'
+			content = open('download.txt','w')
+			content.write(form.cleaned_data['content'])
+			content.close()
+			tar = tarfile.open('download.tar','w')
+			tar.add('download.txt')
+			tar.close()
+			response.write(open('download.tar').read())
+			return response
+	return HttpResponseRedirect('/ocr/')
+