work-in-flight for uploading/serving of static content for courses

This commit is contained in:
Chris Dodge
2012-09-26 15:24:21 -04:00
parent 702c26eafc
commit 85e6c23386
6 changed files with 113 additions and 7 deletions

View File

@@ -2,14 +2,16 @@ from util.json_request import expect_json
import json
import logging
import sys
import mimetypes
from collections import defaultdict
from django.http import HttpResponse, Http404
from django.http import HttpResponse, Http404, HttpResponseBadRequest, HttpResponseForbidden
from django.contrib.auth.decorators import login_required
from django.core.context_processors import csrf
from django_future.csrf import ensure_csrf_cookie
from django.core.urlresolvers import reverse
from django.conf import settings
from django import forms
from xmodule.modulestore import Location
from xmodule.x_module import ModuleSystem
@@ -26,6 +28,13 @@ from functools import partial
from itertools import groupby
from operator import attrgetter
from xmodule.contentstore.django import contentstore
from xmodule.contentstore import StaticContent
#from django.core.cache import cache
from cache_toolbox.core import set_cached_content, get_cached_content
log = logging.getLogger(__name__)
@@ -89,9 +98,19 @@ def course_index(request, org, course, name):
raise Http404 # TODO (vshnayder): better error
# TODO (cpennington): These need to be read in from the active user
course = modulestore().get_item(location)
weeks = course.get_children()
return render_to_response('course_index.html', {'weeks': weeks})
_course = modulestore().get_item(location)
weeks = _course.get_children()
upload_asset_callback_url = "/{org}/{course}/course/{name}/upload_asset".format(
org = org,
course = course,
name = name
)
return render_to_response('course_index.html', {
'weeks': weeks,
'upload_asset_callback_url': upload_asset_callback_url
})
@login_required
@@ -115,12 +134,13 @@ def edit_item(request):
lms_link = "{lms_base}/courses/{course_id}/jump_to/{location}".format(
lms_base=settings.LMS_BASE,
# TODO: These will need to be changed to point to the particular instance of this problem in the particular course
course_id=modulestore().get_containing_courses(item.location)[0].id,
course_id= modulestore().get_containing_courses(item.location)[0].id,
location=item.location,
)
else:
lms_link = None
return render_to_response('unit.html', {
'contents': item.get_html(),
'js_module': item.js_module_name,
@@ -390,3 +410,55 @@ def clone_item(request):
modulestore().update_children(parent_location, parent.definition.get('children', []) + [new_item.location.url()])
return HttpResponse()
'''
cdodge: this method allows for POST uploading of files into the course asset library, which will
be supported by GridFS in MongoDB.
'''
#@login_required
#@ensure_csrf_cookie
def upload_asset(request, org, course, coursename):
if request.method != 'POST':
# (cdodge) @todo: Is there a way to do a - say - 'raise Http400'?
return HttpResponseBadRequest()
# construct a location from the passed in path
location = ['i4x', org, course, 'course', coursename]
if not has_access(request.user, location):
return HttpResponseForbidden()
# Does the course actually exist?!?
try:
item = modulestore().get_item(location)
except:
# no return it as a Bad Request response
logging.error('Could not find course' + location)
return HttpResponseBadRequest()
# compute a 'filename' which is similar to the location formatting, we're using the 'filename'
# nomenclature since we're using a FileSystem paradigm here. We're just imposing
# the Location string formatting expectations to keep things a bit more consistent
name = request.FILES['file'].name
mime_type = request.FILES['file'].content_type
filedata = request.FILES['file'].read()
file_location = StaticContent.compute_location_filename(org, course, name)
content = StaticContent(file_location, name, mime_type, filedata)
# first commit to the DB
contentstore().update(content)
# then update the cache so we're not serving up stale content
set_cached_content(content)
return HttpResponse('Upload completed')
class UploadFileForm(forms.Form):
title = forms.CharField(max_length=50)
file = forms.FileField()