Skip to content
......@@ -3,9 +3,8 @@
{% block content %}
<div class="alert alert-info">
<i class="fa fa-info-circle"></i>
{% trans "RELATE" as RELATE %}
{% blocktrans trimmed %}
<i class="bi bi-info-circle"></i>
{% blocktrans trimmed with RELATE=relate_site_name %}
{{ RELATE }} is currently in maintenance mode. Sorry for the interruption,
we'll be back shortly.
{% endblocktrans %}
......
......@@ -20,7 +20,7 @@
<a href="{% url 'relate-reset_password' 'instid'%}">{% trans 'Institutional ID' %}</a>
</li>
</ul>
<div class="well">
<div class="relate-well">
<br>
{{ form_text|safe }}
{% crispy form %}
......
......@@ -2,7 +2,7 @@
{% load i18n %}
{% block content %}
<h1>{% trans "Sign in to RELATE" %}</h1>
<h1>{% blocktrans %}Sign in to {{ relate_site_name }}{% endblocktrans %}</h1>
<ul class="list-sign-in-methods">
{% if relate_sign_in_by_saml2_enabled %}
......@@ -10,7 +10,7 @@
<a
class="btn btn-primary"
href="{% url 'saml2_login' %}{{next_uri}}"
role="button"><i class="fa fa-institution"></i>
role="button"><i class="bi bi-building"></i>
{% trans "Sign in using your institution's login" %} &raquo;</a>
</li>
{% endif %}
......@@ -19,7 +19,7 @@
<a
class="btn btn-primary"
href="{% url 'relate-sign_in_by_email' %}{{next_uri}}"
role="button"><i class="fa fa-envelope-o"></i>
role="button"><i class="bi bi-envelope"></i>
{% trans "Sign in using your email" %} &raquo;</a>
</li>
{% endif %}
......@@ -28,7 +28,7 @@
<a
class="btn btn-primary"
href="{% url 'relate-sign_up' %}"
role="button"><i class="fa fa-user-plus"></i>
role="button"><i class="bi bi-person-plus"></i>
{% trans "Sign up for an account" %} &raquo;</a>
</li>
{% endif %}
......@@ -37,16 +37,27 @@
<a
class="btn btn-primary"
href="{% url 'relate-check_in_for_exam' %}"
role="button"><i class="fa fa-ticket"></i>
role="button"><i class="bi bi-ticket-perforated"></i>
{% trans "Sign in using an exam ticket" %} &raquo;</a>
</li>
{% endif %}
{% if relate_sign_in_by_username_enabled %}
<li>
<a
class="btn btn-primary"
href="{% url 'relate-sign_in_by_user_pw' %}{{next_uri}}"
role="button"><i class="fa fa-key"></i>
{% trans "Sign in with a RELATE-specific user name and password" %} &raquo;</a>
role="button"><i class="bi bi-key"></i>
{% blocktrans %}Sign in with a {{ relate_site_name }}-specific user name and password{% endblocktrans %} &raquo;</a>
</li>
{% endif %}
{% for backend in backends.backends %}
<li>
<a
class="btn btn-primary"
href="{% url "social:begin" backend %}{{next_uri}}"
role="button"><i class="bi bi-{{ social_provider_to_logo|get_item_or_key:backend }}"></i>
{% blocktrans with bkend=social_provider_to_human_name|get_item_or_key:backend %}Sign in with {{ bkend }}{% endblocktrans %} &raquo;</a>
</li>
{% endfor %}
</ul>
{% endblock %}
......@@ -2,9 +2,9 @@
{% load i18n %}
{% block content %}
<h1>{% trans "You're already signed in" %}</h1>
<h1>{% trans "You've already signed in" %}</h1>
<h4>{% trans "Would you like to sign out and proceed with your previous operation?" %}</h4>
<a href="{% url 'relate-logout' %}{{next_uri}}" class="btn btn-default">{% trans "Sign out" %}</a>
<a href="{% url 'relate-home' %}" class="btn btn-default">{% trans "Cancel" %}</a>
<a href="{% url 'relate-logout' %}{{next_uri}}" class="btn btn-danger">{% trans "Sign out" %}</a>
<button class="btn btn-secondary" onclick="window.history.back()">{% trans "Cancel" %}</button>
{% endblock %}
{% extends "base.html" %}
{% extends "generic-form.html" %}
{% load i18n %}
{% load crispy_forms_tags %}
{% block page_bottom_javascript_extra %}
{% if enable_profile_form_js %}
<script type="text/javascript">
{% block content %}
<h1>{% trans "User Profile" %}</h1>
<div class="well">
{% crispy user_form %}
</div>
<div class="well">
<a href="{% url 'relate-logout' %}" class="btn btn-default col-lg-offset-2">{% trans "Sign out" %}</a>
</div>
var no_inst_id_checkbox_html = '<div class="form-group">\n' +
' <div class="controls col-lg-offset-2 col-lg-8">\n' +
' <div id="div_id_no_institutional_id" class="checkbox">\n' +
' <label for="id_no_institutional_id" class="">\n' +
' <input type="checkbox" name="no_institutional_id" class="checkboxinput"\n' +
' id="id_no_institutional_id">\n' +
' {% trans "I have no Institutional ID" %}\n' +
' <p id="hint_id_no_institutional_id" class="help-block">\n' +
' {% trans "Check the checkbox if you are not a student or you forget your institutional id." %}\n' +
' </p>\n' +
' </label>\n' +
' </div>\n' +
' </div>\n' +
'</div>';
{# show/hide/disabled of fields #}
function disable_inst_id(){
$('#div_id_institutional_id_confirm').addClass("hidden");
$('#id_institutional_id').prop('disabled', true);
$('#id_institutional_id_confirm').addClass('hidden');
$("#id_no_institutional_id").prop('checked', true);
}
<script type="text/javascript">
function enable_inst_id(){
$('#div_id_institutional_id_confirm').removeClass("hidden");
$('#id_institutional_id_confirm').removeClass('hidden');
$('#id_institutional_id').prop('disabled', false);
$("#id_no_institutional_id").prop('checked', false);
}
{# show/hide/disabled of fields #}
function disable_inst_id(){
$('#div_id_institutional_id_confirm').hide();
$('#id_institutional_id').prop('disabled', true);
$("#id_no_institutional_id").prop('checked', true);
}
{# When no_id is checked, clear input data in inst_id #}
$(document).on('click', '#id_no_institutional_id', function(event) {
if (this.checked){
disable_inst_id();
$('#id_institutional_id').val("");
$('#id_institutional_id_confirm').val("")}
else {enable_inst_id();}
});
function enable_inst_id(){
$('#div_id_institutional_id_confirm').show();
$('#id_institutional_id').prop('disabled', false);
$("#id_no_institutional_id").prop('checked', false);
}
{# When no_id is checked, clear input data in inst_id #}
$(document).on('click', '#id_no_institutional_id', function(event) {
if (this.checked){
disable_inst_id();
$('#id_institutional_id').val("");
$('#id_institutional_id_confirm').val("")}
else {enable_inst_id();}
});
{# crispy form layout cannot hide wrapping div of a field. #}
$(document).ready(function(){
{% if is_inst_id_locked %}
disable_inst_id();
{% elif enable_inst_id_if_not_locked %}
{# crispy form layout cannot hide wrapping div of a field. #}
$(document).ready(function(){
$('#institutional_id_block').prepend(no_inst_id_checkbox_html);
{% if is_requesting_inst_id %}
enable_inst_id();
{% else %}
{% else %}
if ($('#id_institutional_id').val().length == 0 &&
$('#id_institutional_id_confirm').val().length == 0 &&
$('#error_1_id_institutional_id_confirm').length == 0 &&
......@@ -51,8 +57,10 @@
{disable_inst_id();}
else
{enable_inst_id();}
{% endif %}
});
{% endif %}
});
</script>
{% endblock %}
</script>
{% endif %}
{{ block.super }}
{% endblock %}
\ No newline at end of file
# -*- coding: utf-8 -*-
from __future__ import annotations
from __future__ import division
__copyright__ = "Copyright (C) 2014 Andreas Kloeckner"
......@@ -24,71 +23,75 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""
from django.conf.urls import include, url
from django.contrib import admin
from django.conf import settings
from course.constants import COURSE_ID_REGEX, FLOW_ID_REGEX, STATICPAGE_PATH_REGEX
from django.urls import include, path, re_path
import course.analytics
import course.api
import course.auth
import course.views
import course.im
import course.sandbox
import course.calendar
import course.exam
import course.flow
import course.grades
import course.grading
import course.calendar
import course.im
import course.sandbox
import course.versioning
import course.flow
import course.analytics
import course.exam
import course.views
from course.constants import COURSE_ID_REGEX, FLOW_ID_REGEX, STATICPAGE_PATH_REGEX
urlpatterns = [
url(r"^login/$",
path("login/",
course.auth.sign_in_choice,
name="relate-sign_in_choice"),
url(r"^login/user-password/$",
path("login/user-password/",
course.auth.sign_in_by_user_pw,
name="relate-sign_in_by_user_pw"),
url(r"^login/sign-up/$",
path("login/sign-up/",
course.auth.sign_up,
name="relate-sign_up"),
url(r"^login/reset-password/$",
path("login/reset-password/",
course.auth.reset_password,
name="relate-reset_password"),
url(r"^login/reset-password/(?P<field>instid)/$",
re_path(r"^login/reset-password/(?P<field>instid)/$",
course.auth.reset_password,
name="relate-reset_password"),
url(r"^login/reset-password/stage-2"
re_path(r"^login/reset-password/stage-2"
"/(?P<user_id>[0-9]+)"
"/(?P<sign_in_key>[a-zA-Z0-9]+)",
course.auth.reset_password_stage2,
name="relate-reset_password_stage2"),
url(r"^login/by-email/$",
path("login/by-email/",
course.auth.sign_in_by_email,
name="relate-sign_in_by_email"),
url(r"^login/token"
re_path(r"^login/token"
"/(?P<user_id>[0-9]+)"
"/(?P<sign_in_key>[a-zA-Z0-9]+)"
"/$",
course.auth.sign_in_stage2_with_token,
name="relate-sign_in_stage2_with_token"),
url(r"^logout/$",
path("logout/",
course.auth.sign_out,
name="relate-logout"),
url(r"^logout-confirmation/$",
path("logout-confirmation/",
course.auth.sign_out_confirmation,
name="relate-logout-confirmation"),
url(r"^profile/$",
path("profile/",
course.auth.user_profile,
name="relate-user_profile"),
url(r"^profile/auth-token/$",
course.auth.manage_authentication_token,
name="relate-manage_authentication_token"),
url(r"^generate-ssh-key/$",
re_path(
r"^course"
"/" + COURSE_ID_REGEX
+ "/auth-tokens/$",
course.auth.manage_authentication_tokens,
name="relate-manage_authentication_tokens"),
path("generate-ssh-key/",
course.views.generate_ssh_keypair,
name="relate-generate_ssh_keypair"),
url(r"^monitor-task"
re_path(r"^monitor-task"
"/(?P<task_id>[-0-9a-f]+)"
"$",
course.views.monitor_task,
......@@ -96,18 +99,19 @@ urlpatterns = [
# {{{ troubleshooting
url(r'^user/impersonate/$',
path("user/impersonate/",
course.auth.impersonate,
name="relate-impersonate"),
url(r'^user/stop_impersonating/$',
path("user/stop_impersonating/",
course.auth.stop_impersonating,
name="relate-stop_impersonating"),
url(r'^time/set-fake-time/$',
path("time/set-fake-time/",
course.views.set_fake_time,
name="relate-set_fake_time"),
url(r'^time/set-pretend-facilities/$',
path("time/set-pretend-facilities/",
course.views.set_pretend_facilities,
name="relate-set_pretend_facilities"),
......@@ -115,114 +119,118 @@ urlpatterns = [
# {{{ course
url(r'^$', course.views.home, name='relate-home'),
path("", course.views.home, name="relate-home"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/$",
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/$",
course.views.course_page,
name="relate-course_page"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/edit/$",
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/edit/$",
course.views.edit_course,
name="relate-edit_course"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/page"
"/" + STATICPAGE_PATH_REGEX +
"/$",
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/page"
"/" + STATICPAGE_PATH_REGEX
+ "/$",
course.views.static_page,
name="relate-content_page"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/instant-message/$",
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/instant-message/$",
course.im.send_instant_message,
name="relate-send_instant_message"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/sandbox/markup/$",
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/sandbox/markup/$",
course.sandbox.view_markup_sandbox,
name="relate-view_markup_sandbox"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/sandbox/page/$",
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/sandbox/page/$",
course.sandbox.view_page_sandbox,
name="relate-view_page_sandbox"),
path("purge-pageview-data/",
course.flow.purge_page_view_data,
name="relate-purge_page_view_data"),
# }}}
# {{{ grading
url(r"^course"
"/" + COURSE_ID_REGEX +
"/grading/my/$",
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/grading/my/$",
course.grades.view_participant_grades,
name="relate-view_participant_grades"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/grading/participant"
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/grading/participant"
"/(?P<participation_id>[0-9]+)"
"/$",
course.grades.view_participant_grades,
name="relate-view_participant_grades"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/grading/participants/$",
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/grading/participants/$",
course.grades.view_participant_list,
name="relate-view_participant_list"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/grading/opportunities/$",
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/grading/opportunities/$",
course.grades.view_grading_opportunity_list,
name="relate-view_grading_opportunity_list"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/grading/overview/$",
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/grading/overview/$",
course.grades.view_gradebook,
name="relate-view_gradebook"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/grading/overview/csv/$",
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/grading/overview/csv/$",
course.grades.export_gradebook_csv,
name="relate-export_gradebook_csv"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/grading/by-opportunity"
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/grading/by-opportunity"
"/(?P<opp_id>[0-9]+)"
"/$",
course.grades.view_grades_by_opportunity,
name="relate-view_grades_by_opportunity"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/grading/single-grade"
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/grading/single-grade"
"/(?P<participation_id>[0-9]+)"
"/(?P<opportunity_id>[0-9]+)"
"/$",
course.grades.view_single_grade,
name="relate-view_single_grade"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/grading/reopen-session"
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/grading/reopen-session"
"/(?P<flow_session_id>[0-9]+)"
"/(?P<opportunity_id>[0-9]+)"
"/$",
course.grades.view_reopen_session,
name="relate-view_reopen_session"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/grading"
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/grading"
"/csv-import"
"/$",
course.grades.import_grades,
name="relate-import_grades"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/grading"
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/grading"
"/flow-page"
"/(?P<flow_session_id>[0-9]+)"
"/(?P<page_ordinal>[0-9]+)"
......@@ -230,25 +238,36 @@ urlpatterns = [
course.grading.grade_flow_page,
name="relate-grade_flow_page"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/grading/statistics"
"/" + FLOW_ID_REGEX +
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/prev-grades"
"/flow-page"
"/(?P<flow_session_id>[0-9]+)"
"/(?P<page_ordinal>[0-9]+)"
"/(?P<prev_grade_id>[0-9]+|None)"
"/$",
course.grading.get_prev_grades_dropdown_content,
name="relate-get_prev_grades_dropdown_content"),
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/grading/statistics"
"/" + FLOW_ID_REGEX
+ "/$",
course.grading.show_grader_statistics,
name="relate-show_grader_statistics"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/grading/download-submissions"
"/" + FLOW_ID_REGEX +
"/$",
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/grading/download-submissions"
"/" + FLOW_ID_REGEX
+ "/$",
course.grades.download_all_submissions,
name="relate-download_all_submissions"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/edit-grading-opportunity"
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/edit-grading-opportunity"
"/(?P<opportunity_id>[-0-9]+)"
"/$",
course.grades.edit_grading_opportunity,
......@@ -258,26 +277,26 @@ urlpatterns = [
# {{{ enrollment
url(r"^course"
"/" + COURSE_ID_REGEX +
"/enroll/$",
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/enroll/$",
course.enrollment.enroll_view,
name="relate-enroll"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/preapprove"
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/preapprove"
"/$",
course.enrollment.create_preapprovals,
name="relate-create_preapprovals"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/query-participations"
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/query-participations"
"/$",
course.enrollment.query_participations,
name="relate-query_participations"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/edit-participation"
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/edit-participation"
"/(?P<participation_id>[-0-9]+)"
"/$",
course.enrollment.edit_participation,
......@@ -287,23 +306,23 @@ urlpatterns = [
# {{{ media
url(r"^course"
"/" + COURSE_ID_REGEX +
"/media/(?P<commit_sha>[a-f0-9]+)"
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/media/(?P<commit_sha>[a-f0-9]+)"
"/(?P<media_path>.*)$",
course.views.get_media,
name="relate-get_media"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/file-version/(?P<commit_sha>[a-f0-9]+)"
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/file-version/(?P<commit_sha>[a-f0-9]+)"
"/(?P<path>.*)$",
course.views.get_repo_file,
name="relate-get_repo_file"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/f"
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/f"
"/(?P<path>.*)$",
course.views.get_current_repo_file,
name="relate-get_current_repo_file"),
......@@ -312,19 +331,19 @@ urlpatterns = [
# {{{ calendar
url(r"^course"
"/" + COURSE_ID_REGEX +
"/create-recurring-events/$",
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/create-recurring-events/$",
course.calendar.create_recurring_events,
name="relate-create_recurring_events"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/renumber-events/$",
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/renumber-events/$",
course.calendar.renumber_events,
name="relate-renumber_events"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/calendar/$",
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/calendar/$",
course.calendar.view_calendar,
name="relate-view_calendar"),
......@@ -332,118 +351,152 @@ urlpatterns = [
# {{{ versioning
url(r"^new-course/$",
path("new-course/",
course.versioning.set_up_new_course,
name="relate-set_up_new_course"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/update/$",
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/update/$",
course.versioning.update_course,
name="relate-update_course"),
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/git"
"/(?P<git_path>.*)"
"$",
course.versioning.git_endpoint,
name="relate-git_endpoint"),
# }}}
# {{{ flow-related
url(r"^course"
"/" + COURSE_ID_REGEX +
"/flow"
"/" + FLOW_ID_REGEX +
"/start"
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/flow"
"/" + FLOW_ID_REGEX
+ "/start"
"/$",
course.flow.view_start_flow,
name="relate-view_start_flow"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/flow-session"
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/flow-session"
"/(?P<flow_session_id>[-0-9]+)"
"/resume"
"/$",
course.flow.view_resume_flow,
name="relate-view_resume_flow"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/flow-session"
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/flow-session"
"/(?P<flow_session_id>[0-9]+)"
"/(?P<ordinal>[0-9]+)"
"/(?P<page_ordinal>[0-9]+)"
"/$",
course.flow.view_flow_page,
name="relate-view_flow_page"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/flow-session"
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/flow-session"
"/(?P<flow_session_id>[0-9]+)"
"/(?P<page_ordinal>[0-9]+)"
"/ext-resource-tabs"
"/$",
course.flow.view_flow_page_with_ext_resource_tabs,
name="relate-view_flow_page_with_ext_resource_tabs"),
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/prev_answers"
"/flow-page"
"/(?P<flow_session_id>[0-9]+)"
"/(?P<page_ordinal>[0-9]+)"
"/(?P<prev_visit_id>[0-9]+|None)"
"/$",
course.flow.get_prev_answer_visits_dropdown_content,
name="relate-get_prev_answer_visits_dropdown_content"),
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/flow-session"
"/(?P<flow_session_id>[-0-9]+)"
"/update-expiration-mode"
"/$",
course.flow.update_expiration_mode,
name="relate-update_expiration_mode"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/flow-session"
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/flow-session"
"/(?P<flow_session_id>[-0-9]+)"
"/(?P<ordinal>[0-9]+)"
"/(?P<page_ordinal>[0-9]+)"
"/update-bookmark-state"
"/$",
course.flow.update_page_bookmark_state,
name="relate-update_page_bookmark_state"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/flow-session"
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/flow-session"
"/(?P<flow_session_id>[0-9]+)"
"/finish"
"/$",
course.flow.finish_flow_session_view,
name="relate-finish_flow_session_view"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/flow-session"
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/flow-session"
"/(?P<flow_session_id>[0-9]+)"
"/(?P<ordinal>[0-9]+)"
"/(?P<page_ordinal>[0-9]+)"
"/flow-page-interaction-email"
"/$",
course.flow.send_email_about_flow_page,
name="relate-flow_page_interaction_email"),
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/flow-session"
"/(?P<flow_session_id>[0-9]+)"
"/(?P<page_ordinal>[0-9]+)"
"/unsubmit/$",
course.flow.view_unsubmit_flow_page,
name="relate-unsubmit_flow_page"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/test-flow"
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/test-flow"
"/$",
course.views.test_flow,
name="relate-test_flow"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/instant-flow"
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/instant-flow"
"/$",
course.views.manage_instant_flow_requests,
name="relate-manage_instant_flow_requests"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/regrade-flows"
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/regrade-flows"
"/$",
course.flow.regrade_flows_view,
name="relate-regrade_flows_view"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/grant-exception"
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/grant-exception"
"/$",
course.views.grant_exception,
name="relate-grant_exception"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/grant-exception"
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/grant-exception"
"/(?P<participation_id>[0-9]+)"
"/" + FLOW_ID_REGEX +
"/$",
"/" + FLOW_ID_REGEX
+ "/$",
course.views.grant_exception_stage_2,
name="relate-grant_exception_stage_2"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/grant-exception"
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/grant-exception"
"/(?P<participation_id>[0-9]+)"
"/" + FLOW_ID_REGEX +
"/(?P<session_id>[0-9]+)"
"/" + FLOW_ID_REGEX
+ "/(?P<session_id>[0-9]+)"
"/$",
course.views.grant_exception_stage_3,
name="relate-grant_exception_stage_3"),
......@@ -452,24 +505,24 @@ urlpatterns = [
# {{{ analytics
url(r"^course"
"/" + COURSE_ID_REGEX +
"/flow-analytics"
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/flow-analytics"
"/$",
course.analytics.flow_list,
name="relate-flow_list"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/flow-analytics"
"/" + FLOW_ID_REGEX +
"/$",
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/flow-analytics"
"/" + FLOW_ID_REGEX
+ "/$",
course.analytics.flow_analytics,
name="relate-flow_analytics"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/flow-analytics"
"/" + FLOW_ID_REGEX +
"/page"
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/flow-analytics"
"/" + FLOW_ID_REGEX
+ "/page"
"/(?P<group_id>[-_a-zA-Z0-9]+)"
"/(?P<page_id>[-_a-zA-Z0-9]+)"
"/$",
......@@ -480,43 +533,50 @@ urlpatterns = [
# {{{ exams
url(r"^issue-exam-ticket"
"/$",
path("issue-exam-ticket/",
course.exam.issue_exam_ticket,
name="relate-issue_exam_ticket"),
url(r"^course"
"/" + COURSE_ID_REGEX +
"/batch-issue-exam-tickets"
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/batch-issue-exam-tickets"
"/$",
course.exam.batch_issue_exam_tickets,
name="relate-batch_issue_exam_tickets"),
url(r"^exam-check-in/$",
path("exam-check-in/",
course.exam.check_in_for_exam,
name="relate-check_in_for_exam"),
url(r"^list-available-exams/$",
path("list-available-exams/",
course.exam.list_available_exams,
name="relate-list_available_exams"),
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/access-exam"
"/$",
course.exam.access_exam,
name="relate-access_exam"),
path("prairietest/", include("prairietest.urls")),
# }}}
# {{{ django-select2
path(r"select2/", include("django_select2.urls")),
url(r'^select2/', include('django_select2.urls')),
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/api/v1/get-flow-sessions$",
course.api.get_flow_sessions,
name="relate-course_get_flow_session"),
#}}}
re_path(r"^course"
"/" + COURSE_ID_REGEX
+ "/api/v1/get-flow-session-content$",
course.api.get_flow_session_content,
name="relate-course_get_flow_session_content"),
url(r'^admin/', admin.site.urls),
]
path(r"admin/", admin.site.urls),
if settings.RELATE_SIGN_IN_BY_SAML2_ENABLED:
urlpatterns.extend([
url(r'^saml2/', include('djangosaml2.urls')),
])
if settings.DEBUG:
import djangosaml2.views
urlpatterns.extend([
# Keep commented unless debugging SAML2.
url(r'^saml2-test/', djangosaml2.views.echo_attributes),
])
path("social-auth/", include("social_django.urls"), name="social"),
path(r"saml2/", include("djangosaml2.urls")),
]
# vim: fdm=marker
# -*- coding: utf-8 -*-
from __future__ import annotations
from __future__ import division
__copyright__ = "Copyright (C) 2014 Andreas Kloeckner"
......@@ -25,41 +24,60 @@ THE SOFTWARE.
"""
import six
from typing import Text, Union, List, Dict, Tuple, Optional, Any # noqa
import datetime # noqa
import datetime
from collections.abc import Collection, Mapping
from ipaddress import IPv4Address, IPv6Address, ip_address, ip_network
from typing import (
Any,
TypeVar,
)
from zoneinfo import ZoneInfo
import django.forms as forms
import dulwich.repo
from django.http import HttpRequest
from django.utils.text import format_lazy
from django.utils.translation import gettext_lazy as _
class StyledForm(forms.Form):
def __init__(self, *args, **kwargs):
# type: (...) -> None
from crispy_forms.helper import FormHelper
self.helper = FormHelper()
self.helper.form_class = "form-horizontal"
self.helper.label_class = "col-lg-2"
self.helper.field_class = "col-lg-8"
T = TypeVar("T")
class RelateHttpRequest(HttpRequest):
# add monkey-patched request attributes
# added by FacilityFindingMiddleware
relate_facilities: Collection[str]
super(StyledForm, self).__init__(*args, **kwargs)
# added by ExamLockdownMiddleware
relate_exam_lockdown: bool
class StyledInlineForm(forms.Form):
def __init__(self, *args, **kwargs):
# type: (...) -> None
def not_none(obj: T | None) -> T:
assert obj is not None
return obj
def string_concat(*strings: Any) -> str:
return format_lazy("{}" * len(strings), *strings)
class StyledForm(forms.Form):
def __init__(self, *args, **kwargs) -> None:
from crispy_forms.helper import FormHelper
self.helper = FormHelper()
self.helper.form_class = "form-inline"
self.helper.label_class = "sr-only"
self._configure_helper()
super(StyledInlineForm, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
def _configure_helper(self) -> None:
self.helper.form_class = "form-horizontal"
self.helper.label_class = "col-lg-2"
self.helper.field_class = "col-lg-8"
class StyledModelForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
# type: (...) -> None
def __init__(self, *args, **kwargs) -> None:
from crispy_forms.helper import FormHelper
self.helper = FormHelper()
......@@ -67,32 +85,50 @@ class StyledModelForm(forms.ModelForm):
self.helper.label_class = "col-lg-2"
self.helper.field_class = "col-lg-8"
super(StyledModelForm, self).__init__(*args, **kwargs)
super().__init__(*args, **kwargs)
# {{{ repo-ish types
class SubdirRepoWrapper(object):
def __init__(self, repo, subdir):
# type: (dulwich.Repo, Text) -> None
class SubdirRepoWrapper:
def __init__(self, repo: dulwich.repo.Repo, subdir: str) -> None:
self.repo = repo
# This wrapper should only get used if there is a subdir to be had.
assert subdir
self.subdir = subdir
def controldir(self):
def controldir(self) -> str:
return self.repo.controldir()
def close(self):
def close(self) -> None:
self.repo.close()
def __enter__(self) -> SubdirRepoWrapper:
return self
Repo_ish = Union[dulwich.repo.Repo, SubdirRepoWrapper]
def __exit__(self, exc_type, exc_val, exc_tb) -> None:
self.close()
def get_refs(self) -> Mapping[bytes, bytes]:
return self.repo.get_refs()
def __setitem__(self, item: bytes, value: bytes) -> None:
self.repo[item] = value
def __delitem__(self, item: bytes) -> None:
del self.repo[item]
Repo_ish = dulwich.repo.Repo | SubdirRepoWrapper
# }}}
def remote_address_from_request(request: HttpRequest) -> IPv4Address | IPv6Address:
return ip_address(str(request.META["REMOTE_ADDR"]))
# {{{ maintenance mode
def is_maintenance_mode(request):
......@@ -102,20 +138,17 @@ def is_maintenance_mode(request):
if maintenance_mode:
exceptions = getattr(settings, "RELATE_MAINTENANCE_MODE_EXCEPTIONS", [])
import ipaddress
remote_address = ipaddress.ip_address(
six.text_type(request.META['REMOTE_ADDR']))
remote_address = remote_address_from_request(request)
for exc in exceptions:
if remote_address in ipaddress.ip_network(six.text_type(exc)):
if remote_address in ip_network(str(exc)):
maintenance_mode = False
break
return maintenance_mode
class MaintenanceMiddleware(object):
class MaintenanceMiddleware:
def __init__(self, get_response):
self.get_response = get_response
......@@ -129,12 +162,28 @@ class MaintenanceMiddleware(object):
# }}}
def get_site_name() -> str:
from django.conf import settings
return getattr(settings, "RELATE_SITE_NAME", "RELATE")
def render_email_template(template_name: str, context: dict | None = None,
request: HttpRequest | None = None, using: str | None = None) -> str:
if context is None:
context = {}
context.update({"relate_site_name": _(get_site_name())})
from django.template.loader import render_to_string
return render_to_string(template_name, context, request, using)
def settings_context_processor(request):
from django.conf import settings
return {
"student_sign_in_view": "relate-sign_in_choice",
"relate_sign_in_by_email_enabled":
settings.RELATE_SIGN_IN_BY_EMAIL_ENABLED,
"relate_sign_in_by_username_enabled":
settings.RELATE_SIGN_IN_BY_USERNAME_ENABLED,
"relate_registration_enabled":
settings.RELATE_REGISTRATION_ENABLED,
"relate_sign_in_by_exam_tickets_enabled":
......@@ -143,55 +192,52 @@ def settings_context_processor(request):
settings.RELATE_SIGN_IN_BY_SAML2_ENABLED,
"maintenance_mode": is_maintenance_mode(request),
"site_announcement": getattr(settings, "RELATE_SITE_ANNOUNCEMENT", None),
"relate_site_name": _(get_site_name())
}
def as_local_time(dtm):
# type: (datetime.datetime) -> datetime.datetime
def as_local_time(dtm: datetime.datetime) -> datetime.datetime:
"""Takes a timezone-aware datetime and applies the server timezone."""
from django.conf import settings
from pytz import timezone
tz = timezone(settings.TIME_ZONE)
tz = ZoneInfo(settings.TIME_ZONE)
return dtm.astimezone(tz)
def localize_datetime(dtm):
def localize_datetime(dtm: datetime.datetime) -> datetime.datetime:
"""Takes an timezone-naive datetime and applies the server timezone."""
from django.conf import settings
from pytz import timezone
tz = timezone(settings.TIME_ZONE)
return tz.localize(dtm)
assert dtm.tzinfo is None
from django.conf import settings
tz = ZoneInfo(settings.TIME_ZONE)
return dtm.replace(tzinfo=tz)
def local_now():
# type: () -> datetime.datetime
def local_now() -> datetime.datetime:
from django.conf import settings
from pytz import timezone
tz = timezone(settings.TIME_ZONE)
return tz.localize(datetime.datetime.now())
tz = ZoneInfo(settings.TIME_ZONE)
return datetime.datetime.now(tz)
def format_datetime_local(datetime, format='DATETIME_FORMAT'):
# type: (datetime.datetime, str) -> str
def format_datetime_local(
datetime: datetime.datetime, format: str = "DATETIME_FORMAT") -> str:
"""
Format a datetime object to a localized string via python.
Note: The datetime rendered in template is itself locale aware.
A custom format must be defined in settings.py.
When a custom format uses a same name with an existing built-in
format, it will be overrided by built-in format if l10n
format, it will be overridden by built-in format if l10n
is enabled.
"""
from django.utils import formats
from django.utils.dateformat import format as dformat
try:
return formats.date_format(datetime, format)
except AttributeError:
try:
from django.utils.dateformat import format as dformat
return dformat(datetime, format)
except AttributeError:
return formats.date_format(datetime, "DATETIME_FORMAT")
......@@ -199,40 +245,39 @@ def format_datetime_local(datetime, format='DATETIME_FORMAT'):
# {{{ dict_to_struct
class Struct(object):
def __init__(self, entries):
# type: (Dict) -> None
for name, val in six.iteritems(entries):
self.__dict__[name] = val
class Struct:
def __init__(self, entries: dict) -> None:
for name, val in entries.items():
setattr(self, name, val)
self._field_names = list(six.iterkeys(entries))
self._field_names = list(entries.keys())
def __repr__(self):
return repr(self.__dict__)
def dict_to_struct(data):
# type: (Dict) -> Struct
def dict_to_struct(data: dict) -> Struct:
if isinstance(data, list):
return [dict_to_struct(d) for d in data]
elif isinstance(data, dict):
return Struct({k: dict_to_struct(v) for k, v in six.iteritems(data)})
return Struct({k: dict_to_struct(v) for k, v in data.items()})
else:
return data
def struct_to_dict(data):
# type: (Struct) -> Dict
return dict(
(name, val)
for name, val in six.iteritems(data.__dict__)
if not name.startswith("_"))
def struct_to_dict(data: Struct) -> dict:
return {
name: val
for name, val in data.__dict__.items()
if not name.startswith("_")}
# }}}
def retry_transaction(f, args, kwargs={}, max_tries=None, serializable=None):
# type: (Any, Tuple, Dict, Optional[int], Optional[bool]) -> Any
def retry_transaction(f: Any, args: tuple, kwargs: dict | None = None,
max_tries: int | None = None, serializable: bool | None = None) -> Any:
if kwargs is None:
kwargs = {}
from django.db import transaction
from django.db.utils import OperationalError
......@@ -247,7 +292,7 @@ def retry_transaction(f, args, kwargs={}, max_tries=None, serializable=None):
try:
with transaction.atomic():
if serializable:
from django.db import connections, DEFAULT_DB_ALIAS
from django.db import DEFAULT_DB_ALIAS, connections
conn = connections[DEFAULT_DB_ALIAS]
if conn.vendor == "postgresql":
cursor = conn.cursor()
......@@ -265,14 +310,13 @@ def retry_transaction(f, args, kwargs={}, max_tries=None, serializable=None):
sleep(uniform(0.05, 0.2))
class retry_transaction_decorator(object):
def __init__(self, max_tries=None, serializable=None):
# type: (Optional[int], Optional[bool]) -> None
class retry_transaction_decorator: # noqa
def __init__(self, max_tries: int | None = None,
serializable: bool | None = None) -> None:
self.max_tries = max_tries
self.serializable = serializable
def __call__(self, f):
# type: (Any) -> Any
def __call__(self, f: Any) -> Any:
from functools import update_wrapper
def wrapper(*args, **kwargs):
......@@ -286,58 +330,41 @@ class retry_transaction_decorator(object):
# {{{ hang debugging
def dumpstacks(signal, frame):
import threading
def dumpstacks(signal, frame): # pragma: no cover
import sys
import threading
import traceback
id2name = dict([(th.ident, th.name) for th in threading.enumerate()])
id2name = {th.ident: th.name for th in threading.enumerate()}
code = []
for threadId, stack in sys._current_frames().items():
code.append("\n# Thread: %s(%d)" % (id2name.get(threadId, ""), threadId))
for thread_id, stack in sys._current_frames().items():
code.append("\n# Thread: %s(%d)" % (id2name.get(thread_id, ""), thread_id))
for filename, lineno, name, line in traceback.extract_stack(stack):
code.append('File: "%s", line %d, in %s' % (filename, lineno, name))
if line:
code.append(" %s" % (line.strip()))
code.append(f" {line.strip()}")
print("\n".join(code))
if 0:
import signal
import os
print("*** HANG DUMP HANDLER ACTIVATED: 'kill -USR1 %s' to dump stacks"
% os.getpid())
import signal
print(f"*** HANG DUMP HANDLER ACTIVATED: 'kill -USR1 {os.getpid()}' to dump stacks")
signal.signal(signal.SIGUSR1, dumpstacks)
# }}}
# {{{ convert django language name to js styled language name
def to_js_lang_name(dj_lang_name):
"""
Turns a django language name (en-us) into a js styled language
name (en-US).
"""
p = dj_lang_name.find('-')
if p >= 0:
return dj_lang_name[:p].lower() + '-' + dj_lang_name[p + 1:].upper()
else:
return dj_lang_name.lower()
# }}}
#{{{ Allow multiple email connections
# {{{ Allow multiple email connections
# https://gist.github.com/niran/840999
def get_outbound_mail_connection(label=None, **kwargs):
# type: (Optional[Text], **Any) -> Any
def get_outbound_mail_connection(label: str | None = None, **kwargs: Any) -> Any:
from django.conf import settings
if label is None:
label = getattr(settings, 'EMAIL_CONNECTION_DEFAULT', None)
label = getattr(settings, "EMAIL_CONNECTION_DEFAULT", None)
try:
connections = getattr(settings, 'EMAIL_CONNECTIONS')
connections = settings.EMAIL_CONNECTIONS # type: ignore[misc]
options = connections[label]
except (KeyError, AttributeError):
# Neither EMAIL_CONNECTIONS nor
......@@ -352,11 +379,11 @@ def get_outbound_mail_connection(label=None, **kwargs):
from django.core import mail
return mail.get_connection(**options)
#}}}
# }}}
def ignore_no_such_table(f, *args):
from django.db import connections, DEFAULT_DB_ALIAS
from django.db import DEFAULT_DB_ALIAS, connections
conn = connections[DEFAULT_DB_ALIAS]
if conn.vendor == "postgresql":
......@@ -392,4 +419,43 @@ def ignore_no_such_table(f, *args):
raise
def force_remove_path(path: str) -> None:
"""
Work around deleting read-only path on Windows.
Ref: https://docs.python.org/3.5/library/shutil.html#rmtree-example
"""
import os
import shutil
import stat
def remove_readonly(func, path, _):
"""Clear the readonly bit and reattempt the removal"""
os.chmod(path, stat.S_IWRITE)
func(path)
shutil.rmtree(path, onerror=remove_readonly)
# {{{ date/datetime input
HTML5_DATE_FORMAT = "%Y-%m-%d"
HTML5_DATETIME_FORMAT = "%Y-%m-%dT%H:%M"
class HTML5DateInput(forms.DateInput):
def __init__(self) -> None:
super().__init__(
attrs={"type": "date"},
format=HTML5_DATE_FORMAT)
class HTML5DateTimeInput(forms.DateTimeInput):
def __init__(self) -> None:
super().__init__(
attrs={"type": "datetime-local"},
format=HTML5_DATETIME_FORMAT)
# }}}
# vim: foldmethod=marker
......@@ -6,9 +6,14 @@ It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/
"""
from __future__ import annotations
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "relate.settings")
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()
......@@ -4,9 +4,8 @@ USER=www-data
RELATE_GIT_ROOT=/web/relate-git-root
for i in "$RELATE_GIT_ROOT"/*; do
echo $i
if test -d "$i/.git"; then
su $USER -c "cd $i ; git repack -a -d"
(cd $i; sudo -u $USER git repack -a -d)
fi
done
django>=1.10,<1.11
# Automatically renders Django forms in a pretty, Bootstrap-compatible way.
django-crispy-forms>=1.5.1
# Page data, answer data, ... all represented in JSON. This makes that editable
# in the Django admin.
jsonfield>=1.0.3,<1.1
# django-yamlfield
# but for
# https://github.com/datadesk/django-yamlfield/pull/12
# use
git+https://github.com/piotrpawlaczek/django-yamlfield.git@feature/dj-1.9.x
# For easy content formatting:
markdown>=2.6.3
# For rendering macros in content:
jinja2
six
# For math/symbolic questions
pymbolic
sympy
# Django timezone support
pytz
# Course content is stored in YAML.
pyyaml
# dulwich (git for Py3 support)
dulwich>=0.14
ecdsa
paramiko
# A date picker widget
git+https://github.com/inducer/django-bootstrap3-datetimepicker.git
# For in-class instant messaging
#dnspython # Py2
dnspython3 # Py3
# Py2 broken was broken in 1.3.1
git+https://github.com/fritzy/SleekXMPP.git@6e27f28c854ce4ae1d9f0cc8ee407bda8de97d3b
# To manage web dependencies
#django-bower
# pending release with https://github.com/nvbn/django-bower/issues/68
git+https://github.com/nvbn/django-bower.git
# For comfortable code entry (git for py3 fix in git rev 8979538)
git+https://github.com/lambdalisue/django-codemirror-widget.git
#django-codemirror-widget
# Optional, used for caching, requires 'libmemcached-dev' (Debian package name)
# pylibmc
# For code isolation in code questions
docker-py
# For code highlighting, required via the CodeHilite extension to
# Python-Markdown
pygments
# For grade export
unicodecsv
# To support network matching for facility recognition
ipaddress
# {{{ For interoperation with SAML2/Shibboleth
pysaml2
git+https://github.com/knaperek/djangosaml2.git
# Note: djangosaml2 uses a non-Py3 version of python-memcached directly. #
# Override that. Also: Don't be tempted to use this for Django caching--it's
# broken.
python-memcached==1.58
# }}}
# {{{ A task queue, used to execute long-running tasks
celery
# kombu 4 removes the Django ORM transport
kombu<4
vine
# Package index release 3.1.17 (current as of Nov 6, 2015)
# still affected by https://github.com/celery/django-celery/issues/411
git+https://github.com/celery/django-celery.git@6232c79557517afa74967fcb980c0db2fb1174d8
# }}}
# For searchable select widgets
django_select2>=5.5.0
# To sanitize HTML generated by user code
bleach
# Custom user migration created using
# https://bitbucket.org/spookylukey/django_custom_user_migration
# (but this package no longer needs to be installed)
# For query lexing
pytools
# For mypy (static type checking) support
typing
# vim: foldmethod=marker
import resolve from '@rollup/plugin-node-resolve';
import { brotliCompress } from 'zlib';
import { promisify } from 'util';
import commonjs from '@rollup/plugin-commonjs';
import terser from '@rollup/plugin-terser';
import styles from 'rollup-plugin-styler';
import gzipPlugin from 'rollup-plugin-gzip';
import replace from '@rollup/plugin-replace';
// `npm run build` -> `production` is true
// `npm run dev` -> `production` is false
const production = !process.env.ROLLUP_WATCH;
const brotliPromise = promisify(brotliCompress);
const defaultPlugins = [
resolve(),
styles(),
commonjs(),
production && terser(), // minify, but only in production
production && gzipPlugin(),
production && gzipPlugin({
customCompression: (content) => brotliPromise(Buffer.from(content)),
fileName: '.br',
}),
replace({
values: {
'process.env.NODE_ENV': JSON.stringify('production'),
},
preventAssignment: true,
}),
];
export default [
{
input: 'frontend/js/base.js',
output: {
file: 'frontend-dist/bundle-base.js',
format: 'iife',
sourcemap: true,
},
plugins: defaultPlugins,
},
{
input: 'frontend/js/base-with-markup.js',
output: {
file: 'frontend-dist/bundle-base-with-markup.js',
format: 'iife',
sourcemap: true,
},
plugins: defaultPlugins,
},
{
input: 'frontend/js/fullcalendar.js',
output: {
file: 'frontend-dist/bundle-fullcalendar.js',
format: 'iife',
sourcemap: true,
name: 'rlFullCalendar',
},
plugins: defaultPlugins,
},
{
input: 'frontend/js/datatables.js',
output: {
file: 'frontend-dist/bundle-datatables.js',
format: 'iife',
sourcemap: true,
name: 'rlDataTables',
// Otherwise there are complaints about datatables trying to set attributes
// on window.
strict: false,
},
plugins: defaultPlugins,
},
{
input: 'frontend/js/codemirror.js',
output: {
file: 'frontend-dist/bundle-codemirror.js',
format: 'iife',
sourcemap: true,
name: 'rlCodemirror',
},
plugins: defaultPlugins,
},
{
input: 'frontend/js/prosemirror.js',
output: {
file: 'frontend-dist/bundle-prosemirror.js',
format: 'iife',
sourcemap: true,
name: 'rlProsemirror',
},
plugins: defaultPlugins,
},
];
#! /bin/bash
mypy \
--fast-parser \
--strict-optional \
--silent-imports \
--disallow-untyped-calls \
relate course
# --disallow-untyped-defs \
#! /bin/bash
set -e
echo "-----------------------------------------------"
echo "Current directory: $(pwd)"
echo "Python executable: ${PY_EXE}"
echo "-----------------------------------------------"
# {{{ clean up
rm -Rf .env
rm -Rf build
find . -name '*.pyc' -delete
# }}}
git submodule update --init --recursive
# {{{ virtualenv
VENV_VERSION="virtualenv-13.0.3"
rm -Rf "$VENV_VERSION"
curl -k https://pypi.python.org/packages/source/v/virtualenv/$VENV_VERSION.tar.gz | tar xfz -
VIRTUALENV="${PY_EXE} -m venv"
${VIRTUALENV} -h > /dev/null || VIRTUALENV="$VENV_VERSION/virtualenv.py --no-setuptools -p ${PY_EXE}"
if [ -d ".env" ]; then
echo "**> virtualenv exists"
else
echo "**> creating virtualenv"
${VIRTUALENV} .env
fi
. .env/bin/activate
# }}}
# {{{ setuptools
#curl -k https://bitbucket.org/pypa/setuptools/raw/bootstrap-py24/ez_setup.py | python -
#curl -k https://ssl.tiker.net/software/ez_setup.py | python -
#curl -k https://bootstrap.pypa.io/ez_setup.py | python -
SETUPTOOLS_VERSION="setuptools-17.1.1"
curl -k https://pypi.python.org/packages/source/s/setuptools/$SETUPTOOLS_VERSION.tar.gz | tar xfz -
$PY_EXE $SETUPTOOLS_VERSION/setup.py install
# }}}
curl -k https://gitlab.tiker.net/inducer/pip/raw/7.0.3/contrib/get-pip.py | python -
# Not sure why pip ends up there, but in Py3.3, it sometimes does.
export PATH=`pwd`/.env/local/bin:$PATH
PIP="${PY_EXE} $(which pip)"
grep -v dnspython requirements.txt > req.txt
if [[ "$PY_EXE" = python2* ]]; then
$PIP install dnspython
else
$PIP install dnspython3
fi
$PIP install -r req.txt
cp local_settings.example.py local_settings.py
cd test
python ../manage.py test
__author__ = 'rolandh'
EDUPERSON_OID = "urn:oid:1.3.6.1.4.1.5923.1.1.1."
X500ATTR_OID = "urn:oid:2.5.4."
NOREDUPERSON_OID = "urn:oid:1.3.6.1.4.1.2428.90.1."
NETSCAPE_LDAP = "urn:oid:2.16.840.1.113730.3.1."
UCL_DIR_PILOT = 'urn:oid:0.9.2342.19200300.100.1.'
PKCS_9 = "urn:oid:1.2.840.113549.1.9.1."
UMICH = "urn:oid:1.3.6.1.4.1.250.1.57."
SCHAC = "urn:oid:1.3.6.1.4.1.25178.2."
MAP = {
"identifier": "urn:oasis:names:tc:SAML:2.0:attrname-format:uri",
"fro": {
EDUPERSON_OID+'2': 'eduPersonNickname',
EDUPERSON_OID+'9': 'eduPersonScopedAffiliation',
EDUPERSON_OID+'11': 'eduPersonAssurance',
EDUPERSON_OID+'10': 'eduPersonTargetedID',
EDUPERSON_OID+'4': 'eduPersonOrgUnitDN',
NOREDUPERSON_OID+'6': 'norEduOrgAcronym',
NOREDUPERSON_OID+'7': 'norEduOrgUniqueIdentifier',
NOREDUPERSON_OID+'4': 'norEduPersonLIN',
EDUPERSON_OID+'1': 'eduPersonAffiliation',
NOREDUPERSON_OID+'2': 'norEduOrgUnitUniqueNumber',
NETSCAPE_LDAP+'40': 'userSMIMECertificate',
NOREDUPERSON_OID+'1': 'norEduOrgUniqueNumber',
NETSCAPE_LDAP+'241': 'displayName',
UCL_DIR_PILOT+'37': 'associatedDomain',
EDUPERSON_OID+'6': 'eduPersonPrincipalName',
NOREDUPERSON_OID+'8': 'norEduOrgUnitUniqueIdentifier',
NOREDUPERSON_OID+'9': 'federationFeideSchemaVersion',
X500ATTR_OID+'53': 'deltaRevocationList',
X500ATTR_OID+'52': 'supportedAlgorithms',
X500ATTR_OID+'51': 'houseIdentifier',
X500ATTR_OID+'50': 'uniqueMember',
X500ATTR_OID+'19': 'physicalDeliveryOfficeName',
X500ATTR_OID+'18': 'postOfficeBox',
X500ATTR_OID+'17': 'postalCode',
X500ATTR_OID+'16': 'postalAddress',
X500ATTR_OID+'15': 'businessCategory',
X500ATTR_OID+'14': 'searchGuide',
EDUPERSON_OID+'5': 'eduPersonPrimaryAffiliation',
X500ATTR_OID+'12': 'title',
X500ATTR_OID+'11': 'ou',
X500ATTR_OID+'10': 'o',
X500ATTR_OID+'37': 'cACertificate',
X500ATTR_OID+'36': 'userCertificate',
X500ATTR_OID+'31': 'member',
X500ATTR_OID+'30': 'supportedApplicationContext',
X500ATTR_OID+'33': 'roleOccupant',
X500ATTR_OID+'32': 'owner',
NETSCAPE_LDAP+'1': 'carLicense',
PKCS_9+'1': 'email',
NETSCAPE_LDAP+'3': 'employeeNumber',
NETSCAPE_LDAP+'2': 'departmentNumber',
X500ATTR_OID+'39': 'certificateRevocationList',
X500ATTR_OID+'38': 'authorityRevocationList',
NETSCAPE_LDAP+'216': 'userPKCS12',
EDUPERSON_OID+'8': 'eduPersonPrimaryOrgUnitDN',
X500ATTR_OID+'9': 'street',
X500ATTR_OID+'8': 'st',
NETSCAPE_LDAP+'39': 'preferredLanguage',
EDUPERSON_OID+'7': 'eduPersonEntitlement',
X500ATTR_OID+'2': 'knowledgeInformation',
X500ATTR_OID+'7': 'l',
X500ATTR_OID+'6': 'c',
X500ATTR_OID+'5': 'serialNumber',
X500ATTR_OID+'4': 'sn',
UCL_DIR_PILOT+'60': 'jpegPhoto',
X500ATTR_OID+'65': 'pseudonym',
NOREDUPERSON_OID+'5': 'norEduPersonNIN',
UCL_DIR_PILOT+'3': 'mail',
UCL_DIR_PILOT+'25': 'dc',
X500ATTR_OID+'40': 'crossCertificatePair',
X500ATTR_OID+'42': 'givenName',
X500ATTR_OID+'43': 'initials',
X500ATTR_OID+'44': 'generationQualifier',
X500ATTR_OID+'45': 'x500UniqueIdentifier',
X500ATTR_OID+'46': 'dnQualifier',
X500ATTR_OID+'47': 'enhancedSearchGuide',
X500ATTR_OID+'48': 'protocolInformation',
X500ATTR_OID+'54': 'dmdName',
NETSCAPE_LDAP+'4': 'employeeType',
X500ATTR_OID+'22': 'teletexTerminalIdentifier',
X500ATTR_OID+'23': 'facsimileTelephoneNumber',
X500ATTR_OID+'20': 'telephoneNumber',
X500ATTR_OID+'21': 'telexNumber',
X500ATTR_OID+'26': 'registeredAddress',
X500ATTR_OID+'27': 'destinationIndicator',
X500ATTR_OID+'24': 'x121Address',
X500ATTR_OID+'25': 'internationaliSDNNumber',
X500ATTR_OID+'28': 'preferredDeliveryMethod',
X500ATTR_OID+'29': 'presentationAddress',
EDUPERSON_OID+'3': 'eduPersonOrgDN',
NOREDUPERSON_OID+'3': 'norEduPersonBirthDate',
UMICH+'57': 'labeledURI',
UCL_DIR_PILOT+'1': 'uid',
SCHAC+'1': 'schacMotherTongue',
SCHAC+'2': 'schacGender',
SCHAC+'3': 'schacDateOfBirth',
SCHAC+'4': 'schacPlaceOfBirth',
SCHAC+'5': 'schacCountryOfCitizenship',
SCHAC+'6': 'schacSn1',
SCHAC+'7': 'schacSn2',
SCHAC+'8': 'schacPersonalTitle',
SCHAC+'9': 'schacHomeOrganization',
SCHAC+'10': 'schacHomeOrganizationType',
SCHAC+'11': 'schacCountryOfResidence',
SCHAC+'12': 'schacUserPresenceID',
SCHAC+'13': 'schacPersonalPosition',
SCHAC+'14': 'schacPersonalUniqueCode',
SCHAC+'15': 'schacPersonalUniqueID',
SCHAC+'17': 'schacExpiryDate',
SCHAC+'18': 'schacUserPrivateAttribute',
SCHAC+'19': 'schacUserStatus',
SCHAC+'20': 'schacProjectMembership',
SCHAC+'21': 'schacProjectSpecificRole',
},
"to": {
'roleOccupant': X500ATTR_OID+'33',
'gn': X500ATTR_OID+'42',
'norEduPersonNIN': NOREDUPERSON_OID+'5',
'title': X500ATTR_OID+'12',
'facsimileTelephoneNumber': X500ATTR_OID+'23',
'mail': UCL_DIR_PILOT+'3',
'postOfficeBox': X500ATTR_OID+'18',
'fax': X500ATTR_OID+'23',
'telephoneNumber': X500ATTR_OID+'20',
'norEduPersonBirthDate': NOREDUPERSON_OID+'3',
'rfc822Mailbox': UCL_DIR_PILOT+'3',
'dc': UCL_DIR_PILOT+'25',
'countryName': X500ATTR_OID+'6',
'emailAddress': PKCS_9+'1',
'employeeNumber': NETSCAPE_LDAP+'3',
'organizationName': X500ATTR_OID+'10',
'eduPersonAssurance': EDUPERSON_OID+'11',
'norEduOrgAcronym': NOREDUPERSON_OID+'6',
'registeredAddress': X500ATTR_OID+'26',
'physicalDeliveryOfficeName': X500ATTR_OID+'19',
'associatedDomain': UCL_DIR_PILOT+'37',
'l': X500ATTR_OID+'7',
'stateOrProvinceName': X500ATTR_OID+'8',
'federationFeideSchemaVersion': NOREDUPERSON_OID+'9',
'pkcs9email': PKCS_9+'1',
'givenName': X500ATTR_OID+'42',
'givenname': X500ATTR_OID+'42',
'x500UniqueIdentifier': X500ATTR_OID+'45',
'eduPersonNickname': EDUPERSON_OID+'2',
'houseIdentifier': X500ATTR_OID+'51',
'street': X500ATTR_OID+'9',
'supportedAlgorithms': X500ATTR_OID+'52',
'preferredLanguage': NETSCAPE_LDAP+'39',
'postalAddress': X500ATTR_OID+'16',
'email': PKCS_9+'1',
'norEduOrgUnitUniqueIdentifier': NOREDUPERSON_OID+'8',
'eduPersonPrimaryOrgUnitDN': EDUPERSON_OID+'8',
'c': X500ATTR_OID+'6',
'teletexTerminalIdentifier': X500ATTR_OID+'22',
'o': X500ATTR_OID+'10',
'cACertificate': X500ATTR_OID+'37',
'telexNumber': X500ATTR_OID+'21',
'ou': X500ATTR_OID+'11',
'initials': X500ATTR_OID+'43',
'eduPersonOrgUnitDN': EDUPERSON_OID+'4',
'deltaRevocationList': X500ATTR_OID+'53',
'norEduPersonLIN': NOREDUPERSON_OID+'4',
'supportedApplicationContext': X500ATTR_OID+'30',
'eduPersonEntitlement': EDUPERSON_OID+'7',
'generationQualifier': X500ATTR_OID+'44',
'eduPersonAffiliation': EDUPERSON_OID+'1',
'edupersonaffiliation': EDUPERSON_OID+'1',
'eduPersonPrincipalName': EDUPERSON_OID+'6',
'edupersonprincipalname': EDUPERSON_OID+'6',
'localityName': X500ATTR_OID+'7',
'owner': X500ATTR_OID+'32',
'norEduOrgUnitUniqueNumber': NOREDUPERSON_OID+'2',
'searchGuide': X500ATTR_OID+'14',
'certificateRevocationList': X500ATTR_OID+'39',
'organizationalUnitName': X500ATTR_OID+'11',
'userCertificate': X500ATTR_OID+'36',
'preferredDeliveryMethod': X500ATTR_OID+'28',
'internationaliSDNNumber': X500ATTR_OID+'25',
'uniqueMember': X500ATTR_OID+'50',
'departmentNumber': NETSCAPE_LDAP+'2',
'enhancedSearchGuide': X500ATTR_OID+'47',
'userPKCS12': NETSCAPE_LDAP+'216',
'eduPersonTargetedID': EDUPERSON_OID+'10',
'norEduOrgUniqueNumber': NOREDUPERSON_OID+'1',
'x121Address': X500ATTR_OID+'24',
'destinationIndicator': X500ATTR_OID+'27',
'eduPersonPrimaryAffiliation': EDUPERSON_OID+'5',
'surname': X500ATTR_OID+'4',
'jpegPhoto': UCL_DIR_PILOT+'60',
'eduPersonScopedAffiliation': EDUPERSON_OID+'9',
'edupersonscopedaffiliation': EDUPERSON_OID+'9',
'protocolInformation': X500ATTR_OID+'48',
'knowledgeInformation': X500ATTR_OID+'2',
'employeeType': NETSCAPE_LDAP+'4',
'userSMIMECertificate': NETSCAPE_LDAP+'40',
'member': X500ATTR_OID+'31',
'streetAddress': X500ATTR_OID+'9',
'dmdName': X500ATTR_OID+'54',
'postalCode': X500ATTR_OID+'17',
'pseudonym': X500ATTR_OID+'65',
'dnQualifier': X500ATTR_OID+'46',
'crossCertificatePair': X500ATTR_OID+'40',
'eduPersonOrgDN': EDUPERSON_OID+'3',
'authorityRevocationList': X500ATTR_OID+'38',
'displayName': NETSCAPE_LDAP+'241',
'businessCategory': X500ATTR_OID+'15',
'serialNumber': X500ATTR_OID+'5',
'norEduOrgUniqueIdentifier': NOREDUPERSON_OID+'7',
'st': X500ATTR_OID+'8',
'carLicense': NETSCAPE_LDAP+'1',
'presentationAddress': X500ATTR_OID+'29',
'sn': X500ATTR_OID+'4',
'domainComponent': UCL_DIR_PILOT+'25',
'labeledURI': UMICH+'57',
'uid': UCL_DIR_PILOT+'1',
'schacMotherTongue':SCHAC+'1',
'schacGender': SCHAC+'2',
'schacDateOfBirth':SCHAC+'3',
'schacPlaceOfBirth': SCHAC+'4',
'schacCountryOfCitizenship':SCHAC+'5',
'schacSn1': SCHAC+'6',
'schacSn2': SCHAC+'7',
'schacPersonalTitle':SCHAC+'8',
'schacHomeOrganization': SCHAC+'9',
'schacHomeOrganizationType': SCHAC+'10',
'schacCountryOfResidence': SCHAC+'11',
'schacUserPresenceID': SCHAC+'12',
'schacPersonalPosition': SCHAC+'13',
'schacPersonalUniqueCode': SCHAC+'14',
'schacPersonalUniqueID': SCHAC+'15',
'schacExpiryDate': SCHAC+'17',
'schacUserPrivateAttribute': SCHAC+'18',
'schacUserStatus': SCHAC+'19',
'schacProjectMembership': SCHAC+'20',
'schacProjectSpecificRole': SCHAC+'21',
}
}
\ No newline at end of file
[flake8]
ignore = E126,E127,E128,E123,E226,E241,E242,E265,E402,W503
max-line-length=85
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from setuptools import setup, find_packages
# This script (for now) is only intended to install the 'relate' content helper
# script. relate-validate. Its use is not needed for (and unrelated to)
# deploying RELATE as a web service.
# Use 'pip install -r requirements.txt' first to install prerequisites.
setup(name="relate-courseware",
version="2016.1",
description="RELATE courseware",
long_description=open("README.rst", "rt").read(),
scripts=["bin/relate"],
author="Andreas Kloeckner",
url="https://github.com/inducer/relate",
author_email="inform@tiker.net",
license="MIT",
packages=find_packages(),
install_requires=[
"colorama",
])
from __future__ import annotations
__copyright__ = "Copyright (C) 2017 Dong Zhuang, Andreas Kloeckner, Zesheng Wang"
__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 datetime
import hashlib
import os
import re
import shutil
import sys
import tempfile
from collections import OrderedDict
from copy import deepcopy
from functools import partial
from types import MethodType
import memcache
from django.conf import settings
from django.contrib.auth import get_user_model
from django.core.exceptions import ImproperlyConfigured
from django.test import Client, RequestFactory, override_settings
from django.urls import resolve, reverse
from course.constants import (
flow_permission as fperm,
grade_aggregation_strategy as g_strategy,
participation_status,
user_status,
)
from course.content import get_course_repo_path, get_repo_blob
from course.flow import GradeInfo
from course.models import (
Course,
FlowPageData,
FlowPageVisit,
FlowSession,
GradingOpportunity,
Participation,
ParticipationRole,
)
from tests.constants import (
COMMIT_SHA_MAP,
FAKED_YAML_PATH,
QUIZ_FLOW_ID,
TEST_PAGE_TUPLE,
)
from tests.utils import mock
# {{{ data
CORRECTNESS_ATOL = 1e-05
CREATE_SUPERUSER_KWARGS = {
"username": "test_admin",
"password": "test_admin",
"email": "test_admin@example.com",
"first_name": "Test",
"last_name": "Admin"}
SINGLE_COURSE_SETUP_LIST = [
{
"course": {
"identifier": "test-course",
"name": "Test Course",
"number": "CS123",
"time_period": "Fall 2016",
"hidden": False,
"listed": True,
"accepts_enrollment": True,
"git_source": "https://github.com/inducer/relate-sample.git",
"course_file": "course.yml",
"events_file": "events.yml",
"enrollment_approval_required": False,
"enrollment_required_email_suffix": "",
"preapproval_require_verified_inst_id": True,
"from_email": "inform@tiker.net",
"notify_email": "inform@tiker.net",
},
"participations": [
{
"role_identifier": "instructor",
"user": {
"username": "test_instructor",
"password": "test_instructor",
"email": "test_instructor@example.com",
"first_name": "Test_ins",
"last_name": "Instructor"},
"status": participation_status.active
},
{
"role_identifier": "ta",
"user": {
"username": "test_ta",
"password": "test",
"email": "test_ta@example.com",
"first_name": "Test_ta",
"last_name": "TA"},
"status": participation_status.active
},
{
"role_identifier": "student",
"user": {
"username": "test_student",
"password": "test",
"email": "test_student@example.com",
"first_name": "Test_stu",
"last_name": "Student"},
"status": participation_status.active
}
],
}
]
TWO_COURSE_SETUP_LIST = deepcopy(SINGLE_COURSE_SETUP_LIST)
TWO_COURSE_SETUP_LIST[0]["course"]["identifier"] = "test-course1"
TWO_COURSE_SETUP_LIST += deepcopy(SINGLE_COURSE_SETUP_LIST)
TWO_COURSE_SETUP_LIST[1]["course"]["identifier"] = "test-course2"
NONE_PARTICIPATION_USER_CREATE_KWARG_LIST = [
{
"username": "test_user1",
"password": "test_user1",
"email": "test_user1@suffix.com",
"first_name": "Test",
"last_name": "User1",
"institutional_id": "test_user1_institutional_id",
"institutional_id_verified": True,
"status": user_status.active
},
{
"username": "test_user2",
"password": "test_user2",
"email": "test_user2@nosuffix.com",
"first_name": "Test",
"last_name": "User2",
"institutional_id": "test_user2_institutional_id",
"institutional_id_verified": False,
"status": user_status.active
},
{
"username": "test_user3",
"password": "test_user3",
"email": "test_user3@suffix.com",
"first_name": "Test",
"last_name": "User3",
"institutional_id": "test_user3_institutional_id",
"institutional_id_verified": True,
"status": user_status.unconfirmed
},
{
"username": "test_user4",
"password": "test_user4",
"email": "test_user4@no_suffix.com",
"first_name": "Test",
"last_name": "User4",
"institutional_id": "test_user4_institutional_id",
"institutional_id_verified": False,
"status": user_status.unconfirmed
}
]
try:
mc = memcache.Client(["127.0.0.1:11211"])
except Exception:
pass
SELECT2_HTML_FIELD_ID_SEARCH_PATTERN = re.compile(r'data-field_id="([^"]+)"')
# }}}
def git_source_url_to_cache_keys(url):
url_hash = hashlib.md5(url.encode("utf-8")).hexdigest()
return (
f"test_course:{url_hash}",
f"test_sha:{url_hash}"
)
class CourseCreateFailure(Exception):
pass
class classmethod_with_client: # noqa: N801
"""This acts like Python's built-in ``classmethod``, with one change:
When called on an instance (i.e. not a class), it automatically supplies
``self.client`` as the first argument.
.. note::
This isn't immensely logical, but it helped avoid an expensive
refactor of the test code to explicitly always pass the client.
(The prior state was much worse: a class-global client was being
used. Almost fortunately, Django 3.2 broke this usage.)
"""
def __init__(self, f):
self.f = f
def __get__(self, obj, cls=None):
if obj is None:
return MethodType(self.f, cls)
else:
return partial(MethodType(self.f, type(obj)), obj.client)
# {{{ ResponseContextMixin
class ResponseContextMixin:
"""
Response context refers to "the template Context instance that was used
to render the template that produced the response content".
Ref: https://docs.djangoproject.com/en/dev/topics/testing/tools/#django.test.Response.context
"""
def get_response_context_value_by_name(self, response, context_name):
try:
value = response.context[context_name]
except KeyError:
self.fail(f"{context_name} does not exist in given response")
else:
return value
def assertResponseHasNoContext(self, response, context_name): # noqa
has_context = True
try:
response.context[context_name]
except KeyError:
has_context = False
if has_context:
self.fail(f"{context_name} unexpectedly exist in given response")
def assertResponseContextIsNone(self, resp, context_name): # noqa
try:
value = self.get_response_context_value_by_name(resp, context_name)
except AssertionError:
# the context item doesn't exist
pass
else:
self.assertIsNone(value)
def assertResponseContextIsNotNone(self, resp, context_name, msg=""): # noqa
value = self.get_response_context_value_by_name(resp, context_name)
self.assertIsNotNone(value, msg)
def assertResponseContextEqual(self, resp, context_name, expected_value): # noqa
value = self.get_response_context_value_by_name(resp, context_name)
try:
self.assertTrue(float(value) - float(expected_value) <= 1e-04)
return
except Exception:
self.assertEqual(value, expected_value)
def assertResponseContextContains(self, resp, # noqa
context_name, expected_value, html=False,
in_bulk=False):
value = self.get_response_context_value_by_name(resp, context_name)
if in_bulk:
if not isinstance(expected_value, list):
expected_value = [expected_value]
for v in expected_value:
if not html:
self.assertIn(v, value)
else:
self.assertInHTML(v, value)
else:
if not html:
self.assertIn(expected_value, value)
else:
self.assertInHTML(expected_value, value)
def assertResponseContextRegex( # noqa
self, resp,
context_name, expected_value_regex):
value = self.get_response_context_value_by_name(resp, context_name)
self.assertRegex(value, expected_value_regex)
def get_response_context_answer_feedback(self, response):
return self.get_response_context_value_by_name(response, "feedback")
def get_response_context_answer_feedback_string(self, response,
include_bulk_feedback=True):
answer_feedback = self.get_response_context_value_by_name(
response, "feedback")
self.assertTrue(hasattr(answer_feedback, "feedback"))
if not include_bulk_feedback:
return answer_feedback.feedback
if answer_feedback.bulk_feedback is None:
return answer_feedback.feedback
else:
if answer_feedback.feedback is None:
return answer_feedback.bulk_feedback
return answer_feedback.feedback + answer_feedback.bulk_feedback
def assertResponseContextAnswerFeedbackContainsFeedback( # noqa
self, response, expected_feedback,
include_bulk_feedback=True, html=False):
feedback_str = self.get_response_context_answer_feedback_string(
response, include_bulk_feedback)
if not html:
self.assertIn(expected_feedback, feedback_str)
else:
self.assertInHTML(expected_feedback, feedback_str)
def assertResponseContextAnswerFeedbackNotContainsFeedback( # noqa
self, response, expected_feedback,
include_bulk_feedback=True,
html=False):
feedback_str = self.get_response_context_answer_feedback_string(
response, include_bulk_feedback)
if not html:
self.assertNotIn(expected_feedback, feedback_str)
else:
self.assertInHTML(expected_feedback, feedback_str, count=0)
def assertResponseContextAnswerFeedbackCorrectnessEquals( # noqa
self, response, expected_correctness):
answer_feedback = self.get_response_context_answer_feedback(response)
if expected_correctness is None:
try:
self.assertTrue(hasattr(answer_feedback, "correctness"))
except AssertionError:
pass
else:
self.assertIsNone(answer_feedback.correctness)
else:
if answer_feedback.correctness is None:
return self.fail(f"The returned correctness is None, not {expected_correctness}") # noqa: E501
self.assertTrue(
abs(float(answer_feedback.correctness)
- float(str(expected_correctness))) < CORRECTNESS_ATOL,
f"{str(answer_feedback.correctness)[:5]} does not equal {str(expected_correctness)[:5]}") # noqa: E501
def get_response_body(self, response):
return self.get_response_context_value_by_name(response, "body")
def get_page_response_correct_answer(self, response):
return self.get_response_context_value_by_name(response, "correct_answer")
def get_page_response_feedback(self, response):
return self.get_response_context_value_by_name(response, "feedback")
def debug_print_response_context_value(self, resp, context_name):
try:
value = self.get_response_context_value_by_name(resp, context_name)
print(f"\n-----------context {context_name}-------------")
if isinstance(value, list | tuple):
from course.validation import ValidationWarning
for v in value:
if isinstance(v, ValidationWarning):
print(v.text)
else:
print(repr(v))
else:
print(value)
print("-----------context end-------------\n")
except AssertionError:
print(f"\n-------no value for context {context_name}----------")
def get_select2_field_id_from_response(self, response,
form_context_name="form"):
self.assertResponseContextIsNotNone(
response, form_context_name,
f"The response doesn't contain a context named '{form_context_name}'")
form_str = str(response.context[form_context_name])
m = SELECT2_HTML_FIELD_ID_SEARCH_PATTERN.search(form_str)
assert m, f"pattern not found in {form_str}"
return m.group(1)
def select2_get_request(self, field_id, term=None,
select2_urlname="django_select2:auto-json"):
select2_url = reverse(select2_urlname)
params = {"field_id": field_id}
if term is not None:
assert isinstance(term, str)
term = term.strip()
if term:
params["term"] = term
return self.client.get(select2_url, params,
HTTP_X_REQUESTED_WITH="XMLHttpRequest")
def get_select2_response_data(self, response, key="results"):
import json
return json.loads(response.content.decode("utf-8"))[key]
class SuperuserCreateMixin(ResponseContextMixin):
create_superuser_kwargs = CREATE_SUPERUSER_KWARGS
@classmethod
def setUpTestData(cls):
# Create superuser, without this, we cannot
# create user, course and participation.
cls.superuser = cls.create_superuser()
cls.settings_git_root_override = (
override_settings(GIT_ROOT=tempfile.mkdtemp()))
cls.settings_git_root_override.enable()
super().setUpTestData()
@classmethod
def add_user_permission(cls, user, perm, model=Course):
from django.contrib.contenttypes.models import ContentType
content_type = ContentType.objects.get_for_model(model)
from django.contrib.auth.models import Permission
permission = Permission.objects.get(
codename=perm, content_type=content_type)
user.user_permissions.add(permission)
@classmethod
def create_superuser(cls):
return get_user_model().objects.create_superuser(
**cls.create_superuser_kwargs)
@classmethod
def get_sign_up_view_url(cls):
return reverse("relate-sign_up")
@classmethod_with_client
def get_sign_up(cls, client, *, follow=True): # noqa: N805
return client.get(cls.get_sign_up_view_url(), follow=follow)
@classmethod_with_client
def post_sign_up(cls, client, data, *, follow=True): # noqa: N805
return client.post(cls.get_sign_up_view_url(), data, follow=follow)
@classmethod
def get_profile_view_url(cls):
return reverse("relate-user_profile")
@classmethod_with_client
def get_profile(cls, client, *, follow=True): # noqa: N805
return client.get(cls.get_profile_view_url(), follow=follow)
@classmethod_with_client
def post_profile(cls, client, data, *, follow=True): # noqa: N805
data.update({"submit_user": [""]})
return client.post(cls.get_profile_view_url(), data, follow=follow)
@classmethod
def post_signout(cls, client, data, *, follow=True):
return client.post(cls.get_sign_up_view_url(), data, follow=follow)
@classmethod
def get_impersonate_view_url(cls):
return reverse("relate-impersonate")
@classmethod
def get_stop_impersonate_view_url(cls):
return reverse("relate-stop_impersonating")
@classmethod_with_client
def get_impersonate_view(cls, client): # noqa: N805
return client.get(cls.get_impersonate_view_url())
@classmethod_with_client
def post_impersonate_view(cls, client, # noqa: N805
impersonatee, *, follow=True):
data = {"add_impersonation_header": ["on"],
"submit": [""],
}
data["user"] = [str(impersonatee.pk)]
return client.post(cls.get_impersonate_view_url(), data, follow=follow)
@classmethod_with_client
def get_stop_impersonate(cls, client, *, follow=True): # noqa: N805
return client.get(cls.get_stop_impersonate_view_url(), follow=follow)
@classmethod_with_client
def post_stop_impersonate(cls, client, *, # noqa: N805
data=None, follow=True):
if not data:
data = {"stop_impersonating": ""}
return client.post(
cls.get_stop_impersonate_view_url(), data, follow=follow)
@classmethod
def get_confirm_stop_impersonate_view_url(cls):
return reverse("relate-confirm_stop_impersonating")
@classmethod
def get_confirm_stop_impersonate(cls, client, *, follow=True):
return client.get(
cls.get_confirm_stop_impersonate_view_url(), follow=follow)
@classmethod
def post_confirm_stop_impersonate(cls, client, *, follow=True):
return client.post(
cls.get_confirm_stop_impersonate_view_url(), {}, follow=follow)
@classmethod
def get_reset_password_url(cls, use_instid=False):
kwargs = {}
if use_instid:
kwargs["field"] = "instid"
return reverse("relate-reset_password", kwargs=kwargs)
@classmethod_with_client
def get_reset_password(cls, client, *, use_instid=False): # noqa: N805
return client.get(cls.get_reset_password_url(use_instid))
@classmethod_with_client
def post_reset_password(cls, client, data, *, use_instid=False): # noqa: N805
return client.post(cls.get_reset_password_url(use_instid),
data=data)
def get_reset_password_stage2_url(self, user_id, sign_in_key, **kwargs):
url = reverse("relate-reset_password_stage2", args=(user_id, sign_in_key))
querystring = kwargs.pop("querystring", None)
if querystring is not None:
assert isinstance(querystring, dict)
url += ("?{}".format("&".join(
[f"{k}={v}"
for (k, v) in querystring.items()])))
return url
def get_reset_password_stage2(self, user_id, sign_in_key, **kwargs):
return self.client.get(self.get_reset_password_stage2_url(
user_id=user_id, sign_in_key=sign_in_key, **kwargs))
def post_reset_password_stage2(self, user_id, sign_in_key, data, **kwargs):
return self.client.post(self.get_reset_password_stage2_url(
user_id=user_id, sign_in_key=sign_in_key, **kwargs), data=data)
@classmethod
def get_fake_time_url(cls):
return reverse("relate-set_fake_time")
@classmethod_with_client
def get_set_fake_time(cls, client): # noqa: N805
return client.get(cls.get_fake_time_url())
@classmethod_with_client
def post_set_fake_time(cls, client, data, *, follow=True): # noqa: N805
return client.post(cls.get_fake_time_url(), data, follow=follow)
def assertSessionFakeTimeEqual(self, session, expected_date_time): # noqa
fake_time_timestamp = session.get("relate_fake_time", None)
if fake_time_timestamp is None:
faked_time = None
if expected_date_time is not None:
raise AssertionError(
"the session doesn't have 'relate_fake_time' attribute")
else:
faked_time = datetime.datetime.fromtimestamp(fake_time_timestamp)
self.assertEqual(faked_time, expected_date_time)
def assertSessionFakeTimeIsNone(self, session): # noqa
self.assertSessionFakeTimeEqual(session, None)
@classmethod
def get_set_pretend_facilities_url(cls):
return reverse("relate-set_pretend_facilities")
@classmethod_with_client
def get_set_pretend_facilities(cls, client): # noqa: N805
return client.get(cls.get_set_pretend_facilities_url())
@classmethod_with_client
def post_set_pretend_facilities(cls, client, data, *, # noqa: N805
follow=True):
return client.post(cls.get_set_pretend_facilities_url(), data,
follow=follow)
@classmethod
def force_remove_all_course_dir(cls):
# This is only necessary for courses which are created test wise,
# not class wise.
from course.content import get_course_repo_path
from relate.utils import force_remove_path
for c in Course.objects.all():
force_remove_path(get_course_repo_path(c))
def assertSessionPretendFacilitiesContains(self, session, expected_facilities): # noqa
pretended = session.get("relate_pretend_facilities", None)
if expected_facilities is None:
return self.assertIsNone(pretended)
if pretended is None:
raise AssertionError(
"the session doesn't have "
"'relate_pretend_facilities' attribute")
if isinstance(expected_facilities, list | tuple):
self.assertTrue(set(expected_facilities).issubset(set(pretended)))
else:
self.assertTrue(expected_facilities in pretended)
def assertSessionPretendFacilitiesIsNone(self, session): # noqa
pretended = session.get("relate_pretend_facilities", None)
self.assertIsNone(pretended)
def assertFormErrorLoose(self, response, errors, form_name="form"): # noqa
"""Assert that errors is found in response.context['form'] errors"""
import itertools
if errors is None:
errors = []
if not isinstance(errors, list | tuple):
errors = [errors]
try:
form_errors = ". ".join(list(
itertools.chain(*response.context[form_name].errors.values())))
except TypeError:
form_errors = None
if form_errors is None or not form_errors:
if errors:
self.fail(f"{form_name} has no error")
else:
return
if form_errors:
if not errors:
self.fail(
f"{form_name} unexpectedly has following errors: {form_errors!r}")
for err in errors:
self.assertIn(err, form_errors)
# }}}
# {{{ get_flow_page_ordinal_from_page_id, get_flow_page_id_from_page_ordinal
def get_flow_page_ordinal_from_page_id(flow_session_id, page_id,
with_group_id=False):
flow_page_data = FlowPageData.objects.get(
flow_session__id=flow_session_id,
page_id=page_id
)
if with_group_id:
return flow_page_data.page_ordinal, flow_page_data.group_id
return flow_page_data.page_ordinal
def get_flow_page_id_from_page_ordinal(flow_session_id, page_ordinal,
with_group_id=False):
flow_page_data = FlowPageData.objects.get(
flow_session__id=flow_session_id,
page_ordinal=page_ordinal
)
if with_group_id:
return flow_page_data.page_id, flow_page_data.group_id
return flow_page_data.page_id
# }}}
# {{{ CoursesTestMixinBase
class _ClientUserSwitcher:
def __init__(self, client, logged_in_user, switch_to):
self.client = client
self.logged_in_user = logged_in_user
self.switch_to = switch_to
def __enter__(self):
if self.logged_in_user == self.switch_to:
return
if self.switch_to is None:
self.client.logout()
return
self.client.force_login(self.switch_to)
def __exit__(self, exc_type, exc_val, exc_tb):
if self.logged_in_user == self.switch_to:
return
if self.logged_in_user is None:
self.client.logout()
return
self.client.force_login(self.logged_in_user)
def __call__(self, func):
from functools import wraps
@wraps(func)
def wrapper(*args, **kw):
with self:
return func(*args, **kw)
return wrapper
class CoursesTestMixinBase(SuperuserCreateMixin):
# A list of Dicts, each of which contain a course dict and a list of
# participations. See SINGLE_COURSE_SETUP_LIST for the setup for one course.
courses_setup_list = []
none_participation_user_create_kwarg_list = []
courses_attributes_extra_list = None
override_settings_at_post_create_course = {}
@classmethod
def setUpTestData(cls):
super().setUpTestData()
client = Client()
client.force_login(cls.superuser)
cls.default_flow_params = None
cls.n_courses = 0
if cls.courses_attributes_extra_list is not None:
if (len(cls.courses_attributes_extra_list)
!= len(cls.courses_setup_list)):
raise ValueError(
"'courses_attributes_extra_list' must has equal length "
"with courses")
for i, course_setup in enumerate(cls.courses_setup_list):
if "course" not in course_setup:
continue
cls.n_courses += 1
course_identifier = course_setup["course"]["identifier"]
course_setup_kwargs = course_setup["course"]
if cls.courses_attributes_extra_list:
extra_attrs = cls.courses_attributes_extra_list[i]
assert isinstance(extra_attrs, dict)
course_setup_kwargs.update(extra_attrs)
cls.create_course(client, course_setup_kwargs)
course = Course.objects.get(identifier=course_identifier)
if "participations" in course_setup:
for participation in course_setup["participations"]:
create_user_kwargs = participation.get("user")
if not create_user_kwargs:
continue
role_identifier = participation.get("role_identifier")
if not role_identifier:
continue
cls.create_participation(
course=course,
user_or_create_user_kwargs=create_user_kwargs,
role_identifier=role_identifier,
status=participation.get("status",
participation_status.active)
)
# Remove superuser from participation for further test
# such as impersonate in auth module
try:
superuser_participations = (
Participation.objects.filter(user=cls.superuser))
for sp in superuser_participations:
Participation.delete(sp)
except Participation.DoesNotExist:
pass
cls.non_participation_users = get_user_model().objects.none()
if cls.none_participation_user_create_kwarg_list:
pks = []
for create_user_kwargs in (
cls.none_participation_user_create_kwarg_list):
user = cls.create_user(create_user_kwargs)
pks.append(user.pk)
cls.non_participation_users = (
get_user_model().objects.filter(pk__in=pks))
cls.course_qset = Course.objects.all()
@classmethod
def create_user(cls, create_user_kwargs):
user, created = get_user_model().objects.get_or_create(
email__iexact=create_user_kwargs["email"], defaults=create_user_kwargs)
if created:
try:
# TODO: why pop failed here?
password = create_user_kwargs["password"]
except Exception:
raise
user.set_password(password)
user.save()
return user
@classmethod
def create_participation(
cls, course, user_or_create_user_kwargs,
role_identifier=None, status=None):
if isinstance(user_or_create_user_kwargs, get_user_model()):
user = user_or_create_user_kwargs
else:
assert isinstance(user_or_create_user_kwargs, dict)
user = cls.create_user(user_or_create_user_kwargs)
if status is None:
status = participation_status.active
participation, p_created = Participation.objects.get_or_create(
user=user,
course=course,
status=status
)
if role_identifier is None:
role_identifier = "student"
if p_created:
role = ParticipationRole.objects.filter(
course=course, identifier=role_identifier)
participation.roles.set(role)
return participation
@classmethod_with_client
def post_create_course(cls, client, create_course_kwargs, *, # noqa: N805
raise_error=True, login_superuser=True):
# To speed up, use create_course instead, this is better used for tests
if login_superuser:
client.force_login(cls.superuser)
existing_course_count = Course.objects.count()
with override_settings(**cls.override_settings_at_post_create_course):
resp = client.post(cls.get_set_up_new_course_url(),
data=create_course_kwargs)
if raise_error:
all_courses = Course.objects.all()
if not all_courses.count() == existing_course_count + 1:
error_string = None
# most probably the reason course creation form error
form_context = resp.context.__getitem__("form")
assert form_context
error_list = []
if form_context.errors:
error_list = [
"{}: {}".format(field,
"\n".join([f"{type(e).__name__}:{e!s}"
for e in errs]))
for field, errs
in form_context.errors.as_data().items()]
non_field_errors = form_context.non_field_errors()
if non_field_errors:
error_list.append(repr(non_field_errors))
if error_list:
error_string = "\n".join(error_list)
if not error_string:
error_string = ("course creation failed for unknown errors")
raise CourseCreateFailure(error_string)
# the course is created successfully
last_course = all_courses.last()
assert last_course
if "trusted_for_markup" in create_course_kwargs:
# This attribute is not settable via POST by the user, so set it
# manually here.
last_course.trusted_for_markup = \
create_course_kwargs["trusted_for_markup"]
last_course.save()
url_cache_key, commit_sha_cache_key = (
git_source_url_to_cache_keys(last_course.git_source))
mc.set_multi({url_cache_key: get_course_repo_path(last_course),
commit_sha_cache_key: last_course.active_git_commit_sha},
time=120000
)
return resp
@classmethod_with_client
def create_course(cls, client, create_course_kwargs, *, # noqa: N805
raise_error=True):
has_cached_repo = False
repo_cache_key, commit_sha_cache_key = (
git_source_url_to_cache_keys(create_course_kwargs["git_source"]))
try:
exist_course_repo_path = mc.get(repo_cache_key)
exist_commit_sha = mc.get(commit_sha_cache_key)
if os.path.isdir(exist_course_repo_path):
has_cached_repo = bool(exist_course_repo_path and exist_commit_sha)
else:
has_cached_repo = False
except Exception:
pass
if not has_cached_repo:
# fall back to post create
return cls.post_create_course(
client, create_course_kwargs, raise_error=raise_error)
existing_course_count = Course.objects.count()
new_course_repo_path = os.path.join(settings.GIT_ROOT,
create_course_kwargs["identifier"])
shutil.copytree(exist_course_repo_path, new_course_repo_path)
create_kwargs = deepcopy(create_course_kwargs)
create_kwargs["active_git_commit_sha"] = exist_commit_sha
Course.objects.create(**create_kwargs)
assert Course.objects.count() == existing_course_count + 1
@classmethod
def get_course_view_url(cls, view_name, course_identifier=None):
course_identifier = (
course_identifier or cls.get_default_course_identifier())
return reverse(view_name, args=[course_identifier])
@classmethod
def get_course_calender_url(cls, course_identifier=None):
return cls.get_course_view_url(
"relate-view_calendar", course_identifier)
@classmethod
def get_set_up_new_course_url(cls):
return reverse("relate-set_up_new_course")
@classmethod_with_client
def get_set_up_new_course(cls, client): # noqa: N805
return client.get(cls.get_update_course_url)
@classmethod
def get_edit_course_url(cls, course_identifier=None):
course_identifier = (
course_identifier or cls.get_default_course_identifier())
return cls.get_course_view_url("relate-edit_course", course_identifier)
@classmethod_with_client
def post_edit_course(cls, client, data, *, course=None): # noqa: N805
course = course or cls.get_default_course()
edit_course_url = cls.get_edit_course_url(course.identifier)
return client.post(edit_course_url, data)
@classmethod_with_client
def get_edit_course(cls, client, *, course=None): # noqa: N805
course = course or cls.get_default_course()
return client.get(cls.get_edit_course_url(course.identifier))
@classmethod
def get_course_page_url(cls, course_identifier=None):
return cls.get_course_view_url("relate-course_page", course_identifier)
@classmethod
def get_finish_flow_session_view_url(cls, course_identifier=None,
flow_session_id=None):
course_identifier = (
course_identifier or cls.get_default_course_identifier())
if flow_session_id is None:
flow_session_id = cls.get_default_flow_session_id(course_identifier)
kwargs = {"course_identifier": course_identifier,
"flow_session_id": flow_session_id}
return reverse("relate-finish_flow_session_view", kwargs=kwargs)
@classmethod
def _get_grades_url(cls, args=None, kwargs=None):
return reverse("relate-view_participant_grades",
args=args, kwargs=kwargs)
@classmethod
def get_my_grades_url(cls, course_identifier=None):
course_identifier = (
course_identifier or cls.get_default_course_identifier())
return cls._get_grades_url(args=[course_identifier])
@classmethod_with_client
def get_my_grades_view(cls, client, *, course_identifier=None): # noqa: N805
return client.get(cls.get_my_grades_url(course_identifier))
@classmethod
def get_participant_grades_url(cls, participation_id, course_identifier=None):
course_identifier = (
course_identifier or cls.get_default_course_identifier())
return cls._get_grades_url(
kwargs={"course_identifier": course_identifier,
"participation_id": participation_id})
@classmethod_with_client
def get_participant_grades_view(
cls, client, participation_id, *, # noqa: N805
course_identifier=None, force_login_instructor=True):
course_identifier = (
course_identifier or cls.get_default_course_identifier())
if force_login_instructor:
switch_to = cls.get_default_instructor_user(course_identifier)
else:
switch_to = cls.get_logged_in_user()
with cls.temporarily_switch_to_user(switch_to):
return client.get(
cls.get_participant_grades_url(participation_id, course_identifier))
@classmethod
def get_gradebook_url_by_opp_id(cls, opp_id, course_identifier=None):
course_identifier = (
course_identifier or cls.get_default_course_identifier())
kwargs = {"course_identifier": course_identifier,
"opp_id": opp_id}
return reverse("relate-view_grades_by_opportunity",
kwargs=kwargs)
def view_participant_grades_url(self, participation_id, course_identifier=None):
course_identifier = (
course_identifier or self.get_default_course_identifier())
kwargs = {"course_identifier": course_identifier}
if participation_id is not None:
kwargs["participation_id"] = participation_id
return reverse("relate-view_participant_grades", kwargs=kwargs)
def get_view_participant_grades(self, participation_id, course_identifier=None):
return self.client.get(self.view_participant_grades_url(
participation_id, course_identifier))
def get_view_my_grades(self, course_identifier=None):
return self.client.get(self.view_participant_grades_url(
participation_id=None, course_identifier=course_identifier))
@classmethod
def get_gradebook_by_opp_url(
cls, gopp_identifier, view_page_grades=False, course_identifier=None):
course_identifier = (
course_identifier or cls.get_default_course_identifier())
opp_id = GradingOpportunity.objects.get(
course__identifier=course_identifier,
identifier=gopp_identifier).pk
url = cls.get_gradebook_url_by_opp_id(opp_id, course_identifier)
if view_page_grades:
url += "?view_page_grades=1"
return url
@classmethod_with_client
def get_gradebook_by_opp_view(
cls, client, gopp_identifier, *, # noqa: N805
view_page_grades=False, course_identifier=None,
force_login_instructor=True):
course_identifier = (
course_identifier or cls.get_default_course_identifier())
if force_login_instructor:
switch_to = cls.get_default_instructor_user(course_identifier)
else:
switch_to = cls.get_logged_in_user(client)
with cls.temporarily_switch_to_user(client, switch_to):
return client.get(cls.get_gradebook_by_opp_url(
gopp_identifier, view_page_grades, course_identifier))
@classmethod_with_client
def post_gradebook_by_opp_view(
cls, client, gopp_identifier, post_data, *, # noqa: N805
view_page_grades=False,
course_identifier=None,
force_login_instructor=True):
course_identifier = (
course_identifier or cls.get_default_course_identifier())
if force_login_instructor:
switch_to = cls.get_default_instructor_user(course_identifier)
else:
switch_to = cls.get_logged_in_user(client)
with cls.temporarily_switch_to_user(client, switch_to):
return client.post(
cls.get_gradebook_by_opp_url(
gopp_identifier, view_page_grades, course_identifier),
data=post_data)
@classmethod
def get_reopen_session_url(cls, gopp_identifier, flow_session_id=None,
course_identifier=None):
course_identifier = (
course_identifier or cls.get_default_course_identifier())
opp_id = GradingOpportunity.objects.get(
course__identifier=course_identifier,
identifier=gopp_identifier).pk
if flow_session_id is None:
flow_session_id = cls.get_default_flow_session_id(course_identifier)
kwargs = {"course_identifier": course_identifier,
"opportunity_id": opp_id,
"flow_session_id": flow_session_id}
return reverse("relate-view_reopen_session", kwargs=kwargs)
@classmethod_with_client
def get_reopen_session_view(cls, client, # noqa: N805
gopp_identifier, *, flow_session_id=None,
course_identifier=None, force_login_instructor=True):
course_identifier = (
course_identifier or cls.get_default_course_identifier())
if force_login_instructor:
switch_to = cls.get_default_instructor_user(course_identifier)
else:
switch_to = cls.get_logged_in_user()
with cls.temporarily_switch_to_user(client, switch_to):
return client.get(
cls.get_reopen_session_url(
gopp_identifier, flow_session_id, course_identifier))
@classmethod_with_client
def post_reopen_session_view(cls, client, # noqa: N805
gopp_identifier, data, *,
flow_session_id=None, course_identifier=None,
force_login_instructor=True):
course_identifier = (
course_identifier or cls.get_default_course_identifier())
if force_login_instructor:
switch_to = cls.get_default_instructor_user(course_identifier)
else:
switch_to = cls.get_logged_in_user()
with cls.temporarily_switch_to_user(client, switch_to):
return client.post(
cls.get_reopen_session_url(
gopp_identifier, flow_session_id, course_identifier), data=data)
@classmethod
def get_single_grade_url(cls, participation_id, opp_id,
course_identifier=None):
course_identifier = (
course_identifier or cls.get_default_course_identifier())
kwargs = {"course_identifier": course_identifier,
"opportunity_id": opp_id,
"participation_id": participation_id}
return reverse("relate-view_single_grade", kwargs=kwargs)
@classmethod_with_client
def get_view_single_grade(cls, client, # noqa: N805
participation, gopp, *,
course_identifier=None, force_login_instructor=True):
course_identifier = (
course_identifier or cls.get_default_course_identifier())
opp_id = GradingOpportunity.objects.get(
course__identifier=course_identifier,
identifier=gopp.identifier).pk
if force_login_instructor:
switch_to = cls.get_default_instructor_user(course_identifier)
else:
switch_to = cls.get_logged_in_user(client)
with cls.temporarily_switch_to_user(client, switch_to):
return client.get(cls.get_single_grade_url(
participation.pk, opp_id, course_identifier))
@classmethod_with_client
def post_view_single_grade(cls, client, # noqa: N805
participation, gopp, data, *,
course_identifier=None, force_login_instructor=True):
course_identifier = (
course_identifier or cls.get_default_course_identifier())
opp_id = GradingOpportunity.objects.get(
course__identifier=course_identifier,
identifier=gopp.identifier).pk
if force_login_instructor:
switch_to = cls.get_default_instructor_user(course_identifier)
else:
switch_to = cls.get_logged_in_user(client)
with cls.temporarily_switch_to_user(client, switch_to):
return client.post(cls.get_single_grade_url(
participation.pk, opp_id, course_identifier),
data=data)
@classmethod_with_client
def get_logged_in_user(cls, client): # noqa: N805
try:
logged_in_user_id = client.session["_auth_user_id"]
from django.contrib.auth import get_user_model
logged_in_user = get_user_model().objects.get(
pk=int(logged_in_user_id))
except KeyError:
logged_in_user = None
return logged_in_user
@classmethod_with_client
def temporarily_switch_to_user(cls, client, switch_to): # noqa: N805
return _ClientUserSwitcher(
client, cls.get_logged_in_user(client), switch_to)
@classmethod
def get_default_course(cls):
if Course.objects.count() > 1:
raise AttributeError(
"'course' arg can not be omitted for "
"testcases with more than one courses")
raise NotImplementedError
@classmethod
def get_default_course_identifier(cls):
if Course.objects.count() > 1:
raise AttributeError(
"'course_identifier' arg can not be omitted for "
"testcases with more than one courses")
raise NotImplementedError
@classmethod
def get_latest_session_id(cls, course_identifier):
flow_session_qset = FlowSession.objects.filter(
course__identifier=course_identifier).order_by("-pk")[:1]
if flow_session_qset:
return flow_session_qset[0].id
else:
return None
@classmethod
def get_default_flow_session_id(cls, course_identifier):
raise NotImplementedError
@classmethod
def update_default_flow_session_id(cls, course_identifier):
raise NotImplementedError
@classmethod
def get_default_instructor_user(cls, course_identifier):
return Participation.objects.filter(
course__identifier=course_identifier,
roles__identifier="instructor",
status=participation_status.active
).first().user
@classmethod
def update_course_attribute(cls, attrs, course=None):
# course instead of course_identifier because we need to do
# refresh_from_db
assert isinstance(attrs, dict)
course = course or cls.get_default_course()
if attrs:
course.__dict__.update(attrs)
course.save()
course.refresh_from_db()
@classmethod
def get_view_start_flow_url(cls, flow_id, course_identifier=None):
course_identifier = course_identifier or cls.get_default_course_identifier()
kwargs = {"course_identifier": course_identifier,
"flow_id": flow_id}
return reverse("relate-view_start_flow", kwargs=kwargs)
@classmethod_with_client
def start_flow(cls, client, flow_id, *, # noqa: N805
course_identifier=None,
ignore_cool_down=True, assume_success=True):
"""
Notice: be cautious to use this in setUpTestData, because this will
create many related objects in db, if those objects are changed in
individual test, other tests followed might fail.
"""
existing_session_count = FlowSession.objects.all().count()
if ignore_cool_down:
cool_down_seconds = 0
else:
cool_down_seconds = settings.RELATE_SESSION_RESTART_COOLDOWN_SECONDS
with override_settings(
RELATE_SESSION_RESTART_COOLDOWN_SECONDS=cool_down_seconds):
resp = client.post(
cls.get_view_start_flow_url(flow_id, course_identifier))
if assume_success:
assert resp.status_code == 302, resp.content
new_session_count = FlowSession.objects.all().count()
assert new_session_count == existing_session_count + 1
_, _, params = resolve(resp.url)
del params["page_ordinal"]
cls.default_flow_params = params
cls.update_default_flow_session_id(course_identifier)
return resp
@classmethod_with_client
def end_flow(cls, client, *, # noqa: N805
course_identifier=None, flow_session_id=None,
post_parameter="submit"):
if not course_identifier or not flow_session_id:
if cls.default_flow_params is None:
raise RuntimeError(
"There's no started flow_sessions, or "
"the session is not started by start_flow")
resp = client.post(
cls.get_finish_flow_session_view_url(
course_identifier, flow_session_id),
data={post_parameter: [""]})
return resp
@classmethod
def get_resume_flow_url(cls, course_identifier=None, flow_session_id=None):
flow_params = cls.get_flow_params(course_identifier, flow_session_id)
return reverse("relate-view_resume_flow", kwargs=flow_params)
@classmethod
def get_flow_params(cls, course_identifier=None, flow_session_id=None):
course_identifier = (
course_identifier or cls.get_default_course_identifier())
if flow_session_id is None:
flow_session_id = cls.get_default_flow_session_id(course_identifier)
return {
"course_identifier": course_identifier,
"flow_session_id": flow_session_id
}
@classmethod
def get_page_params(cls, course_identifier=None, flow_session_id=None,
page_ordinal=None):
page_params = cls.get_flow_params(course_identifier, flow_session_id)
if page_ordinal is None:
page_ordinal = 0
page_params.update({"page_ordinal": page_ordinal})
return page_params
@classmethod
def get_page_ordinal_via_page_id(
cls, page_id, course_identifier=None, flow_session_id=None,
with_group_id=False):
flow_params = cls.get_flow_params(course_identifier, flow_session_id)
return (
get_flow_page_ordinal_from_page_id(
flow_params["flow_session_id"], page_id,
with_group_id=with_group_id))
@classmethod
def get_page_id_via_page_oridnal(
cls, page_ordinal, course_identifier=None, flow_session_id=None,
with_group_id=False):
flow_params = cls.get_flow_params(course_identifier, flow_session_id)
return (
get_flow_page_id_from_page_ordinal(
flow_params["flow_session_id"], page_ordinal,
with_group_id=with_group_id))
@classmethod
def get_page_view_url_by_ordinal(
cls, viewname, page_ordinal, course_identifier=None,
flow_session_id=None):
page_params = cls.get_page_params(
course_identifier, flow_session_id, page_ordinal)
return reverse(viewname, kwargs=page_params)
@classmethod
def get_page_view_url_by_page_id(
cls, viewname, page_id, course_identifier=None, flow_session_id=None):
page_ordinal = cls.get_page_ordinal_via_page_id(
page_id, course_identifier, flow_session_id)
return cls.get_page_view_url_by_ordinal(
viewname, page_ordinal, course_identifier, flow_session_id)
@classmethod
def get_page_url_by_ordinal(
cls, page_ordinal, course_identifier=None, flow_session_id=None,
visit_id=None):
url = cls.get_page_view_url_by_ordinal(
"relate-view_flow_page",
page_ordinal, course_identifier, flow_session_id)
if visit_id is not None:
url += f"?visit_id={visit_id!s}"
return url
@classmethod
def get_page_url_by_page_id(
cls, page_id, course_identifier=None, flow_session_id=None,
visit_id=None):
page_ordinal = cls.get_page_ordinal_via_page_id(
page_id, course_identifier, flow_session_id)
return cls.get_page_url_by_ordinal(
page_ordinal, course_identifier, flow_session_id, visit_id)
@classmethod
def get_page_grading_url_by_ordinal(
cls, page_ordinal, course_identifier=None, flow_session_id=None):
return cls.get_page_view_url_by_ordinal(
"relate-grade_flow_page",
page_ordinal, course_identifier, flow_session_id)
@classmethod
def get_page_grading_url_by_page_id(
cls, page_id, course_identifier=None, flow_session_id=None):
page_ordinal = cls.get_page_ordinal_via_page_id(
page_id, course_identifier, flow_session_id)
return cls.get_page_grading_url_by_ordinal(
page_ordinal, course_identifier, flow_session_id)
@classmethod_with_client
def post_answer_by_ordinal(cls, client, # noqa: N805
page_ordinal, answer_data, *,
course_identifier=None, flow_session_id=None, visit_id=None):
submit_data = answer_data
submit_data.update({"submit": ["Submit final answer"]})
resp = client.post(
cls.get_page_url_by_ordinal(
page_ordinal, course_identifier, flow_session_id, visit_id),
submit_data)
return resp
@classmethod_with_client
def post_answer_by_page_id(
cls, client, page_id, answer_data, *, # noqa: N805
course_identifier=None, flow_session_id=None, visit_id=None):
page_ordinal = cls.get_page_ordinal_via_page_id(
page_id, course_identifier, flow_session_id)
return cls.post_answer_by_ordinal(client,
page_ordinal, answer_data,
course_identifier=course_identifier,
flow_session_id=flow_session_id, visit_id=visit_id)
@classmethod_with_client
def post_answer_by_ordinal_class(cls, client, # noqa: N805
page_ordinal, answer_data,
course_identifier, flow_session_id):
submit_data = answer_data
submit_data.update({"submit": ["Submit final answer"]})
page_params = {
"course_identifier": course_identifier,
"flow_session_id": flow_session_id,
"page_ordinal": page_ordinal
}
page_url = reverse("relate-view_flow_page", kwargs=page_params)
resp = client.post(page_url, submit_data)
return resp
@classmethod_with_client
def post_answer_by_page_id_class(cls, client, # noqa: N805
page_id, answer_data, course_identifier, flow_session_id):
page_ordinal = get_flow_page_ordinal_from_page_id(flow_session_id, page_id)
return cls.post_answer_by_ordinal_class(page_ordinal, answer_data,
course_identifier, flow_session_id)
@classmethod_with_client
def post_grade_by_ordinal(cls, client, # noqa: N805
page_ordinal, grade_data, *,
course_identifier=None, flow_session_id=None,
force_login_instructor=True):
post_data = {"submit": [""]}
post_data.update(grade_data)
page_params = cls.get_page_params(
course_identifier, flow_session_id, page_ordinal)
force_login_user = cls.get_logged_in_user(client)
if force_login_instructor:
force_login_user = cls.get_default_instructor_user(
page_params["course_identifier"])
with cls.temporarily_switch_to_user(client, force_login_user):
response = client.post(
cls.get_page_grading_url_by_ordinal(**page_params),
data=post_data,
follow=True)
return response
@classmethod_with_client
def post_grade_by_page_id(cls, client, # noqa: N805
page_id, grade_data, *,
course_identifier=None, flow_session_id=None,
force_login_instructor=True):
page_ordinal = cls.get_page_ordinal_via_page_id(
page_id, course_identifier, flow_session_id)
return cls.post_grade_by_ordinal(client,
page_ordinal, grade_data,
course_identifier=course_identifier,
flow_session_id=flow_session_id,
force_login_instructor=force_login_instructor)
@classmethod
def assertSessionScoreEqual( # noqa
cls, expected_score, course_identifier=None, flow_session_id=None):
if flow_session_id is None:
flow_params = cls.get_flow_params(course_identifier, flow_session_id)
flow_session_id = flow_params["flow_session_id"]
flow_session = FlowSession.objects.get(id=flow_session_id)
if expected_score is not None:
from decimal import Decimal
assert flow_session.points == Decimal(str(expected_score)), (
f"The flow session got '{flow_session.points!s}' "
f"instead of '{Decimal(str(expected_score))!s}'"
)
else:
assert flow_session.points is None, (
f"This flow session unexpectedly got {flow_session.points} "
"instead of None")
@classmethod
def get_page_submit_history_url_by_ordinal(
cls, page_ordinal, course_identifier=None, flow_session_id=None):
page_params = cls.get_page_params(
course_identifier, flow_session_id, page_ordinal)
page_params["prev_visit_id"] = None
return reverse(
"relate-get_prev_answer_visits_dropdown_content",
kwargs=page_params)
@classmethod
def get_page_grade_history_url_by_ordinal(
cls, page_ordinal, course_identifier=None, flow_session_id=None):
page_params = cls.get_page_params(
course_identifier, flow_session_id, page_ordinal)
page_params["prev_grade_id"] = None
return reverse("relate-get_prev_grades_dropdown_content", kwargs=page_params)
@classmethod_with_client
def get_page_submit_history_by_ordinal(
cls, client, page_ordinal, *, # noqa: N805
course_identifier=None, flow_session_id=None):
resp = client.get(
cls.get_page_submit_history_url_by_ordinal(
page_ordinal, course_identifier, flow_session_id),
HTTP_X_REQUESTED_WITH="XMLHttpRequest")
return resp
@classmethod_with_client
def get_page_grade_history_by_ordinal(
cls, client, page_ordinal, *, # noqa: N805
course_identifier=None, flow_session_id=None):
resp = client.get(
cls.get_page_grade_history_url_by_ordinal(
page_ordinal, course_identifier, flow_session_id),
HTTP_X_REQUESTED_WITH="XMLHttpRequest")
return resp
def assertSubmitHistoryItemsCount( # noqa
self, page_ordinal, expected_count, course_identifier=None,
flow_session_id=None):
resp = self.get_page_submit_history_by_ordinal(
page_ordinal, course_identifier=course_identifier,
flow_session_id=flow_session_id)
content = resp.content.decode()
if "No submission" in content:
count = 0
else:
count = content.count("<li")
assert count == expected_count
def assertGradeHistoryItemsCount( # noqa
self, page_ordinal, expected_count,
course_identifier=None,
flow_session_id=None,
force_login_instructor=True):
if course_identifier is None:
course_identifier = self.get_default_course_identifier()
if force_login_instructor:
switch_to = self.get_default_instructor_user(course_identifier)
else:
switch_to = self.get_logged_in_user()
with self.temporarily_switch_to_user(switch_to):
resp = self.get_page_grade_history_by_ordinal(
page_ordinal, course_identifier=course_identifier,
flow_session_id=flow_session_id)
content = resp.content.decode()
if "No submission" in content:
count = 0
else:
count = content.count("<li")
assert count == expected_count
@classmethod
def get_update_course_url(cls, course_identifier=None):
if course_identifier is None:
course_identifier = cls.get_default_course_identifier()
return reverse("relate-update_course", args=[course_identifier])
@classmethod
def get_course_commit_sha(cls, participation, course=None):
course = course or cls.get_default_course()
from course.content import get_course_commit_sha
return get_course_commit_sha(course, participation)
@classmethod_with_client
def post_update_course_content(cls, client, commit_sha, *, # noqa: N805
prevent_discarding_revisions=True,
force_login_instructor=True,
course=None,
command="update",
):
# course instead of course_identifier because we need to do
# refresh_from_db
course = course or cls.get_default_course()
try:
commit_sha = commit_sha.decode()
except Exception:
pass
data = {"new_sha": [commit_sha]}
if not prevent_discarding_revisions:
data["prevent_discarding_revisions"] = ["on"]
# normally, command should be in
# ["fetch", "fetch_update", "update", "fetch_preview", "preview",
# "end_preview"]
data[command] = "on"
force_login_user = cls.get_logged_in_user(client)
if force_login_instructor:
force_login_user = cls.get_default_instructor_user(course.identifier)
with cls.temporarily_switch_to_user(client, force_login_user):
response = client.post(
cls.get_update_course_url(course.identifier), data)
course.refresh_from_db()
return response
@classmethod
def get_page_data_by_page_id(
cls, page_id, course_identifier=None, flow_session_id=None):
flow_params = cls.get_flow_params(course_identifier, flow_session_id)
return FlowPageData.objects.get(
flow_session_id=flow_params["flow_session_id"], page_id=page_id)
@classmethod
def get_page_visits(cls, course_identifier=None,
flow_session_id=None, page_ordinal=None, page_id=None,
**kwargs):
query_kwargs = {}
if kwargs.get("answer_visit", False):
query_kwargs.update({"answer__isnull": False})
flow_params = cls.get_flow_params(course_identifier, flow_session_id)
query_kwargs.update({"flow_session_id": flow_params["flow_session_id"]})
if page_ordinal is not None:
query_kwargs.update({"page_data__page_ordinal": page_ordinal})
elif page_id is not None:
query_kwargs.update({"page_data__page_id": page_id})
return FlowPageVisit.objects.filter(**query_kwargs)
@classmethod
def get_last_answer_visit(cls, course_identifier=None,
flow_session_id=None, page_ordinal=None,
page_id=None, assert_not_none=True):
result_qset = cls.get_page_visits(course_identifier,
flow_session_id, page_ordinal, page_id,
answer_visit=True).order_by("-pk")[:1]
if result_qset:
result = result_qset[0]
else:
result = None
if assert_not_none:
assert result is not None, "The query returns None"
return result
@classmethod
def download_all_submissions_url(cls, flow_id, course_identifier):
params = {"course_identifier": course_identifier,
"flow_id": flow_id}
return reverse("relate-download_all_submissions", kwargs=params)
@classmethod_with_client
def get_download_all_submissions(cls, client, flow_id, *, # noqa: N805
course_identifier=None):
if course_identifier is None:
course_identifier = cls.get_default_course_identifier()
return client.get(
cls.download_all_submissions_url(flow_id, course_identifier))
@classmethod_with_client
def post_download_all_submissions_by_group_page_id(
cls, client, # noqa: N805
group_page_id, flow_id, *, course_identifier=None, **kwargs):
"""
:param group_page_id: format: group_id/page_id
:param flow_id:
:param course_identifier:
:param kwargs: for updating the default post_data
:return: response
"""
if course_identifier is None:
course_identifier = cls.get_default_course_identifier()
data = {"restrict_to_rules_tag": "<<<ALL>>>",
"which_attempt": "last",
"extra_file": "", "download": "Download",
"page_id": group_page_id,
"non_in_progress_only": "on"}
non_in_progress_only = kwargs.pop("non_in_progress_only", True)
if not non_in_progress_only:
del data["non_in_progress_only"]
data.update(kwargs)
return client.post(
cls.download_all_submissions_url(flow_id, course_identifier),
data=data
)
@classmethod
def get_flow_page_analytics_url(cls, flow_id, group_id, page_id,
course_identifier=None,
restrict_to_first_attempt=False):
if course_identifier is None:
course_identifier = cls.get_default_course_identifier()
params = {"course_identifier": course_identifier,
"flow_id": flow_id,
"group_id": group_id,
"page_id": page_id}
url = reverse("relate-page_analytics", kwargs=params)
if restrict_to_first_attempt:
url += "?restrict_to_first_attempt=1"
return url
@classmethod_with_client
def get_flow_page_analytics(cls, client, # noqa: N805
flow_id, group_id, page_id, *,
course_identifier=None,
force_login_instructor=True,
restrict_to_first_attempt=False):
course_identifier = course_identifier or cls.get_default_course_identifier()
url = cls.get_flow_page_analytics_url(
flow_id, group_id, page_id, course_identifier, restrict_to_first_attempt)
if not force_login_instructor:
user = cls.get_logged_in_user(client)
else:
user = cls.instructor_participation.user
with cls.temporarily_switch_to_user(client, user):
return client.get(url)
# {{{ hack getting session rules
default_session_start_rule = {
"tag_session": None,
"may_start_new_session": True,
"may_list_existing_sessions": True,
"default_expiration_mode": None}
def get_hacked_session_start_rule(self, **kwargs):
"""
Used for mocking session_start_rule
:param kwargs: attributes in the mocked FlowSessionStartRule instance
:return: a :class:`FlowSessionStartRule` instance
Example:
with mock.patch(
"course.flow.get_session_start_rule") as mock_get_nrule:
mock_get_nrule.return_value = (
self.get_hacked_session_start_rule())
"""
from course.utils import FlowSessionStartRule
defaults = deepcopy(self.default_session_start_rule)
defaults.update(kwargs)
return FlowSessionStartRule(**defaults)
default_session_access_rule = {
"permissions": [fperm.view, fperm.end_session]}
def get_hacked_session_access_rule(self, **kwargs):
"""
Used for mocking session_access_rule
:param kwargs: attributes in the mocked FlowSessionAccessRule instance
:return: a :class:`FlowSessionAccessRule` instance
Example:
with mock.patch(
"course.flow.get_session_access_rule") as mock_get_arule:
mock_get_arule.return_value = (
self.get_hacked_session_access_rule(
permissions=[fperm.end_session]))
"""
from course.utils import FlowSessionAccessRule
defaults = deepcopy(self.default_session_access_rule)
defaults.update(kwargs)
return FlowSessionAccessRule(**defaults)
default_session_grading_rule = {
"grade_identifier": "la_quiz",
"grade_aggregation_strategy": g_strategy.use_latest,
"due": None,
"generates_grade": True,
"description": None,
"credit_percent": 100,
"use_last_activity_as_completion_time": False,
"bonus_points": 0,
"max_points": None,
"max_points_enforced_cap": None,
}
def get_hacked_session_grading_rule(self, **kwargs):
"""
Used for mocking session_grading_rule
:param kwargs: attributes in the mocked FlowSessionGradingRule instance
:return: a :class:`FlowSessionGradingRule` instance
Example:
with mock.patch(
"course.flow.get_session_grading_rule") as mock_get_grule:
mock_get_grule.return_value = \
self.get_hacked_session_grading_rule(bonus_points=2)
"""
from course.utils import FlowSessionGradingRule
defaults = deepcopy(self.default_session_grading_rule)
defaults.update(kwargs)
return FlowSessionGradingRule(**defaults)
# }}}
def get_form_submit_inputs(self, form):
from crispy_forms.layout import Submit
inputs = [
(input.name, input.value) for input in form.helper.inputs
if isinstance(input, Submit)
]
names = list(dict(inputs).keys())
values = list(dict(inputs).values())
return names, values
def get_flow_analytics_url(self, flow_id, course_identifier,
restrict_to_first_attempt=None):
course_identifier = course_identifier or self.get_default_course_identifier()
kwargs = {
"flow_id": flow_id,
"course_identifier": course_identifier}
result = reverse("relate-flow_analytics", kwargs=kwargs)
if restrict_to_first_attempt:
result += f"?restrict_to_first_attempt={restrict_to_first_attempt}"
return result
def get_flow_analytics_view(self, flow_id, course_identifier=None,
restrict_to_first_attempt=None,
force_login_instructor=True):
course_identifier = course_identifier or self.get_default_course_identifier()
if not force_login_instructor:
user = self.get_logged_in_user()
else:
user = self.instructor_participation.user
with self.temporarily_switch_to_user(user):
return self.client.get(
self.get_flow_analytics_url(
flow_id, course_identifier=course_identifier,
restrict_to_first_attempt=restrict_to_first_attempt))
def get_manage_authentication_token_url(self, course_identifier=None):
course_identifier = course_identifier or self.get_default_course_identifier()
return reverse("relate-manage_authentication_tokens",
args=(course_identifier,))
# }}}
# {{{ SingleCourseTestMixin
class SingleCourseTestMixin(CoursesTestMixinBase):
courses_setup_list = SINGLE_COURSE_SETUP_LIST
initial_commit_sha = None
@classmethod
def setUpTestData(cls):
super().setUpTestData()
assert len(cls.course_qset) == 1
cls.course = cls.course_qset.first()
if cls.initial_commit_sha is not None:
cls.course.active_git_commit_sha = cls.initial_commit_sha
cls.course.save()
cls.instructor_participation = Participation.objects.filter(
course=cls.course,
roles__identifier="instructor",
status=participation_status.active
).first()
assert cls.instructor_participation
cls.student_participation = Participation.objects.filter(
course=cls.course,
roles__identifier="student",
status=participation_status.active
).first()
assert cls.student_participation
cls.ta_participation = Participation.objects.filter(
course=cls.course,
roles__identifier="ta",
status=participation_status.active
).first()
assert cls.ta_participation
cls.course_page_url = cls.get_course_page_url()
def setUp(self):
super().setUp()
# reload objects created during setUpTestData in case they were modified in
# tests. Ref: https://goo.gl/AuzJRC#django.test.TestCase.setUpTestData
self.course.refresh_from_db()
self.instructor_participation.refresh_from_db()
self.student_participation.refresh_from_db()
self.ta_participation.refresh_from_db()
self.client.force_login(self.student_participation.user)
@classmethod
def get_default_course(cls):
return cls.course
@classmethod
def get_default_course_identifier(cls):
return cls.get_default_course().identifier
def copy_course_dict_and_set_attrs_for_post(self, attrs_dict=None):
if attrs_dict is None:
attrs_dict = {}
from course.models import Course
kwargs = Course.objects.first().__dict__
kwargs.update(attrs_dict)
for k, v in kwargs.items():
if v is None:
kwargs[k] = ""
return kwargs
@classmethod
def get_course_page_context(cls, user):
rf = RequestFactory()
request = rf.get(cls.get_course_page_url())
request.user = user
from course.utils import CoursePageContext
pctx = CoursePageContext(request, cls.course.identifier)
return pctx
@classmethod
def get_hacked_flow_desc(
cls, user=None, flow_id=None, commit_sha=None,
del_rules=False, as_dict=False, **kwargs):
"""
Get a hacked version of flow_desc
:param user: the flow_desc viewed by which user, default to a student
:param flow_id: the flow_desc of which flow_id, default to `quiz-test`
:param commit_sha: default to current running commit_sha
:param kwargs: the attributes of the hacked flow_dec
:return: the faked flow_desc
"""
# {{{ get the actual flow_desc by a real visit
rf = RequestFactory()
request = rf.get(cls.get_course_page_url())
if user is None:
user = cls.student_participation.user
request.user = user
if flow_id is None:
flow_id = QUIZ_FLOW_ID
if commit_sha is None:
commit_sha = cls.course.active_git_commit_sha
if isinstance(commit_sha, str):
commit_sha = commit_sha.encode()
from course.content import get_flow_desc
with cls.get_course_page_context(user) as pctx:
flow_desc = get_flow_desc(
pctx.repo, pctx.course, flow_id, commit_sha)
# }}}
from relate.utils import dict_to_struct, struct_to_dict
flow_desc_dict = struct_to_dict(flow_desc)
if del_rules:
del flow_desc_dict["rules"]
flow_desc_dict.update(kwargs)
if as_dict:
return flow_desc_dict
return dict_to_struct(flow_desc_dict)
def get_hacked_flow_desc_with_access_rule_tags(self, rule_tags):
assert isinstance(rule_tags, list)
from relate.utils import dict_to_struct, struct_to_dict
hacked_flow_desc_dict = self.get_hacked_flow_desc(as_dict=True)
rules = hacked_flow_desc_dict["rules"]
rules_dict = struct_to_dict(rules)
rules_dict["tags"] = rule_tags
rules = dict_to_struct(rules_dict)
hacked_flow_desc_dict["rules"] = rules
hacked_flow_desc = dict_to_struct(hacked_flow_desc_dict)
assert hacked_flow_desc.rules.tags == rule_tags
return hacked_flow_desc
# }}}
# {{{ TwoCourseTestMixin
class TwoCourseTestMixin(CoursesTestMixinBase):
courses_setup_list = TWO_COURSE_SETUP_LIST
@classmethod
def setUpTestData(cls):
super().setUpTestData()
assert len(cls.course_qset) == 2, (
"'courses_setup_list' should contain two courses")
cls.course1 = cls.course_qset.first()
cls.course1_instructor_participation = Participation.objects.filter(
course=cls.course1,
roles__identifier="instructor",
status=participation_status.active
).first()
assert cls.course1_instructor_participation
cls.course1_student_participation = Participation.objects.filter(
course=cls.course1,
roles__identifier="student",
status=participation_status.active
).first()
assert cls.course1_student_participation
cls.course1_ta_participation = Participation.objects.filter(
course=cls.course1,
roles__identifier="ta",
status=participation_status.active
).first()
assert cls.course1_ta_participation
cls.course1_page_url = cls.get_course_page_url(cls.course1.identifier)
cls.course2 = cls.course_qset.last()
cls.course2_instructor_participation = Participation.objects.filter(
course=cls.course2,
roles__identifier="instructor",
status=participation_status.active
).first()
assert cls.course2_instructor_participation
cls.course2_student_participation = Participation.objects.filter(
course=cls.course2,
roles__identifier="student",
status=participation_status.active
).first()
assert cls.course2_student_participation
cls.course2_ta_participation = Participation.objects.filter(
course=cls.course2,
roles__identifier="ta",
status=participation_status.active
).first()
assert cls.course2_ta_participation
cls.course2_page_url = cls.get_course_page_url(cls.course2.identifier)
def setUp(self):
super().setUp()
# reload objects created during setUpTestData in case they were modified in
# tests. Ref: https://goo.gl/AuzJRC#django.test.TestCase.setUpTestData
self.course1.refresh_from_db()
self.course1_instructor_participation.refresh_from_db()
self.course1_student_participation.refresh_from_db()
self.course1_ta_participation.refresh_from_db()
self.course2.refresh_from_db()
self.course2_instructor_participation.refresh_from_db()
self.course2_student_participation.refresh_from_db()
self.course2_ta_participation.refresh_from_db()
# }}}
# {{{ SingleCoursePageTestMixin
class SingleCoursePageTestMixin(SingleCourseTestMixin):
# This serves as cache
_default_session_id = None
flow_id = QUIZ_FLOW_ID
@classmethod
def update_default_flow_session_id(cls, course_identifier):
cls._default_session_id = cls.default_flow_params["flow_session_id"]
@classmethod
def get_default_flow_session_id(cls, course_identifier):
if cls._default_session_id is not None:
return cls._default_session_id
cls._default_session_id = cls.get_latest_session_id(course_identifier)
return cls._default_session_id
# }}}
# {{{ TwoCoursePageTestMixin
class TwoCoursePageTestMixin(TwoCourseTestMixin):
_course1_default_session_id = None
_course2_default_session_id = None
@property
def flow_id(self):
raise NotImplementedError
@classmethod
def get_default_flow_session_id(cls, course_identifier):
if course_identifier == cls.course1.identifier:
if cls._course1_default_session_id is not None:
return cls._course1_default_session_id
cls._course1_default_session_id = (
cls.get_last_session_id(course_identifier))
return cls._course1_default_session_id
if course_identifier == cls.course2.identifier:
if cls._course2_default_session_id is not None:
return cls._course2_default_session_id
cls._course2_default_session_id = (
cls.get_last_session_id(course_identifier))
return cls._course2_default_session_id
@classmethod
def update_default_flow_session_id(cls, course_identifier):
new_session_id = cls.default_flow_params["flow_session_id"]
if course_identifier == cls.course1.identifier:
cls._course1_default_session_id = new_session_id
elif course_identifier == cls.course2.identifier:
cls._course2_default_session_id = new_session_id
# }}}
# {{{ SingleCourseQuizPageTestMixin
class SingleCourseQuizPageTestMixin(SingleCoursePageTestMixin):
skip_code_question = True
@classmethod_with_client
def ensure_grading_ui_get(cls, client, page_id): # noqa: N805
with cls.temporarily_switch_to_user(
client, cls.instructor_participation.user):
url = cls.get_page_grading_url_by_page_id(page_id)
resp = client.get(url)
assert resp.status_code == 200
@classmethod_with_client
def ensure_analytic_page_get(cls, client, group_id, page_id): # noqa: N805
with cls.temporarily_switch_to_user(
client, cls.instructor_participation.user):
resp = cls.get_flow_page_analytics(
client,
flow_id=cls.flow_id, group_id=group_id,
page_id=page_id)
assert resp.status_code == 200
@classmethod_with_client
def ensure_download_submission(
cls, client, group_id, page_id, *, # noqa: N805
dl_file_extension=None, file_with_ext_count=None):
with cls.temporarily_switch_to_user(
client, cls.instructor_participation.user):
group_page_id = f"{group_id}/{page_id}"
resp = cls.post_download_all_submissions_by_group_page_id(
client,
group_page_id=group_page_id, flow_id=cls.flow_id)
assert resp.status_code == 200
prefix, zip_file = resp["Content-Disposition"].split("=")
assert prefix == "attachment; filename"
assert resp.get("Content-Type") == "application/zip"
import io
if dl_file_extension:
buf = io.BytesIO(resp.content)
import zipfile
with zipfile.ZipFile(buf, "r") as zf:
assert zf.testzip() is None
# todo: make more assertions in terms of file content
for f in zf.filelist:
assert f.file_size > 0
if file_with_ext_count is None:
assert len([f for f in zf.filelist if
f.filename.endswith(dl_file_extension)]) > 0, \
("The zipped file unexpectedly didn't contain "
f"file with extension '{dl_file_extension}', "
"the actual file list "
f"is {[f.filename for f in zf.filelist]!r}")
else:
assert (
len([f for f in zf.filelist if
f.filename.endswith(dl_file_extension)])
== file_with_ext_count), \
("The zipped file unexpectedly didn't contain "
"%d files with extension '%s', the actual file list "
"is %s" % (
file_with_ext_count,
dl_file_extension,
repr([f.filename for f in zf.filelist])))
@classmethod_with_client
def submit_page_answer_by_ordinal_and_test(
cls, client, page_ordinal, *, # noqa: N805
use_correct_answer=True, answer_data=None,
skip_code_question=True,
expected_grades=None, expected_post_answer_status_code=200,
do_grading=False, do_human_grade=False, grade_data=None,
grade_data_extra_kwargs=None,
dl_file_extension=None,
ensure_grading_ui_get_before_grading=False,
ensure_grading_ui_get_after_grading=False,
ensure_analytic_page_get_before_submission=False,
ensure_analytic_page_get_after_submission=False,
ensure_analytic_page_get_before_grading=False,
ensure_analytic_page_get_after_grading=False,
ensure_download_before_submission=False,
ensure_download_after_submission=False,
ensure_download_before_grading=False,
ensure_download_after_grading=False,
dl_file_with_ext_count=None):
page_id = cls.get_page_id_via_page_oridnal(page_ordinal)
return cls.submit_page_answer_by_page_id_and_test(
client, page_id,
use_correct_answer=use_correct_answer,
answer_data=answer_data, skip_code_question=skip_code_question,
expected_grades=expected_grades,
expected_post_answer_status_code=expected_post_answer_status_code,
do_grading=do_grading, do_human_grade=do_human_grade,
grade_data=grade_data, grade_data_extra_kwargs=grade_data_extra_kwargs,
dl_file_extension=dl_file_extension,
ensure_grading_ui_get_before_grading=(
ensure_grading_ui_get_before_grading),
ensure_grading_ui_get_after_grading=ensure_grading_ui_get_after_grading,
ensure_analytic_page_get_before_submission=(
ensure_analytic_page_get_before_submission),
ensure_analytic_page_get_after_submission=(
ensure_analytic_page_get_after_submission),
ensure_analytic_page_get_before_grading=(
ensure_analytic_page_get_before_grading),
ensure_analytic_page_get_after_grading=(
ensure_analytic_page_get_after_grading),
ensure_download_before_submission=ensure_download_before_submission,
ensure_download_after_submission=ensure_download_after_submission,
ensure_download_before_grading=ensure_download_before_grading,
ensure_download_after_grading=ensure_download_after_grading,
dl_file_with_ext_count=dl_file_with_ext_count)
@classmethod_with_client
def submit_page_answer_by_page_id_and_test(
cls, client, page_id, *, # noqa: N805
use_correct_answer=True, answer_data=None,
skip_code_question=True,
expected_grades=None, expected_post_answer_status_code=200,
do_grading=False, do_human_grade=False, grade_data=None,
grade_data_extra_kwargs=None,
dl_file_extension=None,
ensure_grading_ui_get_before_grading=False,
ensure_grading_ui_get_after_grading=False,
ensure_analytic_page_get_before_submission=False,
ensure_analytic_page_get_after_submission=False,
ensure_analytic_page_get_before_grading=False,
ensure_analytic_page_get_after_grading=False,
ensure_download_before_submission=False,
ensure_download_after_submission=False,
ensure_download_before_grading=False,
ensure_download_after_grading=False,
dl_file_with_ext_count=None):
if answer_data is not None:
assert isinstance(answer_data, dict)
use_correct_answer = False
submit_answer_response = None
post_grade_response = None
for page_tuple in TEST_PAGE_TUPLE:
if skip_code_question and page_tuple.need_runpy:
continue
if page_id == page_tuple.page_id:
group_id = page_tuple.group_id
if ensure_grading_ui_get_before_grading:
cls.ensure_grading_ui_get(client, page_id)
if ensure_analytic_page_get_before_submission:
cls.ensure_analytic_page_get(client, group_id, page_id)
if ensure_download_before_submission:
cls.ensure_download_submission(client, group_id, page_id)
if page_tuple.correct_answer is not None:
if answer_data is None:
answer_data = page_tuple.correct_answer
if page_id in ["anyup", "proof_upload"]:
file_path = answer_data["uploaded_file"]
if not file_path:
# submitting an empty answer
submit_answer_response = (
cls.post_answer_by_page_id(
client, page_id, answer_data))
else:
if isinstance(file_path, list):
file_path, = file_path
file_path = file_path.strip()
with open(file_path, "rb") as fp:
answer_data = {"uploaded_file": fp}
submit_answer_response = (
cls.post_answer_by_page_id(client,
page_id, answer_data))
else:
submit_answer_response = (
cls.post_answer_by_page_id(client, page_id, answer_data))
# Fixed #514
# https://github.com/inducer/relate/issues/514
submit_answer_response.context["form"].as_p()
assert (submit_answer_response.status_code
== expected_post_answer_status_code)
if ensure_analytic_page_get_after_submission:
cls.ensure_analytic_page_get(client, group_id, page_id)
if ensure_download_after_submission:
cls.ensure_download_submission(client, group_id, page_id)
if not do_grading:
break
assert cls.end_flow(client).status_code == 200
if ensure_analytic_page_get_before_grading:
cls.ensure_analytic_page_get(client, group_id, page_id)
if ensure_download_before_grading:
cls.ensure_download_submission(client, group_id, page_id)
if page_tuple.correct_answer is not None:
if use_correct_answer:
expected_grades = page_tuple.full_points
if page_tuple.need_human_grade:
if not do_human_grade:
cls.assertSessionScoreEqual(None)
break
if grade_data is not None:
assert isinstance(grade_data, dict)
else:
grade_data = page_tuple.grade_data.copy()
if grade_data_extra_kwargs:
assert isinstance(grade_data_extra_kwargs, dict)
grade_data.update(grade_data_extra_kwargs)
post_grade_response = cls.post_grade_by_page_id(
client, page_id, grade_data)
cls.assertSessionScoreEqual(expected_grades)
if not dl_file_extension:
dl_file_extension = page_tuple.dl_file_extension
if ensure_download_after_grading:
cls.ensure_download_submission(
client,
group_id, page_id,
dl_file_extension=dl_file_extension,
file_with_ext_count=dl_file_with_ext_count)
if ensure_analytic_page_get_after_grading:
cls.ensure_analytic_page_get(client, group_id, page_id)
if ensure_grading_ui_get_after_grading:
cls.ensure_grading_ui_get(client, page_id)
return submit_answer_response, post_grade_response
def default_submit_page_answer_by_page_id_and_test(self, page_id,
answer_data=None,
expected_grade=None,
do_grading=True,
grade_data=None,
grade_data_extra_kwargs=None,
):
return self.submit_page_answer_by_page_id_and_test(
page_id, answer_data=answer_data,
skip_code_question=self.skip_code_question,
expected_grades=expected_grade, expected_post_answer_status_code=200,
do_grading=do_grading, do_human_grade=True, grade_data=grade_data,
grade_data_extra_kwargs=grade_data_extra_kwargs,
ensure_grading_ui_get_before_grading=True,
ensure_grading_ui_get_after_grading=True,
ensure_analytic_page_get_before_submission=True,
ensure_analytic_page_get_after_submission=True,
ensure_analytic_page_get_before_grading=True,
ensure_analytic_page_get_after_grading=True,
ensure_download_before_submission=True,
ensure_download_after_submission=True,
ensure_download_before_grading=True,
ensure_download_after_grading=True)
@classmethod_with_client
def submit_page_human_grading_by_page_id_and_test(
cls, client, page_id, *, # noqa: N805
expected_post_grading_status_code=200,
grade_data=None,
expected_grades=None,
do_session_score_equal_assertion=True,
grade_data_extra_kwargs=None,
force_login_instructor=True,
ensure_grading_ui_get_before_grading=False,
ensure_grading_ui_get_after_grading=False,
ensure_analytic_page_get_before_grading=False,
ensure_analytic_page_get_after_grading=False,
ensure_download_before_grading=False,
ensure_download_after_grading=False):
# this helper is expected to be used when the session is finished
post_grade_response = None
for page_tuple in TEST_PAGE_TUPLE:
if page_id == page_tuple.page_id:
group_id = page_tuple.group_id
if ensure_grading_ui_get_before_grading:
cls.ensure_grading_ui_get(page_id)
if ensure_analytic_page_get_before_grading:
cls.ensure_analytic_page_get(group_id, page_id)
if ensure_download_before_grading:
cls.ensure_download_submission(group_id, page_id)
if not page_tuple.need_human_grade:
break
assign_full_grades = True
if grade_data is not None:
assert isinstance(grade_data, dict)
assign_full_grades = False
else:
grade_data = page_tuple.grade_data.copy()
if assign_full_grades:
expected_grades = page_tuple.full_points
if grade_data_extra_kwargs:
assert isinstance(grade_data_extra_kwargs, dict)
grade_data.update(grade_data_extra_kwargs)
post_grade_response = cls.post_grade_by_page_id(
client,
page_id, grade_data,
force_login_instructor=force_login_instructor)
assert (post_grade_response.status_code
== expected_post_grading_status_code)
if post_grade_response.status_code == 200:
if do_session_score_equal_assertion:
cls.assertSessionScoreEqual(expected_grades)
if ensure_download_after_grading:
cls.ensure_download_submission(group_id, page_id)
if ensure_analytic_page_get_after_grading:
cls.ensure_analytic_page_get(group_id, page_id)
if ensure_grading_ui_get_after_grading:
cls.ensure_grading_ui_get(page_id)
return post_grade_response
# }}}
# {{{ MockAddMessageMixing
class MockAddMessageMixing:
"""
The mixing for testing django.contrib.messages.add_message
"""
def setUp(self):
super().setUp()
self._fake_add_message_path = "django.contrib.messages.add_message"
fake_add_message = mock.patch(self._fake_add_message_path)
self._mock_add_message = fake_add_message.start()
self.addCleanup(fake_add_message.stop)
def _get_added_messages(self, join=True):
try:
msgs = [
f"'{arg[2]!s}'"
for arg, _ in self._mock_add_message.call_args_list]
except IndexError:
self.fail(f"{self._fake_add_message_path} is unexpectedly not called.")
else:
if join:
return "; ".join(msgs)
return msgs
def assertAddMessageCallCount(self, expected_call_count, reset=False): # noqa
fail_msg = (
"%s is unexpectedly called %d times, instead of %d times." %
(self._fake_add_message_path, self._mock_add_message.call_count,
expected_call_count))
if self._mock_add_message.call_count > 0:
fail_msg += (
f"The called messages are: {self._get_added_messages(join=False)!r}")
self.assertEqual(
self._mock_add_message.call_count, expected_call_count, msg=fail_msg)
if reset:
self._mock_add_message.reset_mock()
def assertAddMessageCalledWith(self, expected_messages, reset=True): # noqa
joined_msgs = self._get_added_messages()
if not isinstance(expected_messages, list):
expected_messages = [expected_messages]
not_called = []
for msg in expected_messages:
if msg not in joined_msgs:
not_called.append(msg)
if not_called:
fail_msg = f"{not_called!r} unexpectedly not added in messages. "
if joined_msgs:
fail_msg += f'the actual message are "{joined_msgs}"'
self.fail(fail_msg)
if reset:
self._mock_add_message.reset_mock()
def assertAddMessageNotCalledWith(self, expected_messages, reset=False): # noqa
joined_msgs = self._get_added_messages()
if not isinstance(expected_messages, list):
expected_messages = [expected_messages]
called = []
for msg in expected_messages:
if msg in joined_msgs:
called.append(msg)
if called:
fail_msg = f"{called!r} unexpectedly added in messages. "
fail_msg += f'the actual message are "{joined_msgs}"'
self.fail(fail_msg)
if reset:
self._mock_add_message.reset_mock()
def reset_add_message_mock(self):
self._mock_add_message.reset_mock()
# }}}
# {{{ SubprocessRunpyContainerMixin
class SubprocessRunpyContainerMixin:
"""
This mixin is used to fake a runpy container, only needed when
the TestCase include test(s) for code questions
"""
@classmethod
def setUpClass(cls):
super().setUpClass()
python_executable = os.getenv("PY_EXE")
if not python_executable:
python_executable = sys.executable
import subprocess
args = [python_executable,
os.path.abspath(
os.path.join(
os.path.dirname(__file__), os.pardir,
"docker-image-run-py", "runcode")),
]
cls.faked_container_process = subprocess.Popen(
args,
stdout=subprocess.DEVNULL,
# because runpy prints to stderr
stderr=subprocess.DEVNULL
)
def setUp(self):
super().setUp()
self.faked_container_patch = mock.patch(
"course.page.code.SPAWN_CONTAINERS", False)
self.faked_container_patch.start()
self.addCleanup(self.faked_container_patch.stop)
@classmethod
def tearDownClass(cls):
super().tearDownClass()
from course.page.code import SPAWN_CONTAINERS
# Make sure SPAWN_CONTAINERS is reset to True
assert SPAWN_CONTAINERS
if sys.platform.startswith("win"):
# Without these lines, tests on Appveyor hanged when all tests
# finished.
# However, On nix platforms, these lines resulted in test
# failure when there were more than one TestCases which were using
# this mixin. So we don't kill the subprocess, and it won't bring
# bad side effects to remainder tests.
cls.faked_container_process.kill()
# }}}
def improperly_configured_cache_patch():
# can be used as context manager or decorator
built_in_import_path = "builtins.__import__"
import builtins
built_in_import = builtins.__import__
def my_disable_cache_import(name, globals=None, locals=None, fromlist=(),
level=0):
if name == "django.core.cache":
raise ImproperlyConfigured()
return built_in_import(name, globals, locals, fromlist, level)
return mock.patch(built_in_import_path, side_effect=my_disable_cache_import)
# {{{ admin
ADMIN_TWO_COURSE_SETUP_LIST = deepcopy(TWO_COURSE_SETUP_LIST)
# switch roles
ADMIN_TWO_COURSE_SETUP_LIST[1]["participations"][0]["role_identifier"] = "ta"
ADMIN_TWO_COURSE_SETUP_LIST[1]["participations"][1]["role_identifier"] = "instructor"
class AdminTestMixin(TwoCourseTestMixin):
courses_setup_list = ADMIN_TWO_COURSE_SETUP_LIST
none_participation_user_create_kwarg_list = (
NONE_PARTICIPATION_USER_CREATE_KWARG_LIST)
@classmethod
def setUpTestData(cls):
super().setUpTestData()
# create 2 participation (with new user) for course1
from tests.factories import ParticipationFactory
cls.course1_student_participation2 = (
ParticipationFactory.create(course=cls.course1))
cls.course1_student_participation3 = (
ParticipationFactory.create(course=cls.course1))
cls.instructor1 = cls.course1_instructor_participation.user
cls.instructor2 = cls.course2_instructor_participation.user
assert cls.instructor1 != cls.instructor2
# grant all admin permissions to instructors
from django.contrib.auth.models import Permission
for user in [cls.instructor1, cls.instructor2]:
user.is_staff = True
user.save()
for perm in Permission.objects.all():
user.user_permissions.add(perm)
@classmethod
def get_admin_change_list_view_url(cls, app_name, model_name):
return reverse(f"admin:{app_name}_{model_name}_changelist")
@classmethod
def get_admin_change_view_url(cls, app_name, model_name, args=None):
if args is None:
args = []
return reverse(f"admin:{app_name}_{model_name}_change", args=args)
@classmethod
def get_admin_add_view_url(cls, app_name, model_name, args=None):
if args is None:
args = []
return reverse(f"admin:{app_name}_{model_name}_add", args=args)
def get_admin_form_fields(self, response):
"""
Return a list of AdminFields for the AdminForm in the response.
"""
admin_form = response.context["adminform"]
fieldsets = list(admin_form)
field_lines = []
for fieldset in fieldsets:
field_lines += list(fieldset)
fields = []
for field_line in field_lines:
fields += list(field_line)
return fields
def get_admin_form_fields_names(self, response):
return [f.field.name for f in self.get_admin_form_fields(response)]
def get_changelist(self, request, model, model_admin):
from django.contrib.admin.views.main import ChangeList
return ChangeList(
request, model, model_admin.list_display,
model_admin.list_display_links, model_admin.get_list_filter(request),
model_admin.date_hierarchy, model_admin.search_fields,
model_admin.list_select_related, model_admin.list_per_page,
model_admin.list_max_show_all, model_admin.list_editable,
model_admin=model_admin,
sortable_by=model_admin.sortable_by,
search_help_text="(no help text)",
)
def get_filterspec_list(self, request, changelist=None, model=None,
model_admin=None):
if changelist is None:
assert request and model and model_admin
changelist = self.get_changelist(request, model, model_admin)
filterspecs = changelist.get_filters(request)[0]
filterspec_list = []
for filterspec in filterspecs:
choices = tuple(c["display"] for c in filterspec.choices(changelist))
filterspec_list.append(choices)
return filterspec_list
# }}}
# {{{ api
class APITestMixin(SingleCoursePageTestMixin):
# test manage_authentication_tokens
flow_id = QUIZ_FLOW_ID
force_login_student_for_each_test = False
default_token_hash_str = "my0token0string"
def get_get_flow_session_api_url(
self, course_identifier=None, flow_id=None,
auto_add_default_flow_id=True):
course_identifier = (
course_identifier or self.get_default_course_identifier())
if auto_add_default_flow_id:
flow_id = flow_id or self.flow_id
kwargs = {"course_identifier": course_identifier}
url = reverse("relate-course_get_flow_session", kwargs=kwargs)
if flow_id:
url += f"?flow_id={flow_id}"
return url
def get_get_flow_session_content_url(
self, course_identifier=None, flow_session_id=None,
auto_add_default_flow_session_id=True):
course_identifier = (
course_identifier or self.get_default_course_identifier())
if auto_add_default_flow_session_id:
flow_session_id = (
flow_session_id
or self.get_default_flow_session_id(course_identifier))
kwargs = {"course_identifier": course_identifier}
url = reverse("relate-course_get_flow_session_content", kwargs=kwargs)
if flow_session_id:
url += f"?flow_session_id={flow_session_id}"
return url
def create_token(self, token_hash_str=None, participation=None, **kwargs):
token_hash_str = token_hash_str or self.default_token_hash_str
participation = participation or self.instructor_participation
from tests.factories import AuthenticationTokenFactory
with mock.patch("tests.factories.make_sign_in_key") as mock_mk_sign_in_key:
mock_mk_sign_in_key.return_value = token_hash_str
token = AuthenticationTokenFactory(
user=participation.user,
participation=participation,
**kwargs
)
return token
def create_basic_auth(self, token=None, participation=None, user=None):
participation = participation or self.instructor_participation
user = user or participation.user
token = token or self.create_token(participation=participation)
basic_auth_str = f"{user.username}:{token.id}_{self.default_token_hash_str}"
from base64 import b64encode
return b64encode(basic_auth_str.encode("utf-8")).decode()
# }}}
# {{{ HackRepoMixin
class HackRepoMixin:
# This need to be configured when the module tested imported get_repo_blob
# at module level
get_repo_blob_patching_path = "course.content.get_repo_blob"
@classmethod
def setUpTestData(cls):
super().setUpTestData()
class Blob:
def __init__(self, yaml_file_name):
with open(os.path.join(FAKED_YAML_PATH, yaml_file_name), "rb") as f:
data = f.read()
self.data = data
def get_repo_side_effect(repo, full_name, commit_sha):
commit_sha_path_maps = COMMIT_SHA_MAP.get(full_name)
if commit_sha_path_maps:
assert isinstance(commit_sha_path_maps, list)
for cs_map in commit_sha_path_maps:
if commit_sha.decode() in cs_map:
path = cs_map[commit_sha.decode()]["path"]
return Blob(path)
return get_repo_blob(repo, full_name, repo[b"HEAD"].id)
cls.batch_fake_get_repo_blob = mock.patch(cls.get_repo_blob_patching_path)
cls.mock_get_repo_blob = cls.batch_fake_get_repo_blob.start()
cls.mock_get_repo_blob.side_effect = get_repo_side_effect
@classmethod
def tearDownClass(cls):
# This must be done to avoid inconsistency
super().tearDownClass()
cls.batch_fake_get_repo_blob.stop()
def get_current_page_ids(self):
current_sha = self.course.active_git_commit_sha
for commit_sha_path_maps in COMMIT_SHA_MAP.values():
for cs_map in commit_sha_path_maps:
if current_sha in cs_map:
return cs_map[current_sha]["page_ids"]
raise ValueError("Page_ids for that commit_sha doesn't exist")
def assertGradeInfoEqual(self, resp, expected_grade_info_dict=None): # noqa
grade_info = resp.context["grade_info"]
assert isinstance(grade_info, GradeInfo)
if not expected_grade_info_dict:
import json
error_msg = ("\n{}".format(json.dumps(OrderedDict(
sorted(grade_info.__dict__.items())),
indent=4)))
error_msg = error_msg.replace("null", "None")
self.fail(error_msg)
assert isinstance(expected_grade_info_dict, dict)
grade_info_dict = grade_info.__dict__
not_match_infos = []
for k in grade_info_dict.keys():
if grade_info_dict[k] != expected_grade_info_dict[k]:
not_match_infos.append(
f"'{k}' is expected to be {expected_grade_info_dict[k]!s}, while got {grade_info_dict[k]!s}") # noqa: E501
if not_match_infos:
self.fail("\n".join(not_match_infos))
# }}}
# vim: fdm=marker
from __future__ import annotations
import pytest
# from pytest_factoryboy import register
def pytest_addoption(parser):
parser.addoption(
"--slow", action="store_true", default=False, help="run slow tests",
)
parser.addoption(
"--all", action="store_true", default=False, help="run all tests",
)
def _is_connection_psql():
from django.db import connection
return connection.vendor == "postgresql"
def pytest_collection_modifyitems(config, items):
skip_pg = pytest.mark.skip(reason="connection is not a postgres database")
if not _is_connection_psql():
for item in items:
if "postgres" in item.keywords:
item.add_marker(skip_pg)
if config.getoption("--all"):
return
elif config.getoption("--slow"):
skip_non_slow = pytest.mark.skip(reason="need --slow option to run")
for item in items:
if "slow" not in item.keywords:
item.add_marker(skip_non_slow)
else:
skip_slow = pytest.mark.skip(reason="need --slow option to run")
for item in items:
if "slow" in item.keywords:
item.add_marker(skip_slow)
from __future__ import annotations
import os
from collections import namedtuple
from relate.utils import (
HTML5_DATETIME_FORMAT as DATE_TIME_PICKER_TIME_FORMAT, # noqa: F401
)
QUIZ_FLOW_ID = "quiz-test"
MESSAGE_ANSWER_SAVED_TEXT = "Answer saved."
MESSAGE_ANSWER_FAILED_SAVE_TEXT = "Failed to submit answer."
FIXTURE_PATH = os.path.join(os.path.dirname(__file__), "resource")
CSV_PATH = os.path.join(FIXTURE_PATH, "csv")
FAKED_YAML_PATH = os.path.join(FIXTURE_PATH, "faked_yamls")
def get_upload_file_path(file_name, fixture_path=FIXTURE_PATH):
return os.path.join(fixture_path, file_name)
TEST_TEXT_FILE_PATH = get_upload_file_path("test_file.txt")
TEST_PDF_FILE_PATH = get_upload_file_path("test_file.pdf")
TEST_HGTEXT_MARKDOWN_ANSWER = """
type: ChoiceQuestion
id: myquestion
shuffle: True
prompt: |
# What is a quarter?
choices:
- "1"
- "2"
- ~CORRECT~ 1/4
- ~CORRECT~ $\\frac{1}{4}$
- 四分之三
"""
TEST_HGTEXT_MARKDOWN_ANSWER_WRONG = """
type: ChoiceQuestion
id: myquestion
shuffle: True
prompt: |
# What is a quarter?
choices:
- "1"
- "2"
- 1/4
- $\\frac{1}{4}$
- 四分之三
"""
TEST_HGTEXT_MARKDOWN_ANSWER_TYPE_WRONG = """
type: Page
id: myquestion
content: |
# Title
content
"""
PageTuple = namedtuple(
"PageTuple", [
"page_id",
"group_id",
"need_human_grade",
"expecting_grade",
"need_runpy",
"correct_answer",
"grade_data",
"full_points",
"dl_file_extension",
]
)
TEST_AUDIO_OUTPUT_ANSWER = """
import numpy as np
t = np.linspace(0, 1, sample_rate, endpoint=False)
signal = np.sin(2*np.pi*t * 440)
output_audio(signal)
"""
TEST_PAGE_TUPLE = (
PageTuple("welcome", "intro", False, False, False, None, {}, None, None),
PageTuple("half", "quiz_start", False, True, False, {"answer": "0.5"}, {}, 5,
".txt"),
PageTuple("krylov", "quiz_start", False, True, False, {"choice": ["0"]}, {}, 2,
".json"),
PageTuple("ice_cream_toppings", "quiz_start", False, True, False,
{"choice": ["0", "1", "4"]}, {}, 1, ".json"),
PageTuple("matrix_props", "quiz_start", False, True, False,
{"choice": ["0", "3"]}, {}, 1, ".json"),
PageTuple("inlinemulti", "quiz_start", False, True, False,
{"blank1": "Bar", "blank_2": "0.2", "blank3": "1",
"blank4": "5", "blank5": "Bar", "choice2": "0",
"choice_a": "0"}, {}, 10, ".json"),
PageTuple("fear", "quiz_start", False, False, False, {"answer": "NOTHING!!!"},
{}, 0, ".txt"),
PageTuple("age_group", "quiz_start", False, False, False, {"choice": 3},
{}, 0, ".json"),
PageTuple("hgtext", "quiz_tail", True, True, False,
{"answer": TEST_HGTEXT_MARKDOWN_ANSWER},
{"grade_percent": "100", "released": "on"}, 5, ".txt"),
PageTuple("addition", "quiz_tail", False, True, True, {"answer": "c = b + a\r"},
{"grade_percent": "100", "released": "on"}, 1, ".py"),
PageTuple("pymult", "quiz_tail", True, True, True, {"answer": "c = a * b\r"},
{"grade_percent": "100", "released": "on"}, 4, ".py"),
PageTuple("neumann", "quiz_tail", False, True, False, {"answer": "1/(1-A)"}, {},
5, ".txt"),
PageTuple("py_simple_list", "quiz_tail", True, True, True,
{"answer": "b = [a] * 50\r"},
{"grade_percent": "100", "released": "on"}, 4, ".py"),
# Skipped
# PageTuple("test_audio_output", "quiz_tail", True, True, True,
# {"answer": TEST_AUDIO_OUTPUT_ANSWER}, {}, 1),
PageTuple("quarter", "quiz_tail", False, True, False, {"answer": ["0.25"]},
{}, 0, ".txt"),
PageTuple("anyup", "quiz_tail", True, False, False,
{"uploaded_file": TEST_TEXT_FILE_PATH},
{"grade_percent": "100", "released": "on"}, 5, None),
PageTuple("proof_upload", "quiz_tail", True, False, False,
{"uploaded_file": TEST_PDF_FILE_PATH},
{"grade_percent": "100", "released": "on"}, 5, ".pdf"),
PageTuple("eigvec", "quiz_tail", False, True, False, {"answer": "matrix"}, {},
2, ".txt"),
PageTuple("lsq", "quiz_tail", False, True, False, {"choice": ["2"]}, {}, 1,
".json"),
)
PAGE_WARNINGS = "page_warnings"
PAGE_ERRORS = "page_errors"
HAVE_VALID_PAGE = "have_valid_page"
COMMIT_SHA_MAP = {
# This didn't use os.path.join, because "get_flow_desc" used "flows/%s.yml" to
# get the path.
f"flows/{QUIZ_FLOW_ID}.yml": [
# key: commit_sha, value: attributes
{"my_fake_commit_sha_1": {"path": "fake-quiz-test1.yml"}},
{"my_fake_commit_sha_2": {"path": "fake-quiz-test2.yml"}},
{"my_fake_commit_sha_for_grades1": {
"path": "fake-quiz-test-for-grade1.yml",
"page_ids": ["half", "krylov", "quarter"]}},
{"my_fake_commit_sha_for_grades2": {
"path": "fake-quiz-test-for-grade2.yml",
"page_ids": ["krylov", "quarter"]}},
{"my_fake_commit_sha_for_finish_flow_session": {
"path": "fake-quiz-test-for-finish_flow_session.yml",
"page_ids": ["half", "krylov", "matrix_props", "age_group",
"anyup", "proof_upload", "neumann"]
}},
{"my_fake_commit_sha_for_grade_flow_session": {
"path": "fake-quiz-test-for-grade_flow_session.yml",
"page_ids": ["anyup"]}},
{"my_fake_commit_sha_for_view_flow_page": {
"path": "fake-quiz-test-for-view_flow_page.yml",
"page_ids": ["half", "half2"]}},
{"my_fake_commit_sha_for_download_submissions": {
"path": "fake-quiz-test-for-download-submissions.yml",
"page_ids": ["half", "proof"]}},
{"my_fake_commit_sha_for_flow_analytics": {
"path": "fake-quiz-test-for-flow_analytics.yml"}},
{"my_fake_commit_sha_for_page_analytics": {
"path": "fake-quiz-test-for-page_analytics.yml"
}}
],
# This had to use path join
os.path.join("images", ".attributes.yml"): [
# faked commit sha for .attributes.yml
{"abcdef001":
{"path": "fake-images-attributes.yml"}},
],
"questions/pdf-file-upload-example.yml": [
{"my_fake_commit_sha_for_normalized_bytes_answer":
{"path": "fake-pdf-file-upload-example.yml"}},
],
"course.yml": [
{"my_fake_commit_sha_for_course_desc": {
"path":
"fake-course-desc-for-page-chunk-tests.yml"}}
],
"events.yml": [
{"my_fake_commit_sha_for_events": {
"path":
"fake-events-desr-for-calendar-tests1.yml"}}
]
}