Newer
Older
# -*- 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.
"""
from django.shortcuts import ( # noqa
render, get_object_or_404, redirect)
from django.core.exceptions import PermissionDenied, SuspiciousOperation
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.contrib.auth.forms import \
AuthenticationForm as AuthenticationFormBase
from django.core.urlresolvers import reverse
UserStatus, user_status,
Course, course_validation_state, Participation,
participation_role, participation_status)
get_course_repo, get_course_desc, parse_date_spec,
def get_role_and_participation(request, course):
# "wake up" lazy object
# http://stackoverflow.com/questions/20534577/int-argument-must-be-a-string-or-a-number-not-simplelazyobject # noqa
user = (request.user._wrapped
if hasattr(request.user, '_wrapped')
else request.user)
if not user.is_authenticated():
return participation_role.unenrolled, None
participations = list(Participation.objects.filter(
user=user, course=course))
# The uniqueness constraint should have ensured that.
assert len(participations) <= 1
participation = participations[0]
if participation.status != participation_status.active:
if participation.temporary_role:
return participation.temporary_role, participation
else:
return participation.role, participation
def get_active_commit_sha(course, participation):
sha = course.active_git_commit_sha
if participation is not None and participation.preview_git_commit_sha:
sha = participation.preview_git_commit_sha
return sha.encode()
def get_flow_permissions(course_desc, participation, role, flow_id, flow_desc):
for exc in (
FlowAccessException.objects
.filter(participation=participation, flow_id=flow_id)
.order_by("expiration")):
if exc.expiration is not None and exc.expiration < now:
continue
stipulations = exc.stipulations
if not isinstance(stipulations, dict):
stipulations = {}
from course.content import dict_to_struct
stipulations = dict_to_struct(exc.stipulations)
return (
[entry.permission for entry in exc.entries.all()],
stipulations
)
# }}}
# {{{ interpret flow rules
if hasattr(rule, "roles"):
if role not in rule.roles:
continue
if hasattr(rule, "start"):
start_date = parse_date_spec(course_desc, rule.start)
if now < start_date:
continue
if hasattr(rule, "end"):
end_date = parse_date_spec(course_desc, rule.end)
if end_date < now:
continue
raise ValueError("Flow access rules of flow '%s' did not resolve "
"to access answer for '%s'" % (flow_id, participation))
courses_and_descs_and_invalid_flags = []
repo = get_course_repo(course)
desc = get_course_desc(repo, course.active_git_commit_sha.encode())
role, participation = get_role_and_participation(request, course)
show = True
if course.hidden:
if role not in [participation_role.teaching_assistant,
participation_role.instructor]:
show = False
if course.validation_state != course_validation_state.valid:
if role != participation_role.instructor:
show = False
if show:
courses_and_descs_and_invalid_flags.append(
(course, desc,
course.validation_state != course_validation_state.valid))
courses_and_descs_and_invalid_flags.sort(key=course_sort_key)
return render(request, "course/home.html", {
"courses_and_descs_and_invalid_flags": courses_and_descs_and_invalid_flags
180
181
182
183
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
# {{{ conventional login
class LoginForm(AuthenticationFormBase):
def __init__(self, *args, **kwargs):
self.helper = FormHelper()
self.helper.form_tag = False
self.helper.label_class = "col-lg-2"
self.helper.field_class = "col-lg-8"
self.helper.field_class = "col-lg-8"
self.helper.add_input(Submit("submit", "Sign in",
css_class="col-lg-offset-2"))
super(LoginForm, self).__init__(*args, **kwargs)
def sign_in(request):
from django.contrib.auth.views import login
return login(request, template_name="course/login.html",
authentication_form=LoginForm)
# }}}
# {{{ email sign-in flow
class SignInByEmailForm(forms.Form):
email = forms.EmailField(required=True)
def __init__(self, *args, **kwargs):
self.helper = FormHelper()
self.helper.form_class = "form-horizontal"
self.helper.label_class = "col-lg-2"
self.helper.field_class = "col-lg-8"
self.helper.add_input(
Submit("submit", "Send sign-in email",
css_class="col-lg-offset-2"))
super(SignInByEmailForm, self).__init__(*args, **kwargs)
def make_sign_in_key(user):
# try to ensure these hashes aren't guessable.
import random
import hashlib
from time import time
m = hashlib.sha1()
m.update(user.email)
m.update(hex(random.getrandbits(128)))
m.update(str(time()))
return m.hexdigest()
def sign_in_by_email(request):
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
if request.method == 'POST':
form = SignInByEmailForm(request.POST)
if form.is_valid():
from django.contrib.auth.models import User
email = form.cleaned_data["email"]
users = User.objects.filter(email=email)
if users.count() > 1:
messages.add_message(request, messages.ERROR,
"More than one user with this email. "
"Please contact site staff.")
raise PermissionDenied("duplicate email")
if users.count() == 0:
user = User()
user.username = email
user.email = email
user.set_unusable_password()
user.save()
ustatus = UserStatus()
ustatus.user = user
ustatus.status = user_status.requested
ustatus.sign_in_key = make_sign_in_key(user)
ustatus.save()
messages.add_message(request, messages.INFO,
"Please check your email and click the link.")
elif users.count() == 1:
user, = users
ustatus = user.user_status
ustatus.user = user
ustatus.sign_in_key = make_sign_in_key(user)
ustatus.save()
from django.template.loader import render_to_string
message = render_to_string("course/sign-in-email.txt", {
"user": user,
"sign_in_uri": request.build_absolute_uri(
reverse(
"course.views.sign_in_link",
args=(ustatus.sign_in_key,))),
"home_uri": request.build_absolute_uri(reverse("course.views.home"))
})
from django.core.mail import send_mail
send_mail("Your CourseFlow sign-in link", message,
settings.ROBOT_EMAIL_FROM, recipient_list=[email])
return redirect("course.views.home")
else:
form = SignInByEmailForm()
return render(request, "course/login-by-email.html", {
"form_description": "",
"form": form
})
def sign_in_link(request, sign_in_key):
pass
# }}}
@login_required
def enroll(request, course_identifier):
# FIXME
raise NotImplementedError()
def check_course_state(course, role):
if course.hidden:
if role not in [participation_role.teaching_assistant,
participation_role.instructor]:
raise PermissionDenied("only course staff have access")
elif course.validation_state != course_validation_state.valid:
if role != participation_role.instructor:
raise PermissionDenied("only the instructor has access")
def course_page(request, course_identifier):
course = get_object_or_404(Course, identifier=course_identifier)
role, participation = get_role_and_participation(request, course)
commit_sha = get_active_commit_sha(course, participation)
repo = get_course_repo(course)
course_desc = get_course_desc(repo, commit_sha)
from course.content import get_processed_course_chunks
chunks = get_processed_course_chunks(course, course_desc,
role)
return render(request, "course/course-page.html", {
"course": course,
"course_desc": course_desc,
"course_validation_state": course_validation_state,
def start_flow(request, course_identifier, flow_identifier):
course = get_object_or_404(Course, identifier=course_identifier)
role, participation = get_role_and_participation(request, course)
commit_sha = get_active_commit_sha(course, participation)
repo = get_course_repo(course)
course_desc = get_course_desc(repo, commit_sha)
course = get_object_or_404(Course, identifier=course_identifier)
flow_desc = get_flow_desc(repo, course, flow_identifier, commit_sha)
permissions, stipulations = get_flow_permissions(
course_desc, participation, role, flow_identifier, flow_desc)
from course.models import flow_permission
if flow_permission.view not in permissions:
raise PermissionDenied()
if request.method == "POST":
from course.content import set_up_flow_visit_page_data
if ("start_no_credit" in request.POST
or "start_credit" in request.POST):
visit = FlowVisit()
visit.participation = participation
visit.active_git_commit_sha = commit_sha.decode()
visit.flow_id = flow_identifier
visit.in_progress = True
visit.for_credit = "start_credit" in request.POST
visit.save()
request.session["flow_visit_id"] = visit.id
page_count = set_up_flow_visit_page_data(repo, visit,
flow_desc, commit_sha)
visit.page_count = page_count
return redirect("course.views.view_flow_page",
course_identifier,
flow_identifier,
0)
else:
raise SuspiciousOperation("unrecognized POST action")
can_start_credit = flow_permission.start_credit in permissions
can_start_no_credit = flow_permission.start_no_credit in permissions
# FIXME take into account max attempts
# FIXME resumption
# FIXME view past
return render(request, "course/flow-start.html", {
"participation": participation,
"course_desc": course_desc,
"course": course,
"flow_identifier": flow_identifier,
"can_start_credit": can_start_credit,
"can_start_no_credit": can_start_no_credit,
})
class FlowPageContext(object):
"""This object acts as a container for all the information that a flow page
may need to render itself or respond to a POST.
"""
def __init__(self, request, course_identifier, flow_identifier,
ordinal, flow_visit):
self.flow_visit = flow_visit
self.course_identifier = course_identifier
self.flow_identifier = flow_identifier
self.course = get_object_or_404(Course, identifier=course_identifier)
self.role, self.participation = get_role_and_participation(
request, self.course)
check_course_state(self.course, self.role)
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
self.commit_sha = self.flow_visit.active_git_commit_sha.encode()
self.repo = get_course_repo(self.course)
self.course_desc = get_course_desc(self.repo, self.commit_sha)
self.flow_desc = get_flow_desc(self.repo, self.course,
flow_identifier, self.commit_sha)
self.permissions, self.stipulations = get_flow_permissions(
self.course_desc, self.participation, self.role,
flow_identifier, self.flow_desc)
from course.models import FlowPageData
page_data = self.page_data = get_object_or_404(
FlowPageData, flow_visit=flow_visit, ordinal=ordinal)
from course.content import get_flow_page_desc
self.page_desc = get_flow_page_desc(
flow_visit, self.flow_desc, page_data.group_id, page_data.page_id)
from course.content import instantiate_flow_page
self.page = instantiate_flow_page(
"course '%s', flow '%s', page '%s/%s'"
% (course_identifier, flow_identifier,
page_data.group_id, page_data.page_id),
self.repo, self.page_desc, self.commit_sha)
from course.page import PageContext
self.page_context = PageContext(
course=self.course,
ordinal=page_data.ordinal,
page_count=flow_visit.page_count,
)
# {{{ dig for previous answers
from course.models import FlowPageVisit
previous_answer_visits = (FlowPageVisit.objects
.filter(flow_visit=flow_visit)
.filter(page_data=page_data)
.filter(answer__isnull=False)
.order_by("-visit_time"))
self.prev_answer_is_final = False
self.prev_answer = None
for prev_visit in previous_answer_visits:
self.prev_answer = prev_visit.answer
self.prev_answer_is_final = prev_visit.answer_is_final
break
# }}}
@property
def ordinal(self):
return self.page_data.ordinal
@property
def page_count(self):
return self.flow_visit.page_count
def create_visit(self):
from course.models import FlowPageVisit
page_visit = FlowPageVisit()
page_visit.flow_visit = self.flow_visit
page_visit.page_data = self.page_data
page_visit.save()
def will_receive_feedback(self):
from course.models import flow_permission
return (
flow_permission.see_correctness in self.permissions
or flow_permission.see_answer in self.permissions)
def find_current_flow_visit(request, flow_identifier):
flow_visit = None
flow_visit_id = request.session.get("flow_visit_id")
if flow_visit_id is not None:
flow_visits = list(FlowVisit.objects.filter(id=flow_visit_id))
if flow_visits and flow_visits[0].flow_id == flow_identifier:
flow_visit, = flow_visits
if not flow_visit.in_progress:
flow_visit = False
def finish_flow_visit(flow_visit):
from django.utils.timezone import now
flow_visit.completion_time = now()
flow_visit.in_progress = False
flow_visit.save()
# FIXME mark answers as final
# FIXME assign grade
def render_flow_completion_response(request, fpctx):
from course.content import html_body
return render(request, "course/flow-completion.html", {
"course": fpctx.course,
"course_desc": fpctx.course_desc,
"flow_identifier": fpctx.flow_identifier,
"flow_desc": fpctx.flow_desc,
"body": html_body(fpctx.course, fpctx.flow_desc.completion_text),
"participation": fpctx.participation,
})
def render_flow_page(request, fpctx, **kwargs):
args = {
"course": fpctx.course,
"course_desc": fpctx.course_desc,
"flow_identifier": fpctx.flow_identifier,
"flow_desc": fpctx.flow_desc,
"ordinal": fpctx.ordinal,
"page_data": fpctx.page_data,
"flow_visit": fpctx.flow_visit,
"participation": fpctx.participation,
}
args.update(kwargs)
return render(request, "course/flow-page.html", args)
def finalize_flow_page_form(fpctx, form):
if form is not None and not fpctx.prev_answer_is_final:
from crispy_forms.layout import Submit
form.helper.add_input(
Submit("save", "Save answer",
css_class="col-lg-offset-2"))
if fpctx.will_receive_feedback():
form.helper.add_input(Submit("submit", "Submit final answer"))
else:
# Only offer 'save and move on' if student will receive no feedback
if fpctx.page_data.ordinal + 1 < fpctx.flow_visit.page_count:
form.helper.add_input(
Submit("save_and_next", "Save answer and move on"))
else:
form.helper.add_input(
Submit("save_and_finish", "Save answer and finish"))
return form
def view_flow_page(request, course_identifier, flow_identifier, ordinal):
flow_visit = find_current_flow_visit(request, flow_identifier)
if flow_visit is None:
messages.add_message(request, messages.WARNING,
"No in-progress visit record found for this flow. "
"Redirected to flow start page.")
return redirect("course.views.start_flow",
course_identifier,
flow_identifier)
fpctx = FlowPageContext(request, course_identifier, flow_identifier,
ordinal, flow_visit)
page_context = fpctx.page_context
page_data = fpctx.page_data
from course.models import FlowPageVisit, flow_permission
if flow_permission.view not in fpctx.permissions:
raise PermissionDenied("not allowed to view flow")
if request.method == "POST":
if "finish" in request.POST:
finish_flow_visit(flow_visit)
request.session["flow_visit_id"] = None
return render_flow_completion_response(request, fpctx)
# reject if previous answer was final
if fpctx.prev_answer_is_final:
raise PermissionDenied("already have final answer")
form = fpctx.page.post_form(fpctx.page_context, fpctx.page_data.data,
post_data=request.POST, files_data=request.POST)
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
# {{{ figure out which button was pressed
buttons = ["save", "save_and_next", "save_and_finish", "submit"]
pressed_button = None
for button in buttons:
if button in form.data:
pressed_button = button
break
if pressed_button is None:
raise SuspiciousOperation("could not find which button was pressed")
# }}}
if form.is_valid():
# {{{ form validated, process answer
messages.add_message(request, messages.INFO,
"Answer saved.")
page_visit = FlowPageVisit()
page_visit.flow_visit = fpctx.flow_visit
page_visit.page_data = fpctx.page_data
page_visit.answer = fpctx.page.make_answer_data(
fpctx.page_context, fpctx.page_data.data,
form)
page_visit.answer_is_final = pressed_button == "submit"
page_visit.save()
if (pressed_button == "save_and_next"
and not fpctx.will_receive_feedback()):
return redirect("course.views.view_flow_page",
course_identifier,
flow_identifier,
fpctx.ordinal + 1)
elif (pressed_button == "save_and_finish"
and not fpctx.will_receive_feedback()):
finish_flow_visit(flow_visit)
request.session["flow_visit_id"] = None
return render_flow_completion_response(request, fpctx)
else:
title = fpctx.page.title(page_context, page_data.data)
body = fpctx.page.body(page_context, page_data.data)
form = fpctx.page.form_with_answer(page_context, page_data.data,
page_visit.answer, page_visit.answer_is_final)
form = finalize_flow_page_form(fpctx, form)
# FIXME generate feedback
return render_flow_page(request, fpctx,
title=title, body=body, form=form)
# }}}
else:
# {{{ form did not validate
fpctx.create_visit()
title = fpctx.page.title(page_context, page_data.data)
body = fpctx.page.body(page_context, page_data.data)
form = finalize_flow_page_form(fpctx, form)
return render_flow_page(request,
fpctx, title=title, body=body, form=form)
# }}}
page_context = fpctx.page_context
page_data = fpctx. page_data
title = fpctx.page.title(page_context, page_data.data)
body = fpctx.page.body(page_context, page_data.data)
if fpctx.prev_answer:
form = fpctx.page.form_with_answer(page_context, page_data.data,
fpctx.prev_answer, fpctx.prev_answer_is_final)
else:
form = fpctx.page.fresh_form(page_context, page_data.data)
# FIXME generate feedback
form = finalize_flow_page_form(fpctx, form)
return render_flow_page(request, fpctx, title=title, body=body, form=form)
# }}}
# vim: foldmethod=marker