upload/views.py
author Madhusudan.C.S <madhusudancs@gmail.com>
Thu, 01 Oct 2009 14:17:44 +0530
changeset 28 56a978b7352e
parent 20 272dced1685b
permissions -rw-r--r--
Removed E-mail address field in the table.
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
18
07408d1ced76 Added CSS and I accept terms and condition.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 17
diff changeset
     5
2
4d2bbb2f3c4e Added models and views.
Shantanu <shantanu@fossee.in>
parents: 1
diff changeset
     6
from forms import ParticipantForm
18
07408d1ced76 Added CSS and I accept terms and condition.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 17
diff changeset
     7
from models import Participant
07408d1ced76 Added CSS and I accept terms and condition.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 17
diff changeset
     8
1
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
     9
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    10
#function to read the upoaded file and store it
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    11
def handle_uploaded_file(f):
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    12
    destination = open(f.name, 'wb+')
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    13
    for chunk in f.chunks():
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    14
        destination.write(chunk)
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    15
    destination.close()
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    16
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    17
#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
    18
def upload_file(request, template_name='index.html'):
1
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    19
    if request.method == 'POST':
12
0a2b4e85a4ab Modified views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 3
diff changeset
    20
        form = ParticipantForm(request.POST, request.FILES)		
1
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    21
        if form.is_valid():
12
0a2b4e85a4ab Modified views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 3
diff changeset
    22
            #handle_uploaded_file(request.FILES['file'])
0a2b4e85a4ab Modified views.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 3
diff changeset
    23
            form.save()	   
15
2dbb5e797989 Fixed views.py to accommodate form validation.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
    24
            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
    25
        else:
2dbb5e797989 Fixed views.py to accommodate form validation.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 12
diff changeset
    26
            return render_to_response(template_name, {'form': form, 'value': True,})
1
6d3b60546075 Added upload file handler.
Shantanu <shantanu@fossee.in>
parents: 0
diff changeset
    27
    return render_to_response(template_name,
18
07408d1ced76 Added CSS and I accept terms and condition.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 17
diff changeset
    28
			  {'form': ParticipantForm(), 'value': True})
17
08a47999f316 Added submissions page.
Shantanu <shantanu@fossee.in>
parents: 15
diff changeset
    29
			  
08a47999f316 Added submissions page.
Shantanu <shantanu@fossee.in>
parents: 15
diff changeset
    30
def submission(request, template_name='submission.html'):
18
07408d1ced76 Added CSS and I accept terms and condition.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 17
diff changeset
    31
    """View to return the submitted videos
07408d1ced76 Added CSS and I accept terms and condition.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 17
diff changeset
    32
    """
20
272dced1685b Table to view uploads.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 18
diff changeset
    33
    context_participants = []
272dced1685b Table to view uploads.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 18
diff changeset
    34
272dced1685b Table to view uploads.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 18
diff changeset
    35
    participants = Participant.objects.all()
272dced1685b Table to view uploads.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 18
diff changeset
    36
    for participant in participants:
272dced1685b Table to view uploads.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 18
diff changeset
    37
      context_participants.append({
272dced1685b Table to view uploads.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 18
diff changeset
    38
          'participant': participant,
272dced1685b Table to view uploads.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 18
diff changeset
    39
          'file_name': str(participant.filename).split('/')[-1],
272dced1685b Table to view uploads.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 18
diff changeset
    40
          })
272dced1685b Table to view uploads.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 18
diff changeset
    41
      
18
07408d1ced76 Added CSS and I accept terms and condition.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 17
diff changeset
    42
    context = {
20
272dced1685b Table to view uploads.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 18
diff changeset
    43
        'participants': context_participants,
18
07408d1ced76 Added CSS and I accept terms and condition.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 17
diff changeset
    44
        }
07408d1ced76 Added CSS and I accept terms and condition.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 17
diff changeset
    45
07408d1ced76 Added CSS and I accept terms and condition.
Madhusudan.C.S <madhusudancs@gmail.com>
parents: 17
diff changeset
    46
    return render_to_response(template_name, context)