Commit 5a2b2e01 by Calen Pennington

Add an index page that lists all courses

parent a5d861d2
......@@ -3,12 +3,27 @@ import json
from django.http import HttpResponse
from django_future.csrf import ensure_csrf_cookie
from fs.osfs import OSFS
from django.core.urlresolvers import reverse
from mitxmako.shortcuts import render_to_response
from xmodule.modulestore.django import modulestore
@ensure_csrf_cookie
def index(request):
courses = modulestore().get_items(['i4x', None, None, 'course', None])
print courses
return render_to_response('index.html', {
'courses': [(course.metadata['display_name'],
reverse('course_index', args=[
course.location.org,
course.location.course,
course.location.name]))
for course in courses]
})
@ensure_csrf_cookie
def course_index(request, org, course, name):
# TODO (cpennington): These need to be read in from the active user
course = modulestore().get_item(['i4x', org, course, 'course', name])
......
<%inherit file="base.html" />
<%block name="title">Courses</%block>
<%block name="content">
<section class="main-container">
<ol>
%for course, url in courses:
<li><a href="${url}">${course}</a></li>
%endfor
</ol>
</section>
</%block>
......@@ -153,6 +153,18 @@ class ModuleStore(object):
location is found
"""
raise NotImplementedError
def get_items(self, location, default_class=None):
"""
Returns a list of XModuleDescriptor instances for the items
that match location. Any element of location that is None is treated
as a wildcard that matches any value
location: Something that can be passed to Location
default_class: An XModuleDescriptor subclass to use if no plugin matching the
location is found
"""
raise NotImplementedError
# TODO (cpennington): Replace with clone_item
def create_item(self, location, editor):
......
......@@ -58,6 +58,29 @@ class MongoModuleStore(ModuleStore):
return XModuleDescriptor.load_from_json(
item, MakoDescriptorSystem(load_item=self.get_item, resources_fs=None, render_template=render_to_string), self.default_class)
def get_items(self, location, default_class=None):
query = {}
for key, val in Location(location).dict().iteritems():
if val is not None:
query['location.{key}'.format(key=key)] = val
items = self.collection.find(
query,
sort=[('revision', pymongo.ASCENDING)],
)
# TODO (cpennington): Pass a proper resources_fs to the system
system = MakoDescriptorSystem(
load_item=self.get_item,
resources_fs=None,
render_template=render_to_string
)
return [
XModuleDescriptor.load_from_json(item, system, self.default_class)
for item in items
]
def create_item(self, location):
"""
Create an empty item at the specified location with the supplied editor
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment