Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# -*- coding: utf-8 -*-
from __future__ import division
__copyright__ = "Copyright (C) 2014 Andreas Kloeckner"
__license__ = """
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
import django.forms as forms
from django.utils.safestring import mark_safe
from django.contrib import messages # noqa
from crispy_forms.layout import Submit
from courseflow.utils import StyledForm
from course.utils import course_view, render_course_page
# {{{ sandbox form
class SandboxForm(StyledForm):
def __init__(self, initial_text,
editor_mode, vim_mode, help_text, *args, **kwargs):
super(SandboxForm, self).__init__(*args, **kwargs)
from codemirror import CodeMirrorTextarea, CodeMirrorJavascript
self.fields["content"] = forms.CharField(
required=False,
initial=initial_text,
widget=CodeMirrorTextarea(
mode=editor_mode,
theme="default",
config={
"fixedGutter": True,
"autofocus": True,
"indentUnit": 2,
"vimMode": vim_mode,
"extraKeys": CodeMirrorJavascript("""
{
"Tab": function(cm)
{
var spaces = \
Array(cm.getOption("indentUnit") + 1).join(" ");
cm.replaceSelection(spaces);
}
}
""")
}),
help_text=mark_safe(
help_text + " Press Alt/Cmd+(Shift+)P to preview."))
self.fields["vim_mode"] = forms.BooleanField(
self.helper.add_input(
Submit(
"preview", "Preview",
css_class="col-lg-offset-2",
accesskey="p"))
# }}}
# {{{ markup sandbox
@course_view
def view_markup_sandbox(pctx):
request = pctx.request
preview_text = ""
def make_form(data=None):
help_text = ("Enter <a href=\"http://documen.tician.de/"
"courseflow/content.html#courseflow-markup\">"
"CourseFlow markup</a>.")
return SandboxForm(
None, "markdown", vim_mode,
help_text,
data)
vim_mode = pctx.request.session.get(CF_SANDBOX_VIM_MODE, False)
if request.method == "POST":
form = make_form(request.POST)
if form.is_valid():
pctx.request.session[CF_SANDBOX_VIM_MODE] = \
vim_mode = form.cleaned_data["vim_mode"]
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from course.content import markup_to_html
try:
preview_text = markup_to_html(
pctx.course, pctx.repo, pctx.course_commit_sha,
form.cleaned_data["content"])
except:
import sys
tp, e, _ = sys.exc_info()
messages.add_message(pctx.request, messages.ERROR,
"Markup failed to render: "
"%s: %s" % (tp.__name__, e))
form = make_form(request.POST)
else:
form = make_form()
return render_course_page(pctx, "course/sandbox-markup.html", {
"form": form,
"preview_text": preview_text,
})
# }}}
# {{{ page sandbox
@course_view
def view_page_sandbox(pctx):
from courseflow.utils import dict_to_struct
import yaml
SESSION_KEY = "cf_validate_sandbox_page"
request = pctx.request
page_source = pctx.request.session.get(SESSION_KEY)
is_preview_post = (request.method == "POST" and "preview" in request.POST)
def make_form(data=None):
return SandboxForm(
page_source, "yaml", vim_mode,
"Enter YAML markup for a flow page.",
data)
vim_mode = pctx.request.session.get(CF_SANDBOX_VIM_MODE, False)
if is_preview_post:
edit_form = make_form(pctx.request.POST)
if edit_form.is_valid():
pctx.request.session[CF_SANDBOX_VIM_MODE] = \
vim_mode = edit_form.cleaned_data["vim_mode"]
try:
new_page_source = edit_form.cleaned_data["content"]
page_desc = dict_to_struct(yaml.load(new_page_source))
from course.validation import validate_flow_page, ValidationContext
vctx = ValidationContext(
repo=pctx.repo,
commit_sha=pctx.course_commit_sha)
validate_flow_page(vctx, "sandbox", page_desc)
except:
import sys
tp, e, _ = sys.exc_info()
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
"Page failed to load/validate: "
"%s: %s" % (tp.__name__, e))
else:
# Yay, it did validate.
request.session[SESSION_KEY] = page_source = new_page_source
del new_page_source
edit_form = make_form(pctx.request.POST)
else:
edit_form = make_form()
have_valid_page = page_source is not None
if have_valid_page:
page_desc = dict_to_struct(yaml.load(page_source))
from course.content import instantiate_flow_page
page = instantiate_flow_page("sandbox", pctx.repo, page_desc,
pctx.course_commit_sha)
page_data = page.make_page_data()
from course.page import PageContext
page_context = PageContext(
course=pctx.course,
repo=pctx.repo,
commit_sha=pctx.course_commit_sha,
flow_session=None)
body = page.body(page_context, page_data)
feedback = None
answer_data = None
page_form_html = None
if page.expects_answer():
if request.method == "POST" and not is_preview_post:
page_form = page.post_form(page_context, page_data,
request.POST, request.FILES)
if page_form.is_valid():
answer_data = page.answer_data(page_context, page_data,
page_form, request.FILES)
feedback = page.grade(page_context, page_data, answer_data,
grade_data=None)
else:
page_form = page.make_form(page_context, page_data,
None, answer_is_final=False)
if page_form is not None:
page_form.helper.add_input(
Submit("submit", "Submit answer", accesskey="g",
css_class="col-lg-offset-2"))
page_form_html = page.form_to_html(
pctx.request, page_context, page_form, answer_data)
correct_answer = page.correct_answer(
page_context, page_data, answer_data,
grade_data=None)
return render_course_page(pctx, "course/sandbox-page.html", {
"edit_form": edit_form,
"form": edit_form, # to placate form.media
"have_valid_page": True,
"body": body,
"page_form_html": page_form_html,
"feedback": feedback,
"correct_answer": correct_answer,
})
else:
return render_course_page(pctx, "course/sandbox-page.html", {
"edit_form": edit_form,
"form": edit_form, # to placate form.media
"have_valid_page": False,
})
# }}}
# vim: foldmethod=marker