Skip to content

Commit

Permalink
DigitalCampus#227: Added validation error messages to the response (p…
Browse files Browse the repository at this point in the history
…ending: show this messages in Moodle)
  • Loading branch information
jjoseba committed Feb 26, 2016
1 parent 0a970ee commit 73560ae
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions oppia/api/publish.py
@@ -1,40 +1,49 @@
# This is a workaround since Tastypie doesn't accept file Uploads
import os
import math

import os
from django.conf import settings
from django.contrib.auth import authenticate
from django.http import HttpResponseRedirect, Http404, HttpResponse
from django.shortcuts import render,render_to_response
from django.template import RequestContext
from django.http import HttpResponse, JsonResponse
from django.utils.translation import ugettext_lazy as _
from django.views.decorators.csrf import csrf_exempt

from oppia.uploader import handle_uploaded_file
from oppia.models import Tag, CourseTag
from django.contrib import messages
from oppia.uploader import handle_uploaded_file

COURSE_FILE_FIELD = 'course_file'

@csrf_exempt
def publish_view(request):

if request.method != 'POST':
return HttpResponse(status=405)

required = ['username','password','tags','is_draft']

for r in required:
if r not in request.POST:
print r + " not found"
return HttpResponse(status=400)


if 'course_file' not in request.FILES:

validationErrors = []

for field in required:
if field not in request.POST:
print field + " not found"
validationErrors.append("field '{0}' missing".format(field))


if COURSE_FILE_FIELD not in request.FILES:
print "Course file not found"
return HttpResponse(status=400)
validationErrors.append("file '{0}' missing".format(COURSE_FILE_FIELD))
else:
file = request.FILES['course_file']
# check the file size of the course doesnt exceed the max
file = request.FILES[COURSE_FILE_FIELD]
if file is not None and file._size > settings.OPPIA_MAX_UPLOAD_SIZE:
return HttpResponse(status=400)
size = int(math.floor(settings.OPPIA_MAX_UPLOAD_SIZE / 1024 / 1024))
validationErrors.append((_("Your file is larger than the maximum allowed (%(size)d Mb). You may want to check your course for large includes, such as images etc.") % {'size':size, }))

if file is not None and file.content_type != 'application/zip' and file.content_type != 'application/x-zip-compressed':
return HttpResponse(status=400)
validationErrors.append(_("You may only upload a zip file"))

if validationErrors:
return JsonResponse({ 'errors' : validationErrors }, status=400, )

# authenticate user
username = request.POST['username']
Expand Down

0 comments on commit 73560ae

Please sign in to comment.