Exemplo n.º 1
0
def uploadFile(request):
    """
    Uploads a submissions file to be viewed on the site
    """

    def postUploadUpdate(upload_instance):
        """
        Will set a mostRecent flag to true to determine a user's most recent submission for each task
        as well as set the uploadURL based on the automatically generated id
        """ 

        sameSubType = Upload.objects.filter(userID=upload_instance.userID, task=upload_instance.task)
        
        for sub in sameSubType: 
            if sub.mostRecent == True:
                sub.mostRecent = False
                sub.save()
        
        upload_instance.mostRecent = True

        upload_instance.uploadURL = '/'.join(['/tasks', str(upload_instance.task.id), str(upload_instance.id)])
        upload_instance.save()

        
    if request.method == 'POST':
        form = UploadForm(request.POST, request.FILES)
        
        if form.is_valid():
            uploadedFile = request.FILES['fileUpload']
            upload_instance = form.save(commit=False)  # Saves the form but don't commit it since some fields are still empty.
            upload_instance.userID = request.user  # Saves the current user's id to ForeignKey to allow sorting in Tasks view.
            upload_instance.task = Task.objects.get(
                    pk=request.POST['taskID'])  # Save the current task's ID to allow sorting in Tasks view
            userName = str(upload_instance.userID)
            fileName = upload_instance.task.fileName
            uploadContainer = uploadFileName(userName, upload_instance.task, datetime.now())

            try:  
                os.makedirs(os.path.join(settings.MEDIA_ROOT,'file_uploads', userName, uploadContainer))
            except:
                pass    
            
            default_storage.save(os.path.join(settings.MEDIA_ROOT,'file_uploads', userName, uploadContainer, fileName), uploadedFile)  # Save to disc   #changed        
            
            upload_instance.fileUpload = os.path.join(settings.MEDIA_ROOT,'file_uploads', userName, uploadContainer, fileName) # Save to server #changed

            upload_instance.save()
            postUploadUpdate(upload_instance) # post_save method would probably be more appropriate
            
            evaluate(upload_instance) 
            
            return HttpResponseRedirect('/tasks/' + str(upload_instance.task.id) + '/' + str(upload_instance.id))
        else: 
            return HttpResponseRedirect('/tasks/')  # If not valid, reloads task page - eventually may be preventable.
    else:
        form = Upload()
     
    return render_to_response('GameAndGrade_Base_StudentTasks.html', {'form': form})
Exemplo n.º 2
0
def newSub(request):
    """
    Sends you to a new blank submission form
    """

    if request.method == 'POST':
        form = UploadForm(request.POST)

        if form.is_valid():
            taskID = request.POST['taskID']

            return render_to_response('ViewSubmissions.html',
                             {'currSub': " ", 'evalSub': None, 'submission': None, 'taskID': request},
                             context_instance=RequestContext(request))