Commit 064274a7 by Bridger Maxwell

Most of the wiki pages are now loading without errors.

parent baa2c068
......@@ -11,7 +11,7 @@ from mitxmako.shortcuts import render_to_response
from multicourse import multicourse_settings
from models import Revision, Article, CreateArticleForm, RevisionFormWithTitle, RevisionForm
from models import Revision, Article, Namespace, CreateArticleForm, RevisionFormWithTitle, RevisionForm
import wiki_settings
def get_course(course_id):
......@@ -20,6 +20,16 @@ def get_course(course_id):
except KeyError:
raise Http404("Course not found")
return course
def wiki_article_reverse(wiki_page, article, course, args=[], kwargs={}):
return reverse(wiki_page, kwargs=dict({'course_id': course.id, 'article_path' : article.get_path()}, **kwargs), args=args)
def wiki_namespace_reverse(wiki_page, namespace = None, course=None, args=[], kwargs={}):
kwargs=dict({'course_id' : course.id}, **kwargs)
if namespace:
kwargs['namespace'] = namespace
return reverse(wiki_page, kwargs=kwargs, args=args)
def view(request, course_id, article_path):
course = get_course(course_id)
......@@ -37,6 +47,7 @@ def view(request, course_id, article_path):
'wiki_current_revision_deleted' : not (article.current_revision.deleted == 0),
'wiki_title' : article.title + " - edX %s Wiki" % course.title,
'course' : course,
'namespace' : article.namespace.name,
}
d.update(csrf(request))
return render_to_response('simplewiki/simplewiki_view.html', d)
......@@ -66,6 +77,7 @@ def view_revision(request, course_id, revision_number, article_path):
'wiki_attachments_write': article.can_attach(request.user),
'wiki_current_revision_deleted' : not (revision.deleted == 0),
'course' : course,
'namespace' : article.namespace.name,
}
d.update(csrf(request))
return render_to_response('simplewiki/simplewiki_view.html', d)
......@@ -82,80 +94,75 @@ def root_redirect(request, course_id):
return HttpResponseRedirect(reverse('wiki_view', kwargs={'course_id' : course_id, 'article_path' : root.get_path()} ))
def create(request, wiki_url):
url_path = get_url_path(wiki_url)
if url_path != [] and url_path[0].startswith('_'):
d = {'wiki_err_keyword': True,
'wiki_url': '/'.join(url_path) }
d.update(csrf(request))
return render_to_response('simplewiki/simplewiki_error.html', d)
def create(request, course_id, article_path):
course = get_course(course_id)
article_path_components = article_path.split('/')
# Lookup path
try:
# Ensure that the path exists...
root = Article.get_root()
# Remove root slug if present in path
if url_path and root.slug == url_path[0]:
url_path = url_path[1:]
# Ensure the namespace exists
if not len(article_path_components) >= 1 or len(article_path_components[0]) == 0:
d = {'wiki_err_no_namespace': True,
'course' : course}
d.update(csrf(request))
return render_to_response('simplewiki/simplewiki_error.html', d)
path = Article.get_url_reverse(url_path[:-1], root)
if not path:
d = {'wiki_err_noparent': True,
'wiki_url_parent': '/'.join(url_path[:-1]) }
d.update(csrf(request))
return render_to_response('simplewiki/simplewiki_error.html', d)
namespace = None
try:
namespace = Namespace.objects.get(name__exact = article_path_components[0])
except Namespace.DoesNotExist, ValueError:
d = {'wiki_err_bad_namespace': True,
'course' : course}
d.update(csrf(request))
return render_to_response('simplewiki/simplewiki_error.html', d)
perm_err = check_permissions(request, path[-1], check_locked=False, check_write=True, check_deleted=True)
if perm_err:
return perm_err
# Ensure doesn't already exist
article = Article.get_url_reverse(url_path, root)
if article:
return HttpResponseRedirect(reverse('wiki_view', args=(article[-1].get_url(),)))
# TODO: Somehow this doesnt work...
#except ShouldHaveExactlyOneRootSlug, (e):
except:
if Article.objects.filter(parent=None).count() > 0:
return HttpResponseRedirect(reverse('wiki_view', args=('/',)))
# Root not found...
path = []
url_path = [""]
# See if the article already exists
article_slug = article_path_components[1] if len(article_path_components) >= 2 else ''
#TODO: Make sure the slug only contains legal characters (which is already done a bit by the url regex)
try:
existing_article = Article.objects.get(namespace = namespace, slug__exact = article_slug)
#It already exists, so we just redirect to view the article
return HttpResponseRedirect(wiki_article_reverse("wiki_view", existing_article, course))
except Article.DoesNotExist:
#This is good. The article doesn't exist
pass
#TODO: Once we have permissions for namespaces, we should check for create permissions
#check_permissions(request, #namespace#, check_locked=False, check_write=True, check_deleted=True)
if request.method == 'POST':
f = CreateArticleForm(request.POST)
if f.is_valid():
article = Article()
article.slug = url_path[-1]
article.slug = article_slug
if not request.user.is_anonymous():
article.created_by = request.user
article.title = f.cleaned_data.get('title')
if path != []:
article.parent = path[-1]
article.namespace = namespace
a = article.save()
new_revision = f.save(commit=False)
if not request.user.is_anonymous():
new_revision.revision_user = request.user
new_revision.article = article
new_revision.save()
import django.db as db
return HttpResponseRedirect(reverse('wiki_view', args=(article.get_url(),)))
return HttpResponseRedirect(wiki_article_reverse("wiki_view", article, course))
else:
f = CreateArticleForm(initial={'title':request.GET.get('wiki_article_name', url_path[-1]),
f = CreateArticleForm(initial={'title':request.GET.get('wiki_article_name', article_slug),
'contents':_('Headline\n===\n\n')})
d = {'wiki_form': f,
'wiki_write': True,
'create_article' : True,
'course' : course,
}
d.update(csrf(request))
return render_to_response('simplewiki/simplewiki_edit.html', d)
def edit(request, course_id, slug, namespace = None):
(article, err) = get_article(request, slug, namespace if namespace else course_id )
def edit(request, course_id, article_path):
course = get_course(course_id)
(article, err) = get_article(request, article_path, course )
if err:
return err
......@@ -177,11 +184,11 @@ def edit(request, course_id, slug, namespace = None):
if request.POST.__contains__('delete'):
if (article.current_revision.deleted == 1): #This article has already been deleted. Redirect
return HttpResponseRedirect(reverse('wiki_view', args=(article.get_url(),)))
return HttpResponseRedirect(wiki_article_reverse('wiki_view', article, course))
new_revision.contents = ""
new_revision.deleted = 1
elif not new_revision.get_diff():
return HttpResponseRedirect(reverse('wiki_view', args=(article.get_url(),)))
return HttpResponseRedirect(wiki_article_reverse('wiki_view', article, course))
if not request.user.is_anonymous():
new_revision.revision_user = request.user
......@@ -189,7 +196,7 @@ def edit(request, course_id, slug, namespace = None):
if wiki_settings.WIKI_ALLOW_TITLE_EDIT:
new_revision.article.title = f.cleaned_data['title']
new_revision.article.save()
return HttpResponseRedirect(reverse('wiki_view', args=(article.get_url(),)))
return HttpResponseRedirect(wiki_article_reverse('wiki_view', article, course))
else:
startContents = article.current_revision.contents if (article.current_revision.deleted == 0) else 'Headline\n===\n\n'
......@@ -200,6 +207,8 @@ def edit(request, course_id, slug, namespace = None):
'wiki_title' : article.title,
'wiki_attachments_write': article.can_attach(request.user),
'create_article' : False,
'course' : course,
'namespace' : article.namespace.name,
}
d.update(csrf(request))
......@@ -213,7 +222,6 @@ def history(request, course_id, article_path, page=1):
perm_err = check_permissions(request, article, check_read=True, check_deleted=False)
if perm_err:
print "returned error " , perm_err
return perm_err
page_size = 10
......@@ -254,9 +262,7 @@ def history(request, course_id, article_path, page=1):
elif request.POST.__contains__('delete_all') and request.user.is_superuser:
Revision.objects.filter(article__exact = article, deleted = 0).update(deleted = 2)
elif request.POST.__contains__('lock_article'):
print "changing locked article " , article.locked
article.locked = not article.locked
print "changed locked article " , article.locked
article.save()
except Exception as e:
print str(e)
......@@ -289,7 +295,8 @@ def history(request, course_id, article_path, page=1):
'wiki_title': article.title,
'wiki_history': history[beginItem:beginItem+page_size],
'show_delete_revision' : request.user.is_superuser,
'course' : course}
'course' : course,
'namespace' : article.namespace.name}
d.update(csrf(request))
return render_to_response('simplewiki/simplewiki_history.html', d)
......@@ -322,21 +329,24 @@ def revision_feed(request, page=1):
return render_to_response('simplewiki/simplewiki_revision_feed.html', d)
def search_articles(request, course_id):
def search_articles(request, course_id, namespace=None):
# blampe: We should check for the presence of other popular django search
# apps and use those if possible. Only fall back on this as a last resort.
# Adding some context to results (eg where matches were) would also be nice.
# todo: maybe do some perm checking here
if request.method == 'POST':
querystring = request.POST['value'].strip()
if request.method == 'GET':
querystring = request.GET.get('value', '').strip()
else:
querystring = ""
results = Article.objects.filter(namespace__exact = course_id)
course = get_course(course_id)
results = Article.objects.all()
if namespace:
results = results.filter(namespace__name__exact = namespace)
if request.user.is_superuser:
results = results.order_by('current_revision__deleted')
else:
......@@ -355,15 +365,17 @@ def search_articles(request, course_id):
results = results._search(Q(current_revision__contents__icontains = queryword) | \
Q(title__icontains = queryword))
results = results.select_related('current_revision__deleted')
results = results.select_related('current_revision__deleted','namespace')
results = sorted(results, key=lambda article: (article.current_revision.deleted, article.get_url().lower()) )
results = sorted(results, key=lambda article: (article.current_revision.deleted, article.get_path().lower()) )
if len(results) == 1 and querystring:
return HttpResponseRedirect(reverse('wiki_view', args=(results[0].get_url(),)))
return HttpResponseRedirect(wiki_article_reverse('wiki_view', article=results[0], course=course ))
else:
d = {'wiki_search_results': results,
'wiki_search_query': querystring,}
'wiki_search_query': querystring,
'course' : course,
'namespace' : namespace}
d.update(csrf(request))
return render_to_response('simplewiki/simplewiki_searchresults.html', d)
......@@ -440,14 +452,16 @@ def remove_related(request, course_id, namespace, slug, related_id):
return HttpResponseRedirect(reverse('wiki_view', args=(article.get_url(),)))
def random_article(request, course_id):
course = get_course(course_id)
from random import randint
num_arts = Article.objects.count()
article = Article.objects.all()[randint(0, num_arts-1)]
return HttpResponseRedirect(reverse('wiki_view', args=(article.get_url(),)))
return HttpResponseRedirect( wiki_article_reverse('wiki_view', article, course))
def encode_err(request, url, course):
d = {'wiki_err_encode': True,
'course' : course}
'course' : course,
'namespace' : course.wiki_namespace}
d.update(csrf(request))
return render_to_response('simplewiki/simplewiki_error.html', d)
......@@ -455,7 +469,8 @@ def not_found(request, wiki_url, course):
"""Generate a NOT FOUND message for some URL"""
d = {'wiki_err_notfound': True,
'wiki_url': wiki_url,
'course' : course}
'course' : course,
'namespace' : course.wiki_namespace}
d.update(csrf(request))
return render_to_response('simplewiki/simplewiki_error.html', d)
......
......@@ -3,7 +3,7 @@ var schematic_width = 640;
$(function(){
$(document).ready(function() {
$("a[rel*=leanModal]").leanModal();
//$("a[rel*=leanModal]").leanModal(); //TODO: Make this work with the new modal library. Try and integrate this with the "slices"
$("body").append('<div id="circuit_editor" class="leanModal_box" style="z-index: 11000; left: 50%; margin-left: -250px; position: absolute; top: 100px; opacity: 1; "><div align="center">'+
'<input class="schematic" height="' + schematic_height + '" width="' + schematic_width + '" id="schematic_editor" name="schematic" type="hidden" value=""/>' +
......@@ -88,15 +88,20 @@ CodeMirror.defineMode("mitx_markdown", function(cmCfg, modeCfg) {
return {width: schematic_width, height:schematic_height};
},
callback: function(node, line) {
update_schematics();
var schmInput = node.firstChild.firstChild;
schmInput.codeMirrorLine = line;
if (schmInput.schematic) { //This is undefined if there was an error making the schematic
schmInput.schematic.canvas.style.display = "block"; //Otherwise, it gets line height and is a weird size
schmInput.schematic.always_draw_grid = true;
schmInput.schematic.redraw_background();
try {
update_schematics();
var schmInput = node.firstChild.firstChild;
schmInput.codeMirrorLine = line;
if (schmInput.schematic) { //This is undefined if there was an error making the schematic
schmInput.schematic.canvas.style.display = "block"; //Otherwise, it gets line height and is a weird size
schmInput.schematic.always_draw_grid = true;
schmInput.schematic.redraw_background();
}
$(node.firstChild).leanModal();
} catch (err) {
console.log("Error in mitx_markdown callback: " + err);
}
$(node.firstChild).leanModal();
}
};
......
......@@ -5,14 +5,11 @@
<%!
from django.core.urlresolvers import reverse
from simplewiki.views import wiki_article_reverse, wiki_namespace_reverse
%>
## A convenience function that calls reverse with the kwargs course_id, namespace, and slug
<%def name="wiki_reverse(wiki_page, args=[], kwargs={})">
${reverse(wiki_page, kwargs=dict({'course_id': course.id, 'article_path' : wiki_article.get_path()}, **kwargs), args=args) }
</%def>
<%block name="headextra">
<script type="text/javascript" src="${static.url('js/simplewiki/bsn.AutoSuggest_c_2.0.js')}"></script>
<script type="text/javascript" src="${static.url('js/simplewiki-AutoSuggest_c_2.0.js')}"></script>
<script type="text/javascript">
function set_related_article_id(s) {
......@@ -23,7 +20,7 @@
var x = window.onload;
window.onload = function(){
var options = {
script: "${ wiki_reverse('search_related')}/?self=${wiki_article.pk}&",
script: "${ wiki_article_reverse('search_related', wiki_article, course)}/?self=${wiki_article.pk}&",
json: true,
varname: "query",
maxresults: 35,
......@@ -83,7 +80,7 @@
<ul class="action">
<li>
<h3>
<a href="${reverse("wiki_list_articles", args=[course.id])}">All Articles</a>
<a href="${wiki_namespace_reverse("wiki_list_articles", namespace, course)}">All Articles</a>
</h3>
</li>
......@@ -93,32 +90,26 @@
</h3>
<div id="wiki_create_form">
<form method="GET" action="${reverse("wiki_create", args=[course.id])}">
<%
baseURL = reverse("wiki_create", kwargs={"course_id" : course.id, "article_path" : course.wiki_namespace + "/" })
%>
<form method="GET" onsubmit="this.action='${baseURL}' + this.wiki_article_name.value.replace(/([^a-zA-Z0-9\-])/g, '');">
<div>
<label for="id_wiki_article_name">Title of article</label>
<input type="text" name="wiki_article_name" id="id_wiki_article_name" /><br/>
<!-- <label for="id_wiki_article_is_child">Create as a child of current article</label> -->
<!-- <input type="checkbox" name="wiki_article_is_child" id="id_wiki_artcile_is_child" disabled="true" ${ 'checked="checked"' if wiki_article is not UNDEFINED else ""}> -->
</div>
<ul>
<li>
<input type="submit" class="button" value="Create" style="display: inline-block; margin-right: 2px; font-weight: bold;" />
</li>
</ul>
</form>
</div>
</li>
<li class="search">
<form method="POST" action='${reverse("wiki_search_articles", args=[course.id])}'>
<form method="GET" action='${wiki_namespace_reverse("wiki_search_articles", namespace, course)}'>
<label class="wiki_box_title">Search</label>
<div style="display:none">
<input type="hidden" name="csrfmiddlewaretoken" value="${csrf_token}"/>
</div>
<input type="text" placeholder="Search" name="value" id="wiki_search_input" style="width: 71%" value="${wiki_search_query if wiki_search_query is not UNDEFINED else '' |h}"/>
<input type="submit" id="wiki_search_input_submit" value="Go!" style="width: 20%" />
</form>
......@@ -141,15 +132,15 @@
<ul>
<li>
<a href="${ wiki_reverse('wiki_view')}" class="view">View</a>
<a href="${ wiki_article_reverse('wiki_view', wiki_article, course)}" class="view">View</a>
</li>
<li>
<a href="${ wiki_reverse('wiki_edit')}" class="edit">Edit</a>
<a href="${ wiki_article_reverse('wiki_edit', wiki_article, course)}" class="edit">Edit</a>
</li>
<li>
<a href="${ wiki_reverse('wiki_history')}" class="history">History</a>
<a href="${ wiki_article_reverse('wiki_history', wiki_article, course)}" class="history">History</a>
</li>
</ul>
</header>
......
......@@ -20,11 +20,11 @@ ${"Edit " + wiki_title + " - " if wiki_title is not UNDEFINED else ""}MITx 6.002
</%block>
<%block name="wiki_head">
<script type="text/javascript" src="${ settings.LIB_URL }CodeMirror/codemirror.js"></script>
<link rel="stylesheet" href="${ settings.LIB_URL }CodeMirror/codemirror.css" />
<script type="text/javascript" src="${ settings.LIB_URL }vendor/CodeMirror/codemirror.js"></script>
<link rel="stylesheet" href="${ settings.LIB_URL }vendor/CodeMirror/codemirror.css" />
<script type="text/javascript" src="${ settings.LIB_URL }CodeMirror/xml.js"></script>
<script type="text/javascript" src="${ settings.LIB_URL }CodeMirror/mitx_markdown.js"></script>
<script type="text/javascript" src="${ settings.LIB_URL }vendor/CodeMirror/xml.js"></script>
<script type="text/javascript" src="${ settings.LIB_URL }vendor/CodeMirror/mitx_markdown.js"></script>
<script>
$(function(){
......
......@@ -41,10 +41,13 @@ ${wiki_error}
does not exist. Click <a href="${reverse("wiki_create", args=[wiki_url_parent])}">here</a>
to create it.
</p>
%else:
%if wiki_err_keyword is not UNDEFINED and wiki_err_keyword:
%elif wiki_err_no_namespace is not UNDEFINED and wiki_err_no_namespace:
<p>
You must specify a namespace to create an article in.
</p>
%elif wiki_err_bad_namespace is not UNDEFINED and wiki_err_bad_namespace:
<p>
The page you're trying to create <b>${wiki_url}</b> starts with <b>_</b>, which is reserved for internal use.
The namespace for this article does not exist. This article cannot be created.
</p>
%elif wiki_err_locked is not UNDEFINED and wiki_err_locked:
<p>
......@@ -87,7 +90,6 @@ ${wiki_error}
</p>
%endif
%endif
%endif
</div>
</%block>
......
......@@ -6,6 +6,7 @@
<%!
from django.core.urlresolvers import reverse
from simplewiki.views import wiki_article_reverse
%>
<%block name="wiki_page_title">
......@@ -64,10 +65,10 @@ ${ wiki_article.title }
<tr>
<td colspan="4">
%if wiki_prev_page:
<a href="${self.wiki_reverse('wiki_history', kwargs={'page' : wiki_prev_page})}">Previous page</a>
<a href="${wiki_article_reverse('wiki_history', wiki_article, course, kwargs={'page' : wiki_prev_page})}">Previous page</a>
%endif
%if wiki_next_page:
<a href="${self.wiki_reverse('wiki_history', kwargs={'page' : wiki_next_page})}">Next page</a>
<a href="${wiki_article_reverse('wiki_history', wiki_article, course, kwargs={'page' : wiki_next_page})}">Next page</a>
%endif
</td>
</tr>
......
......@@ -5,7 +5,7 @@
<%block name="title"><title>Wiki - Search Results - MITx 6.002x</title></%block>
<%!
from django.core.urlresolvers import reverse
from simplewiki.views import wiki_article_reverse
%>
<%block name="wiki_page_title">
......@@ -23,7 +23,7 @@ Displaying all articles
<ul class="article-list">
%for article in wiki_search_results:
<% article_deleted = not article.current_revision.deleted == 0 %>
<li><h3><a href="${reverse("wiki_view", args=[article.get_url()])}">${article.title} ${'(Deleted)' if article_deleted else ''}</a></h3></li>
<li><h3><a href="${wiki_article_reverse("wiki_view", article, course)}">${article.title} ${'(Deleted)' if article_deleted else ''}</a></h3></li>
%endfor
%if not wiki_search_results:
......
......@@ -85,19 +85,21 @@ if settings.COURSEWARE_ENABLED:
)
# Multicourse wiki
article_slug = r"/(?P<article_path>[a-zA-Z\d_-]+/[a-zA-Z\d_-]*)"
namespace_regex = r"[a-zA-Z\d_-]+"
article_slug = r'/(?P<article_path>' + namespace_regex + r'/[a-zA-Z\d_-]*)'
namespace = r'/(?P<namespace>' + namespace_regex + r')'
urlpatterns += (
url(r'^courses/(?P<course_id>[^/]*)/wiki/$', 'simplewiki.views.root_redirect', name='wiki_root'),
url(r'^courses/(?P<course_id>[^/]*)/wiki/view' + article_slug + r'/?$', 'simplewiki.views.view', name='wiki_view'),
url(r'^courses/(?P<course_id>[^/]*)/wiki/view_revision/(?P<revision_number>[0-9]+)' + article_slug + r'/?$', 'simplewiki.views.view_revision', name='wiki_view_revision'),
url(r'^courses/(?P<course_id>[^/]*)/wiki/edit' + article_slug + r'/?$', 'simplewiki.views.edit', name='wiki_edit'),
url(r'^courses/(?P<course_id>[^/]*)/wiki/create/?$', 'simplewiki.views.create', name='wiki_create'),
url(r'^courses/(?P<course_id>[^/]*)/wiki/create' + article_slug + r'/?$', 'simplewiki.views.create', name='wiki_create'),
url(r'^courses/(?P<course_id>[^/]*)/wiki/history' + article_slug + r'(?:/(?P<page>[0-9]+))?/?$', 'simplewiki.views.history', name='wiki_history'),
url(r'^courses/(?P<course_id>[^/]*)/wiki/search_related' + article_slug + r'/?$', 'simplewiki.views.search_add_related', name='search_related'),
url(r'^courses/(?P<course_id>[^/]*)/wiki/random/?$', 'simplewiki.views.random_article', name='wiki_random'),
url(r'^courses/(?P<course_id>[^/]*)/wiki/revision_feed/([0-9]+)/?$', 'simplewiki.views.revision_feed', name='wiki_revision_feed'),
url(r'^courses/(?P<course_id>[^/]*)/wiki/search/?$', 'simplewiki.views.search_articles', name='wiki_search_articles'),
url(r'^courses/(?P<course_id>[^/]*)/wiki/list/?$', 'simplewiki.views.search_articles', name='wiki_list_articles'), #Just an alias for the search, but you usually don't submit a search term
url(r'^courses/(?P<course_id>[^/]*)/wiki/search' + namespace + r'?/?$', 'simplewiki.views.search_articles', name='wiki_search_articles'),
url(r'^courses/(?P<course_id>[^/]*)/wiki/list' + namespace + r'?/?$', 'simplewiki.views.search_articles', name='wiki_list_articles'), #Just an alias for the search, but you usually don't submit a search term
)
......
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