From cfae1cdf62fb74307b88bd091cd4ba927af6b1fc Mon Sep 17 00:00:00 2001 From: Calen Pennington <calen.pennington@gmail.com> Date: Wed, 6 Feb 2013 11:13:50 -0500 Subject: [PATCH] Pep8 autofixes --- cms/__init__.py | 1 - cms/djangoapps/auth/authz.py | 21 +- .../contentstore/course_info_model.py | 49 +++-- .../contentstore/features/common.py | 35 +++- .../contentstore/features/courses.py | 20 +- .../contentstore/features/factories.py | 5 +- .../contentstore/features/section.py | 26 ++- .../contentstore/features/signup.py | 6 +- .../features/studio-overview-togglesection.py | 31 ++- .../contentstore/features/subsection.py | 16 +- .../contentstore/management/commands/clone.py | 1 + .../management/commands/delete_course.py | 4 +- .../management/commands/prompt.py | 3 +- .../contentstore/module_info_model.py | 4 +- .../contentstore/tests/factories.py | 5 + .../contentstore/tests/test_contentstore.py | 65 +++--- .../contentstore/tests/test_core_caching.py | 2 + .../tests/test_course_settings.py | 87 ++++---- .../contentstore/tests/test_course_updates.py | 29 +-- .../contentstore/tests/test_utils.py | 5 +- cms/djangoapps/contentstore/tests/tests.py | 1 + cms/djangoapps/contentstore/tests/utils.py | 8 +- cms/djangoapps/contentstore/utils.py | 11 +- cms/djangoapps/contentstore/views.py | 131 +++++++----- .../models/settings/course_details.py | 50 ++--- .../models/settings/course_grading.py | 134 ++++++------ cms/envs/acceptance.py | 6 +- cms/envs/common.py | 6 +- cms/envs/dev.py | 4 +- cms/envs/dev_ike.py | 6 +- cms/envs/test.py | 8 +- cms/manage.py | 2 +- cms/urls.py | 4 +- common/djangoapps/cache_toolbox/core.py | 4 + common/djangoapps/contentserver/middleware.py | 2 +- common/djangoapps/course_groups/cohorts.py | 7 +- common/djangoapps/course_groups/models.py | 3 +- .../djangoapps/course_groups/tests/tests.py | 7 +- common/djangoapps/course_groups/views.py | 3 + common/djangoapps/external_auth/admin.py | 3 +- common/djangoapps/external_auth/models.py | 14 +- .../tests/test_openid_provider.py | 68 +++--- common/djangoapps/external_auth/views.py | 16 +- common/djangoapps/mitxmako/makoloader.py | 26 +-- common/djangoapps/mitxmako/template.py | 12 +- .../mitxmako/templatetag_helpers.py | 19 +- common/djangoapps/static_replace/__init__.py | 1 + .../test/test_static_replace.py | 1 + common/djangoapps/status/__init__.py | 1 - common/djangoapps/status/status.py | 1 + .../management/commands/6002exportusers.py | 2 +- .../management/commands/add_to_group.py | 1 + .../commands/create_random_users.py | 2 + .../management/commands/pearson_dump.py | 33 ++- .../management/commands/pearson_export_cdd.py | 6 +- .../commands/pearson_import_conf_zip.py | 1 - .../commands/pearson_make_tc_registration.py | 63 +++--- .../commands/pearson_make_tc_user.py | 54 ++--- .../management/commands/pearson_transfer.py | 20 +- .../management/commands/tests/test_pearson.py | 193 +++++++++--------- .../migrations/0020_add_test_center_user.py | 2 +- .../student/migrations/0021_remove_askbot.py | 2 +- ...ed__add_unique_courseenrollmentallowed_.py | 2 +- .../0023_add_test_center_registration.py | 2 +- .../migrations/0024_add_allow_certificate.py | 2 +- common/djangoapps/student/models.py | 32 +-- common/djangoapps/student/tests.py | 3 +- common/djangoapps/student/views.py | 57 ++++-- .../track/migrations/0001_initial.py | 2 +- ...t__chg_field_trackinglog_event_type__ch.py | 2 +- common/djangoapps/track/models.py | 17 +- common/djangoapps/track/views.py | 18 +- common/djangoapps/util/cache.py | 1 - common/djangoapps/util/converters.py | 9 +- common/djangoapps/util/json_request.py | 2 +- common/djangoapps/util/views.py | 1 + common/djangoapps/xmodule_modifiers.py | 26 +-- common/lib/capa/capa/calc.py | 4 +- common/lib/capa/capa/capa_problem.py | 6 +- common/lib/capa/capa/chem/__init__.py | 1 - common/lib/capa/capa/chem/chemcalc.py | 24 +-- common/lib/capa/capa/correctmap.py | 4 +- common/lib/capa/capa/customrender.py | 4 +- common/lib/capa/capa/inputtypes.py | 23 ++- common/lib/capa/capa/responsetypes.py | 8 +- common/lib/capa/capa/tests/__init__.py | 5 +- .../lib/capa/capa/tests/test_customrender.py | 10 +- common/lib/capa/capa/tests/test_inputtypes.py | 17 +- .../lib/capa/capa/tests/test_responsetypes.py | 17 +- common/lib/capa/capa/util.py | 7 +- common/lib/capa/capa/xqueue_interface.py | 13 +- common/lib/supertrace.py | 5 +- common/lib/xmodule/setup.py | 2 +- common/lib/xmodule/xmodule/abtest_module.py | 6 +- common/lib/xmodule/xmodule/capa_module.py | 16 +- .../xmodule/combined_open_ended_module.py | 8 +- .../xmodule/combined_open_ended_rubric.py | 18 +- .../lib/xmodule/xmodule/conditional_module.py | 19 +- .../xmodule/xmodule/contentstore/content.py | 43 ++-- .../xmodule/xmodule/contentstore/django.py | 1 + .../lib/xmodule/xmodule/contentstore/mongo.py | 43 ++-- common/lib/xmodule/xmodule/course_module.py | 69 +++---- .../lib/xmodule/xmodule/discussion_module.py | 2 + common/lib/xmodule/xmodule/errortracker.py | 3 + common/lib/xmodule/xmodule/graders.py | 35 ++-- .../xmodule/xmodule/grading_service_module.py | 6 +- common/lib/xmodule/xmodule/html_checker.py | 1 + common/lib/xmodule/xmodule/html_module.py | 4 +- common/lib/xmodule/xmodule/mako_module.py | 3 +- .../xmodule/xmodule/modulestore/__init__.py | 2 +- .../lib/xmodule/xmodule/modulestore/draft.py | 2 +- .../lib/xmodule/xmodule/modulestore/mongo.py | 4 +- .../lib/xmodule/xmodule/modulestore/search.py | 4 +- .../xmodule/modulestore/store_utilities.py | 32 +-- .../xmodule/modulestore/tests/__init__.py | 2 - .../xmodule/modulestore/tests/factories.py | 18 +- .../modulestore/tests/test_location.py | 8 +- .../modulestore/tests/test_modulestore.py | 2 +- .../xmodule/modulestore/tests/test_mongo.py | 1 - .../xmodule/modulestore/tests/test_xml.py | 3 +- common/lib/xmodule/xmodule/modulestore/xml.py | 14 +- .../xmodule/modulestore/xml_exporter.py | 7 +- .../xmodule/modulestore/xml_importer.py | 80 ++++---- .../xmodule/open_ended_image_submission.py | 7 +- .../lib/xmodule/xmodule/open_ended_module.py | 5 +- common/lib/xmodule/xmodule/openendedchild.py | 10 +- .../xmodule/xmodule/peer_grading_module.py | 32 +-- .../xmodule/xmodule/peer_grading_service.py | 26 ++- .../lib/xmodule/xmodule/randomize_module.py | 4 +- common/lib/xmodule/xmodule/raw_module.py | 3 +- .../xmodule/xmodule/self_assessment_module.py | 1 + common/lib/xmodule/xmodule/seq_module.py | 7 +- common/lib/xmodule/xmodule/stringify.py | 1 + common/lib/xmodule/xmodule/template_module.py | 2 +- common/lib/xmodule/xmodule/tests/__init__.py | 5 +- .../xmodule/xmodule/tests/test_capa_module.py | 8 +- .../xmodule/tests/test_combined_open_ended.py | 45 ++-- .../xmodule/xmodule/tests/test_conditional.py | 15 +- .../lib/xmodule/xmodule/tests/test_content.py | 4 +- .../lib/xmodule/xmodule/tests/test_export.py | 7 +- .../lib/xmodule/xmodule/tests/test_graders.py | 2 +- .../lib/xmodule/xmodule/tests/test_import.py | 5 +- .../xmodule/xmodule/tests/test_progress.py | 1 + .../xmodule/tests/test_randomize_module.py | 1 - .../xmodule/tests/test_self_assessment.py | 4 +- .../xmodule/xmodule/tests/test_stringify.py | 2 + common/lib/xmodule/xmodule/timeparse.py | 2 + common/lib/xmodule/xmodule/util/decorators.py | 10 +- common/lib/xmodule/xmodule/vertical_module.py | 2 +- common/lib/xmodule/xmodule/video_module.py | 2 +- common/lib/xmodule/xmodule/x_module.py | 8 +- common/lib/xmodule/xmodule/xml_module.py | 32 +-- .../docs/source/conf.py | 3 +- common/xml_cleanup.py | 12 +- ...el_field_generatedcertificate_name__add.py | 2 +- ...icate_graded_download_url__del_field_ge.py | 2 +- ...icate_enabled__add_field_generatedcerti.py | 2 +- ...icate_certificate_id__add_field_generat.py | 2 +- ...icate_name__add_field_generatedcertific.py | 2 +- ...field_generatedcertificate_error_reason.py | 2 +- .../migrations/0014_adding_whitelist.py | 2 +- lms/djangoapps/certificates/models.py | 2 + lms/djangoapps/certificates/queue.py | 2 +- lms/djangoapps/course_wiki/course_nav.py | 62 +++--- lms/djangoapps/course_wiki/editors.py | 9 +- .../plugins/markdownedx/__init__.py | 2 +- .../plugins/markdownedx/mdx_mathjax.py | 1 - .../plugins/markdownedx/wiki_plugin.py | 6 +- lms/djangoapps/course_wiki/tests/__init__.py | 1 - lms/djangoapps/course_wiki/tests/tests.py | 100 +++++---- lms/djangoapps/course_wiki/views.py | 34 +-- lms/djangoapps/courseware/access.py | 6 +- lms/djangoapps/courseware/admin.py | 1 - lms/djangoapps/courseware/courses.py | 7 +- lms/djangoapps/courseware/features/courses.py | 20 +- .../courseware/features/courseware.py | 3 +- .../courseware/features/courseware_common.py | 9 +- .../courseware/features/openended.py | 21 +- .../courseware/features/smart-accordion.py | 15 +- lms/djangoapps/courseware/grades.py | 54 ++--- .../management/commands/clean_xml.py | 1 + .../management/commands/metadata_to_json.py | 3 + ..._add_unique_offlinecomputedgrade_user_c.py | 2 +- lms/djangoapps/courseware/models.py | 4 +- lms/djangoapps/courseware/module_render.py | 40 ++-- lms/djangoapps/courseware/tabs.py | 25 ++- lms/djangoapps/courseware/tests/__init__.py | 1 - lms/djangoapps/courseware/tests/factories.py | 5 + .../courseware/tests/test_access.py | 11 +- lms/djangoapps/courseware/tests/tests.py | 52 ++--- lms/djangoapps/courseware/views.py | 26 ++- lms/djangoapps/dashboard/views.py | 10 +- .../django_comment_client/base/urls.py | 2 +- .../django_comment_client/base/views.py | 52 +++-- .../django_comment_client/forum/views.py | 35 ++-- .../django_comment_client/helpers.py | 5 + .../commands/assign_roles_for_course.py | 1 + .../commands/create_roles_for_existing.py | 1 + .../commands/seed_permissions_roles.py | 2 +- .../django_comment_client/middleware.py | 3 +- .../migrations/0001_initial.py | 2 +- .../django_comment_client/models.py | 4 +- .../django_comment_client/mustache_helpers.py | 7 +- .../django_comment_client/permissions.py | 10 +- .../django_comment_client/settings.py | 2 +- .../tests/test_helpers.py | 3 +- .../tests/test_middleware.py | 33 +-- .../tests/test_mustache_helpers.py | 32 +-- .../django_comment_client/tests/test_utils.py | 18 +- lms/djangoapps/django_comment_client/utils.py | 43 +++- .../management/commands/compute_grades.py | 11 +- .../management/commands/dump_grades.py | 24 +-- .../instructor/offline_gradecalc.py | 20 +- lms/djangoapps/instructor/tests.py | 33 ++- lms/djangoapps/instructor/views.py | 132 ++++++------ .../licenses/migrations/0001_initial.py | 2 +- .../management/commands/create_groups.py | 14 +- .../management/commands/create_user.py | 68 +++--- .../commands/manage_course_groups.py | 39 ++-- lms/djangoapps/lms_migration/migrate.py | 52 ++--- .../controller_query_service.py | 36 ++-- .../open_ended_notifications.py | 50 +++-- .../open_ended_grading/open_ended_util.py | 5 +- .../open_ended_grading/staff_grading.py | 1 - .../staff_grading_service.py | 9 +- lms/djangoapps/open_ended_grading/tests.py | 13 +- lms/djangoapps/open_ended_grading/views.py | 54 ++--- lms/djangoapps/portal/features/common.py | 20 +- lms/djangoapps/portal/features/factories.py | 3 + lms/djangoapps/portal/features/homepage.py | 1 + lms/djangoapps/portal/features/login.py | 10 +- .../portal/features/registration.py | 3 + lms/djangoapps/portal/features/signup.py | 5 +- .../management/commands/init_psychometrics.py | 26 +-- lms/djangoapps/psychometrics/models.py | 6 +- lms/djangoapps/psychometrics/psychoanalyze.py | 12 +- lms/djangoapps/simplewiki/mdx_mathjax.py | 1 - lms/djangoapps/simplewiki/views.py | 1 + lms/djangoapps/static_template_view/views.py | 1 - lms/djangoapps/staticbook/views.py | 6 +- lms/djangoapps/terrain/__init__.py | 2 +- lms/djangoapps/terrain/browser.py | 7 +- lms/djangoapps/terrain/factories.py | 23 ++- lms/djangoapps/terrain/steps.py | 40 +++- lms/envs/acceptance.py | 6 +- lms/envs/common.py | 52 ++--- lms/envs/content.py | 6 +- lms/envs/dev.py | 2 +- lms/envs/dev_edx4edx.py | 10 +- lms/envs/dev_ike.py | 16 +- lms/envs/dev_int.py | 6 +- lms/envs/devgroups/courses.py | 25 +-- lms/envs/devgroups/portal.py | 1 - lms/envs/devplus.py | 10 +- lms/envs/edx4edx_aws.py | 14 +- lms/envs/static.py | 4 +- lms/envs/test.py | 4 +- lms/envs/test_ike.py | 6 +- lms/lib/comment_client/comment.py | 3 + lms/lib/comment_client/comment_client.py | 8 + lms/lib/comment_client/commentable.py | 1 + lms/lib/comment_client/legacy.py | 52 ++++- lms/lib/comment_client/models.py | 11 +- lms/lib/comment_client/thread.py | 3 +- lms/lib/comment_client/user.py | 6 + lms/lib/comment_client/utils.py | 9 +- lms/lib/dogfood/check.py | 2 - lms/lib/loncapa/loncapa_check.py | 2 - lms/lib/symmath/symmath_check.py | 8 +- lms/static/admin/js/compress.py | 1 + lms/urls.py | 16 +- 271 files changed, 2395 insertions(+), 1826 deletions(-) diff --git a/cms/__init__.py b/cms/__init__.py index 8b137891791..e69de29bb2d 100644 --- a/cms/__init__.py +++ b/cms/__init__.py @@ -1 +0,0 @@ - diff --git a/cms/djangoapps/auth/authz.py b/cms/djangoapps/auth/authz.py index 22bbc4bc1c4..281e3f46b2f 100644 --- a/cms/djangoapps/auth/authz.py +++ b/cms/djangoapps/auth/authz.py @@ -18,6 +18,8 @@ STAFF_ROLE_NAME = 'staff' # we're just making a Django group for each location/role combo # to do this we're just creating a Group name which is a formatted string # of those two variables + + def get_course_groupname_for_role(location, role): loc = Location(location) # hack: check for existence of a group name in the legacy LMS format <role>_<course> @@ -25,11 +27,12 @@ def get_course_groupname_for_role(location, role): # more information groupname = '{0}_{1}'.format(role, loc.course) - if len(Group.objects.filter(name = groupname)) == 0: - groupname = '{0}_{1}'.format(role,loc.course_id) + if len(Group.objects.filter(name=groupname)) == 0: + groupname = '{0}_{1}'.format(role, loc.course_id) return groupname + def get_users_in_course_group_by_role(location, role): groupname = get_course_groupname_for_role(location, role) (group, created) = Group.objects.get_or_create(name=groupname) @@ -39,6 +42,8 @@ def get_users_in_course_group_by_role(location, role): ''' Create all permission groups for a new course and subscribe the caller into those roles ''' + + def create_all_course_groups(creator, location): create_new_course_group(creator, location, INSTRUCTOR_ROLE_NAME) create_new_course_group(creator, location, STAFF_ROLE_NAME) @@ -46,7 +51,7 @@ def create_all_course_groups(creator, location): def create_new_course_group(creator, location, role): groupname = get_course_groupname_for_role(location, role) - (group, created) =Group.objects.get_or_create(name=groupname) + (group, created) = Group.objects.get_or_create(name=groupname) if created: group.save() @@ -59,6 +64,8 @@ def create_new_course_group(creator, location, role): This is to be called only by either a command line code path or through a app which has already asserted permissions ''' + + def _delete_course_group(location): # remove all memberships instructors = Group.objects.get(name=get_course_groupname_for_role(location, INSTRUCTOR_ROLE_NAME)) @@ -75,6 +82,8 @@ def _delete_course_group(location): This is to be called only by either a command line code path or through an app which has already asserted permissions to do this action ''' + + def _copy_course_group(source, dest): instructors = Group.objects.get(name=get_course_groupname_for_role(source, INSTRUCTOR_ROLE_NAME)) new_instructors_group = Group.objects.get(name=get_course_groupname_for_role(dest, INSTRUCTOR_ROLE_NAME)) @@ -86,7 +95,7 @@ def _copy_course_group(source, dest): new_staff_group = Group.objects.get(name=get_course_groupname_for_role(dest, STAFF_ROLE_NAME)) for user in staff.user_set.all(): user.groups.add(new_staff_group) - user.save() + user.save() def add_user_to_course_group(caller, user, location, role): @@ -133,8 +142,6 @@ def remove_user_from_course_group(caller, user, location, role): def is_user_in_course_group_role(user, location, role): if user.is_active and user.is_authenticated: # all "is_staff" flagged accounts belong to all groups - return user.is_staff or user.groups.filter(name=get_course_groupname_for_role(location,role)).count() > 0 + return user.is_staff or user.groups.filter(name=get_course_groupname_for_role(location, role)).count() > 0 return False - - diff --git a/cms/djangoapps/contentstore/course_info_model.py b/cms/djangoapps/contentstore/course_info_model.py index 6995df06a8a..153d13dd132 100644 --- a/cms/djangoapps/contentstore/course_info_model.py +++ b/cms/djangoapps/contentstore/course_info_model.py @@ -8,6 +8,8 @@ import logging ## TODO store as array of { date, content } and override course_info_module.definition_from_xml ## This should be in a class which inherits from XmlDescriptor + + def get_course_updates(location): """ Retrieve the relevant course_info updates and unpack into the model which the client expects: @@ -21,13 +23,13 @@ def get_course_updates(location): # current db rep: {"_id" : locationjson, "definition" : { "data" : "<ol>[<li><h2>date</h2>content</li>]</ol>"} "metadata" : ignored} location_base = course_updates.location.url() - + # purely to handle free formed updates not done via editor. Actually kills them, but at least doesn't break. try: course_html_parsed = html.fromstring(course_updates.definition['data']) except: course_html_parsed = html.fromstring("<ol></ol>") - + # Confirm that root is <ol>, iterate over <li>, pull out <h2> subs and then rest of val course_upd_collection = [] if course_html_parsed.tag == 'ol': @@ -40,25 +42,26 @@ def get_course_updates(location): content = update[0].tail else: content = "\n".join([html.tostring(ele) for ele in update[1:]]) - + # make the id on the client be 1..len w/ 1 being the oldest and len being the newest - course_upd_collection.append({"id" : location_base + "/" + str(len(course_html_parsed) - idx), - "date" : update.findtext("h2"), - "content" : content}) - + course_upd_collection.append({"id": location_base + "/" + str(len(course_html_parsed) - idx), + "date": update.findtext("h2"), + "content": content}) + return course_upd_collection + def update_course_updates(location, update, passed_id=None): """ Either add or update the given course update. It will add it if the passed_id is absent or None. It will update it if it has an passed_id which has a valid value. Until updates have distinct values, the passed_id is the location url + an index - into the html structure. + into the html structure. """ try: course_updates = modulestore('direct').get_item(location) except ItemNotFoundError: return HttpResponseBadRequest - + # purely to handle free formed updates not done via editor. Actually kills them, but at least doesn't break. try: course_html_parsed = html.fromstring(course_updates.definition['data']) @@ -67,7 +70,7 @@ def update_course_updates(location, update, passed_id=None): # No try/catch b/c failure generates an error back to client new_html_parsed = html.fromstring('<li><h2>' + update['date'] + '</h2>' + update['content'] + '</li>') - + # Confirm that root is <ol>, iterate over <li>, pull out <h2> subs and then rest of val if course_html_parsed.tag == 'ol': # ??? Should this use the id in the json or in the url or does it matter? @@ -80,14 +83,15 @@ def update_course_updates(location, update, passed_id=None): idx = len(course_html_parsed) passed_id = course_updates.location.url() + "/" + str(idx) - + # update db record course_updates.definition['data'] = html.tostring(course_html_parsed) modulestore('direct').update_item(location, course_updates.definition['data']) - - return {"id" : passed_id, - "date" : update['date'], - "content" :update['content']} + + return {"id": passed_id, + "date": update['date'], + "content": update['content']} + def delete_course_update(location, update, passed_id): """ @@ -96,19 +100,19 @@ def delete_course_update(location, update, passed_id): """ if not passed_id: return HttpResponseBadRequest - + try: course_updates = modulestore('direct').get_item(location) except ItemNotFoundError: return HttpResponseBadRequest - + # TODO use delete_blank_text parser throughout and cache as a static var in a class # purely to handle free formed updates not done via editor. Actually kills them, but at least doesn't break. try: course_html_parsed = html.fromstring(course_updates.definition['data']) except: course_html_parsed = html.fromstring("<ol></ol>") - + if course_html_parsed.tag == 'ol': # ??? Should this use the id in the json or in the url or does it matter? idx = get_idx(passed_id) @@ -120,10 +124,11 @@ def delete_course_update(location, update, passed_id): # update db record course_updates.definition['data'] = html.tostring(course_html_parsed) store = modulestore('direct') - store.update_item(location, course_updates.definition['data']) - + store.update_item(location, course_updates.definition['data']) + return get_course_updates(location) - + + def get_idx(passed_id): """ From the url w/ idx appended, get the idx. @@ -131,4 +136,4 @@ def get_idx(passed_id): # TODO compile this regex into a class static and reuse for each call idx_matcher = re.search(r'.*/(\d+)$', passed_id) if idx_matcher: - return int(idx_matcher.group(1)) \ No newline at end of file + return int(idx_matcher.group(1)) diff --git a/cms/djangoapps/contentstore/features/common.py b/cms/djangoapps/contentstore/features/common.py index d910d730854..f868b598a8e 100644 --- a/cms/djangoapps/contentstore/features/common.py +++ b/cms/djangoapps/contentstore/features/common.py @@ -12,6 +12,8 @@ from logging import getLogger logger = getLogger(__name__) ########### STEP HELPERS ############## + + @step('I (?:visit|access|open) the Studio homepage$') def i_visit_the_studio_homepage(step): # To make this go to port 8001, put @@ -20,32 +22,37 @@ def i_visit_the_studio_homepage(step): world.browser.visit(django_url('/')) assert world.browser.is_element_present_by_css('body.no-header', 10) + @step('I am logged into Studio$') def i_am_logged_into_studio(step): log_into_studio() + @step('I confirm the alert$') def i_confirm_with_ok(step): world.browser.get_alert().accept() + @step(u'I press the "([^"]*)" delete icon$') def i_press_the_category_delete_icon(step, category): if category == 'section': css = 'a.delete-button.delete-section-button span.delete-icon' elif category == 'subsection': - css='a.delete-button.delete-subsection-button span.delete-icon' + css = 'a.delete-button.delete-subsection-button span.delete-icon' else: assert False, 'Invalid category: %s' % category css_click(css) ####### HELPER FUNCTIONS ############## + + def create_studio_user( uname='robot', email='robot+studio@edx.org', password='test', - is_staff=False): + is_staff=False): studio_user = UserFactory.build( - username=uname, + username=uname, email=email, password=password, is_staff=is_staff) @@ -58,6 +65,7 @@ def create_studio_user( user_profile = UserProfileFactory(user=studio_user) + def flush_xmodule_store(): # Flush and initialize the module store # It needs the templates because it creates new records @@ -70,26 +78,32 @@ def flush_xmodule_store(): xmodule.modulestore.django.modulestore().collection.drop() xmodule.templates.update_templates() -def assert_css_with_text(css,text): + +def assert_css_with_text(css, text): assert_true(world.browser.is_element_present_by_css(css, 5)) assert_equal(world.browser.find_by_css(css).text, text) + def css_click(css): world.browser.find_by_css(css).first.click() + def css_fill(css, value): world.browser.find_by_css(css).first.fill(value) + def clear_courses(): flush_xmodule_store() + def fill_in_course_info( name='Robot Super Course', org='MITx', num='101'): - css_fill('.new-course-name',name) - css_fill('.new-course-org',org) - css_fill('.new-course-number',num) + css_fill('.new-course-name', name) + css_fill('.new-course-org', org) + css_fill('.new-course-number', num) + def log_into_studio( uname='robot', @@ -108,24 +122,27 @@ def log_into_studio( assert_true(world.browser.is_element_present_by_css('.new-course-button', 5)) + def create_a_course(): css_click('a.new-course-button') fill_in_course_info() css_click('input.new-course-save') assert_true(world.browser.is_element_present_by_css('a#courseware-tab', 5)) + def add_section(name='My Section'): link_css = 'a.new-courseware-section-button' css_click(link_css) name_css = '.new-section-name' save_css = '.new-section-name-save' - css_fill(name_css,name) + css_fill(name_css, name) css_click(save_css) + def add_subsection(name='Subsection One'): css = 'a.new-subsection-item' css_click(css) name_css = 'input.new-subsection-name-input' save_css = 'input.new-subsection-name-save' css_fill(name_css, name) - css_click(save_css) \ No newline at end of file + css_click(save_css) diff --git a/cms/djangoapps/contentstore/features/courses.py b/cms/djangoapps/contentstore/features/courses.py index 2c1cf6281ad..d2d038a9289 100644 --- a/cms/djangoapps/contentstore/features/courses.py +++ b/cms/djangoapps/contentstore/features/courses.py @@ -2,49 +2,61 @@ from lettuce import world, step from common import * ############### ACTIONS #################### + + @step('There are no courses$') def no_courses(step): clear_courses() + @step('I click the New Course button$') def i_click_new_course(step): css_click('.new-course-button') + @step('I fill in the new course information$') def i_fill_in_a_new_course_information(step): fill_in_course_info() + @step('I create a new course$') def i_create_a_course(step): create_a_course() + @step('I click the course link in My Courses$') def i_click_the_course_link_in_my_courses(step): course_css = 'span.class-name' css_click(course_css) ############ ASSERTIONS ################### + + @step('the Courseware page has loaded in Studio$') def courseware_page_has_loaded_in_studio(step): courseware_css = 'a#courseware-tab' assert world.browser.is_element_present_by_css(courseware_css) + @step('I see the course listed in My Courses$') def i_see_the_course_in_my_courses(step): course_css = 'span.class-name' - assert_css_with_text(course_css,'Robot Super Course') + assert_css_with_text(course_css, 'Robot Super Course') + @step('the course is loaded$') def course_is_loaded(step): class_css = 'a.class-name' - assert_css_with_text(class_css,'Robot Super Course') + assert_css_with_text(class_css, 'Robot Super Course') + @step('I am on the "([^"]*)" tab$') def i_am_on_tab(step, tab_name): header_css = 'div.inner-wrapper h1' - assert_css_with_text(header_css,tab_name) + assert_css_with_text(header_css, tab_name) + @step('I see a link for adding a new section$') def i_see_new_section_link(step): link_css = 'a.new-courseware-section-button' - assert_css_with_text(link_css,'New Section') + assert_css_with_text(link_css, 'New Section') diff --git a/cms/djangoapps/contentstore/features/factories.py b/cms/djangoapps/contentstore/features/factories.py index 389f2bac493..087ceaaa2df 100644 --- a/cms/djangoapps/contentstore/features/factories.py +++ b/cms/djangoapps/contentstore/features/factories.py @@ -3,6 +3,7 @@ from student.models import User, UserProfile, Registration from datetime import datetime import uuid + class UserProfileFactory(factory.Factory): FACTORY_FOR = UserProfile @@ -10,12 +11,14 @@ class UserProfileFactory(factory.Factory): name = 'Robot Studio' courseware = 'course.xml' + class RegistrationFactory(factory.Factory): FACTORY_FOR = Registration user = None activation_key = uuid.uuid4().hex + class UserFactory(factory.Factory): FACTORY_FOR = User @@ -28,4 +31,4 @@ class UserFactory(factory.Factory): is_active = True is_superuser = False last_login = datetime.now() - date_joined = datetime.now() \ No newline at end of file + date_joined = datetime.now() diff --git a/cms/djangoapps/contentstore/features/section.py b/cms/djangoapps/contentstore/features/section.py index 8ac30e2170b..3bcaeab6c48 100644 --- a/cms/djangoapps/contentstore/features/section.py +++ b/cms/djangoapps/contentstore/features/section.py @@ -2,54 +2,65 @@ from lettuce import world, step from common import * ############### ACTIONS #################### + + @step('I have opened a new course in Studio$') def i_have_opened_a_new_course(step): clear_courses() log_into_studio() create_a_course() + @step('I click the new section link$') def i_click_new_section_link(step): link_css = 'a.new-courseware-section-button' css_click(link_css) + @step('I enter the section name and click save$') def i_save_section_name(step): name_css = '.new-section-name' save_css = '.new-section-name-save' - css_fill(name_css,'My Section') + css_fill(name_css, 'My Section') css_click(save_css) + @step('I have added a new section$') def i_have_added_new_section(step): add_section() - + + @step('I click the Edit link for the release date$') def i_click_the_edit_link_for_the_release_date(step): button_css = 'div.section-published-date a.edit-button' css_click(button_css) + @step('I save a new section release date$') def i_save_a_new_section_release_date(step): date_css = 'input.start-date.date.hasDatepicker' time_css = 'input.start-time.time.ui-timepicker-input' - css_fill(date_css,'12/25/2013') + css_fill(date_css, '12/25/2013') # click here to make the calendar go away css_click(time_css) - css_fill(time_css,'12:00am') + css_fill(time_css, '12:00am') css_click('a.save-button') ############ ASSERTIONS ################### + + @step('I see my section on the Courseware page$') def i_see_my_section_on_the_courseware_page(step): section_css = 'span.section-name-span' - assert_css_with_text(section_css,'My Section') + assert_css_with_text(section_css, 'My Section') + @step('the section does not exist$') def section_does_not_exist(step): css = 'span.section-name-span' assert world.browser.is_element_not_present_by_css(css) + @step('I see a release date for my section$') def i_see_a_release_date_for_my_section(step): import re @@ -63,18 +74,21 @@ def i_see_a_release_date_for_my_section(step): date_regex = '[01][0-9]\/[0-3][0-9]\/[12][0-9][0-9][0-9]' time_regex = '[0-2][0-9]:[0-5][0-9]' match_string = '%s %s at %s' % (msg, date_regex, time_regex) - assert re.match(match_string,status_text) + assert re.match(match_string, status_text) + @step('I see a link to create a new subsection$') def i_see_a_link_to_create_a_new_subsection(step): css = 'a.new-subsection-item' assert world.browser.is_element_present_by_css(css) + @step('the section release date picker is not visible$') def the_section_release_date_picker_not_visible(step): css = 'div.edit-subsection-publish-settings' assert False, world.browser.find_by_css(css).visible + @step('the section release date is updated$') def the_section_release_date_is_updated(step): css = 'span.published-status' diff --git a/cms/djangoapps/contentstore/features/signup.py b/cms/djangoapps/contentstore/features/signup.py index 7794511f948..e105b674f79 100644 --- a/cms/djangoapps/contentstore/features/signup.py +++ b/cms/djangoapps/contentstore/features/signup.py @@ -1,5 +1,6 @@ from lettuce import world, step + @step('I fill in the registration form$') def i_fill_in_the_registration_form(step): register_form = world.browser.find_by_css('form#register_form') @@ -9,15 +10,18 @@ def i_fill_in_the_registration_form(step): register_form.find_by_name('name').fill('Robot Studio') register_form.find_by_name('terms_of_service').check() + @step('I press the "([^"]*)" button on the registration form$') def i_press_the_button_on_the_registration_form(step, button): register_form = world.browser.find_by_css('form#register_form') register_form.find_by_value(button).click() + @step('I should see be on the studio home page$') def i_should_see_be_on_the_studio_home_page(step): assert world.browser.find_by_css('div.inner-wrapper') + @step(u'I should see the message "([^"]*)"$') def i_should_see_the_message(step, msg): - assert world.browser.is_text_present(msg, 5) \ No newline at end of file + assert world.browser.is_text_present(msg, 5) diff --git a/cms/djangoapps/contentstore/features/studio-overview-togglesection.py b/cms/djangoapps/contentstore/features/studio-overview-togglesection.py index 010678c0e89..00aa39455dc 100644 --- a/cms/djangoapps/contentstore/features/studio-overview-togglesection.py +++ b/cms/djangoapps/contentstore/features/studio-overview-togglesection.py @@ -6,11 +6,13 @@ from nose.tools import assert_true, assert_false, assert_equal from logging import getLogger logger = getLogger(__name__) + @step(u'I have a course with no sections$') def have_a_course(step): clear_courses() course = CourseFactory.create() + @step(u'I have a course with 1 section$') def have_a_course_with_1_section(step): clear_courses() @@ -18,8 +20,9 @@ def have_a_course_with_1_section(step): section = ItemFactory.create(parent_location=course.location) subsection1 = ItemFactory.create( parent_location=section.location, - template = 'i4x://edx/templates/sequential/Empty', - display_name = 'Subsection One',) + template='i4x://edx/templates/sequential/Empty', + display_name='Subsection One',) + @step(u'I have a course with multiple sections$') def have_a_course_with_two_sections(step): @@ -28,19 +31,20 @@ def have_a_course_with_two_sections(step): section = ItemFactory.create(parent_location=course.location) subsection1 = ItemFactory.create( parent_location=section.location, - template = 'i4x://edx/templates/sequential/Empty', - display_name = 'Subsection One',) + template='i4x://edx/templates/sequential/Empty', + display_name='Subsection One',) section2 = ItemFactory.create( parent_location=course.location, display_name='Section Two',) subsection2 = ItemFactory.create( parent_location=section2.location, - template = 'i4x://edx/templates/sequential/Empty', - display_name = 'Subsection Alpha',) + template='i4x://edx/templates/sequential/Empty', + display_name='Subsection Alpha',) subsection3 = ItemFactory.create( parent_location=section2.location, - template = 'i4x://edx/templates/sequential/Empty', - display_name = 'Subsection Beta',) + template='i4x://edx/templates/sequential/Empty', + display_name='Subsection Beta',) + @step(u'I navigate to the course overview page$') def navigate_to_the_course_overview_page(step): @@ -48,15 +52,18 @@ def navigate_to_the_course_overview_page(step): course_locator = '.class-name' css_click(course_locator) + @step(u'I navigate to the courseware page of a course with multiple sections') def nav_to_the_courseware_page_of_a_course_with_multiple_sections(step): step.given('I have a course with multiple sections') step.given('I navigate to the course overview page') + @step(u'I add a section') def i_add_a_section(step): add_section(name='My New Section That I Just Added') + @step(u'I click the "([^"]*)" link$') def i_click_the_text_span(step, text): span_locator = '.toggle-button-sections span' @@ -65,16 +72,19 @@ def i_click_the_text_span(step, text): assert_equal(world.browser.find_by_css(span_locator).value, text) css_click(span_locator) + @step(u'I collapse the first section$') def i_collapse_a_section(step): collapse_locator = 'section.courseware-section a.collapse' css_click(collapse_locator) + @step(u'I expand the first section$') def i_expand_a_section(step): expand_locator = 'section.courseware-section a.expand' css_click(expand_locator) + @step(u'I see the "([^"]*)" link$') def i_see_the_span_with_text(step, text): span_locator = '.toggle-button-sections span' @@ -82,6 +92,7 @@ def i_see_the_span_with_text(step, text): assert_equal(world.browser.find_by_css(span_locator).value, text) assert_true(world.browser.find_by_css(span_locator).visible) + @step(u'I do not see the "([^"]*)" link$') def i_do_not_see_the_span_with_text(step, text): # Note that the span will exist on the page but not be visible @@ -89,6 +100,7 @@ def i_do_not_see_the_span_with_text(step, text): assert_true(world.browser.is_element_present_by_css(span_locator)) assert_false(world.browser.find_by_css(span_locator).visible) + @step(u'all sections are expanded$') def all_sections_are_expanded(step): subsection_locator = 'div.subsection-list' @@ -96,9 +108,10 @@ def all_sections_are_expanded(step): for s in subsections: assert_true(s.visible) + @step(u'all sections are collapsed$') def all_sections_are_expanded(step): subsection_locator = 'div.subsection-list' subsections = world.browser.find_by_css(subsection_locator) for s in subsections: - assert_false(s.visible) \ No newline at end of file + assert_false(s.visible) diff --git a/cms/djangoapps/contentstore/features/subsection.py b/cms/djangoapps/contentstore/features/subsection.py index ea614d3feb9..e2041b8dbf0 100644 --- a/cms/djangoapps/contentstore/features/subsection.py +++ b/cms/djangoapps/contentstore/features/subsection.py @@ -2,6 +2,8 @@ from lettuce import world, step from common import * ############### ACTIONS #################### + + @step('I have opened a new course section in Studio$') def i_have_opened_a_new_course_section(step): clear_courses() @@ -9,31 +11,37 @@ def i_have_opened_a_new_course_section(step): create_a_course() add_section() + @step('I click the New Subsection link') def i_click_the_new_subsection_link(step): css = 'a.new-subsection-item' css_click(css) + @step('I enter the subsection name and click save$') def i_save_subsection_name(step): name_css = 'input.new-subsection-name-input' save_css = 'input.new-subsection-name-save' - css_fill(name_css,'Subsection One') + css_fill(name_css, 'Subsection One') css_click(save_css) + @step('I have added a new subsection$') def i_have_added_a_new_subsection(step): add_subsection() ############ ASSERTIONS ################### + + @step('I see my subsection on the Courseware page$') def i_see_my_subsection_on_the_courseware_page(step): css = 'span.subsection-name' - assert world.browser.is_element_present_by_css(css) + assert world.browser.is_element_present_by_css(css) css = 'span.subsection-name-value' - assert_css_with_text(css,'Subsection One') + assert_css_with_text(css, 'Subsection One') + @step('the subsection does not exist$') def the_subsection_does_not_exist(step): css = 'span.subsection-name' - assert world.browser.is_element_not_present_by_css(css) \ No newline at end of file + assert world.browser.is_element_not_present_by_css(css) diff --git a/cms/djangoapps/contentstore/management/commands/clone.py b/cms/djangoapps/contentstore/management/commands/clone.py index 2357cd1dbd1..abf04f3da35 100644 --- a/cms/djangoapps/contentstore/management/commands/clone.py +++ b/cms/djangoapps/contentstore/management/commands/clone.py @@ -14,6 +14,7 @@ from auth.authz import _copy_course_group # To run from command line: rake cms:clone SOURCE_LOC=MITx/111/Foo1 DEST_LOC=MITx/135/Foo3 # + class Command(BaseCommand): help = \ '''Clone a MongoDB backed course to another location''' diff --git a/cms/djangoapps/contentstore/management/commands/delete_course.py b/cms/djangoapps/contentstore/management/commands/delete_course.py index 0313f7faedf..bb38e72d44c 100644 --- a/cms/djangoapps/contentstore/management/commands/delete_course.py +++ b/cms/djangoapps/contentstore/management/commands/delete_course.py @@ -15,6 +15,7 @@ from auth.authz import _delete_course_group # To run from command line: rake cms:delete_course LOC=MITx/111/Foo1 # + class Command(BaseCommand): help = \ '''Delete a MongoDB backed course''' @@ -35,6 +36,3 @@ class Command(BaseCommand): print 'removing User permissions from course....' # in the django layer, we need to remove all the user permissions groups associated with this course _delete_course_group(loc) - - - diff --git a/cms/djangoapps/contentstore/management/commands/prompt.py b/cms/djangoapps/contentstore/management/commands/prompt.py index 9c8fd81d453..211c48406cf 100644 --- a/cms/djangoapps/contentstore/management/commands/prompt.py +++ b/cms/djangoapps/contentstore/management/commands/prompt.py @@ -1,5 +1,6 @@ import sys + def query_yes_no(question, default="yes"): """Ask a yes/no question via raw_input() and return their answer. @@ -30,4 +31,4 @@ def query_yes_no(question, default="yes"): return valid[choice] else: sys.stdout.write("Please respond with 'yes' or 'no' "\ - "(or 'y' or 'n').\n") \ No newline at end of file + "(or 'y' or 'n').\n") diff --git a/cms/djangoapps/contentstore/module_info_model.py b/cms/djangoapps/contentstore/module_info_model.py index 3b783c8815e..796184baa02 100644 --- a/cms/djangoapps/contentstore/module_info_model.py +++ b/cms/djangoapps/contentstore/module_info_model.py @@ -7,7 +7,8 @@ from lxml import etree import re from django.http import HttpResponseBadRequest, Http404 -def get_module_info(store, location, parent_location = None, rewrite_static_links = False): + +def get_module_info(store, location, parent_location=None, rewrite_static_links=False): try: if location.revision is None: module = store.get_item(location) @@ -36,6 +37,7 @@ def get_module_info(store, location, parent_location = None, rewrite_static_link 'metadata': module.metadata } + def set_module_info(store, location, post_data): module = None isNew = False diff --git a/cms/djangoapps/contentstore/tests/factories.py b/cms/djangoapps/contentstore/tests/factories.py index f9c505d68f2..d15610f11cd 100644 --- a/cms/djangoapps/contentstore/tests/factories.py +++ b/cms/djangoapps/contentstore/tests/factories.py @@ -5,6 +5,7 @@ from student.models import (User, UserProfile, Registration, CourseEnrollmentAllowed) from django.contrib.auth.models import Group + class UserProfileFactory(Factory): FACTORY_FOR = UserProfile @@ -12,12 +13,14 @@ class UserProfileFactory(Factory): name = 'Robot Studio' courseware = 'course.xml' + class RegistrationFactory(Factory): FACTORY_FOR = Registration user = None activation_key = uuid4().hex + class UserFactory(Factory): FACTORY_FOR = User @@ -32,11 +35,13 @@ class UserFactory(Factory): last_login = datetime.now() date_joined = datetime.now() + class GroupFactory(Factory): FACTORY_FOR = Group name = 'test_group' + class CourseEnrollmentAllowedFactory(Factory): FACTORY_FOR = CourseEnrollmentAllowed diff --git a/cms/djangoapps/contentstore/tests/test_contentstore.py b/cms/djangoapps/contentstore/tests/test_contentstore.py index ce7d9e757c4..72ae3821cc6 100644 --- a/cms/djangoapps/contentstore/tests/test_contentstore.py +++ b/cms/djangoapps/contentstore/tests/test_contentstore.py @@ -35,6 +35,7 @@ TEST_DATA_MODULESTORE = copy.deepcopy(settings.MODULESTORE) TEST_DATA_MODULESTORE['default']['OPTIONS']['fs_root'] = path('common/test/data') TEST_DATA_MODULESTORE['direct']['OPTIONS']['fs_root'] = path('common/test/data') + @override_settings(MODULESTORE=TEST_DATA_MODULESTORE) class ContentStoreToyCourseTest(ModuleStoreTestCase): """ @@ -77,20 +78,20 @@ class ContentStoreToyCourseTest(ModuleStoreTestCase): def test_static_tab_reordering(self): import_from_xml(modulestore(), 'common/test/data/', ['full']) - + ms = modulestore('direct') - course = ms.get_item(Location(['i4x','edX','full','course','6.002_Spring_2012', None])) + course = ms.get_item(Location(['i4x', 'edX', 'full', 'course', '6.002_Spring_2012', None])) # reverse the ordering reverse_tabs = [] for tab in course.tabs: if tab['type'] == 'static_tab': reverse_tabs.insert(0, 'i4x://edX/full/static_tab/{0}'.format(tab['url_slug'])) - - resp = self.client.post(reverse('reorder_static_tabs'), json.dumps({'tabs':reverse_tabs}), "application/json") - course = ms.get_item(Location(['i4x','edX','full','course','6.002_Spring_2012', None])) - + resp = self.client.post(reverse('reorder_static_tabs'), json.dumps({'tabs': reverse_tabs}), "application/json") + + course = ms.get_item(Location(['i4x', 'edX', 'full', 'course', '6.002_Spring_2012', None])) + # compare to make sure that the tabs information is in the expected order after the server call course_tabs = [] for tab in course.tabs: @@ -101,17 +102,17 @@ class ContentStoreToyCourseTest(ModuleStoreTestCase): def test_about_overrides(self): ''' - This test case verifies that a course can use specialized override for about data, e.g. /about/Fall_2012/effort.html + This test case verifies that a course can use specialized override for about data, e.g. /about/Fall_2012/effort.html while there is a base definition in /about/effort.html ''' import_from_xml(modulestore(), 'common/test/data/', ['full']) ms = modulestore('direct') - effort = ms.get_item(Location(['i4x','edX','full','about','effort', None])) - self.assertEqual(effort.definition['data'],'6 hours') + effort = ms.get_item(Location(['i4x', 'edX', 'full', 'about', 'effort', None])) + self.assertEqual(effort.definition['data'], '6 hours') # this one should be in a non-override folder - effort = ms.get_item(Location(['i4x','edX','full','about','end_date', None])) - self.assertEqual(effort.definition['data'],'TBD') + effort = ms.get_item(Location(['i4x', 'edX', 'full', 'about', 'end_date', None])) + self.assertEqual(effort.definition['data'], 'TBD') def test_remove_hide_progress_tab(self): import_from_xml(modulestore(), 'common/test/data/', ['full']) @@ -147,14 +148,14 @@ class ContentStoreToyCourseTest(ModuleStoreTestCase): clone_course(ms, cs, source_location, dest_location) - # now loop through all the units in the course and verify that the clone can render them, which + # now loop through all the units in the course and verify that the clone can render them, which # means the objects are at least present - items = ms.get_items(Location(['i4x','edX', 'full', 'vertical', None])) + items = ms.get_items(Location(['i4x', 'edX', 'full', 'vertical', None])) self.assertGreater(len(items), 0) - clone_items = ms.get_items(Location(['i4x', 'MITx','999','vertical', None])) + clone_items = ms.get_items(Location(['i4x', 'MITx', '999', 'vertical', None])) self.assertGreater(len(clone_items), 0) for descriptor in items: - new_loc = descriptor.location._replace(org = 'MITx', course='999') + new_loc = descriptor.location._replace(org='MITx', course='999') print "Checking {0} should now also be at {1}".format(descriptor.location.url(), new_loc.url()) resp = self.client.get(reverse('edit_unit', kwargs={'location': new_loc.url()})) self.assertEqual(resp.status_code, 200) @@ -169,7 +170,7 @@ class ContentStoreToyCourseTest(ModuleStoreTestCase): delete_course(ms, cs, location) - items = ms.get_items(Location(['i4x','edX', 'full', 'vertical', None])) + items = ms.get_items(Location(['i4x', 'edX', 'full', 'vertical', None])) self.assertEqual(len(items), 0) def verify_content_existence(self, modulestore, root_dir, location, dirname, category_name, filename_suffix=''): @@ -185,7 +186,7 @@ class ContentStoreToyCourseTest(ModuleStoreTestCase): def test_export_course(self): ms = modulestore('direct') - cs = contentstore() + cs = contentstore() import_from_xml(ms, 'common/test/data/', ['full']) location = CourseDescriptor.id_to_location('edX/full/6.002_Spring_2012') @@ -205,7 +206,7 @@ class ContentStoreToyCourseTest(ModuleStoreTestCase): # check for custom_tags self.verify_content_existence(ms, root_dir, location, 'custom_tags', 'custom_tag_template') - + # remove old course delete_course(ms, cs, location) @@ -213,23 +214,23 @@ class ContentStoreToyCourseTest(ModuleStoreTestCase): # reimport import_from_xml(ms, root_dir, ['test_export']) - items = ms.get_items(Location(['i4x','edX', 'full', 'vertical', None])) + items = ms.get_items(Location(['i4x', 'edX', 'full', 'vertical', None])) self.assertGreater(len(items), 0) for descriptor in items: print "Checking {0}....".format(descriptor.location.url()) resp = self.client.get(reverse('edit_unit', kwargs={'location': descriptor.location.url()})) self.assertEqual(resp.status_code, 200) - shutil.rmtree(root_dir) + shutil.rmtree(root_dir) def test_course_handouts_rewrites(self): ms = modulestore('direct') - cs = contentstore() + cs = contentstore() # import a test course - import_from_xml(ms, 'common/test/data/', ['full']) + import_from_xml(ms, 'common/test/data/', ['full']) - handout_location= Location(['i4x', 'edX', 'full', 'course_info', 'handouts']) + handout_location = Location(['i4x', 'edX', 'full', 'course_info', 'handouts']) # get module info resp = self.client.get(reverse('module_info', kwargs={'module_location': handout_location})) @@ -239,7 +240,7 @@ class ContentStoreToyCourseTest(ModuleStoreTestCase): # check that /static/ has been converted to the full path # note, we know the link it should be because that's what in the 'full' course in the test data - self.assertContains(resp, '/c4x/edX/full/asset/handouts_schematic_tutorial.pdf') + self.assertContains(resp, '/c4x/edX/full/asset/handouts_schematic_tutorial.pdf') class ContentStoreTest(ModuleStoreTestCase): @@ -302,7 +303,7 @@ class ContentStoreTest(ModuleStoreTestCase): data = parse_json(resp) self.assertEqual(resp.status_code, 200) - self.assertEqual(data['ErrMsg'], + self.assertEqual(data['ErrMsg'], 'There is already a course defined with the same organization and course number.') def test_create_course_with_bad_organization(self): @@ -319,7 +320,7 @@ class ContentStoreTest(ModuleStoreTestCase): """Test viewing the index page with no courses""" # Create a course so there is something to view resp = self.client.get(reverse('index')) - self.assertContains(resp, + self.assertContains(resp, '<h1>My Courses</h1>', status_code=200, html=True) @@ -355,7 +356,7 @@ class ContentStoreTest(ModuleStoreTestCase): } resp = self.client.get(reverse('course_index', kwargs=data)) - self.assertContains(resp, + self.assertContains(resp, '<a href="/MITx/999/course/Robot_Super_Course" class="class-name">Robot Super Course</a>', status_code=200, html=True) @@ -365,8 +366,8 @@ class ContentStoreTest(ModuleStoreTestCase): CourseFactory.create(org='MITx', course='999', display_name='Robot Super Course') section_data = { - 'parent_location' : 'i4x://MITx/999/course/Robot_Super_Course', - 'template' : 'i4x://edx/templates/chapter/Empty', + 'parent_location': 'i4x://MITx/999/course/Robot_Super_Course', + 'template': 'i4x://edx/templates/chapter/Empty', 'display_name': 'Section One', } @@ -374,7 +375,7 @@ class ContentStoreTest(ModuleStoreTestCase): self.assertEqual(resp.status_code, 200) data = parse_json(resp) - self.assertRegexpMatches(data['id'], + self.assertRegexpMatches(data['id'], '^i4x:\/\/MITx\/999\/chapter\/([0-9]|[a-f]){32}$') def test_capa_module(self): @@ -382,8 +383,8 @@ class ContentStoreTest(ModuleStoreTestCase): CourseFactory.create(org='MITx', course='999', display_name='Robot Super Course') problem_data = { - 'parent_location' : 'i4x://MITx/999/course/Robot_Super_Course', - 'template' : 'i4x://edx/templates/problem/Empty' + 'parent_location': 'i4x://MITx/999/course/Robot_Super_Course', + 'template': 'i4x://edx/templates/problem/Empty' } resp = self.client.post(reverse('clone_item'), problem_data) diff --git a/cms/djangoapps/contentstore/tests/test_core_caching.py b/cms/djangoapps/contentstore/tests/test_core_caching.py index ed41e5cc64c..676627a045b 100644 --- a/cms/djangoapps/contentstore/tests/test_core_caching.py +++ b/cms/djangoapps/contentstore/tests/test_core_caching.py @@ -3,6 +3,7 @@ from xmodule.modulestore import Location from xmodule.contentstore.content import StaticContent from django.test import TestCase + class Content: def __init__(self, location, content): self.location = location @@ -11,6 +12,7 @@ class Content: def get_id(self): return StaticContent.get_id_from_location(self.location) + class CachingTestCase(TestCase): # Tests for https://edx.lighthouseapp.com/projects/102637/tickets/112-updating-asset-does-not-refresh-the-cached-copy unicodeLocation = Location(u'c4x', u'mitX', u'800', u'thumbnail', u'monsters.jpg') diff --git a/cms/djangoapps/contentstore/tests/test_course_settings.py b/cms/djangoapps/contentstore/tests/test_course_settings.py index f47733b32c9..84e79b96706 100644 --- a/cms/djangoapps/contentstore/tests/test_course_settings.py +++ b/cms/djangoapps/contentstore/tests/test_course_settings.py @@ -27,21 +27,21 @@ from xmodule.modulestore.tests.factories import CourseFactory class ConvertersTestCase(TestCase): @staticmethod def struct_to_datetime(struct_time): - return datetime.datetime(struct_time.tm_year, struct_time.tm_mon, struct_time.tm_mday, struct_time.tm_hour, - struct_time.tm_min, struct_time.tm_sec, tzinfo = UTC()) - + return datetime.datetime(struct_time.tm_year, struct_time.tm_mon, struct_time.tm_mday, struct_time.tm_hour, + struct_time.tm_min, struct_time.tm_sec, tzinfo=UTC()) + def compare_dates(self, date1, date2, expected_delta): dt1 = ConvertersTestCase.struct_to_datetime(date1) dt2 = ConvertersTestCase.struct_to_datetime(date2) self.assertEqual(dt1 - dt2, expected_delta, str(date1) + "-" + str(date2) + "!=" + str(expected_delta)) - + def test_iso_to_struct(self): self.compare_dates(converters.jsdate_to_time("2013-01-01"), converters.jsdate_to_time("2012-12-31"), datetime.timedelta(days=1)) self.compare_dates(converters.jsdate_to_time("2013-01-01T00"), converters.jsdate_to_time("2012-12-31T23"), datetime.timedelta(hours=1)) self.compare_dates(converters.jsdate_to_time("2013-01-01T00:00"), converters.jsdate_to_time("2012-12-31T23:59"), datetime.timedelta(minutes=1)) self.compare_dates(converters.jsdate_to_time("2013-01-01T00:00:00"), converters.jsdate_to_time("2012-12-31T23:59:59"), datetime.timedelta(seconds=1)) - - + + class CourseTestCase(ModuleStoreTestCase): def setUp(self): """ @@ -68,13 +68,14 @@ class CourseTestCase(ModuleStoreTestCase): self.client = Client() self.client.login(username=uname, password=password) - t='i4x://edx/templates/course/Empty' - o='MITx' - n='999' - dn='Robot Super Course' + t = 'i4x://edx/templates/course/Empty' + o = 'MITx' + n = '999' + dn = 'Robot Super Course' self.course_location = Location('i4x', o, n, 'course', 'Robot_Super_Course') CourseFactory.create(template=t, org=o, number=n, display_name=dn) + class CourseDetailsTestCase(CourseTestCase): def test_virgin_fetch(self): details = CourseDetails.fetch(self.course_location) @@ -86,7 +87,7 @@ class CourseDetailsTestCase(CourseTestCase): self.assertEqual(details.overview, "", "overview somehow initialized" + details.overview) self.assertIsNone(details.intro_video, "intro_video somehow initialized" + str(details.intro_video)) self.assertIsNone(details.effort, "effort somehow initialized" + str(details.effort)) - + def test_encoder(self): details = CourseDetails.fetch(self.course_location) jsondetails = json.dumps(details, cls=CourseSettingsEncoder) @@ -100,7 +101,7 @@ class CourseDetailsTestCase(CourseTestCase): self.assertEqual(jsondetails['overview'], "", "overview somehow initialized") self.assertIsNone(jsondetails['intro_video'], "intro_video somehow initialized") self.assertIsNone(jsondetails['effort'], "effort somehow initialized") - + def test_update_and_fetch(self): ## NOTE: I couldn't figure out how to validly test time setting w/ all the conversions jsondetails = CourseDetails.fetch(self.course_location) @@ -118,6 +119,7 @@ class CourseDetailsTestCase(CourseTestCase): self.assertEqual(CourseDetails.update_from_json(jsondetails.__dict__).effort, jsondetails.effort, "After set effort") + class CourseDetailsViewTest(CourseTestCase): def alter_field(self, url, details, field, val): setattr(details, field, val) @@ -128,36 +130,36 @@ class CourseDetailsViewTest(CourseTestCase): payload['end_date'] = CourseDetailsViewTest.convert_datetime_to_iso(details.end_date) payload['enrollment_start'] = CourseDetailsViewTest.convert_datetime_to_iso(details.enrollment_start) payload['enrollment_end'] = CourseDetailsViewTest.convert_datetime_to_iso(details.enrollment_end) - resp = self.client.post(url, json.dumps(payload), "application/json") + resp = self.client.post(url, json.dumps(payload), "application/json") self.compare_details_with_encoding(json.loads(resp.content), details.__dict__, field + str(val)) - + @staticmethod def convert_datetime_to_iso(datetime): if datetime is not None: return datetime.isoformat("T") else: return None - + def test_update_and_fetch(self): details = CourseDetails.fetch(self.course_location) - - resp = self.client.get(reverse('course_settings', kwargs={'org' : self.course_location.org, 'course' : self.course_location.course, - 'name' : self.course_location.name })) + + resp = self.client.get(reverse('course_settings', kwargs={'org': self.course_location.org, 'course': self.course_location.course, + 'name': self.course_location.name})) self.assertContains(resp, '<li><a href="#" class="is-shown" data-section="details">Course Details</a></li>', status_code=200, html=True) - # resp s/b json from here on - url = reverse('course_settings', kwargs={'org' : self.course_location.org, 'course' : self.course_location.course, - 'name' : self.course_location.name, 'section' : 'details' }) + # resp s/b json from here on + url = reverse('course_settings', kwargs={'org': self.course_location.org, 'course': self.course_location.course, + 'name': self.course_location.name, 'section': 'details'}) resp = self.client.get(url) self.compare_details_with_encoding(json.loads(resp.content), details.__dict__, "virgin get") utc = UTC() - self.alter_field(url, details, 'start_date', datetime.datetime(2012,11,12,1,30, tzinfo=utc)) - self.alter_field(url, details, 'start_date', datetime.datetime(2012,11,1,13,30, tzinfo=utc)) - self.alter_field(url, details, 'end_date', datetime.datetime(2013,2,12,1,30, tzinfo=utc)) - self.alter_field(url, details, 'enrollment_start', datetime.datetime(2012,10,12,1,30, tzinfo=utc)) + self.alter_field(url, details, 'start_date', datetime.datetime(2012, 11, 12, 1, 30, tzinfo=utc)) + self.alter_field(url, details, 'start_date', datetime.datetime(2012, 11, 1, 13, 30, tzinfo=utc)) + self.alter_field(url, details, 'end_date', datetime.datetime(2013, 2, 12, 1, 30, tzinfo=utc)) + self.alter_field(url, details, 'enrollment_start', datetime.datetime(2012, 10, 12, 1, 30, tzinfo=utc)) - self.alter_field(url, details, 'enrollment_end', datetime.datetime(2012,11,15,1,30, tzinfo=utc)) + self.alter_field(url, details, 'enrollment_end', datetime.datetime(2012, 11, 15, 1, 30, tzinfo=utc)) self.alter_field(url, details, 'overview', "Overview") self.alter_field(url, details, 'intro_video', "intro_video") self.alter_field(url, details, 'effort', "effort") @@ -170,7 +172,7 @@ class CourseDetailsViewTest(CourseTestCase): self.assertEqual(details['overview'], encoded['overview'], context + " overviews not ==") self.assertEqual(details['intro_video'], encoded.get('intro_video', None), context + " intro_video not ==") self.assertEqual(details['effort'], encoded['effort'], context + " efforts not ==") - + def compare_date_fields(self, details, encoded, context, field): if details[field] is not None: if field in encoded and encoded[field] is not None: @@ -182,14 +184,15 @@ class CourseDetailsViewTest(CourseTestCase): else: details_encoded = jsdate_to_time(details[field]) dt2 = ConvertersTestCase.struct_to_datetime(details_encoded) - + expected_delta = datetime.timedelta(0) self.assertEqual(dt1 - dt2, expected_delta, str(dt1) + "!=" + str(dt2) + " at " + context) else: self.fail(field + " missing from encoded but in details at " + context) elif field in encoded and encoded[field] is not None: self.fail(field + " included in encoding but missing from details at " + context) - + + class CourseGradingTest(CourseTestCase): def test_initial_grader(self): descriptor = get_modulestore(self.course_location).get_item(self.course_location) @@ -209,56 +212,56 @@ class CourseGradingTest(CourseTestCase): self.assertEqual(self.course_location, test_grader.course_location, "Course locations") self.assertIsNotNone(test_grader.graders, "No graders") self.assertIsNotNone(test_grader.grade_cutoffs, "No cutoffs") - + for i, grader in enumerate(test_grader.graders): subgrader = CourseGradingModel.fetch_grader(self.course_location, i) self.assertDictEqual(grader, subgrader, str(i) + "th graders not equal") - + subgrader = CourseGradingModel.fetch_grader(self.course_location.list(), 0) self.assertDictEqual(test_grader.graders[0], subgrader, "failed with location as list") - + def test_fetch_cutoffs(self): test_grader = CourseGradingModel.fetch_cutoffs(self.course_location) # ??? should this check that it's at least a dict? (expected is { "pass" : 0.5 } I think) self.assertIsNotNone(test_grader, "No cutoffs via fetch") - + test_grader = CourseGradingModel.fetch_cutoffs(self.course_location.url()) self.assertIsNotNone(test_grader, "No cutoffs via fetch with url") - + def test_fetch_grace(self): test_grader = CourseGradingModel.fetch_grace_period(self.course_location) # almost a worthless test self.assertIn('grace_period', test_grader, "No grace via fetch") - + test_grader = CourseGradingModel.fetch_grace_period(self.course_location.url()) self.assertIn('grace_period', test_grader, "No cutoffs via fetch with url") - + def test_update_from_json(self): test_grader = CourseGradingModel.fetch(self.course_location) altered_grader = CourseGradingModel.update_from_json(test_grader.__dict__) self.assertDictEqual(test_grader.__dict__, altered_grader.__dict__, "Noop update") - + test_grader.graders[0]['weight'] = test_grader.graders[0].get('weight') * 2 altered_grader = CourseGradingModel.update_from_json(test_grader.__dict__) self.assertDictEqual(test_grader.__dict__, altered_grader.__dict__, "Weight[0] * 2") - + test_grader.grade_cutoffs['D'] = 0.3 altered_grader = CourseGradingModel.update_from_json(test_grader.__dict__) self.assertDictEqual(test_grader.__dict__, altered_grader.__dict__, "cutoff add D") - + test_grader.grace_period = {'hours' : '4'} altered_grader = CourseGradingModel.update_from_json(test_grader.__dict__) self.assertDictEqual(test_grader.__dict__, altered_grader.__dict__, "4 hour grace period") - + def test_update_grader_from_json(self): test_grader = CourseGradingModel.fetch(self.course_location) altered_grader = CourseGradingModel.update_grader_from_json(test_grader.course_location, test_grader.graders[1]) self.assertDictEqual(test_grader.graders[1], altered_grader, "Noop update") - + test_grader.graders[1]['min_count'] = test_grader.graders[1].get('min_count') + 2 altered_grader = CourseGradingModel.update_grader_from_json(test_grader.course_location, test_grader.graders[1]) self.assertDictEqual(test_grader.graders[1], altered_grader, "min_count[1] + 2") - + test_grader.graders[1]['drop_count'] = test_grader.graders[1].get('drop_count') + 1 altered_grader = CourseGradingModel.update_grader_from_json(test_grader.course_location, test_grader.graders[1]) self.assertDictEqual(test_grader.graders[1], altered_grader, "drop_count[1] + 2") diff --git a/cms/djangoapps/contentstore/tests/test_course_updates.py b/cms/djangoapps/contentstore/tests/test_course_updates.py index 96e4468b312..c57f1322f57 100644 --- a/cms/djangoapps/contentstore/tests/test_course_updates.py +++ b/cms/djangoapps/contentstore/tests/test_course_updates.py @@ -2,29 +2,30 @@ from cms.djangoapps.contentstore.tests.test_course_settings import CourseTestCas from django.core.urlresolvers import reverse import json + class CourseUpdateTest(CourseTestCase): def test_course_update(self): # first get the update to force the creation - url = reverse('course_info', kwargs={'org' : self.course_location.org, 'course' : self.course_location.course, - 'name' : self.course_location.name }) + url = reverse('course_info', kwargs={'org': self.course_location.org, 'course': self.course_location.course, + 'name': self.course_location.name}) self.client.get(url) content = '<iframe width="560" height="315" src="http://www.youtube.com/embed/RocY-Jd93XU" frameborder="0"></iframe>' - payload = { 'content' : content, - 'date' : 'January 8, 2013'} - url = reverse('course_info', kwargs={'org' : self.course_location.org, 'course' : self.course_location.course, - 'provided_id' : ''}) - + payload = {'content': content, + 'date': 'January 8, 2013'} + url = reverse('course_info', kwargs={'org': self.course_location.org, 'course': self.course_location.course, + 'provided_id': ''}) + resp = self.client.post(url, json.dumps(payload), "application/json") - - payload= json.loads(resp.content) - + + payload = json.loads(resp.content) + self.assertHTMLEqual(content, payload['content'], "single iframe") - - url = reverse('course_info', kwargs={'org' : self.course_location.org, 'course' : self.course_location.course, - 'provided_id' : payload['id']}) + + url = reverse('course_info', kwargs={'org': self.course_location.org, 'course': self.course_location.course, + 'provided_id': payload['id']}) content += '<div>div <p>p</p></div>' payload['content'] = content resp = self.client.post(url, json.dumps(payload), "application/json") - + self.assertHTMLEqual(content, json.loads(resp.content)['content'], "iframe w/ div") diff --git a/cms/djangoapps/contentstore/tests/test_utils.py b/cms/djangoapps/contentstore/tests/test_utils.py index 6811d64c121..09e3b045f9e 100644 --- a/cms/djangoapps/contentstore/tests/test_utils.py +++ b/cms/djangoapps/contentstore/tests/test_utils.py @@ -2,15 +2,16 @@ from cms.djangoapps.contentstore import utils import mock from django.test import TestCase + class LMSLinksTestCase(TestCase): def about_page_test(self): - location = 'i4x','mitX','101','course', 'test' + location = 'i4x', 'mitX', '101', 'course', 'test' utils.get_course_id = mock.Mock(return_value="mitX/101/test") link = utils.get_lms_link_for_about_page(location) self.assertEquals(link, "//localhost:8000/courses/mitX/101/test/about") def ls_link_test(self): - location = 'i4x','mitX','101','vertical', 'contacting_us' + location = 'i4x', 'mitX', '101', 'vertical', 'contacting_us' utils.get_course_id = mock.Mock(return_value="mitX/101/test") link = utils.get_lms_link_for_item(location, False) self.assertEquals(link, "//localhost:8000/courses/mitX/101/test/jump_to/i4x://mitX/101/vertical/contacting_us") diff --git a/cms/djangoapps/contentstore/tests/tests.py b/cms/djangoapps/contentstore/tests/tests.py index df2e4dcc799..9af5b092766 100644 --- a/cms/djangoapps/contentstore/tests/tests.py +++ b/cms/djangoapps/contentstore/tests/tests.py @@ -28,6 +28,7 @@ from xmodule.seq_module import SequenceDescriptor from xmodule.modulestore.tests.factories import CourseFactory, ItemFactory from utils import ModuleStoreTestCase, parse_json, user, registration + class ContentStoreTestCase(ModuleStoreTestCase): def _login(self, email, pw): """Login. View should always return 200. The success/fail is in the diff --git a/cms/djangoapps/contentstore/tests/utils.py b/cms/djangoapps/contentstore/tests/utils.py index 7fa5b766852..4e3510463f4 100644 --- a/cms/djangoapps/contentstore/tests/utils.py +++ b/cms/djangoapps/contentstore/tests/utils.py @@ -11,8 +11,9 @@ from django.contrib.auth.models import User import xmodule.modulestore.django from xmodule.templates import update_templates + class ModuleStoreTestCase(TestCase): - """ Subclass for any test case that uses the mongodb + """ Subclass for any test case that uses the mongodb module store. This populates a uniquely named modulestore collection with templates before running the TestCase and drops it they are finished. """ @@ -22,7 +23,7 @@ class ModuleStoreTestCase(TestCase): # Use the current seconds since epoch to differentiate # the mongo collections on jenkins. - sec_since_epoch = '%s' % int(time()*100) + sec_since_epoch = '%s' % int(time() * 100) self.orig_MODULESTORE = copy.deepcopy(settings.MODULESTORE) self.test_MODULESTORE = self.orig_MODULESTORE self.test_MODULESTORE['default']['OPTIONS']['collection'] = 'modulestore_%s' % sec_since_epoch @@ -50,14 +51,17 @@ class ModuleStoreTestCase(TestCase): super(ModuleStoreTestCase, self)._post_teardown() + def parse_json(response): """Parse response, which is assumed to be json""" return json.loads(response.content) + def user(email): """look up a user by email""" return User.objects.get(email=email) + def registration(email): """look up registration object by email""" return Registration.objects.get(user__email=email) diff --git a/cms/djangoapps/contentstore/utils.py b/cms/djangoapps/contentstore/utils.py index da2993e4639..b14dd8b353d 100644 --- a/cms/djangoapps/contentstore/utils.py +++ b/cms/djangoapps/contentstore/utils.py @@ -5,18 +5,20 @@ from xmodule.modulestore.exceptions import ItemNotFoundError DIRECT_ONLY_CATEGORIES = ['course', 'chapter', 'sequential', 'about', 'static_tab', 'course_info'] + def get_modulestore(location): """ Returns the correct modulestore to use for modifying the specified location """ if not isinstance(location, Location): location = Location(location) - + if location.category in DIRECT_ONLY_CATEGORIES: return modulestore('direct') else: return modulestore() + def get_course_location_for_item(location): ''' cdodge: for a given Xmodule, return the course that it belongs to @@ -46,6 +48,7 @@ def get_course_location_for_item(location): return location + def get_course_for_item(location): ''' cdodge: for a given Xmodule, return the course that it belongs to @@ -85,6 +88,7 @@ def get_lms_link_for_item(location, preview=False): return lms_link + def get_lms_link_for_about_page(location): """ Returns the url to the course about page from the location tuple. @@ -99,6 +103,7 @@ def get_lms_link_for_about_page(location): return lms_link + def get_course_id(location): """ Returns the course_id from a given the location tuple. @@ -106,6 +111,7 @@ def get_course_id(location): # TODO: These will need to be changed to point to the particular instance of this problem in the particular course return modulestore().get_containing_courses(Location(location))[0].id + class UnitState(object): draft = 'draft' private = 'private' @@ -135,6 +141,7 @@ def compute_unit_state(unit): def get_date_display(date): return date.strftime("%d %B, %Y at %I:%M %p") + def update_item(location, value): """ If value is None, delete the db entry. Otherwise, update it using the correct modulestore. @@ -142,4 +149,4 @@ def update_item(location, value): if value is None: get_modulestore(location).delete_item(location) else: - get_modulestore(location).update_item(location, value) \ No newline at end of file + get_modulestore(location).update_item(location, value) diff --git a/cms/djangoapps/contentstore/views.py b/cms/djangoapps/contentstore/views.py index fb63cb34edc..137e71b24af 100644 --- a/cms/djangoapps/contentstore/views.py +++ b/cms/djangoapps/contentstore/views.py @@ -81,6 +81,7 @@ def signup(request): csrf_token = csrf(request)['csrf_token'] return render_to_response('signup.html', {'csrf': csrf_token}) + @ssl_login_shortcut @ensure_csrf_cookie def login_page(request): @@ -114,7 +115,7 @@ def index(request): courses = filter(course_filter, courses) return render_to_response('index.html', { - 'new_course_template' : Location('i4x', 'edx', 'templates', 'course', 'Empty'), + 'new_course_template': Location('i4x', 'edx', 'templates', 'course', 'Empty'), 'courses': [(course.metadata.get('display_name'), reverse('course_index', args=[ course.location.org, @@ -159,10 +160,10 @@ def course_index(request, org, course, name): if not has_access(request.user, location): raise PermissionDenied() - upload_asset_callback_url = reverse('upload_asset', kwargs = { - 'org' : org, - 'course' : course, - 'coursename' : name + upload_asset_callback_url = reverse('upload_asset', kwargs={ + 'org': org, + 'course': course, + 'coursename': name }) course = modulestore().get_item(location) @@ -213,7 +214,7 @@ def edit_subsection(request, location): # remove all metadata from the generic dictionary that is presented in a more normalized UI - policy_metadata = dict((key,value) for key, value in item.metadata.iteritems() + policy_metadata = dict((key, value) for key, value in item.metadata.iteritems() if key not in ['display_name', 'start', 'due', 'format'] and key not in item.system_metadata_fields) can_view_live = False @@ -233,9 +234,9 @@ def edit_subsection(request, location): 'course_graders': json.dumps(CourseGradingModel.fetch(course.location).graders), 'parent_location': course.location, 'parent_item': parent, - 'policy_metadata' : policy_metadata, - 'subsection_units' : subsection_units, - 'can_view_live' : can_view_live + 'policy_metadata': policy_metadata, + 'subsection_units': subsection_units, + 'can_view_live': can_view_live }) @@ -294,7 +295,7 @@ def edit_unit(request, location): # so let's generate the link url here # need to figure out where this item is in the list of children as the preview will need this - index =1 + index = 1 for child in containing_subsection.get_children(): if child.location == item.location: break @@ -348,6 +349,7 @@ def preview_component(request, location): 'editor': wrap_xmodule(component.get_html, component, 'xmodule_edit.html')(), }) + @expect_json @login_required @ensure_csrf_cookie @@ -362,7 +364,7 @@ def assignment_type_update(request, org, course, category, name): if request.method == 'GET': return HttpResponse(json.dumps(CourseGradingModel.get_section_grader_type(location)), mimetype="application/json") - elif request.method == 'POST': # post or put, doesn't matter. + elif request.method == 'POST': # post or put, doesn't matter. return HttpResponse(json.dumps(CourseGradingModel.update_section_grader_type(location, request.POST)), mimetype="application/json") @@ -527,7 +529,7 @@ def load_preview_module(request, preview_id, descriptor, instance_state, shared_ module.get_html = replace_static_urls( module.get_html, module.metadata.get('data_dir', module.location.course), - course_namespace = Location([module.location.tag, module.location.org, module.location.course, None, None]) + course_namespace=Location([module.location.tag, module.location.org, module.location.course, None, None]) ) save_preview_state(request, preview_id, descriptor.location.url(), module.get_instance_state(), module.get_shared_state()) @@ -588,7 +590,7 @@ def delete_item(request): # semantics of delete_item whereby the store is draft aware. Right now calling # delete_item on a vertical tries to delete the draft version leaving the # requested delete to never occur - if item.location.revision is None and item.location.category=='vertical' and delete_all_versions: + if item.location.revision is None and item.location.category == 'vertical' and delete_all_versions: modulestore('direct').delete_item(item.location) return HttpResponse() @@ -664,6 +666,7 @@ def create_draft(request): return HttpResponse() + @login_required @expect_json def publish_draft(request): @@ -693,6 +696,7 @@ def unpublish_unit(request): return HttpResponse() + @login_required @expect_json def clone_item(request): @@ -725,6 +729,8 @@ def clone_item(request): #@login_required #@ensure_csrf_cookie + + def upload_asset(request, org, course, coursename): ''' cdodge: this method allows for POST uploading of files into the course asset library, which will @@ -775,11 +781,11 @@ def upload_asset(request, org, course, coursename): # readback the saved content - we need the database timestamp readback = contentstore().find(content.location) - response_payload = {'displayname' : content.name, - 'uploadDate' : get_date_display(readback.last_modified_at), - 'url' : StaticContent.get_url_path_from_location(content.location), - 'thumb_url' : StaticContent.get_url_path_from_location(thumbnail_location) if thumbnail_content is not None else None, - 'msg' : 'Upload completed' + response_payload = {'displayname': content.name, + 'uploadDate': get_date_display(readback.last_modified_at), + 'url': StaticContent.get_url_path_from_location(content.location), + 'thumb_url': StaticContent.get_url_path_from_location(thumbnail_location) if thumbnail_content is not None else None, + 'msg': 'Upload completed' } response = HttpResponse(json.dumps(response_payload)) @@ -789,6 +795,8 @@ def upload_asset(request, org, course, coursename): ''' This view will return all CMS users who are editors for the specified course ''' + + @login_required @ensure_csrf_cookie def manage_users(request, location): @@ -803,16 +811,16 @@ def manage_users(request, location): 'active_tab': 'users', 'context_course': course_module, 'staff': get_users_in_course_group_by_role(location, STAFF_ROLE_NAME), - 'add_user_postback_url' : reverse('add_user', args=[location]).rstrip('/'), - 'remove_user_postback_url' : reverse('remove_user', args=[location]).rstrip('/'), - 'allow_actions' : has_access(request.user, location, role=INSTRUCTOR_ROLE_NAME), - 'request_user_id' : request.user.id + 'add_user_postback_url': reverse('add_user', args=[location]).rstrip('/'), + 'remove_user_postback_url': reverse('remove_user', args=[location]).rstrip('/'), + 'allow_actions': has_access(request.user, location, role=INSTRUCTOR_ROLE_NAME), + 'request_user_id': request.user.id }) -def create_json_response(errmsg = None): +def create_json_response(errmsg=None): if errmsg is not None: - resp = HttpResponse(json.dumps({'Status': 'Failed', 'ErrMsg' : errmsg})) + resp = HttpResponse(json.dumps({'Status': 'Failed', 'ErrMsg': errmsg})) else: resp = HttpResponse(json.dumps({'Status': 'OK'})) @@ -822,13 +830,15 @@ def create_json_response(errmsg = None): This POST-back view will add a user - specified by email - to the list of editors for the specified course ''' + + @expect_json @login_required @ensure_csrf_cookie def add_user(request, location): email = request.POST["email"] - if email=='': + if email == '': return create_json_response('Please specify an email address.') # check that logged in user has admin permissions to this course @@ -854,6 +864,8 @@ def add_user(request, location): This POST-back view will remove a user - specified by email - from the list of editors for the specified course ''' + + @expect_json @login_required @ensure_csrf_cookie @@ -881,6 +893,7 @@ def remove_user(request, location): def landing(request, org, course, coursename): return render_to_response('temp-course-landing.html', {}) + @login_required @ensure_csrf_cookie def static_pages(request, org, course, coursename): @@ -921,7 +934,7 @@ def reorder_static_tabs(request): return HttpResponseBadRequest() # load all reference tabs, return BadRequest if we can't find any of them - tab_items =[] + tab_items = [] for tab in tabs: item = modulestore('direct').get_item(Location(tab)) if item is None: @@ -935,8 +948,8 @@ def reorder_static_tabs(request): for tab in course.tabs: if tab['type'] == 'static_tab': reordered_tabs.append({'type': 'static_tab', - 'name' : tab_items[static_tab_idx].metadata.get('display_name'), - 'url_slug' : tab_items[static_tab_idx].location.name}) + 'name': tab_items[static_tab_idx].metadata.get('display_name'), + 'url_slug': tab_items[static_tab_idx].location.name}) static_tab_idx += 1 else: reordered_tabs.append(tab) @@ -980,10 +993,11 @@ def edit_tabs(request, org, course, coursename): return render_to_response('edit-tabs.html', { 'active_tab': 'pages', - 'context_course':course_item, + 'context_course': course_item, 'components': components }) + def not_found(request): return render_to_response('error.html', {'error': '404'}) @@ -1014,11 +1028,12 @@ def course_info(request, org, course, name, provided_id=None): return render_to_response('course_info.html', { 'active_tab': 'courseinfo-tab', 'context_course': course_module, - 'url_base' : "/" + org + "/" + course + "/", - 'course_updates' : json.dumps(get_course_updates(location)), + 'url_base': "/" + org + "/" + course + "/", + 'course_updates': json.dumps(get_course_updates(location)), 'handouts_location': Location(['i4x', org, course, 'course_info', 'handouts']).url() }) + @expect_json @login_required @ensure_csrf_cookie @@ -1075,8 +1090,8 @@ def module_info(request, module_location): else: real_method = request.method - rewrite_static_links = request.GET.get('rewrite_url_links','True') in ['True', 'true'] - logging.debug('rewrite_static_links = {0} {1}'.format(request.GET.get('rewrite_url_links','False'), rewrite_static_links)) + rewrite_static_links = request.GET.get('rewrite_url_links', 'True') in ['True', 'true'] + logging.debug('rewrite_static_links = {0} {1}'.format(request.GET.get('rewrite_url_links', 'False'), rewrite_static_links)) # check that logged in user has permissions to this item if not has_access(request.user, location): @@ -1089,6 +1104,7 @@ def module_info(request, module_location): else: return HttpResponseBadRequest() + @login_required @ensure_csrf_cookie def get_course_settings(request, org, course, name): @@ -1109,9 +1125,10 @@ def get_course_settings(request, org, course, name): return render_to_response('settings.html', { 'active_tab': 'settings', 'context_course': course_module, - 'course_details' : json.dumps(course_details, cls=CourseSettingsEncoder) + 'course_details': json.dumps(course_details, cls=CourseSettingsEncoder) }) + @expect_json @login_required @ensure_csrf_cookie @@ -1137,12 +1154,13 @@ def course_settings_updates(request, org, course, name, section): if request.method == 'GET': # Cannot just do a get w/o knowing the course name :-( - return HttpResponse(json.dumps(manager.fetch(Location(['i4x', org, course, 'course',name])), cls=CourseSettingsEncoder), + return HttpResponse(json.dumps(manager.fetch(Location(['i4x', org, course, 'course', name])), cls=CourseSettingsEncoder), mimetype="application/json") - elif request.method == 'POST': # post or put, doesn't matter. + elif request.method == 'POST': # post or put, doesn't matter. return HttpResponse(json.dumps(manager.update_from_json(request.POST), cls=CourseSettingsEncoder), mimetype="application/json") + @expect_json @login_required @ensure_csrf_cookie @@ -1167,14 +1185,14 @@ def course_grader_updates(request, org, course, name, grader_index=None): if real_method == 'GET': # Cannot just do a get w/o knowing the course name :-( - return HttpResponse(json.dumps(CourseGradingModel.fetch_grader(Location(['i4x', org, course, 'course',name]), grader_index)), + return HttpResponse(json.dumps(CourseGradingModel.fetch_grader(Location(['i4x', org, course, 'course', name]), grader_index)), mimetype="application/json") elif real_method == "DELETE": # ??? Shoudl this return anything? Perhaps success fail? - CourseGradingModel.delete_grader(Location(['i4x', org, course, 'course',name]), grader_index) + CourseGradingModel.delete_grader(Location(['i4x', org, course, 'course', name]), grader_index) return HttpResponse() - elif request.method == 'POST': # post or put, doesn't matter. - return HttpResponse(json.dumps(CourseGradingModel.update_grader_from_json(Location(['i4x', org, course, 'course',name]), request.POST)), + elif request.method == 'POST': # post or put, doesn't matter. + return HttpResponse(json.dumps(CourseGradingModel.update_grader_from_json(Location(['i4x', org, course, 'course', name]), request.POST)), mimetype="application/json") @@ -1193,10 +1211,10 @@ def asset_index(request, org, course, name): raise PermissionDenied() - upload_asset_callback_url = reverse('upload_asset', kwargs = { - 'org' : org, - 'course' : course, - 'coursename' : name + upload_asset_callback_url = reverse('upload_asset', kwargs={ + 'org': org, + 'course': course, + 'coursename': name }) course_module = modulestore().get_item(location) @@ -1237,13 +1255,14 @@ def asset_index(request, org, course, name): def edge(request): return render_to_response('university_profiles/edge.html', {}) + @login_required @expect_json def create_new_course(request): # This logic is repeated in xmodule/modulestore/tests/factories.py # so if you change anything here, you need to also change it there. # TODO: write a test that creates two courses, one with the factory and - # the other with this method, then compare them to make sure they are + # the other with this method, then compare them to make sure they are # equivalent. template = Location(request.POST['template']) org = request.POST.get('org') @@ -1288,6 +1307,7 @@ def create_new_course(request): return HttpResponse(json.dumps({'id': new_course.location.url()})) + def initialize_course_tabs(course): # set up the default tabs # I've added this because when we add static tabs, the LMS either expects a None for the tabs list or @@ -1297,7 +1317,7 @@ def initialize_course_tabs(course): # This logic is repeated in xmodule/modulestore/tests/factories.py # so if you change anything here, you need to also change it there. - course.tabs = [{"type": "courseware"}, + course.tabs = [{"type": "courseware"}, {"type": "course_info", "name": "Course Info"}, {"type": "discussion", "name": "Discussion"}, {"type": "wiki", "name": "Wiki"}, @@ -1305,6 +1325,7 @@ def initialize_course_tabs(course): modulestore('direct').update_metadata(course.location.url(), course.own_metadata) + @ensure_csrf_cookie @login_required def import_course(request, org, course, name): @@ -1343,7 +1364,7 @@ def import_course(request, org, course, name): # find the 'course.xml' file - for r,d,f in os.walk(course_dir): + for r, d, f in os.walk(course_dir): for files in f: if files == 'course.xml': break @@ -1357,10 +1378,10 @@ def import_course(request, org, course, name): if r != course_dir: for fname in os.listdir(r): - shutil.move(r/fname, course_dir) + shutil.move(r / fname, course_dir) module_store, course_items = import_from_xml(modulestore('direct'), settings.GITHUB_REPO_ROOT, - [course_subdir], load_error_modules=False, static_content_store=contentstore(), target_location_namespace = Location(location)) + [course_subdir], load_error_modules=False, static_content_store=contentstore(), target_location_namespace=Location(location)) # we can blow this away when we're done importing. shutil.rmtree(course_dir) @@ -1376,12 +1397,13 @@ def import_course(request, org, course, name): return render_to_response('import.html', { 'context_course': course_module, 'active_tab': 'import', - 'successful_import_redirect_url' : reverse('course_index', args=[ + 'successful_import_redirect_url': reverse('course_index', args=[ course_module.location.org, course_module.location.course, course_module.location.name]) }) + @ensure_csrf_cookie @login_required def generate_export_course(request, org, course, name): @@ -1391,7 +1413,7 @@ def generate_export_course(request, org, course, name): raise PermissionDenied() loc = Location(location) - export_file = NamedTemporaryFile(prefix=name+'.', suffix=".tar.gz") + export_file = NamedTemporaryFile(prefix=name + '.', suffix=".tar.gz") root_dir = path(mkdtemp()) @@ -1404,11 +1426,11 @@ def generate_export_course(request, org, course, name): logging.debug('tar file being generated at {0}'.format(export_file.name)) tf = tarfile.open(name=export_file.name, mode='w:gz') - tf.add(root_dir/name, arcname=name) + tf.add(root_dir / name, arcname=name) tf.close() # remove temp dir - shutil.rmtree(root_dir/name) + shutil.rmtree(root_dir / name) wrapper = FileWrapper(export_file) response = HttpResponse(wrapper, content_type='application/x-tgz') @@ -1430,9 +1452,10 @@ def export_course(request, org, course, name): return render_to_response('export.html', { 'context_course': course_module, 'active_tab': 'export', - 'successful_import_redirect_url' : '' + 'successful_import_redirect_url': '' }) + def event(request): ''' A noop to swallow the analytics call so that cms methods don't spook and poor developers looking at diff --git a/cms/djangoapps/models/settings/course_details.py b/cms/djangoapps/models/settings/course_details.py index d01e784d747..b27f4e38044 100644 --- a/cms/djangoapps/models/settings/course_details.py +++ b/cms/djangoapps/models/settings/course_details.py @@ -31,16 +31,16 @@ class CourseDetails(object): """ if not isinstance(course_location, Location): course_location = Location(course_location) - + course = cls(course_location) - + descriptor = get_modulestore(course_location).get_item(course_location) - + course.start_date = descriptor.start course.end_date = descriptor.end course.enrollment_start = descriptor.enrollment_start course.enrollment_end = descriptor.enrollment_end - + temploc = course_location._replace(category='about', name='syllabus') try: course.syllabus = get_modulestore(temploc).get_item(temploc).definition['data'] @@ -52,32 +52,32 @@ class CourseDetails(object): course.overview = get_modulestore(temploc).get_item(temploc).definition['data'] except ItemNotFoundError: pass - + temploc = temploc._replace(name='effort') try: course.effort = get_modulestore(temploc).get_item(temploc).definition['data'] except ItemNotFoundError: pass - + temploc = temploc._replace(name='video') try: raw_video = get_modulestore(temploc).get_item(temploc).definition['data'] - course.intro_video = CourseDetails.parse_video_tag(raw_video) + course.intro_video = CourseDetails.parse_video_tag(raw_video) except ItemNotFoundError: pass - + return course - + @classmethod def update_from_json(cls, jsondict): """ Decode the json into CourseDetails and save any changed attrs to the db """ - ## TODO make it an error for this to be undefined & for it to not be retrievable from modulestore + ## TODO make it an error for this to be undefined & for it to not be retrievable from modulestore course_location = jsondict['course_location'] ## Will probably want to cache the inflight courses because every blur generates an update descriptor = get_modulestore(course_location).get_item(course_location) - + dirty = False if 'start_date' in jsondict: @@ -87,7 +87,7 @@ class CourseDetails(object): if converted != descriptor.start: dirty = True descriptor.start = converted - + if 'end_date' in jsondict: converted = jsdate_to_time(jsondict['end_date']) else: @@ -96,7 +96,7 @@ class CourseDetails(object): if converted != descriptor.end: dirty = True descriptor.end = converted - + if 'enrollment_start' in jsondict: converted = jsdate_to_time(jsondict['enrollment_start']) else: @@ -105,7 +105,7 @@ class CourseDetails(object): if converted != descriptor.enrollment_start: dirty = True descriptor.enrollment_start = converted - + if 'enrollment_end' in jsondict: converted = jsdate_to_time(jsondict['enrollment_end']) else: @@ -114,10 +114,10 @@ class CourseDetails(object): if converted != descriptor.enrollment_end: dirty = True descriptor.enrollment_end = converted - + if dirty: get_modulestore(course_location).update_metadata(course_location, descriptor.metadata) - + # NOTE: below auto writes to the db w/o verifying that any of the fields actually changed # to make faster, could compare against db or could have client send over a list of which fields changed. temploc = Location(course_location)._replace(category='about', name='syllabus') @@ -125,19 +125,19 @@ class CourseDetails(object): temploc = temploc._replace(name='overview') update_item(temploc, jsondict['overview']) - + temploc = temploc._replace(name='effort') update_item(temploc, jsondict['effort']) - + temploc = temploc._replace(name='video') recomposed_video_tag = CourseDetails.recompose_video_tag(jsondict['intro_video']) update_item(temploc, recomposed_video_tag) - - + + # Could just generate and return a course obj w/o doing any db reads, but I put the reads in as a means to confirm # it persisted correctly return CourseDetails.fetch(course_location) - + @staticmethod def parse_video_tag(raw_video): """ @@ -147,17 +147,17 @@ class CourseDetails(object): """ if not raw_video: return None - + keystring_matcher = re.search('(?<=embed/)[a-zA-Z0-9_-]+', raw_video) if keystring_matcher is None: keystring_matcher = re.search('<?=\d+:[a-zA-Z0-9_-]+', raw_video) - + if keystring_matcher: return keystring_matcher.group(0) else: logging.warn("ignoring the content because it doesn't not conform to expected pattern: " + raw_video) return None - + @staticmethod def recompose_video_tag(video_key): # TODO should this use a mako template? Of course, my hope is that this is a short-term workaround for the db not storing @@ -168,7 +168,7 @@ class CourseDetails(object): video_key + '?autoplay=1&rel=0" frameborder="0" allowfullscreen=""></iframe>' return result - + # TODO move to a more general util? Is there a better way to do the isinstance model check? class CourseSettingsEncoder(json.JSONEncoder): diff --git a/cms/djangoapps/models/settings/course_grading.py b/cms/djangoapps/models/settings/course_grading.py index 9cfa18c8c97..f4c6fd3d7cd 100644 --- a/cms/djangoapps/models/settings/course_grading.py +++ b/cms/djangoapps/models/settings/course_grading.py @@ -6,55 +6,55 @@ from util import converters class CourseGradingModel(object): """ - Basically a DAO and Model combo for CRUD operations pertaining to grading policy. + Basically a DAO and Model combo for CRUD operations pertaining to grading policy. """ def __init__(self, course_descriptor): self.course_location = course_descriptor.location - self.graders = [CourseGradingModel.jsonize_grader(i, grader) for i, grader in enumerate(course_descriptor.raw_grader)] # weights transformed to ints [0..100] + self.graders = [CourseGradingModel.jsonize_grader(i, grader) for i, grader in enumerate(course_descriptor.raw_grader)] # weights transformed to ints [0..100] self.grade_cutoffs = course_descriptor.grade_cutoffs self.grace_period = CourseGradingModel.convert_set_grace_period(course_descriptor) - - @classmethod + + @classmethod def fetch(cls, course_location): """ Fetch the course details for the given course from persistence and return a CourseDetails model. """ if not isinstance(course_location, Location): course_location = Location(course_location) - + descriptor = get_modulestore(course_location).get_item(course_location) model = cls(descriptor) return model - + @staticmethod def fetch_grader(course_location, index): """ - Fetch the course's nth grader + Fetch the course's nth grader Returns an empty dict if there's no such grader. """ if not isinstance(course_location, Location): course_location = Location(course_location) - + descriptor = get_modulestore(course_location).get_item(course_location) # # ??? it would be good if these had the course_location in them so that they stand alone sufficiently # # but that would require not using CourseDescriptor's field directly. Opinions? - index = int(index) - if len(descriptor.raw_grader) > index: + index = int(index) + if len(descriptor.raw_grader) > index: return CourseGradingModel.jsonize_grader(index, descriptor.raw_grader[index]) - + # return empty model else: return { - "id" : index, - "type" : "", - "min_count" : 0, - "drop_count" : 0, - "short_label" : None, - "weight" : 0 + "id": index, + "type": "", + "min_count": 0, + "drop_count": 0, + "short_label": None, + "weight": 0 } - + @staticmethod def fetch_cutoffs(course_location): """ @@ -62,7 +62,7 @@ class CourseGradingModel(object): """ if not isinstance(course_location, Location): course_location = Location(course_location) - + descriptor = get_modulestore(course_location).get_item(course_location) return descriptor.grade_cutoffs @@ -73,10 +73,10 @@ class CourseGradingModel(object): """ if not isinstance(course_location, Location): course_location = Location(course_location) - + descriptor = get_modulestore(course_location).get_item(course_location) - return {'grace_period' : CourseGradingModel.convert_set_grace_period(descriptor) } - + return {'grace_period': CourseGradingModel.convert_set_grace_period(descriptor)} + @staticmethod def update_from_json(jsondict): """ @@ -85,32 +85,32 @@ class CourseGradingModel(object): """ course_location = jsondict['course_location'] descriptor = get_modulestore(course_location).get_item(course_location) - + graders_parsed = [CourseGradingModel.parse_grader(jsonele) for jsonele in jsondict['graders']] - + descriptor.raw_grader = graders_parsed descriptor.grade_cutoffs = jsondict['grade_cutoffs'] - + get_modulestore(course_location).update_item(course_location, descriptor.definition['data']) CourseGradingModel.update_grace_period_from_json(course_location, jsondict['grace_period']) - + return CourseGradingModel.fetch(course_location) - - + + @staticmethod def update_grader_from_json(course_location, grader): """ - Create or update the grader of the given type (string key) for the given course. Returns the modified + Create or update the grader of the given type (string key) for the given course. Returns the modified grader which is a full model on the client but not on the server (just a dict) """ if not isinstance(course_location, Location): course_location = Location(course_location) - + descriptor = get_modulestore(course_location).get_item(course_location) # # ??? it would be good if these had the course_location in them so that they stand alone sufficiently # # but that would require not using CourseDescriptor's field directly. Opinions? - # parse removes the id; so, grab it before parse + # parse removes the id; so, grab it before parse index = int(grader.get('id', len(descriptor.raw_grader))) grader = CourseGradingModel.parse_grader(grader) @@ -118,11 +118,11 @@ class CourseGradingModel(object): descriptor.raw_grader[index] = grader else: descriptor.raw_grader.append(grader) - + get_modulestore(course_location).update_item(course_location, descriptor.definition['data']) - + return CourseGradingModel.jsonize_grader(index, descriptor.raw_grader[index]) - + @staticmethod def update_cutoffs_from_json(course_location, cutoffs): """ @@ -131,18 +131,18 @@ class CourseGradingModel(object): """ if not isinstance(course_location, Location): course_location = Location(course_location) - + descriptor = get_modulestore(course_location).get_item(course_location) descriptor.grade_cutoffs = cutoffs get_modulestore(course_location).update_item(course_location, descriptor.definition['data']) - + return cutoffs - - + + @staticmethod def update_grace_period_from_json(course_location, graceperiodjson): """ - Update the course's default grace period. Incoming dict is {hours: h, minutes: m} possibly as a + Update the course's default grace period. Incoming dict is {hours: h, minutes: m} possibly as a grace_period entry in an enclosing dict. It is also safe to call this method with a value of None for graceperiodjson. """ @@ -160,7 +160,7 @@ class CourseGradingModel(object): descriptor = get_modulestore(course_location).get_item(course_location) descriptor.metadata['graceperiod'] = grace_rep get_modulestore(course_location).update_metadata(course_location, descriptor.metadata) - + @staticmethod def delete_grader(course_location, index): """ @@ -168,16 +168,16 @@ class CourseGradingModel(object): """ if not isinstance(course_location, Location): course_location = Location(course_location) - + descriptor = get_modulestore(course_location).get_item(course_location) - index = int(index) + index = int(index) if index < len(descriptor.raw_grader): del descriptor.raw_grader[index] # force propagation to definition descriptor.raw_grader = descriptor.raw_grader get_modulestore(course_location).update_item(course_location, descriptor.definition['data']) - - # NOTE cannot delete cutoffs. May be useful to reset + + # NOTE cannot delete cutoffs. May be useful to reset @staticmethod def delete_cutoffs(course_location, cutoffs): """ @@ -185,13 +185,13 @@ class CourseGradingModel(object): """ if not isinstance(course_location, Location): course_location = Location(course_location) - + descriptor = get_modulestore(course_location).get_item(course_location) descriptor.grade_cutoffs = descriptor.defaut_grading_policy['GRADE_CUTOFFS'] get_modulestore(course_location).update_item(course_location, descriptor.definition['data']) - + return descriptor.grade_cutoffs - + @staticmethod def delete_grace_period(course_location): """ @@ -199,28 +199,28 @@ class CourseGradingModel(object): """ if not isinstance(course_location, Location): course_location = Location(course_location) - + descriptor = get_modulestore(course_location).get_item(course_location) if 'graceperiod' in descriptor.metadata: del descriptor.metadata['graceperiod'] get_modulestore(course_location).update_metadata(course_location, descriptor.metadata) - + @staticmethod def get_section_grader_type(location): if not isinstance(location, Location): location = Location(location) - + descriptor = get_modulestore(location).get_item(location) return { - "graderType" : descriptor.metadata.get('format', u"Not Graded"), - "location" : location, - "id" : 99 # just an arbitrary value to + "graderType": descriptor.metadata.get('format', u"Not Graded"), + "location": location, + "id": 99 # just an arbitrary value to } - + @staticmethod def update_section_grader_type(location, jsondict): if not isinstance(location, Location): location = Location(location) - + descriptor = get_modulestore(location).get_item(location) if 'graderType' in jsondict and jsondict['graderType'] != u"Not Graded": descriptor.metadata['format'] = jsondict.get('graderType') @@ -228,10 +228,10 @@ class CourseGradingModel(object): else: if 'format' in descriptor.metadata: del descriptor.metadata['format'] if 'graded' in descriptor.metadata: del descriptor.metadata['graded'] - - get_modulestore(location).update_metadata(location, descriptor.metadata) - - + + get_modulestore(location).update_metadata(location, descriptor.metadata) + + @staticmethod def convert_set_grace_period(descriptor): # 5 hours 59 minutes 59 seconds => converted to iso format @@ -245,13 +245,13 @@ class CourseGradingModel(object): def parse_grader(json_grader): # manual to clear out kruft result = { - "type" : json_grader["type"], - "min_count" : int(json_grader.get('min_count', 0)), - "drop_count" : int(json_grader.get('drop_count', 0)), - "short_label" : json_grader.get('short_label', None), - "weight" : float(json_grader.get('weight', 0)) / 100.0 + "type": json_grader["type"], + "min_count": int(json_grader.get('min_count', 0)), + "drop_count": int(json_grader.get('drop_count', 0)), + "short_label": json_grader.get('short_label', None), + "weight": float(json_grader.get('weight', 0)) / 100.0 } - + return result @staticmethod @@ -260,6 +260,6 @@ class CourseGradingModel(object): if grader['weight']: grader['weight'] *= 100 if not 'short_label' in grader: - grader['short_label'] = "" - + grader['short_label'] = "" + return grader diff --git a/cms/envs/acceptance.py b/cms/envs/acceptance.py index 5bc9b53fc48..26a8adc92c1 100644 --- a/cms/envs/acceptance.py +++ b/cms/envs/acceptance.py @@ -1,5 +1,5 @@ """ -This config file extends the test environment configuration +This config file extends the test environment configuration so that we can run the lettuce acceptance tests. """ from .test import * @@ -21,14 +21,14 @@ DATA_DIR = COURSES_ROOT # } # } -# Set this up so that rake lms[acceptance] and running the +# Set this up so that rake lms[acceptance] and running the # harvest command both use the same (test) database # which they can flush without messing up your dev db DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': ENV_ROOT / "db" / "test_mitx.db", - 'TEST_NAME': ENV_ROOT / "db" / "test_mitx.db", + 'TEST_NAME': ENV_ROOT / "db" / "test_mitx.db", } } diff --git a/cms/envs/common.py b/cms/envs/common.py index 3ea532d70d6..ef7a4f43fa9 100644 --- a/cms/envs/common.py +++ b/cms/envs/common.py @@ -33,8 +33,8 @@ MITX_FEATURES = { 'USE_DJANGO_PIPELINE': True, 'GITHUB_PUSH': False, 'ENABLE_DISCUSSION_SERVICE': False, - 'AUTH_USE_MIT_CERTIFICATES' : False, - 'STUB_VIDEO_FOR_TESTING': False, # do not display video when running automated acceptance tests + 'AUTH_USE_MIT_CERTIFICATES': False, + 'STUB_VIDEO_FOR_TESTING': False, # do not display video when running automated acceptance tests } ENABLE_JASMINE = False @@ -229,7 +229,7 @@ PIPELINE_JS = { 'source_filenames': sorted( rooted_glob(COMMON_ROOT / 'static/', 'coffee/src/**/*.coffee') + rooted_glob(PROJECT_ROOT / 'static/', 'coffee/src/**/*.coffee') - ) + [ 'js/hesitate.js', 'js/base.js'], + ) + ['js/hesitate.js', 'js/base.js'], 'output_filename': 'js/cms-application.js', }, 'module-js': { diff --git a/cms/envs/dev.py b/cms/envs/dev.py index e29ee62e207..3dee93a3987 100644 --- a/cms/envs/dev.py +++ b/cms/envs/dev.py @@ -12,7 +12,7 @@ TEMPLATE_DEBUG = DEBUG LOGGING = get_logger_config(ENV_ROOT / "log", logging_env="dev", tracking_filename="tracking.log", - dev_env = True, + dev_env=True, debug=True) modulestore_options = { @@ -41,7 +41,7 @@ CONTENTSTORE = { 'ENGINE': 'xmodule.contentstore.mongo.MongoContentStore', 'OPTIONS': { 'host': 'localhost', - 'db' : 'xcontent', + 'db': 'xcontent', } } diff --git a/cms/envs/dev_ike.py b/cms/envs/dev_ike.py index 5fb120854bd..1ebf219d44a 100644 --- a/cms/envs/dev_ike.py +++ b/cms/envs/dev_ike.py @@ -9,8 +9,6 @@ import socket MITX_FEATURES['AUTH_USE_MIT_CERTIFICATES'] = True -MITX_FEATURES['USE_DJANGO_PIPELINE']=False # don't recompile scss - -SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTOCOL', 'https') # django 1.4 for nginx ssl proxy - +MITX_FEATURES['USE_DJANGO_PIPELINE'] = False # don't recompile scss +SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTOCOL', 'https') # django 1.4 for nginx ssl proxy diff --git a/cms/envs/test.py b/cms/envs/test.py index 74c3e349a42..7f39e6818b3 100644 --- a/cms/envs/test.py +++ b/cms/envs/test.py @@ -19,7 +19,7 @@ TEST_RUNNER = 'django_nose.NoseTestSuiteRunner' TEST_ROOT = path('test_root') # Makes the tests run much faster... -SOUTH_TESTS_MIGRATE = False # To disable migrations and use syncdb instead +SOUTH_TESTS_MIGRATE = False # To disable migrations and use syncdb instead # Want static files in the same dir for running on jenkins. STATIC_ROOT = TEST_ROOT / "staticfiles" @@ -62,7 +62,7 @@ CONTENTSTORE = { 'ENGINE': 'xmodule.contentstore.mongo.MongoContentStore', 'OPTIONS': { 'host': 'localhost', - 'db' : 'xcontent', + 'db': 'xcontent', } } @@ -76,7 +76,7 @@ DATABASES = { LMS_BASE = "localhost:8000" CACHES = { - # This is the cache used for most things. Askbot will not work without a + # This is the cache used for most things. Askbot will not work without a # functioning cache -- it relies on caching to load its settings in places. # In staging/prod envs, the sessions also live here. 'default': { @@ -103,4 +103,4 @@ CACHES = { PASSWORD_HASHERS = ( 'django.contrib.auth.hashers.SHA1PasswordHasher', 'django.contrib.auth.hashers.MD5PasswordHasher', -) \ No newline at end of file +) diff --git a/cms/manage.py b/cms/manage.py index f8773c0641b..723fa59da17 100644 --- a/cms/manage.py +++ b/cms/manage.py @@ -2,7 +2,7 @@ from django.core.management import execute_manager import imp try: - imp.find_module('settings') # Assumed to be in the same directory. + imp.find_module('settings') # Assumed to be in the same directory. except ImportError: import sys sys.stderr.write("Error: Can't find the file 'settings.py' in the directory containing %r. " diff --git a/cms/urls.py b/cms/urls.py index c928e74d197..ad4dd87d747 100644 --- a/cms/urls.py +++ b/cms/urls.py @@ -48,7 +48,7 @@ urlpatterns = ('', url(r'^(?P<org>[^/]+)/(?P<course>[^/]+)/(?P<category>[^/]+)/(?P<name>[^/]+)/gradeas.*$', 'contentstore.views.assignment_type_update', name='assignment_type_update'), - url(r'^pages/(?P<org>[^/]+)/(?P<course>[^/]+)/course/(?P<coursename>[^/]+)$', 'contentstore.views.static_pages', + url(r'^pages/(?P<org>[^/]+)/(?P<course>[^/]+)/course/(?P<coursename>[^/]+)$', 'contentstore.views.static_pages', name='static_pages'), url(r'^edit_static/(?P<org>[^/]+)/(?P<course>[^/]+)/course/(?P<coursename>[^/]+)$', 'contentstore.views.edit_static', name='edit_static'), url(r'^edit_tabs/(?P<org>[^/]+)/(?P<course>[^/]+)/course/(?P<coursename>[^/]+)$', 'contentstore.views.edit_tabs', name='edit_tabs'), @@ -56,7 +56,7 @@ urlpatterns = ('', # this is a generic method to return the data/metadata associated with a xmodule url(r'^module_info/(?P<module_location>.*)$', 'contentstore.views.module_info', name='module_info'), - + # temporary landing page for a course url(r'^edge/(?P<org>[^/]+)/(?P<course>[^/]+)/course/(?P<coursename>[^/]+)$', 'contentstore.views.landing', name='landing'), diff --git a/common/djangoapps/cache_toolbox/core.py b/common/djangoapps/cache_toolbox/core.py index 3bd184bc2c6..a9c7002aa6b 100644 --- a/common/djangoapps/cache_toolbox/core.py +++ b/common/djangoapps/cache_toolbox/core.py @@ -14,6 +14,7 @@ from django.db import DEFAULT_DB_ALIAS from . import app_settings from xmodule.contentstore.content import StaticContent + def get_instance(model, instance_or_pk, timeout=None, using=None): """ Returns the ``model`` instance with a primary key of ``instance_or_pk``. @@ -108,11 +109,14 @@ def instance_key(model, instance_or_pk): getattr(instance_or_pk, 'pk', instance_or_pk), ) + def set_cached_content(content): cache.set(str(content.location), content) + def get_cached_content(location): return cache.get(str(location)) + def del_cached_content(location): cache.delete(str(location)) diff --git a/common/djangoapps/contentserver/middleware.py b/common/djangoapps/contentserver/middleware.py index 1d139bcaa0f..c5e887801e3 100644 --- a/common/djangoapps/contentserver/middleware.py +++ b/common/djangoapps/contentserver/middleware.py @@ -12,7 +12,7 @@ from xmodule.exceptions import NotFoundError class StaticContentServer(object): def process_request(self, request): # look to see if the request is prefixed with 'c4x' tag - if request.path.startswith('/' + XASSET_LOCATION_TAG +'/'): + if request.path.startswith('/' + XASSET_LOCATION_TAG + '/'): loc = StaticContent.get_location_from_path(request.path) # first look in our cache so we don't have to round-trip to the DB content = get_cached_content(loc) diff --git a/common/djangoapps/course_groups/cohorts.py b/common/djangoapps/course_groups/cohorts.py index f84e18b2145..155f82e0c71 100644 --- a/common/djangoapps/course_groups/cohorts.py +++ b/common/djangoapps/course_groups/cohorts.py @@ -13,6 +13,7 @@ from .models import CourseUserGroup log = logging.getLogger(__name__) + def is_course_cohorted(course_id): """ Given a course id, return a boolean for whether or not the course is @@ -115,6 +116,7 @@ def get_course_cohorts(course_id): ### Helpers for cohort management views + def get_cohort_by_name(course_id, name): """ Return the CourseUserGroup object for the given cohort. Raises DoesNotExist @@ -124,6 +126,7 @@ def get_cohort_by_name(course_id, name): group_type=CourseUserGroup.COHORT, name=name) + def get_cohort_by_id(course_id, cohort_id): """ Return the CourseUserGroup object for the given cohort. Raises DoesNotExist @@ -133,6 +136,7 @@ def get_cohort_by_id(course_id, cohort_id): group_type=CourseUserGroup.COHORT, id=cohort_id) + def add_cohort(course_id, name): """ Add a cohort to a course. Raises ValueError if a cohort of the same name already @@ -148,12 +152,14 @@ def add_cohort(course_id, name): group_type=CourseUserGroup.COHORT, name=name) + class CohortConflict(Exception): """ Raised when user to be added is already in another cohort in same course. """ pass + def add_user_to_cohort(cohort, username_or_email): """ Look up the given user, and if successful, add them to the specified cohort. @@ -211,4 +217,3 @@ def delete_empty_cohort(course_id, name): name, course_id)) cohort.delete() - diff --git a/common/djangoapps/course_groups/models.py b/common/djangoapps/course_groups/models.py index 957d230d923..8bab17493be 100644 --- a/common/djangoapps/course_groups/models.py +++ b/common/djangoapps/course_groups/models.py @@ -5,6 +5,7 @@ from django.db import models log = logging.getLogger(__name__) + class CourseUserGroup(models.Model): """ This model represents groups of users in a course. Groups may have different types, @@ -30,5 +31,3 @@ class CourseUserGroup(models.Model): COHORT = 'cohort' GROUP_TYPE_CHOICES = ((COHORT, 'Cohort'),) group_type = models.CharField(max_length=20, choices=GROUP_TYPE_CHOICES) - - diff --git a/common/djangoapps/course_groups/tests/tests.py b/common/djangoapps/course_groups/tests/tests.py index 86f0be07915..0fbf863fee1 100644 --- a/common/djangoapps/course_groups/tests/tests.py +++ b/common/djangoapps/course_groups/tests/tests.py @@ -12,7 +12,8 @@ from xmodule.modulestore.django import modulestore, _MODULESTORES # NOTE: running this with the lms.envs.test config works without # manually overriding the modulestore. However, running with -# cms.envs.test doesn't. +# cms.envs.test doesn't. + def xml_store_config(data_dir): return { @@ -28,6 +29,7 @@ def xml_store_config(data_dir): TEST_DATA_DIR = settings.COMMON_TEST_DATA_ROOT TEST_DATA_XML_MODULESTORE = xml_store_config(TEST_DATA_DIR) + @override_settings(MODULESTORE=TEST_DATA_XML_MODULESTORE) class TestCohorts(django.test.TestCase): @@ -184,6 +186,3 @@ class TestCohorts(django.test.TestCase): self.assertTrue( is_commentable_cohorted(course.id, to_id("Feedback")), "Feedback was listed as cohorted. Should be.") - - - diff --git a/common/djangoapps/course_groups/views.py b/common/djangoapps/course_groups/views.py index d591c443565..6d5ac43fb06 100644 --- a/common/djangoapps/course_groups/views.py +++ b/common/djangoapps/course_groups/views.py @@ -22,6 +22,7 @@ import track.views log = logging.getLogger(__name__) + def json_http_response(data): """ Return an HttpResponse with the data json-serialized and the right content @@ -29,6 +30,7 @@ def json_http_response(data): """ return HttpResponse(json.dumps(data), content_type="application/json") + def split_by_comma_and_whitespace(s): """ Split a string both by commas and whitespice. Returns a list. @@ -177,6 +179,7 @@ def add_users_to_cohort(request, course_id, cohort_id): 'conflict': conflict, 'unknown': unknown}) + @ensure_csrf_cookie @require_POST def remove_user_from_cohort(request, course_id, cohort_id): diff --git a/common/djangoapps/external_auth/admin.py b/common/djangoapps/external_auth/admin.py index e93325bcb21..1ee18dadc19 100644 --- a/common/djangoapps/external_auth/admin.py +++ b/common/djangoapps/external_auth/admin.py @@ -5,8 +5,9 @@ django admin pages for courseware model from external_auth.models import * from django.contrib import admin + class ExternalAuthMapAdmin(admin.ModelAdmin): - search_fields = ['external_id','user__username'] + search_fields = ['external_id', 'user__username'] date_hierarchy = 'dtcreated' admin.site.register(ExternalAuthMap, ExternalAuthMapAdmin) diff --git a/common/djangoapps/external_auth/models.py b/common/djangoapps/external_auth/models.py index e43b306bbb1..6c2f38d8b31 100644 --- a/common/djangoapps/external_auth/models.py +++ b/common/djangoapps/external_auth/models.py @@ -12,20 +12,20 @@ file and check it in at the same time as your model changes. To do that, from django.db import models from django.contrib.auth.models import User + class ExternalAuthMap(models.Model): class Meta: unique_together = (('external_id', 'external_domain'), ) external_id = models.CharField(max_length=255, db_index=True) external_domain = models.CharField(max_length=255, db_index=True) - external_credentials = models.TextField(blank=True) # JSON dictionary + external_credentials = models.TextField(blank=True) # JSON dictionary external_email = models.CharField(max_length=255, db_index=True) - external_name = models.CharField(blank=True,max_length=255, db_index=True) + external_name = models.CharField(blank=True, max_length=255, db_index=True) user = models.OneToOneField(User, unique=True, db_index=True, null=True) - internal_password = models.CharField(blank=True, max_length=31) # randomly generated - dtcreated = models.DateTimeField('creation date',auto_now_add=True) - dtsignup = models.DateTimeField('signup date',null=True) # set after signup - + internal_password = models.CharField(blank=True, max_length=31) # randomly generated + dtcreated = models.DateTimeField('creation date', auto_now_add=True) + dtsignup = models.DateTimeField('signup date', null=True) # set after signup + def __unicode__(self): s = "[%s] = (%s / %s)" % (self.external_id, self.external_name, self.external_email) return s - diff --git a/common/djangoapps/external_auth/tests/test_openid_provider.py b/common/djangoapps/external_auth/tests/test_openid_provider.py index 9c522f88b44..570dfbf9ee8 100644 --- a/common/djangoapps/external_auth/tests/test_openid_provider.py +++ b/common/djangoapps/external_auth/tests/test_openid_provider.py @@ -13,9 +13,10 @@ from django.test import TestCase, LiveServerTestCase from django.core.urlresolvers import reverse from django.test.client import RequestFactory + class MyFetcher(HTTPFetcher): """A fetcher that uses server-internal calls for performing HTTP - requests. + requests. """ def __init__(self, client): @@ -42,7 +43,7 @@ class MyFetcher(HTTPFetcher): if headers and 'Accept' in headers: data['CONTENT_TYPE'] = headers['Accept'] response = self.client.get(url, data) - + # Translate the test client response to the fetcher's HTTP response abstraction content = response.content final_url = url @@ -60,6 +61,7 @@ class MyFetcher(HTTPFetcher): status=status, ) + class OpenIdProviderTest(TestCase): # def setUp(self): @@ -78,7 +80,7 @@ class OpenIdProviderTest(TestCase): provider_url = reverse('openid-provider-xrds') factory = RequestFactory() request = factory.request() - abs_provider_url = request.build_absolute_uri(location = provider_url) + abs_provider_url = request.build_absolute_uri(location=provider_url) # In order for this absolute URL to work (i.e. to get xrds, then authentication) # in the test environment, we either need a live server that works with the default @@ -86,10 +88,10 @@ class OpenIdProviderTest(TestCase): # Here we do the latter: fetcher = MyFetcher(self.client) openid.fetchers.setDefaultFetcher(fetcher, wrap_exceptions=False) - + # now we can begin the login process by invoking a local openid client, # with a pointer to the (also-local) openid provider: - with self.settings(OPENID_SSO_SERVER_URL = abs_provider_url): + with self.settings(OPENID_SSO_SERVER_URL=abs_provider_url): url = reverse('openid-login') resp = self.client.post(url) code = 200 @@ -107,7 +109,7 @@ class OpenIdProviderTest(TestCase): provider_url = reverse('openid-provider-login') factory = RequestFactory() request = factory.request() - abs_provider_url = request.build_absolute_uri(location = provider_url) + abs_provider_url = request.build_absolute_uri(location=provider_url) # In order for this absolute URL to work (i.e. to get xrds, then authentication) # in the test environment, we either need a live server that works with the default @@ -115,10 +117,10 @@ class OpenIdProviderTest(TestCase): # Here we do the latter: fetcher = MyFetcher(self.client) openid.fetchers.setDefaultFetcher(fetcher, wrap_exceptions=False) - + # now we can begin the login process by invoking a local openid client, # with a pointer to the (also-local) openid provider: - with self.settings(OPENID_SSO_SERVER_URL = abs_provider_url): + with self.settings(OPENID_SSO_SERVER_URL=abs_provider_url): url = reverse('openid-login') resp = self.client.post(url) code = 200 @@ -143,43 +145,43 @@ class OpenIdProviderTest(TestCase): self.assertContains(resp, '<input type="submit" value="Continue" />', html=True) # this should work on the server: self.assertContains(resp, '<input name="openid.realm" type="hidden" value="http://testserver/" />', html=True) - + # not included here are elements that will vary from run to run: # <input name="openid.return_to" type="hidden" value="http://testserver/openid/complete/?janrain_nonce=2013-01-23T06%3A20%3A17ZaN7j6H" /> # <input name="openid.assoc_handle" type="hidden" value="{HMAC-SHA1}{50ff8120}{rh87+Q==}" /> - - + + def testOpenIdSetup(self): if not settings.MITX_FEATURES.get('AUTH_USE_OPENID_PROVIDER'): return url = reverse('openid-provider-login') post_args = { - "openid.mode" : "checkid_setup", - "openid.return_to" : "http://testserver/openid/complete/?janrain_nonce=2013-01-23T06%3A20%3A17ZaN7j6H", - "openid.assoc_handle" : "{HMAC-SHA1}{50ff8120}{rh87+Q==}", - "openid.claimed_id" : "http://specs.openid.net/auth/2.0/identifier_select", - "openid.ns" : "http://specs.openid.net/auth/2.0", - "openid.realm" : "http://testserver/", - "openid.identity" : "http://specs.openid.net/auth/2.0/identifier_select", - "openid.ns.ax" : "http://openid.net/srv/ax/1.0", - "openid.ax.mode" : "fetch_request", - "openid.ax.required" : "email,fullname,old_email,firstname,old_nickname,lastname,old_fullname,nickname", - "openid.ax.type.fullname" : "http://axschema.org/namePerson", - "openid.ax.type.lastname" : "http://axschema.org/namePerson/last", - "openid.ax.type.firstname" : "http://axschema.org/namePerson/first", - "openid.ax.type.nickname" : "http://axschema.org/namePerson/friendly", - "openid.ax.type.email" : "http://axschema.org/contact/email", - "openid.ax.type.old_email" : "http://schema.openid.net/contact/email", - "openid.ax.type.old_nickname" : "http://schema.openid.net/namePerson/friendly", - "openid.ax.type.old_fullname" : "http://schema.openid.net/namePerson", + "openid.mode": "checkid_setup", + "openid.return_to": "http://testserver/openid/complete/?janrain_nonce=2013-01-23T06%3A20%3A17ZaN7j6H", + "openid.assoc_handle": "{HMAC-SHA1}{50ff8120}{rh87+Q==}", + "openid.claimed_id": "http://specs.openid.net/auth/2.0/identifier_select", + "openid.ns": "http://specs.openid.net/auth/2.0", + "openid.realm": "http://testserver/", + "openid.identity": "http://specs.openid.net/auth/2.0/identifier_select", + "openid.ns.ax": "http://openid.net/srv/ax/1.0", + "openid.ax.mode": "fetch_request", + "openid.ax.required": "email,fullname,old_email,firstname,old_nickname,lastname,old_fullname,nickname", + "openid.ax.type.fullname": "http://axschema.org/namePerson", + "openid.ax.type.lastname": "http://axschema.org/namePerson/last", + "openid.ax.type.firstname": "http://axschema.org/namePerson/first", + "openid.ax.type.nickname": "http://axschema.org/namePerson/friendly", + "openid.ax.type.email": "http://axschema.org/contact/email", + "openid.ax.type.old_email": "http://schema.openid.net/contact/email", + "openid.ax.type.old_nickname": "http://schema.openid.net/namePerson/friendly", + "openid.ax.type.old_fullname": "http://schema.openid.net/namePerson", } resp = self.client.post(url, post_args) code = 200 self.assertEqual(resp.status_code, code, "got code {0} for url '{1}'. Expected code {2}" .format(resp.status_code, url, code)) - - + + # In order for this absolute URL to work (i.e. to get xrds, then authentication) # in the test environment, we either need a live server that works with the default # fetcher (i.e. urlopen2), or a test server that is reached through a custom fetcher. @@ -196,11 +198,11 @@ class OpenIdProviderLiveServerTest(LiveServerTestCase): provider_url = reverse('openid-provider-xrds') factory = RequestFactory() request = factory.request() - abs_provider_url = request.build_absolute_uri(location = provider_url) + abs_provider_url = request.build_absolute_uri(location=provider_url) # now we can begin the login process by invoking a local openid client, # with a pointer to the (also-local) openid provider: - with self.settings(OPENID_SSO_SERVER_URL = abs_provider_url): + with self.settings(OPENID_SSO_SERVER_URL=abs_provider_url): url = reverse('openid-login') resp = self.client.post(url) code = 200 diff --git a/common/djangoapps/external_auth/views.py b/common/djangoapps/external_auth/views.py index d557b33e9cd..effae923b38 100644 --- a/common/djangoapps/external_auth/views.py +++ b/common/djangoapps/external_auth/views.py @@ -215,7 +215,7 @@ def ssl_dn_extract_info(dn): else: return None return (user, email, fullname) - + def ssl_get_cert_from_request(request): """ @@ -460,7 +460,7 @@ def provider_login(request): openid_request.answer(False), {}) # checkid_setup, so display login page - # (by falling through to the provider_login at the + # (by falling through to the provider_login at the # bottom of this method). elif openid_request.mode == 'checkid_setup': if openid_request.idSelect(): @@ -482,7 +482,7 @@ def provider_login(request): # handle login redirection: these are also sent to this view function, # but are distinguished by lacking the openid mode. We also know that - # they are posts, because they come from the popup + # they are posts, because they come from the popup elif request.method == 'POST' and 'openid_setup' in request.session: # get OpenID request from session openid_setup = request.session['openid_setup'] @@ -495,7 +495,7 @@ def provider_login(request): return default_render_failure(request, "Invalid OpenID trust root") # check if user with given email exists - # Failure is redirected to this method (by using the original URL), + # Failure is redirected to this method (by using the original URL), # which will bring up the login dialog. email = request.POST.get('email', None) try: @@ -542,17 +542,17 @@ def provider_login(request): # missing fields is up to the Consumer. The proper change # should only return the username, however this will likely # break the CS50 client. Temporarily we will be returning - # username filling in for fullname in addition to username + # username filling in for fullname in addition to username # as sreg nickname. - - # Note too that this is hardcoded, and not really responding to + + # Note too that this is hardcoded, and not really responding to # the extensions that were registered in the first place. results = { 'nickname': user.username, 'email': user.email, 'fullname': user.username } - + # the request succeeded: return provider_respond(server, openid_request, response, results) diff --git a/common/djangoapps/mitxmako/makoloader.py b/common/djangoapps/mitxmako/makoloader.py index 1379027e075..29184299b68 100644 --- a/common/djangoapps/mitxmako/makoloader.py +++ b/common/djangoapps/mitxmako/makoloader.py @@ -12,34 +12,35 @@ import mitxmako.middleware log = logging.getLogger(__name__) + class MakoLoader(object): """ This is a Django loader object which will load the template as a Mako template if the first line is "## mako". It is based off BaseLoader in django.template.loader. """ - + is_usable = False def __init__(self, base_loader): # base_loader is an instance of a BaseLoader subclass self.base_loader = base_loader - + module_directory = getattr(settings, 'MAKO_MODULE_DIR', None) - + if module_directory is None: log.warning("For more caching of mako templates, set the MAKO_MODULE_DIR in settings!") module_directory = tempfile.mkdtemp() - + self.module_directory = module_directory - - + + def __call__(self, template_name, template_dirs=None): return self.load_template(template_name, template_dirs) def load_template(self, template_name, template_dirs=None): source, file_path = self.load_template_source(template_name, template_dirs) - + if source.startswith("## mako\n"): # This is a mako template template = Template(filename=file_path, module_directory=self.module_directory, uri=template_name) @@ -56,23 +57,24 @@ class MakoLoader(object): # This allows for correct identification (later) of the actual template that does # not exist. return source, file_path - + def load_template_source(self, template_name, template_dirs=None): # Just having this makes the template load as an instance, instead of a class. return self.base_loader.load_template_source(template_name, template_dirs) def reset(self): self.base_loader.reset() - + class MakoFilesystemLoader(MakoLoader): is_usable = True - + def __init__(self): MakoLoader.__init__(self, FilesystemLoader()) - + + class MakoAppDirectoriesLoader(MakoLoader): is_usable = True - + def __init__(self): MakoLoader.__init__(self, AppDirectoriesLoader()) diff --git a/common/djangoapps/mitxmako/template.py b/common/djangoapps/mitxmako/template.py index 947dc8c1a4c..6ef8058c7c0 100644 --- a/common/djangoapps/mitxmako/template.py +++ b/common/djangoapps/mitxmako/template.py @@ -20,13 +20,15 @@ from mitxmako import middleware django_variables = ['lookup', 'output_encoding', 'encoding_errors'] # TODO: We should make this a Django Template subclass that simply has the MakoTemplate inside of it? (Intead of inheriting from MakoTemplate) + + class Template(MakoTemplate): """ This bridges the gap between a Mako template and a djano template. It can be rendered like it is a django template because the arguments are transformed in a way that MakoTemplate can understand. """ - + def __init__(self, *args, **kwargs): """Overrides base __init__ to provide django variable overrides""" if not kwargs.get('no_django', False): @@ -34,8 +36,8 @@ class Template(MakoTemplate): overrides['lookup'] = overrides['lookup']['main'] kwargs.update(overrides) super(Template, self).__init__(*args, **kwargs) - - + + def render(self, context_instance): """ This takes a render call with a context (from Django) and translates @@ -43,7 +45,7 @@ class Template(MakoTemplate): """ # collapse context_instance to a single dictionary for mako context_dictionary = {} - + # In various testing contexts, there might not be a current request context. if middleware.requestcontext is not None: for d in middleware.requestcontext: @@ -53,5 +55,5 @@ class Template(MakoTemplate): context_dictionary['settings'] = settings context_dictionary['MITX_ROOT_URL'] = settings.MITX_ROOT_URL context_dictionary['django_context'] = context_instance - + return super(Template, self).render_unicode(**context_dictionary) diff --git a/common/djangoapps/mitxmako/templatetag_helpers.py b/common/djangoapps/mitxmako/templatetag_helpers.py index e254625d3d8..cd871a0fc52 100644 --- a/common/djangoapps/mitxmako/templatetag_helpers.py +++ b/common/djangoapps/mitxmako/templatetag_helpers.py @@ -2,14 +2,15 @@ from django.template import loader from django.template.base import Template, Context from django.template.loader import get_template, select_template + def django_template_include(file_name, mako_context): """ This can be used within a mako template to include a django template in the way that a django-style {% include %} does. Pass it context which can be the mako context ('context') or a dictionary. """ - - dictionary = dict( mako_context ) + + dictionary = dict(mako_context) return loader.render_to_string(file_name, dictionary=dictionary) @@ -18,7 +19,7 @@ def render_inclusion(func, file_name, takes_context, django_context, *args, **kw This allows a mako template to call a template tag function (written for django templates) that is an "inclusion tag". These functions are decorated with @register.inclusion_tag. - + -func: This is the function that is registered as an inclusion tag. You must import it directly using a python import statement. -file_name: This is the filename of the template, passed into the @@ -29,10 +30,10 @@ def render_inclusion(func, file_name, takes_context, django_context, *args, **kw a copy of the django context is available as 'django_context'. -*args and **kwargs are the arguments to func. """ - + if takes_context: args = [django_context] + list(args) - + _dict = func(*args, **kwargs) if isinstance(file_name, Template): t = file_name @@ -40,14 +41,12 @@ def render_inclusion(func, file_name, takes_context, django_context, *args, **kw t = select_template(file_name) else: t = get_template(file_name) - + nodelist = t.nodelist - + new_context = Context(_dict) csrf_token = django_context.get('csrf_token', None) if csrf_token is not None: new_context['csrf_token'] = csrf_token - - return nodelist.render(new_context) - + return nodelist.render(new_context) diff --git a/common/djangoapps/static_replace/__init__.py b/common/djangoapps/static_replace/__init__.py index cfef798bdf4..6bd81255805 100644 --- a/common/djangoapps/static_replace/__init__.py +++ b/common/djangoapps/static_replace/__init__.py @@ -21,6 +21,7 @@ def _url_replace_regex(prefix): (?P=quote) # the first matching closing quote """.format(prefix=prefix) + def try_staticfiles_lookup(path): """ Try to lookup a path in staticfiles_storage. If it fails, return diff --git a/common/djangoapps/static_replace/test/test_static_replace.py b/common/djangoapps/static_replace/test/test_static_replace.py index e08c66c59f4..50c0fbd246c 100644 --- a/common/djangoapps/static_replace/test/test_static_replace.py +++ b/common/djangoapps/static_replace/test/test_static_replace.py @@ -53,6 +53,7 @@ def test_mongo_filestore(mock_modulestore, mock_static_content): mock_static_content.convert_legacy_static_url.assert_called_once_with('file.png', NAMESPACE) + @patch('static_replace.settings') @patch('static_replace.modulestore') @patch('static_replace.staticfiles_storage') diff --git a/common/djangoapps/status/__init__.py b/common/djangoapps/status/__init__.py index 8b137891791..e69de29bb2d 100644 --- a/common/djangoapps/status/__init__.py +++ b/common/djangoapps/status/__init__.py @@ -1 +0,0 @@ - diff --git a/common/djangoapps/status/status.py b/common/djangoapps/status/status.py index c06a70f5a10..deacd9c6319 100644 --- a/common/djangoapps/status/status.py +++ b/common/djangoapps/status/status.py @@ -10,6 +10,7 @@ import sys log = logging.getLogger(__name__) + def get_site_status_msg(course_id): """ Look for a file settings.STATUS_MESSAGE_PATH. If found, read it, diff --git a/common/djangoapps/student/management/commands/6002exportusers.py b/common/djangoapps/student/management/commands/6002exportusers.py index fcf565fb83b..31d8092d3f8 100644 --- a/common/djangoapps/student/management/commands/6002exportusers.py +++ b/common/djangoapps/student/management/commands/6002exportusers.py @@ -57,7 +57,7 @@ from student.userprofile. ''' d[key] = item return d - extracted = [{'up':extract_dict(up_keys, t[0]), 'u':extract_dict(user_keys, t[1])} for t in user_tuples] + extracted = [{'up': extract_dict(up_keys, t[0]), 'u':extract_dict(user_keys, t[1])} for t in user_tuples] fp = open('transfer_users.txt', 'w') json.dump(extracted, fp) fp.close() diff --git a/common/djangoapps/student/management/commands/add_to_group.py b/common/djangoapps/student/management/commands/add_to_group.py index 209d25da85b..bdbb4027f7d 100644 --- a/common/djangoapps/student/management/commands/add_to_group.py +++ b/common/djangoapps/student/management/commands/add_to_group.py @@ -3,6 +3,7 @@ from optparse import make_option from django.core.management.base import BaseCommand, CommandError from django.contrib.auth.models import User, Group + class Command(BaseCommand): option_list = BaseCommand.option_list + ( make_option('--list', diff --git a/common/djangoapps/student/management/commands/create_random_users.py b/common/djangoapps/student/management/commands/create_random_users.py index c6cf452a431..70374d02f2f 100644 --- a/common/djangoapps/student/management/commands/create_random_users.py +++ b/common/djangoapps/student/management/commands/create_random_users.py @@ -8,6 +8,7 @@ from student.models import UserProfile, CourseEnrollment from student.views import _do_create_account, get_random_post_override + def create(n, course_id): """Create n users, enrolling them in course_id if it's not None""" for i in range(n): @@ -15,6 +16,7 @@ def create(n, course_id): if course_id is not None: CourseEnrollment.objects.create(user=user, course_id=course_id) + class Command(BaseCommand): help = """Create N new users, with random parameters. diff --git a/common/djangoapps/student/management/commands/pearson_dump.py b/common/djangoapps/student/management/commands/pearson_dump.py index 228508efb15..2aade8cf5f2 100644 --- a/common/djangoapps/student/management/commands/pearson_dump.py +++ b/common/djangoapps/student/management/commands/pearson_dump.py @@ -36,7 +36,7 @@ class Command(BaseCommand): outputfile = datetime.utcnow().strftime("pearson-dump-%Y%m%d-%H%M%S.json") else: outputfile = args[0] - + # construct the query object to dump: registrations = TestCenterRegistration.objects.all() if 'course_id' in options and options['course_id']: @@ -44,24 +44,24 @@ class Command(BaseCommand): if 'exam_series_code' in options and options['exam_series_code']: registrations = registrations.filter(exam_series_code=options['exam_series_code']) - # collect output: + # collect output: output = [] for registration in registrations: if 'accommodation_pending' in options and options['accommodation_pending'] and not registration.accommodation_is_pending: continue - record = {'username' : registration.testcenter_user.user.username, - 'email' : registration.testcenter_user.email, - 'first_name' : registration.testcenter_user.first_name, - 'last_name' : registration.testcenter_user.last_name, - 'client_candidate_id' : registration.client_candidate_id, - 'client_authorization_id' : registration.client_authorization_id, - 'course_id' : registration.course_id, - 'exam_series_code' : registration.exam_series_code, - 'accommodation_request' : registration.accommodation_request, - 'accommodation_code' : registration.accommodation_code, - 'registration_status' : registration.registration_status(), - 'demographics_status' : registration.demographics_status(), - 'accommodation_status' : registration.accommodation_status(), + record = {'username': registration.testcenter_user.user.username, + 'email': registration.testcenter_user.email, + 'first_name': registration.testcenter_user.first_name, + 'last_name': registration.testcenter_user.last_name, + 'client_candidate_id': registration.client_candidate_id, + 'client_authorization_id': registration.client_authorization_id, + 'course_id': registration.course_id, + 'exam_series_code': registration.exam_series_code, + 'accommodation_request': registration.accommodation_request, + 'accommodation_code': registration.accommodation_code, + 'registration_status': registration.registration_status(), + 'demographics_status': registration.demographics_status(), + 'accommodation_status': registration.accommodation_status(), } if len(registration.upload_error_message) > 0: record['registration_error'] = registration.upload_error_message @@ -71,8 +71,7 @@ class Command(BaseCommand): record['needs_uploading'] = True output.append(record) - + # dump output: with open(outputfile, 'w') as outfile: dump(output, outfile, indent=2) - diff --git a/common/djangoapps/student/management/commands/pearson_export_cdd.py b/common/djangoapps/student/management/commands/pearson_export_cdd.py index 463eec6b707..bad98b9d25c 100644 --- a/common/djangoapps/student/management/commands/pearson_export_cdd.py +++ b/common/djangoapps/student/management/commands/pearson_export_cdd.py @@ -39,7 +39,7 @@ class Command(BaseCommand): ("LastUpdate", "user_updated_at"), # in UTC, so same as what we store ]) - # define defaults, even thought 'store_true' shouldn't need them. + # define defaults, even thought 'store_true' shouldn't need them. # (call_command will set None as default value for all options that don't have one, # so one cannot rely on presence/absence of flags in that world.) option_list = BaseCommand.option_list + ( @@ -56,7 +56,7 @@ class Command(BaseCommand): ) def handle(self, **options): - # update time should use UTC in order to be comparable to the user_updated_at + # update time should use UTC in order to be comparable to the user_updated_at # field uploaded_at = datetime.utcnow() @@ -100,7 +100,7 @@ class Command(BaseCommand): extrasaction='ignore') writer.writeheader() for tcu in TestCenterUser.objects.order_by('id'): - if tcu.needs_uploading: # or dump_all + if tcu.needs_uploading: # or dump_all record = dict((csv_field, ensure_encoding(getattr(tcu, model_field))) for csv_field, model_field in Command.CSV_TO_MODEL_FIELDS.items()) diff --git a/common/djangoapps/student/management/commands/pearson_import_conf_zip.py b/common/djangoapps/student/management/commands/pearson_import_conf_zip.py index d94c3ba8630..d0b2938df03 100644 --- a/common/djangoapps/student/management/commands/pearson_import_conf_zip.py +++ b/common/djangoapps/student/management/commands/pearson_import_conf_zip.py @@ -116,4 +116,3 @@ class Command(BaseCommand): tcuser.save() except TestCenterUser.DoesNotExist: Command.datadog_error(" Failed to find record for client_candidate_id {}".format(client_candidate_id), vcdcfile.name) - diff --git a/common/djangoapps/student/management/commands/pearson_make_tc_registration.py b/common/djangoapps/student/management/commands/pearson_make_tc_registration.py index b59241240d9..b10cf143a08 100644 --- a/common/djangoapps/student/management/commands/pearson_make_tc_registration.py +++ b/common/djangoapps/student/management/commands/pearson_make_tc_registration.py @@ -9,6 +9,7 @@ from student.views import course_from_id from xmodule.course_module import CourseDescriptor from xmodule.modulestore.exceptions import ItemNotFoundError + class Command(BaseCommand): option_list = BaseCommand.option_list + ( # registration info: @@ -16,23 +17,23 @@ class Command(BaseCommand): '--accommodation_request', action='store', dest='accommodation_request', - ), + ), make_option( '--accommodation_code', action='store', dest='accommodation_code', - ), + ), make_option( '--client_authorization_id', action='store', dest='client_authorization_id', - ), - # exam info: + ), + # exam info: make_option( '--exam_series_code', action='store', dest='exam_series_code', - ), + ), make_option( '--eligibility_appointment_date_first', action='store', @@ -51,32 +52,32 @@ class Command(BaseCommand): action='store', dest='authorization_id', help='ID we receive from Pearson for a particular authorization' - ), + ), make_option( '--upload_status', action='store', dest='upload_status', help='status value assigned by Pearson' - ), + ), make_option( '--upload_error_message', action='store', dest='upload_error_message', help='error message provided by Pearson on a failure.' - ), + ), # control values: make_option( '--ignore_registration_dates', action='store_true', dest='ignore_registration_dates', help='find exam info for course based on exam_series_code, even if the exam is not active.' - ), + ), make_option( '--create_dummy_exam', action='store_true', dest='create_dummy_exam', help='create dummy exam info for course, even if course exists' - ), + ), ) args = "<student_username course_id>" help = "Create or modify a TestCenterRegistration entry for a given Student" @@ -103,7 +104,7 @@ class Command(BaseCommand): testcenter_user = TestCenterUser.objects.get(user=student) except TestCenterUser.DoesNotExist: raise CommandError("User \"{}\" does not have an existing demographics record".format(username)) - + # get an "exam" object. Check to see if a course_id was specified, and use information from that: exam = None create_dummy_exam = 'create_dummy_exam' in our_options and our_options['create_dummy_exam'] @@ -115,14 +116,14 @@ class Command(BaseCommand): exam = examlist[0] if len(examlist) > 0 else None else: exam = course.current_test_center_exam - except ItemNotFoundError: + except ItemNotFoundError: pass else: - # otherwise use explicit values (so we don't have to define a course): + # otherwise use explicit values (so we don't have to define a course): exam_name = "Dummy Placeholder Name" - exam_info = { 'Exam_Series_Code': our_options['exam_series_code'], - 'First_Eligible_Appointment_Date' : our_options['eligibility_appointment_date_first'], - 'Last_Eligible_Appointment_Date' : our_options['eligibility_appointment_date_last'], + exam_info = {'Exam_Series_Code': our_options['exam_series_code'], + 'First_Eligible_Appointment_Date': our_options['eligibility_appointment_date_first'], + 'Last_Eligible_Appointment_Date': our_options['eligibility_appointment_date_last'], } exam = CourseDescriptor.TestCenterExam(course_id, exam_name, exam_info) # update option values for date_first and date_last to use YYYY-MM-DD format @@ -134,15 +135,15 @@ class Command(BaseCommand): raise CommandError("Exam for course_id {} does not exist".format(course_id)) exam_code = exam.exam_series_code - - UPDATE_FIELDS = ( 'accommodation_request', + + UPDATE_FIELDS = ('accommodation_request', 'accommodation_code', 'client_authorization_id', 'exam_series_code', 'eligibility_appointment_date_first', 'eligibility_appointment_date_last', ) - + # create and save the registration: needs_updating = False registrations = get_testcenter_registration(student, course_id, exam_code) @@ -152,29 +153,29 @@ class Command(BaseCommand): if fieldname in our_options and registration.__getattribute__(fieldname) != our_options[fieldname]: needs_updating = True; else: - accommodation_request = our_options.get('accommodation_request','') + accommodation_request = our_options.get('accommodation_request', '') registration = TestCenterRegistration.create(testcenter_user, exam, accommodation_request) needs_updating = True - + if needs_updating: # first update the record with the new values, if any: for fieldname in UPDATE_FIELDS: - if fieldname in our_options and fieldname not in TestCenterRegistrationForm.Meta.fields: + if fieldname in our_options and fieldname not in TestCenterRegistrationForm.Meta.fields: registration.__setattr__(fieldname, our_options[fieldname]) - - # the registration form normally populates the data dict with + + # the registration form normally populates the data dict with # the accommodation request (if any). But here we want to # specify only those values that might change, so update the dict with existing # values. form_options = dict(our_options) for propname in TestCenterRegistrationForm.Meta.fields: - if propname not in form_options: + if propname not in form_options: form_options[propname] = registration.__getattribute__(propname) form = TestCenterRegistrationForm(instance=registration, data=form_options) if form.is_valid(): form.update_and_save() - print "Updated registration information for user's registration: username \"{}\" course \"{}\", examcode \"{}\"".format(student.username, course_id, exam_code) + print "Updated registration information for user's registration: username \"{}\" course \"{}\", examcode \"{}\"".format(student.username, course_id, exam_code) else: if (len(form.errors) > 0): print "Field Form errors encountered:" @@ -185,24 +186,22 @@ class Command(BaseCommand): print "Non-field Form errors encountered:" for nonfielderror in form.non_field_errors: print "Non-field Form Error: %s" % nonfielderror - + else: print "No changes necessary to make to existing user's registration." - + # override internal values: change_internal = False if 'exam_series_code' in our_options: exam_code = our_options['exam_series_code'] registration = get_testcenter_registration(student, course_id, exam_code)[0] - for internal_field in [ 'upload_error_message', 'upload_status', 'authorization_id']: + for internal_field in ['upload_error_message', 'upload_status', 'authorization_id']: if internal_field in our_options: registration.__setattr__(internal_field, our_options[internal_field]) change_internal = True - + if change_internal: print "Updated confirmation information in existing user's registration." registration.save() else: print "No changes necessary to make to confirmation information in existing user's registration." - - diff --git a/common/djangoapps/student/management/commands/pearson_make_tc_user.py b/common/djangoapps/student/management/commands/pearson_make_tc_user.py index 87e0b4dadd6..10ef0bd067d 100644 --- a/common/djangoapps/student/management/commands/pearson_make_tc_user.py +++ b/common/djangoapps/student/management/commands/pearson_make_tc_user.py @@ -5,60 +5,61 @@ from django.core.management.base import BaseCommand, CommandError from student.models import TestCenterUser, TestCenterUserForm + class Command(BaseCommand): option_list = BaseCommand.option_list + ( - # demographics: + # demographics: make_option( '--first_name', action='store', dest='first_name', - ), + ), make_option( '--middle_name', action='store', dest='middle_name', - ), + ), make_option( '--last_name', action='store', dest='last_name', - ), + ), make_option( '--suffix', action='store', dest='suffix', - ), + ), make_option( '--salutation', action='store', dest='salutation', - ), + ), make_option( '--address_1', action='store', dest='address_1', - ), + ), make_option( '--address_2', action='store', dest='address_2', - ), + ), make_option( '--address_3', action='store', dest='address_3', - ), + ), make_option( '--city', action='store', dest='city', - ), + ), make_option( '--state', action='store', dest='state', help='Two letter code (e.g. MA)' - ), + ), make_option( '--postal_code', action='store', @@ -75,12 +76,12 @@ class Command(BaseCommand): action='store', dest='phone', help='Pretty free-form (parens, spaces, dashes), but no country code' - ), + ), make_option( '--extension', action='store', dest='extension', - ), + ), make_option( '--phone_country_code', action='store', @@ -92,7 +93,7 @@ class Command(BaseCommand): action='store', dest='fax', help='Pretty free-form (parens, spaces, dashes), but no country code' - ), + ), make_option( '--fax_country_code', action='store', @@ -103,26 +104,26 @@ class Command(BaseCommand): '--company_name', action='store', dest='company_name', - ), + ), # internal values: make_option( '--client_candidate_id', action='store', dest='client_candidate_id', help='ID we assign a user to identify them to Pearson' - ), + ), make_option( '--upload_status', action='store', dest='upload_status', help='status value assigned by Pearson' - ), + ), make_option( '--upload_error_message', action='store', dest='upload_error_message', help='error message provided by Pearson on a failure.' - ), + ), ) args = "<student_username>" help = "Create or modify a TestCenterUser entry for a given Student" @@ -142,20 +143,20 @@ class Command(BaseCommand): student = User.objects.get(username=username) try: testcenter_user = TestCenterUser.objects.get(user=student) - needs_updating = testcenter_user.needs_update(our_options) + needs_updating = testcenter_user.needs_update(our_options) except TestCenterUser.DoesNotExist: # do additional initialization here: testcenter_user = TestCenterUser.create(student) needs_updating = True - + if needs_updating: - # the registration form normally populates the data dict with + # the registration form normally populates the data dict with # all values from the testcenter_user. But here we only want to # specify those values that change, so update the dict with existing # values. form_options = dict(our_options) for propname in TestCenterUser.user_provided_fields(): - if propname not in form_options: + if propname not in form_options: form_options[propname] = testcenter_user.__getattribute__(propname) form = TestCenterUserForm(instance=testcenter_user, data=form_options) if form.is_valid(): @@ -170,21 +171,20 @@ class Command(BaseCommand): errorlist.append("Non-field Form errors encountered:") for nonfielderror in form.non_field_errors: errorlist.append("Non-field Form Error: {}".format(nonfielderror)) - raise CommandError("\n".join(errorlist)) + raise CommandError("\n".join(errorlist)) else: print "No changes necessary to make to existing user's demographics." - + # override internal values: change_internal = False testcenter_user = TestCenterUser.objects.get(user=student) - for internal_field in [ 'upload_error_message', 'upload_status', 'client_candidate_id']: + for internal_field in ['upload_error_message', 'upload_status', 'client_candidate_id']: if internal_field in our_options: testcenter_user.__setattr__(internal_field, our_options[internal_field]) change_internal = True - + if change_internal: testcenter_user.save() print "Updated confirmation information in existing user's demographics." else: print "No changes necessary to make to confirmation information in existing user's demographics." - diff --git a/common/djangoapps/student/management/commands/pearson_transfer.py b/common/djangoapps/student/management/commands/pearson_transfer.py index 6811e1833df..5eded6484a8 100644 --- a/common/djangoapps/student/management/commands/pearson_transfer.py +++ b/common/djangoapps/student/management/commands/pearson_transfer.py @@ -46,10 +46,10 @@ class Command(BaseCommand): if not hasattr(settings, value): raise CommandError('No entry in the AWS settings' '(env/auth.json) for {0}'.format(value)) - + # check additional required settings for import and export: if options['mode'] in ('export', 'both'): - for value in ['LOCAL_EXPORT','SFTP_EXPORT']: + for value in ['LOCAL_EXPORT', 'SFTP_EXPORT']: if value not in settings.PEARSON: raise CommandError('No entry in the PEARSON settings' '(env/auth.json) for {0}'.format(value)) @@ -57,9 +57,9 @@ class Command(BaseCommand): source_dir = settings.PEARSON['LOCAL_EXPORT'] if not os.path.isdir(source_dir): os.makedirs(source_dir) - + if options['mode'] in ('import', 'both'): - for value in ['LOCAL_IMPORT','SFTP_IMPORT']: + for value in ['LOCAL_IMPORT', 'SFTP_IMPORT']: if value not in settings.PEARSON: raise CommandError('No entry in the PEARSON settings' '(env/auth.json) for {0}'.format(value)) @@ -76,7 +76,7 @@ class Command(BaseCommand): t.connect(username=settings.PEARSON['SFTP_USERNAME'], password=settings.PEARSON['SFTP_PASSWORD']) sftp = paramiko.SFTPClient.from_transport(t) - + if mode == 'export': try: sftp.chdir(files_to) @@ -92,7 +92,7 @@ class Command(BaseCommand): except IOError: raise CommandError('SFTP source path does not exist: {}'.format(files_from)) for filename in sftp.listdir('.'): - # skip subdirectories + # skip subdirectories if not S_ISDIR(sftp.stat(filename).st_mode): sftp.get(filename, files_to + '/' + filename) # delete files from sftp server once they are successfully pulled off: @@ -112,7 +112,7 @@ class Command(BaseCommand): try: for filename in os.listdir(files_from): source_file = os.path.join(files_from, filename) - # use mode as name of directory into which to write files + # use mode as name of directory into which to write files dest_file = os.path.join(mode, filename) upload_file_to_s3(bucket, source_file, dest_file) if deleteAfterCopy: @@ -135,17 +135,17 @@ class Command(BaseCommand): k.set_contents_from_filename(source_file) def export_pearson(): - options = { 'dest-from-settings' : True } + options = {'dest-from-settings': True} call_command('pearson_export_cdd', **options) call_command('pearson_export_ead', **options) mode = 'export' - sftp(settings.PEARSON['LOCAL_EXPORT'], settings.PEARSON['SFTP_EXPORT'], mode, deleteAfterCopy = False) + sftp(settings.PEARSON['LOCAL_EXPORT'], settings.PEARSON['SFTP_EXPORT'], mode, deleteAfterCopy=False) s3(settings.PEARSON['LOCAL_EXPORT'], settings.PEARSON['S3_BUCKET'], mode, deleteAfterCopy=True) def import_pearson(): mode = 'import' try: - sftp(settings.PEARSON['SFTP_IMPORT'], settings.PEARSON['LOCAL_IMPORT'], mode, deleteAfterCopy = True) + sftp(settings.PEARSON['SFTP_IMPORT'], settings.PEARSON['LOCAL_IMPORT'], mode, deleteAfterCopy=True) s3(settings.PEARSON['LOCAL_IMPORT'], settings.PEARSON['S3_BUCKET'], mode, deleteAfterCopy=False) except Exception as e: dog_http_api.event('Pearson Import failure', str(e)) diff --git a/common/djangoapps/student/management/commands/tests/test_pearson.py b/common/djangoapps/student/management/commands/tests/test_pearson.py index 2f4878a09d3..12969405def 100644 --- a/common/djangoapps/student/management/commands/tests/test_pearson.py +++ b/common/djangoapps/student/management/commands/tests/test_pearson.py @@ -17,30 +17,31 @@ from student.models import User, TestCenterRegistration, TestCenterUser, get_tes log = logging.getLogger(__name__) + def create_tc_user(username): user = User.objects.create_user(username, '{}@edx.org'.format(username), 'fakepass') options = { - 'first_name' : 'TestFirst', - 'last_name' : 'TestLast', - 'address_1' : 'Test Address', - 'city' : 'TestCity', - 'state' : 'Alberta', - 'postal_code' : 'A0B 1C2', - 'country' : 'CAN', - 'phone' : '252-1866', - 'phone_country_code' : '1', + 'first_name': 'TestFirst', + 'last_name': 'TestLast', + 'address_1': 'Test Address', + 'city': 'TestCity', + 'state': 'Alberta', + 'postal_code': 'A0B 1C2', + 'country': 'CAN', + 'phone': '252-1866', + 'phone_country_code': '1', } call_command('pearson_make_tc_user', username, **options) return TestCenterUser.objects.get(user=user) - - -def create_tc_registration(username, course_id = 'org1/course1/term1', exam_code = 'exam1', accommodation_code = None): - - options = { 'exam_series_code' : exam_code, - 'eligibility_appointment_date_first' : '2013-01-01T00:00', - 'eligibility_appointment_date_last' : '2013-12-31T23:59', - 'accommodation_code' : accommodation_code, - 'create_dummy_exam' : True, + + +def create_tc_registration(username, course_id='org1/course1/term1', exam_code='exam1', accommodation_code=None): + + options = {'exam_series_code': exam_code, + 'eligibility_appointment_date_first': '2013-01-01T00:00', + 'eligibility_appointment_date_last': '2013-12-31T23:59', + 'accommodation_code': accommodation_code, + 'create_dummy_exam': True, } call_command('pearson_make_tc_registration', username, course_id, **options) @@ -48,21 +49,23 @@ def create_tc_registration(username, course_id = 'org1/course1/term1', exam_code registrations = get_testcenter_registration(user, course_id, exam_code) return registrations[0] + def create_multiple_registrations(prefix='test'): username1 = '{}_multiple1'.format(prefix) create_tc_user(username1) create_tc_registration(username1) - create_tc_registration(username1, course_id = 'org1/course2/term1') - create_tc_registration(username1, exam_code = 'exam2') + create_tc_registration(username1, course_id='org1/course2/term1') + create_tc_registration(username1, exam_code='exam2') username2 = '{}_multiple2'.format(prefix) create_tc_user(username2) create_tc_registration(username2) username3 = '{}_multiple3'.format(prefix) create_tc_user(username3) - create_tc_registration(username3, course_id = 'org1/course2/term1') + create_tc_registration(username3, course_id='org1/course2/term1') username4 = '{}_multiple4'.format(prefix) create_tc_user(username4) - create_tc_registration(username4, exam_code = 'exam2') + create_tc_registration(username4, exam_code='exam2') + def get_command_error_text(*args, **options): stderr_string = None @@ -75,21 +78,22 @@ def get_command_error_text(*args, **options): # But these are actually translated into nice messages, # and sys.exit(1) is then called. For testing, we # want to catch what sys.exit throws, and get the - # relevant text either from stdout or stderr. + # relevant text either from stdout or stderr. if (why1.message > 0): stderr_string = sys.stderr.getvalue() else: raise why1 except Exception, why: raise why - + finally: sys.stderr = old_stderr - + if stderr_string is None: - raise Exception("Expected call to {} to fail, but it succeeded!".format(args[0])) + raise Exception("Expected call to {} to fail, but it succeeded!".format(args[0])) return stderr_string - + + def get_error_string_for_management_call(*args, **options): stdout_string = None old_stdout = sys.stdout @@ -103,7 +107,7 @@ def get_error_string_for_management_call(*args, **options): # But these are actually translated into nice messages, # and sys.exit(1) is then called. For testing, we # want to catch what sys.exit throws, and get the - # relevant text either from stdout or stderr. + # relevant text either from stdout or stderr. if (why1.message == 1): stdout_string = sys.stdout.getvalue() stderr_string = sys.stderr.getvalue() @@ -111,15 +115,15 @@ def get_error_string_for_management_call(*args, **options): raise why1 except Exception, why: raise why - + finally: sys.stdout = old_stdout sys.stderr = old_stderr - + if stdout_string is None: - raise Exception("Expected call to {} to fail, but it succeeded!".format(args[0])) + raise Exception("Expected call to {} to fail, but it succeeded!".format(args[0])) return stdout_string, stderr_string - + def get_file_info(dirpath): filelist = os.listdir(dirpath) @@ -132,43 +136,45 @@ def get_file_info(dirpath): numlines = len(filecontents) return filepath, numlines else: - raise Exception("Expected to find a single file in {}, but found {}".format(dirpath,filelist)) - + raise Exception("Expected to find a single file in {}, but found {}".format(dirpath, filelist)) + + class PearsonTestCase(TestCase): ''' Base class for tests running Pearson-related commands ''' import_dir = mkdtemp(prefix="import") export_dir = mkdtemp(prefix="export") - + def assertErrorContains(self, error_message, expected): self.assertTrue(error_message.find(expected) >= 0, 'error message "{}" did not contain "{}"'.format(error_message, expected)) - + def tearDown(self): def delete_temp_dir(dirname): if os.path.exists(dirname): for filename in os.listdir(dirname): os.remove(os.path.join(dirname, filename)) os.rmdir(dirname) - + # clean up after any test data was dumped to temp directory delete_temp_dir(self.import_dir) delete_temp_dir(self.export_dir) - + # and clean up the database: # TestCenterUser.objects.all().delete() # TestCenterRegistration.objects.all().delete() + class PearsonCommandTestCase(PearsonTestCase): def test_missing_demographic_fields(self): - # We won't bother to test all details of form validation here. + # We won't bother to test all details of form validation here. # It is enough to show that it works here, but deal with test cases for the form # validation in the student tests, not these management tests. username = 'baduser' User.objects.create_user(username, '{}@edx.org'.format(username), 'fakepass') options = {} - error_string = get_command_error_text('pearson_make_tc_user', username, **options) + error_string = get_command_error_text('pearson_make_tc_user', username, **options) self.assertTrue(error_string.find('Field Form errors encountered:') >= 0) self.assertTrue(error_string.find('Field Form Error: city') >= 0) self.assertTrue(error_string.find('Field Form Error: first_name') >= 0) @@ -178,11 +184,11 @@ class PearsonCommandTestCase(PearsonTestCase): self.assertTrue(error_string.find('Field Form Error: phone') >= 0) self.assertTrue(error_string.find('Field Form Error: address_1') >= 0) self.assertErrorContains(error_string, 'Field Form Error: address_1') - + def test_create_good_testcenter_user(self): testcenter_user = create_tc_user("test_good_user") self.assertIsNotNone(testcenter_user) - + def test_create_good_testcenter_registration(self): username = 'test_good_registration' create_tc_user(username) @@ -192,21 +198,21 @@ class PearsonCommandTestCase(PearsonTestCase): def test_cdd_missing_option(self): error_string = get_command_error_text('pearson_export_cdd', **{}) self.assertErrorContains(error_string, 'Error: --destination or --dest-from-settings must be used') - + def test_ead_missing_option(self): error_string = get_command_error_text('pearson_export_ead', **{}) self.assertErrorContains(error_string, 'Error: --destination or --dest-from-settings must be used') def test_export_single_cdd(self): # before we generate any tc_users, we expect there to be nothing to output: - options = { 'dest-from-settings' : True } - with self.settings(PEARSON={ 'LOCAL_EXPORT' : self.export_dir }): + options = {'dest-from-settings': True} + with self.settings(PEARSON={'LOCAL_EXPORT': self.export_dir}): call_command('pearson_export_cdd', **options) (filepath, numlines) = get_file_info(self.export_dir) self.assertEquals(numlines, 1, "Expect cdd file to have no non-header lines") os.remove(filepath) - # generating a tc_user should result in a line in the output + # generating a tc_user should result in a line in the output username = 'test_single_cdd' create_tc_user(username) call_command('pearson_export_cdd', **options) @@ -221,23 +227,23 @@ class PearsonCommandTestCase(PearsonTestCase): os.remove(filepath) # if we modify the record, then it should be output again: - user_options = { 'first_name' : 'NewTestFirst', } + user_options = {'first_name': 'NewTestFirst', } call_command('pearson_make_tc_user', username, **user_options) call_command('pearson_export_cdd', **options) (filepath, numlines) = get_file_info(self.export_dir) self.assertEquals(numlines, 2, "Expect cdd file to have one non-header line") os.remove(filepath) - + def test_export_single_ead(self): # before we generate any registrations, we expect there to be nothing to output: - options = { 'dest-from-settings' : True } - with self.settings(PEARSON={ 'LOCAL_EXPORT' : self.export_dir }): + options = {'dest-from-settings': True} + with self.settings(PEARSON={'LOCAL_EXPORT': self.export_dir}): call_command('pearson_export_ead', **options) (filepath, numlines) = get_file_info(self.export_dir) self.assertEquals(numlines, 1, "Expect ead file to have no non-header lines") os.remove(filepath) - # generating a registration should result in a line in the output + # generating a registration should result in a line in the output username = 'test_single_ead' create_tc_user(username) create_tc_registration(username) @@ -251,7 +257,7 @@ class PearsonCommandTestCase(PearsonTestCase): (filepath, numlines) = get_file_info(self.export_dir) self.assertEquals(numlines, 1, "Expect ead file to have no non-header lines") os.remove(filepath) - + # if we modify the record, then it should be output again: create_tc_registration(username, accommodation_code='EQPMNT') call_command('pearson_export_ead', **options) @@ -261,8 +267,8 @@ class PearsonCommandTestCase(PearsonTestCase): def test_export_multiple(self): create_multiple_registrations("export") - with self.settings(PEARSON={ 'LOCAL_EXPORT' : self.export_dir }): - options = { 'dest-from-settings' : True } + with self.settings(PEARSON={'LOCAL_EXPORT': self.export_dir}): + options = {'dest-from-settings': True} call_command('pearson_export_cdd', **options) (filepath, numlines) = get_file_info(self.export_dir) self.assertEquals(numlines, 5, "Expect cdd file to have four non-header lines: total was {}".format(numlines)) @@ -294,6 +300,7 @@ S3_BUCKET = 'edx-pearson-archive' AWS_ACCESS_KEY_ID = 'put yours here' AWS_SECRET_ACCESS_KEY = 'put yours here' + class PearsonTransferTestCase(PearsonTestCase): ''' Class for tests running Pearson transfers @@ -302,14 +309,14 @@ class PearsonTransferTestCase(PearsonTestCase): def test_transfer_config(self): with self.settings(DATADOG_API='FAKE_KEY'): # TODO: why is this failing with the wrong error message?! - stderrmsg = get_command_error_text('pearson_transfer', **{'mode' : 'garbage'}) + stderrmsg = get_command_error_text('pearson_transfer', **{'mode': 'garbage'}) self.assertErrorContains(stderrmsg, 'Error: No PEARSON entries') with self.settings(DATADOG_API='FAKE_KEY'): stderrmsg = get_command_error_text('pearson_transfer') self.assertErrorContains(stderrmsg, 'Error: No PEARSON entries') with self.settings(DATADOG_API='FAKE_KEY', - PEARSON={'LOCAL_EXPORT' : self.export_dir, - 'LOCAL_IMPORT' : self.import_dir }): + PEARSON={'LOCAL_EXPORT': self.export_dir, + 'LOCAL_IMPORT': self.import_dir}): stderrmsg = get_command_error_text('pearson_transfer') self.assertErrorContains(stderrmsg, 'Error: No entry in the PEARSON settings') @@ -317,16 +324,16 @@ class PearsonTransferTestCase(PearsonTestCase): raise SkipTest() create_multiple_registrations('export_missing_dest') with self.settings(DATADOG_API='FAKE_KEY', - PEARSON={'LOCAL_EXPORT' : self.export_dir, - 'SFTP_EXPORT' : 'this/does/not/exist', - 'SFTP_HOSTNAME' : SFTP_HOSTNAME, - 'SFTP_USERNAME' : SFTP_USERNAME, - 'SFTP_PASSWORD' : SFTP_PASSWORD, - 'S3_BUCKET' : S3_BUCKET, + PEARSON={'LOCAL_EXPORT': self.export_dir, + 'SFTP_EXPORT': 'this/does/not/exist', + 'SFTP_HOSTNAME': SFTP_HOSTNAME, + 'SFTP_USERNAME': SFTP_USERNAME, + 'SFTP_PASSWORD': SFTP_PASSWORD, + 'S3_BUCKET': S3_BUCKET, }, - AWS_ACCESS_KEY_ID = AWS_ACCESS_KEY_ID, - AWS_SECRET_ACCESS_KEY = AWS_SECRET_ACCESS_KEY): - options = { 'mode' : 'export'} + AWS_ACCESS_KEY_ID=AWS_ACCESS_KEY_ID, + AWS_SECRET_ACCESS_KEY=AWS_SECRET_ACCESS_KEY): + options = {'mode': 'export'} stderrmsg = get_command_error_text('pearson_transfer', **options) self.assertErrorContains(stderrmsg, 'Error: SFTP destination path does not exist') @@ -334,16 +341,16 @@ class PearsonTransferTestCase(PearsonTestCase): raise SkipTest() create_multiple_registrations("transfer_export") with self.settings(DATADOG_API='FAKE_KEY', - PEARSON={'LOCAL_EXPORT' : self.export_dir, - 'SFTP_EXPORT' : 'results/topvue', - 'SFTP_HOSTNAME' : SFTP_HOSTNAME, - 'SFTP_USERNAME' : SFTP_USERNAME, - 'SFTP_PASSWORD' : SFTP_PASSWORD, - 'S3_BUCKET' : S3_BUCKET, + PEARSON={'LOCAL_EXPORT': self.export_dir, + 'SFTP_EXPORT': 'results/topvue', + 'SFTP_HOSTNAME': SFTP_HOSTNAME, + 'SFTP_USERNAME': SFTP_USERNAME, + 'SFTP_PASSWORD': SFTP_PASSWORD, + 'S3_BUCKET': S3_BUCKET, }, - AWS_ACCESS_KEY_ID = AWS_ACCESS_KEY_ID, - AWS_SECRET_ACCESS_KEY = AWS_SECRET_ACCESS_KEY): - options = { 'mode' : 'export'} + AWS_ACCESS_KEY_ID=AWS_ACCESS_KEY_ID, + AWS_SECRET_ACCESS_KEY=AWS_SECRET_ACCESS_KEY): + options = {'mode': 'export'} # call_command('pearson_transfer', **options) # # confirm that the export directory is still empty: # self.assertEqual(len(os.listdir(self.export_dir)), 0, "expected export directory to be empty") @@ -352,16 +359,16 @@ class PearsonTransferTestCase(PearsonTestCase): raise SkipTest() create_multiple_registrations('import_missing_src') with self.settings(DATADOG_API='FAKE_KEY', - PEARSON={'LOCAL_IMPORT' : self.import_dir, - 'SFTP_IMPORT' : 'this/does/not/exist', - 'SFTP_HOSTNAME' : SFTP_HOSTNAME, - 'SFTP_USERNAME' : SFTP_USERNAME, - 'SFTP_PASSWORD' : SFTP_PASSWORD, - 'S3_BUCKET' : S3_BUCKET, + PEARSON={'LOCAL_IMPORT': self.import_dir, + 'SFTP_IMPORT': 'this/does/not/exist', + 'SFTP_HOSTNAME': SFTP_HOSTNAME, + 'SFTP_USERNAME': SFTP_USERNAME, + 'SFTP_PASSWORD': SFTP_PASSWORD, + 'S3_BUCKET': S3_BUCKET, }, - AWS_ACCESS_KEY_ID = AWS_ACCESS_KEY_ID, - AWS_SECRET_ACCESS_KEY = AWS_SECRET_ACCESS_KEY): - options = { 'mode' : 'import'} + AWS_ACCESS_KEY_ID=AWS_ACCESS_KEY_ID, + AWS_SECRET_ACCESS_KEY=AWS_SECRET_ACCESS_KEY): + options = {'mode': 'import'} stderrmsg = get_command_error_text('pearson_transfer', **options) self.assertErrorContains(stderrmsg, 'Error: SFTP source path does not exist') @@ -369,15 +376,15 @@ class PearsonTransferTestCase(PearsonTestCase): raise SkipTest() create_multiple_registrations('import_missing_src') with self.settings(DATADOG_API='FAKE_KEY', - PEARSON={'LOCAL_IMPORT' : self.import_dir, - 'SFTP_IMPORT' : 'results', - 'SFTP_HOSTNAME' : SFTP_HOSTNAME, - 'SFTP_USERNAME' : SFTP_USERNAME, - 'SFTP_PASSWORD' : SFTP_PASSWORD, - 'S3_BUCKET' : S3_BUCKET, + PEARSON={'LOCAL_IMPORT': self.import_dir, + 'SFTP_IMPORT': 'results', + 'SFTP_HOSTNAME': SFTP_HOSTNAME, + 'SFTP_USERNAME': SFTP_USERNAME, + 'SFTP_PASSWORD': SFTP_PASSWORD, + 'S3_BUCKET': S3_BUCKET, }, - AWS_ACCESS_KEY_ID = AWS_ACCESS_KEY_ID, - AWS_SECRET_ACCESS_KEY = AWS_SECRET_ACCESS_KEY): - options = { 'mode' : 'import'} + AWS_ACCESS_KEY_ID=AWS_ACCESS_KEY_ID, + AWS_SECRET_ACCESS_KEY=AWS_SECRET_ACCESS_KEY): + options = {'mode': 'import'} call_command('pearson_transfer', **options) self.assertEqual(len(os.listdir(self.import_dir)), 0, "expected import directory to be empty") diff --git a/common/djangoapps/student/migrations/0020_add_test_center_user.py b/common/djangoapps/student/migrations/0020_add_test_center_user.py index e308e2d7e01..6c0bf5c4ee5 100644 --- a/common/djangoapps/student/migrations/0020_add_test_center_user.py +++ b/common/djangoapps/student/migrations/0020_add_test_center_user.py @@ -185,4 +185,4 @@ class Migration(SchemaMigration): } } - complete_apps = ['student'] \ No newline at end of file + complete_apps = ['student'] diff --git a/common/djangoapps/student/migrations/0021_remove_askbot.py b/common/djangoapps/student/migrations/0021_remove_askbot.py index 83ad6791f22..8f76e5078c1 100644 --- a/common/djangoapps/student/migrations/0021_remove_askbot.py +++ b/common/djangoapps/student/migrations/0021_remove_askbot.py @@ -36,7 +36,7 @@ class Migration(SchemaMigration): for column in ASKBOT_AUTH_USER_COLUMNS: db.delete_column('auth_user', column) except Exception as ex: - print "Couldn't remove askbot because of {0} -- it was probably never here to begin with.".format(ex) + print "Couldn't remove askbot because of {0} -- it was probably never here to begin with.".format(ex) def backwards(self, orm): raise RuntimeError("Cannot reverse this migration: there's no going back to Askbot.") diff --git a/common/djangoapps/student/migrations/0022_auto__add_courseenrollmentallowed__add_unique_courseenrollmentallowed_.py b/common/djangoapps/student/migrations/0022_auto__add_courseenrollmentallowed__add_unique_courseenrollmentallowed_.py index f7e2571685e..769ad6737de 100644 --- a/common/djangoapps/student/migrations/0022_auto__add_courseenrollmentallowed__add_unique_courseenrollmentallowed_.py +++ b/common/djangoapps/student/migrations/0022_auto__add_courseenrollmentallowed__add_unique_courseenrollmentallowed_.py @@ -152,4 +152,4 @@ class Migration(SchemaMigration): } } - complete_apps = ['student'] \ No newline at end of file + complete_apps = ['student'] diff --git a/common/djangoapps/student/migrations/0023_add_test_center_registration.py b/common/djangoapps/student/migrations/0023_add_test_center_registration.py index c5af38dd378..4c7de6dcd93 100644 --- a/common/djangoapps/student/migrations/0023_add_test_center_registration.py +++ b/common/djangoapps/student/migrations/0023_add_test_center_registration.py @@ -238,4 +238,4 @@ class Migration(SchemaMigration): } } - complete_apps = ['student'] \ No newline at end of file + complete_apps = ['student'] diff --git a/common/djangoapps/student/migrations/0024_add_allow_certificate.py b/common/djangoapps/student/migrations/0024_add_allow_certificate.py index fb3a97cd4ba..56eccf8d709 100644 --- a/common/djangoapps/student/migrations/0024_add_allow_certificate.py +++ b/common/djangoapps/student/migrations/0024_add_allow_certificate.py @@ -169,4 +169,4 @@ class Migration(SchemaMigration): } } - complete_apps = ['student'] \ No newline at end of file + complete_apps = ['student'] diff --git a/common/djangoapps/student/models.py b/common/djangoapps/student/models.py index 71d2177bd46..54bdd77297c 100644 --- a/common/djangoapps/student/models.py +++ b/common/djangoapps/student/models.py @@ -107,6 +107,7 @@ class UserProfile(models.Model): TEST_CENTER_STATUS_ACCEPTED = "Accepted" TEST_CENTER_STATUS_ERROR = "Error" + class TestCenterUser(models.Model): """This is our representation of the User for in-person testing, and specifically for Pearson at this point. A few things to note: @@ -190,7 +191,7 @@ class TestCenterUser(models.Model): @staticmethod def user_provided_fields(): - return [ 'first_name', 'middle_name', 'last_name', 'suffix', 'salutation', + return ['first_name', 'middle_name', 'last_name', 'suffix', 'salutation', 'address_1', 'address_2', 'address_3', 'city', 'state', 'postal_code', 'country', 'phone', 'extension', 'phone_country_code', 'fax', 'fax_country_code', 'company_name'] @@ -208,7 +209,7 @@ class TestCenterUser(models.Model): @staticmethod def _generate_edx_id(prefix): NUM_DIGITS = 12 - return u"{}{:012}".format(prefix, randint(1, 10**NUM_DIGITS-1)) + return u"{}{:012}".format(prefix, randint(1, 10 ** NUM_DIGITS - 1)) @staticmethod def _generate_candidate_id(): @@ -237,10 +238,11 @@ class TestCenterUser(models.Model): def is_pending(self): return not self.is_accepted and not self.is_rejected + class TestCenterUserForm(ModelForm): class Meta: model = TestCenterUser - fields = ( 'first_name', 'middle_name', 'last_name', 'suffix', 'salutation', + fields = ('first_name', 'middle_name', 'last_name', 'suffix', 'salutation', 'address_1', 'address_2', 'address_3', 'city', 'state', 'postal_code', 'country', 'phone', 'extension', 'phone_country_code', 'fax', 'fax_country_code', 'company_name') @@ -313,7 +315,8 @@ ACCOMMODATION_CODES = ( ('SRSGNR', 'Separate Room and Sign Language Interpreter'), ) -ACCOMMODATION_CODE_DICT = { code : name for (code, name) in ACCOMMODATION_CODES } +ACCOMMODATION_CODE_DICT = {code: name for (code, name) in ACCOMMODATION_CODES} + class TestCenterRegistration(models.Model): """ @@ -389,10 +392,10 @@ class TestCenterRegistration(models.Model): elif self.uploaded_at is None: return 'Add' elif self.registration_is_rejected: - # Assume that if the registration was rejected before, - # it is more likely this is the (first) correction + # Assume that if the registration was rejected before, + # it is more likely this is the (first) correction # than a second correction in flight before the first was - # processed. + # processed. return 'Add' else: # TODO: decide what to send when we have uploaded an initial version, @@ -417,7 +420,7 @@ class TestCenterRegistration(models.Model): @classmethod def create(cls, testcenter_user, exam, accommodation_request): - registration = cls(testcenter_user = testcenter_user) + registration = cls(testcenter_user=testcenter_user) registration.course_id = exam.course_id registration.accommodation_request = accommodation_request.strip() registration.exam_series_code = exam.exam_series_code @@ -501,7 +504,7 @@ class TestCenterRegistration(models.Model): return self.accommodation_code.split('*') def get_accommodation_names(self): - return [ ACCOMMODATION_CODE_DICT.get(code, "Unknown code " + code) for code in self.get_accommodation_codes() ] + return [ACCOMMODATION_CODE_DICT.get(code, "Unknown code " + code) for code in self.get_accommodation_codes()] @property def registration_signup_url(self): @@ -512,7 +515,7 @@ class TestCenterRegistration(models.Model): return "Accepted" elif self.demographics_is_rejected: return "Rejected" - else: + else: return "Pending" def accommodation_status(self): @@ -522,7 +525,7 @@ class TestCenterRegistration(models.Model): return "Accepted" elif self.accommodation_is_rejected: return "Rejected" - else: + else: return "Pending" def registration_status(self): @@ -532,12 +535,12 @@ class TestCenterRegistration(models.Model): return "Rejected" else: return "Pending" - + class TestCenterRegistrationForm(ModelForm): class Meta: model = TestCenterRegistration - fields = ( 'accommodation_request', 'accommodation_code' ) + fields = ('accommodation_request', 'accommodation_code') def clean_accommodation_request(self): code = self.cleaned_data['accommodation_request'] @@ -576,6 +579,7 @@ def get_testcenter_registration(user, course_id, exam_series_code): # Correct this (https://nose.readthedocs.org/en/latest/finding_tests.html) get_testcenter_registration.__test__ = False + def unique_id_for_user(user): """ Return a unique id for a user, suitable for inserting into @@ -666,6 +670,7 @@ class CourseEnrollmentAllowed(models.Model): #### Helper methods for use from python manage.py shell and other classes. + def get_user_by_username_or_email(username_or_email): """ Return a User object, looking up by email if username_or_email contains a @@ -767,4 +772,3 @@ def update_user_information(sender, instance, created, **kwargs): log = logging.getLogger("mitx.discussion") log.error(unicode(e)) log.error("update user info to discussion failed for user with id: " + str(instance.id)) - diff --git a/common/djangoapps/student/tests.py b/common/djangoapps/student/tests.py index 8ce407bcd1b..6a2d75e3d8e 100644 --- a/common/djangoapps/student/tests.py +++ b/common/djangoapps/student/tests.py @@ -17,6 +17,7 @@ COURSE_2 = 'edx/full/6.002_Spring_2012' log = logging.getLogger(__name__) + class CourseEndingTest(TestCase): """Test things related to course endings: certificates, surveys, etc""" @@ -40,7 +41,7 @@ class CourseEndingTest(TestCase): {'status': 'processing', 'show_disabled_download_button': False, 'show_download_url': False, - 'show_survey_button': False,}) + 'show_survey_button': False, }) cert_status = {'status': 'unavailable'} self.assertEqual(_cert_info(user, course, cert_status), diff --git a/common/djangoapps/student/views.py b/common/djangoapps/student/views.py index b583599e974..bf279e7b08b 100644 --- a/common/djangoapps/student/views.py +++ b/common/djangoapps/student/views.py @@ -50,6 +50,7 @@ from statsd import statsd log = logging.getLogger("mitx.student") Article = namedtuple('Article', 'title url author image deck publication publish_date') + def csrf_token(context): ''' A csrf token that can be included in a form. ''' @@ -73,8 +74,8 @@ def index(request, extra_context={}, user=None): ''' # The course selection work is done in courseware.courses. - domain = settings.MITX_FEATURES.get('FORCE_UNIVERSITY_DOMAIN') # normally False - if domain==False: # do explicit check, because domain=None is valid + domain = settings.MITX_FEATURES.get('FORCE_UNIVERSITY_DOMAIN') # normally False + if domain == False: # do explicit check, because domain=None is valid domain = request.META.get('HTTP_HOST') courses = get_courses(None, domain=domain) @@ -97,6 +98,7 @@ import re day_pattern = re.compile('\s\d+,\s') multimonth_pattern = re.compile('\s?\-\s?\S+\s') + def get_date_for_press(publish_date): import datetime # strip off extra months, and just use the first: @@ -107,6 +109,7 @@ def get_date_for_press(publish_date): date = datetime.datetime.strptime(date, "%B, %Y") return date + def press(request): json_articles = cache.get("student_press_json_articles") if json_articles == None: @@ -148,6 +151,7 @@ def cert_info(user, course): return _cert_info(user, course, certificate_status_for_student(user, course.id)) + def _cert_info(user, course, cert_status): """ Implements the logic for cert_info -- split out for testing. @@ -175,7 +179,7 @@ def _cert_info(user, course, cert_status): d = {'status': status, 'show_download_url': status == 'ready', - 'show_disabled_download_button': status == 'generating',} + 'show_disabled_download_button': status == 'generating', } if (status in ('generating', 'ready', 'notpassing', 'restricted') and course.end_of_course_survey_url is not None): @@ -204,6 +208,7 @@ def _cert_info(user, course, cert_status): return d + @login_required @ensure_csrf_cookie def dashboard(request): @@ -237,9 +242,9 @@ def dashboard(request): show_courseware_links_for = frozenset(course.id for course in courses if has_access(request.user, course, 'load')) - cert_statuses = { course.id: cert_info(request.user, course) for course in courses} + cert_statuses = {course.id: cert_info(request.user, course) for course in courses} - exam_registrations = { course.id: exam_registration_info(request.user, course) for course in courses} + exam_registrations = {course.id: exam_registration_info(request.user, course) for course in courses} # Get the 3 most recent news top_news = _get_news(top=3) @@ -248,7 +253,7 @@ def dashboard(request): 'message': message, 'staff_access': staff_access, 'errored_courses': errored_courses, - 'show_courseware_links_for' : show_courseware_links_for, + 'show_courseware_links_for': show_courseware_links_for, 'cert_statuses': cert_statuses, 'news': top_news, 'exam_registrations': exam_registrations, @@ -312,7 +317,7 @@ def change_enrollment(request): 'error': 'enrollment in {} not allowed at this time' .format(course.display_name)} - org, course_num, run=course_id.split("/") + org, course_num, run = course_id.split("/") statsd.increment("common.student.enrollment", tags=["org:{0}".format(org), "course:{0}".format(course_num), @@ -326,7 +331,7 @@ def change_enrollment(request): enrollment = CourseEnrollment.objects.get(user=user, course_id=course_id) enrollment.delete() - org, course_num, run=course_id.split("/") + org, course_num, run = course_id.split("/") statsd.increment("common.student.unenrollment", tags=["org:{0}".format(org), "course:{0}".format(course_num), @@ -345,7 +350,7 @@ def change_enrollment(request): def accounts_login(request, error=""): - return render_to_response('accounts_login.html', { 'error': error }) + return render_to_response('accounts_login.html', {'error': error}) @@ -424,6 +429,7 @@ def change_setting(request): return HttpResponse(json.dumps({'success': True, 'location': up.location, })) + def _do_create_account(post_vars): """ Given cleaned post variables, create the User and UserProfile objects, as well as the @@ -551,7 +557,7 @@ def create_account(request, post_override=None): # Ok, looks like everything is legit. Create the account. ret = _do_create_account(post_vars) - if isinstance(ret,HttpResponse): # if there was an error then return that + if isinstance(ret, HttpResponse): # if there was an error then return that return ret (user, profile, registration) = ret @@ -591,7 +597,7 @@ def create_account(request, post_override=None): eamap.user = login_user eamap.dtsignup = datetime.datetime.now() eamap.save() - log.debug('Updated ExternalAuthMap for %s to be %s' % (post_vars['username'],eamap)) + log.debug('Updated ExternalAuthMap for %s to be %s' % (post_vars['username'], eamap)) if settings.MITX_FEATURES.get('BYPASS_ACTIVATION_EMAIL_FOR_EXTAUTH'): log.debug('bypassing activation email') @@ -603,6 +609,7 @@ def create_account(request, post_override=None): js = {'success': True} return HttpResponse(json.dumps(js), mimetype="application/json") + def exam_registration_info(user, course): """ Returns a Registration object if the user is currently registered for a current exam of the course. Returns None if the user is not registered, or if there is no @@ -620,6 +627,7 @@ def exam_registration_info(user, course): registration = None return registration + @login_required @ensure_csrf_cookie def begin_exam_registration(request, course_id): @@ -663,6 +671,7 @@ def begin_exam_registration(request, course_id): return render_to_response('test_center_register.html', context) + @ensure_csrf_cookie def create_exam_registration(request, post_override=None): ''' @@ -725,7 +734,7 @@ def create_exam_registration(request, post_override=None): # this registration screen. else: - accommodation_request = post_vars.get('accommodation_request','') + accommodation_request = post_vars.get('accommodation_request', '') registration = TestCenterRegistration.create(testcenter_user, exam, accommodation_request) needs_saving = True log.info("User {0} enrolled in course {1} creating new exam registration".format(user.username, course_id)) @@ -834,16 +843,17 @@ def password_reset(request): form = PasswordResetForm(request.POST) if form.is_valid(): - form.save(use_https = request.is_secure(), - from_email = settings.DEFAULT_FROM_EMAIL, - request = request, - domain_override = request.get_host()) - return HttpResponse(json.dumps({'success':True, + form.save(use_https=request.is_secure(), + from_email=settings.DEFAULT_FROM_EMAIL, + request=request, + domain_override=request.get_host()) + return HttpResponse(json.dumps({'success': True, 'value': render_to_string('registration/password_reset_done.html', {})})) else: return HttpResponse(json.dumps({'success': False, 'error': 'Invalid e-mail'})) + @ensure_csrf_cookie def reactivation_email(request): ''' Send an e-mail to reactivate a deactivated account, or to @@ -856,6 +866,7 @@ def reactivation_email(request): 'error': 'No inactive user with this e-mail exists'})) return reactivation_email_for_user(user) + def reactivation_email_for_user(user): reg = Registration.objects.get(user=user) @@ -996,11 +1007,11 @@ def pending_name_changes(request): changes = list(PendingNameChange.objects.all()) js = {'students': [{'new_name': c.new_name, - 'rationale':c.rationale, - 'old_name':UserProfile.objects.get(user=c.user).name, - 'email':c.user.email, - 'uid':c.user.id, - 'cid':c.id} for c in changes]} + 'rationale': c.rationale, + 'old_name': UserProfile.objects.get(user=c.user).name, + 'email': c.user.email, + 'uid': c.user.id, + 'cid': c.id} for c in changes]} return render_to_response('name_changes.html', js) @@ -1057,6 +1068,8 @@ def accept_name_change(request): # TODO: This is a giant kludge to give Pearson something to test against ASAP. # Will need to get replaced by something that actually ties into TestCenterUser + + @csrf_exempt def test_center_login(request): if not settings.MITX_FEATURES.get('ENABLE_PEARSON_HACK_TEST'): diff --git a/common/djangoapps/track/migrations/0001_initial.py b/common/djangoapps/track/migrations/0001_initial.py index 0546203cf83..6ec146dd108 100644 --- a/common/djangoapps/track/migrations/0001_initial.py +++ b/common/djangoapps/track/migrations/0001_initial.py @@ -45,4 +45,4 @@ class Migration(SchemaMigration): } } - complete_apps = ['track'] \ No newline at end of file + complete_apps = ['track'] diff --git a/common/djangoapps/track/migrations/0002_auto__add_field_trackinglog_host__chg_field_trackinglog_event_type__ch.py b/common/djangoapps/track/migrations/0002_auto__add_field_trackinglog_host__chg_field_trackinglog_event_type__ch.py index 4c73aa3bfdb..0bb0cde42ec 100644 --- a/common/djangoapps/track/migrations/0002_auto__add_field_trackinglog_host__chg_field_trackinglog_event_type__ch.py +++ b/common/djangoapps/track/migrations/0002_auto__add_field_trackinglog_host__chg_field_trackinglog_event_type__ch.py @@ -48,4 +48,4 @@ class Migration(SchemaMigration): } } - complete_apps = ['track'] \ No newline at end of file + complete_apps = ['track'] diff --git a/common/djangoapps/track/models.py b/common/djangoapps/track/models.py index dfdf7a05589..b6a16706c1c 100644 --- a/common/djangoapps/track/models.py +++ b/common/djangoapps/track/models.py @@ -2,21 +2,20 @@ from django.db import models from django.db import models + class TrackingLog(models.Model): - dtcreated = models.DateTimeField('creation date',auto_now_add=True) - username = models.CharField(max_length=32,blank=True) - ip = models.CharField(max_length=32,blank=True) + dtcreated = models.DateTimeField('creation date', auto_now_add=True) + username = models.CharField(max_length=32, blank=True) + ip = models.CharField(max_length=32, blank=True) event_source = models.CharField(max_length=32) - event_type = models.CharField(max_length=512,blank=True) + event_type = models.CharField(max_length=512, blank=True) event = models.TextField(blank=True) - agent = models.CharField(max_length=256,blank=True) - page = models.CharField(max_length=512,blank=True,null=True) + agent = models.CharField(max_length=256, blank=True) + page = models.CharField(max_length=512, blank=True, null=True) time = models.DateTimeField('event time') - host = models.CharField(max_length=64,blank=True) + host = models.CharField(max_length=64, blank=True) def __unicode__(self): s = "[%s] %s@%s: %s | %s | %s | %s" % (self.time, self.username, self.ip, self.event_source, self.event_type, self.page, self.event) return s - - diff --git a/common/djangoapps/track/views.py b/common/djangoapps/track/views.py index 54bd4767997..ae3a1dcb3e9 100644 --- a/common/djangoapps/track/views.py +++ b/common/djangoapps/track/views.py @@ -17,19 +17,21 @@ from track.models import TrackingLog log = logging.getLogger("tracking") -LOGFIELDS = ['username','ip','event_source','event_type','event','agent','page','time','host'] +LOGFIELDS = ['username', 'ip', 'event_source', 'event_type', 'event', 'agent', 'page', 'time', 'host'] + def log_event(event): event_str = json.dumps(event) log.info(event_str[:settings.TRACK_MAX_EVENT]) if settings.MITX_FEATURES.get('ENABLE_SQL_TRACKING_LOGS'): event['time'] = dateutil.parser.parse(event['time']) - tldat = TrackingLog(**dict( (x,event[x]) for x in LOGFIELDS )) + tldat = TrackingLog(**dict((x, event[x]) for x in LOGFIELDS)) try: tldat.save() except Exception as err: log.exception(err) + def user_track(request): try: # TODO: Do the same for many of the optional META parameters username = request.user.username @@ -87,13 +89,14 @@ def server_track(request, event_type, event, page=None): "host": request.META['SERVER_NAME'], } - if event_type.startswith("/event_logs") and request.user.is_staff: # don't log + if event_type.startswith("/event_logs") and request.user.is_staff: # don't log return log_event(event) + @login_required @ensure_csrf_cookie -def view_tracking_log(request,args=''): +def view_tracking_log(request, args=''): if not request.user.is_staff: return redirect('/') nlen = 100 @@ -104,16 +107,15 @@ def view_tracking_log(request,args=''): nlen = int(arg) if arg.startswith('username='): username = arg[9:] - + record_instances = TrackingLog.objects.all().order_by('-time') if username: record_instances = record_instances.filter(username=username) record_instances = record_instances[0:nlen] - + # fix dtstamp fmt = '%a %d-%b-%y %H:%M:%S' # "%Y-%m-%d %H:%M:%S %Z%z" for rinst in record_instances: rinst.dtstr = rinst.time.replace(tzinfo=pytz.utc).astimezone(pytz.timezone('US/Eastern')).strftime(fmt) - return render_to_response('tracking_log.html',{'records':record_instances}) - + return render_to_response('tracking_log.html', {'records': record_instances}) diff --git a/common/djangoapps/util/cache.py b/common/djangoapps/util/cache.py index 89b5dffd5e7..8ab1b06acdd 100644 --- a/common/djangoapps/util/cache.py +++ b/common/djangoapps/util/cache.py @@ -58,4 +58,3 @@ def cache_if_anonymous(view_func): return view_func(request, *args, **kwargs) return _decorated - diff --git a/common/djangoapps/util/converters.py b/common/djangoapps/util/converters.py index 7f96dc6c307..900371a0dd8 100644 --- a/common/djangoapps/util/converters.py +++ b/common/djangoapps/util/converters.py @@ -1,7 +1,9 @@ -import time, datetime +import time +import datetime import re import calendar + def time_to_date(time_obj): """ Convert a time.time_struct to a true universal time (can pass to js Date constructor) @@ -9,6 +11,7 @@ def time_to_date(time_obj): # TODO change to using the isoformat() function on datetime. js date can parse those return calendar.timegm(time_obj) * 1000 + def jsdate_to_time(field): """ Convert a universal time (iso format) or msec since epoch to a time obj @@ -16,9 +19,9 @@ def jsdate_to_time(field): if field is None: return field elif isinstance(field, basestring): # iso format but ignores time zone assuming it's Z - d=datetime.datetime(*map(int, re.split('[^\d]', field)[:6])) # stop after seconds. Debatable + d = datetime.datetime(*map(int, re.split('[^\d]', field)[:6])) # stop after seconds. Debatable return d.utctimetuple() elif isinstance(field, int) or isinstance(field, float): return time.gmtime(field / 1000) elif isinstance(field, time.struct_time): - return field \ No newline at end of file + return field diff --git a/common/djangoapps/util/json_request.py b/common/djangoapps/util/json_request.py index 4beff7bdc84..840a8282f94 100644 --- a/common/djangoapps/util/json_request.py +++ b/common/djangoapps/util/json_request.py @@ -13,7 +13,7 @@ def expect_json(view_function): def expect_json_with_cloned_request(request, *args, **kwargs): # cdodge: fix postback errors in CMS. The POST 'content-type' header can include additional information # e.g. 'charset', so we can't do a direct string compare - if request.META.get('CONTENT_TYPE','').lower().startswith("application/json"): + if request.META.get('CONTENT_TYPE', '').lower().startswith("application/json"): cloned_request = copy.copy(request) cloned_request.POST = cloned_request.POST.copy() cloned_request.POST.update(json.loads(request.body)) diff --git a/common/djangoapps/util/views.py b/common/djangoapps/util/views.py index 0ccdd033016..cece37757bb 100644 --- a/common/djangoapps/util/views.py +++ b/common/djangoapps/util/views.py @@ -93,6 +93,7 @@ def accepts(request, media_type): accept = parse_accept_header(request.META.get("HTTP_ACCEPT", "")) return media_type in [t for (t, p, q) in accept] + def debug_request(request): """Return a pretty printed version of the request""" diff --git a/common/djangoapps/xmodule_modifiers.py b/common/djangoapps/xmodule_modifiers.py index b171b402ee2..7b19c275539 100644 --- a/common/djangoapps/xmodule_modifiers.py +++ b/common/djangoapps/xmodule_modifiers.py @@ -12,6 +12,7 @@ from xmodule.vertical_module import VerticalModule log = logging.getLogger("mitx.xmodule_modifiers") + def wrap_xmodule(get_html, module, template, context=None): """ Wraps the results of get_html in a standard <section> with identifying @@ -32,7 +33,7 @@ def wrap_xmodule(get_html, module, template, context=None): def _get_html(): context.update({ 'content': get_html(), - 'display_name' : module.metadata.get('display_name') if module.metadata is not None else None, + 'display_name': module.metadata.get('display_name') if module.metadata is not None else None, 'class_': module.__class__.__name__, 'module_name': module.js_module_name }) @@ -52,6 +53,7 @@ def replace_course_urls(get_html, course_id): return static_replace.replace_course_urls(get_html(), course_id) return _get_html + def replace_static_urls(get_html, data_dir, course_namespace=None): """ Updates the supplied module with a new get_html function that wraps @@ -64,6 +66,7 @@ def replace_static_urls(get_html, data_dir, course_namespace=None): return static_replace.replace_static_urls(get_html(), data_dir, course_namespace) return _get_html + def grade_histogram(module_id): ''' Print out a histogram of grades on a given problem. Part of staff member debug info. @@ -98,7 +101,7 @@ def add_histogram(get_html, module, user): @wraps(get_html) def _get_html(): - if type(module) in [SequenceModule, VerticalModule]: # TODO: make this more general, eg use an XModule attribute instead + if type(module) in [SequenceModule, VerticalModule]: # TODO: make this more general, eg use an XModule attribute instead return get_html() module_id = module.id @@ -114,35 +117,35 @@ def add_histogram(get_html, module, user): # doesn't like symlinks) filepath = filename data_dir = osfs.root_path.rsplit('/')[-1] - giturl = module.metadata.get('giturl','https://github.com/MITx') - edit_link = "%s/%s/tree/master/%s" % (giturl,data_dir,filepath) + giturl = module.metadata.get('giturl', 'https://github.com/MITx') + edit_link = "%s/%s/tree/master/%s" % (giturl, data_dir, filepath) else: edit_link = False # Need to define all the variables that are about to be used giturl = "" data_dir = "" - source_file = module.metadata.get('source_file','') # source used to generate the problem XML, eg latex or word + source_file = module.metadata.get('source_file', '') # source used to generate the problem XML, eg latex or word # useful to indicate to staff if problem has been released or not # TODO (ichuang): use _has_access_descriptor.can_load in lms.courseware.access, instead of now>mstart comparison here now = time.gmtime() is_released = "unknown" - mstart = getattr(module.descriptor,'start') + mstart = getattr(module.descriptor, 'start') if mstart is not None: is_released = "<font color='red'>Yes!</font>" if (now > mstart) else "<font color='green'>Not yet</font>" staff_context = {'definition': module.definition.get('data'), 'metadata': json.dumps(module.metadata, indent=4), 'location': module.location, - 'xqa_key': module.metadata.get('xqa_key',''), - 'source_file' : source_file, - 'source_url': '%s/%s/tree/master/%s' % (giturl,data_dir,source_file), + 'xqa_key': module.metadata.get('xqa_key', ''), + 'source_file': source_file, + 'source_url': '%s/%s/tree/master/%s' % (giturl, data_dir, source_file), 'category': str(module.__class__.__name__), # Template uses element_id in js function names, so can't allow dashes - 'element_id': module.location.html_id().replace('-','_'), + 'element_id': module.location.html_id().replace('-', '_'), 'edit_link': edit_link, 'user': user, - 'xqa_server' : settings.MITX_FEATURES.get('USE_XQA_SERVER','http://xqa:server@content-qa.mitx.mit.edu/xqa'), + 'xqa_server': settings.MITX_FEATURES.get('USE_XQA_SERVER', 'http://xqa:server@content-qa.mitx.mit.edu/xqa'), 'histogram': json.dumps(histogram), 'render_histogram': render_histogram, 'module_content': get_html(), @@ -151,4 +154,3 @@ def add_histogram(get_html, module, user): return render_to_string("staff_problem_info.html", staff_context) return _get_html - diff --git a/common/lib/capa/capa/calc.py b/common/lib/capa/capa/calc.py index 40ac14308e5..0f062d17d5c 100644 --- a/common/lib/capa/capa/calc.py +++ b/common/lib/capa/capa/calc.py @@ -121,9 +121,9 @@ def evaluator(variables, functions, string, cs=False): # confusing. They may also conflict with variables if we ever allow e.g. # 5R instead of 5*R suffixes = {'%': 0.01, 'k': 1e3, 'M': 1e6, 'G': 1e9, - 'T': 1e12,# 'P':1e15,'E':1e18,'Z':1e21,'Y':1e24, + 'T': 1e12, # 'P':1e15,'E':1e18,'Z':1e21,'Y':1e24, 'c': 1e-2, 'm': 1e-3, 'u': 1e-6, - 'n': 1e-9, 'p': 1e-12}# ,'f':1e-15,'a':1e-18,'z':1e-21,'y':1e-24} + 'n': 1e-9, 'p': 1e-12} # ,'f':1e-15,'a':1e-18,'z':1e-21,'y':1e-24} def super_float(text): ''' Like float, but with si extensions. 1k goes to 1000''' diff --git a/common/lib/capa/capa/capa_problem.py b/common/lib/capa/capa/capa_problem.py index 4b0faa91a1b..9b8bbd72884 100644 --- a/common/lib/capa/capa/capa_problem.py +++ b/common/lib/capa/capa/capa_problem.py @@ -75,7 +75,7 @@ global_context = {'random': random, 'draganddrop': verifiers.draganddrop} # These should be removed from HTML output, including all subelements -html_problem_semantics = ["codeparam", "responseparam", "answer", "script", "hintgroup", "openendedparam","openendedrubric"] +html_problem_semantics = ["codeparam", "responseparam", "answer", "script", "hintgroup", "openendedparam", "openendedrubric"] log = logging.getLogger('mitx.' + __name__) @@ -453,7 +453,7 @@ class LoncapaProblem(object): exec code in context, context except Exception as err: log.exception("Error while execing script code: " + code) - msg = "Error while executing script code: %s" % str(err).replace('<','<') + msg = "Error while executing script code: %s" % str(err).replace('<', '<') raise responsetypes.LoncapaProblemError(msg) finally: sys.path = original_path @@ -502,7 +502,7 @@ class LoncapaProblem(object): 'id': problemtree.get('id'), 'feedback': {'message': msg, 'hint': hint, - 'hintmode': hintmode,}} + 'hintmode': hintmode, }} input_type_cls = inputtypes.registry.get_class_for_tag(problemtree.tag) the_input = input_type_cls(self.system, problemtree, state) diff --git a/common/lib/capa/capa/chem/__init__.py b/common/lib/capa/capa/chem/__init__.py index 8b137891791..e69de29bb2d 100644 --- a/common/lib/capa/capa/chem/__init__.py +++ b/common/lib/capa/capa/chem/__init__.py @@ -1 +0,0 @@ - diff --git a/common/lib/capa/capa/chem/chemcalc.py b/common/lib/capa/capa/chem/chemcalc.py index 389e688cf43..5b800050449 100644 --- a/common/lib/capa/capa/chem/chemcalc.py +++ b/common/lib/capa/capa/chem/chemcalc.py @@ -17,17 +17,17 @@ from nltk.tree import Tree ARROWS = ('<->', '->') ## Defines a simple pyparsing tokenizer for chemical equations -elements = ['Ac','Ag','Al','Am','Ar','As','At','Au','B','Ba','Be', - 'Bh','Bi','Bk','Br','C','Ca','Cd','Ce','Cf','Cl','Cm', - 'Cn','Co','Cr','Cs','Cu','Db','Ds','Dy','Er','Es','Eu', - 'F','Fe','Fl','Fm','Fr','Ga','Gd','Ge','H','He','Hf', - 'Hg','Ho','Hs','I','In','Ir','K','Kr','La','Li','Lr', - 'Lu','Lv','Md','Mg','Mn','Mo','Mt','N','Na','Nb','Nd', - 'Ne','Ni','No','Np','O','Os','P','Pa','Pb','Pd','Pm', - 'Po','Pr','Pt','Pu','Ra','Rb','Re','Rf','Rg','Rh','Rn', - 'Ru','S','Sb','Sc','Se','Sg','Si','Sm','Sn','Sr','Ta', - 'Tb','Tc','Te','Th','Ti','Tl','Tm','U','Uuo','Uup', - 'Uus','Uut','V','W','Xe','Y','Yb','Zn','Zr'] +elements = ['Ac', 'Ag', 'Al', 'Am', 'Ar', 'As', 'At', 'Au', 'B', 'Ba', 'Be', + 'Bh', 'Bi', 'Bk', 'Br', 'C', 'Ca', 'Cd', 'Ce', 'Cf', 'Cl', 'Cm', + 'Cn', 'Co', 'Cr', 'Cs', 'Cu', 'Db', 'Ds', 'Dy', 'Er', 'Es', 'Eu', + 'F', 'Fe', 'Fl', 'Fm', 'Fr', 'Ga', 'Gd', 'Ge', 'H', 'He', 'Hf', + 'Hg', 'Ho', 'Hs', 'I', 'In', 'Ir', 'K', 'Kr', 'La', 'Li', 'Lr', + 'Lu', 'Lv', 'Md', 'Mg', 'Mn', 'Mo', 'Mt', 'N', 'Na', 'Nb', 'Nd', + 'Ne', 'Ni', 'No', 'Np', 'O', 'Os', 'P', 'Pa', 'Pb', 'Pd', 'Pm', + 'Po', 'Pr', 'Pt', 'Pu', 'Ra', 'Rb', 'Re', 'Rf', 'Rg', 'Rh', 'Rn', + 'Ru', 'S', 'Sb', 'Sc', 'Se', 'Sg', 'Si', 'Sm', 'Sn', 'Sr', 'Ta', + 'Tb', 'Tc', 'Te', 'Th', 'Ti', 'Tl', 'Tm', 'U', 'Uuo', 'Uup', + 'Uus', 'Uut', 'V', 'W', 'Xe', 'Y', 'Yb', 'Zn', 'Zr'] digits = map(str, range(10)) symbols = list("[](){}^+-/") phases = ["(s)", "(l)", "(g)", "(aq)"] @@ -252,7 +252,7 @@ def _get_final_tree(s): ''' tokenized = tokenizer.parseString(s) parsed = parser.parse(tokenized) - merged = _merge_children(parsed, {'S','group'}) + merged = _merge_children(parsed, {'S', 'group'}) final = _clean_parse_tree(merged) return final diff --git a/common/lib/capa/capa/correctmap.py b/common/lib/capa/capa/correctmap.py index c7386219b18..a78b10d07ab 100644 --- a/common/lib/capa/capa/correctmap.py +++ b/common/lib/capa/capa/correctmap.py @@ -3,6 +3,7 @@ # # Used by responsetypes and capa_problem + class CorrectMap(object): """ Stores map between answer_id and response evaluation result for each question @@ -152,6 +153,3 @@ class CorrectMap(object): if not isinstance(other_cmap, CorrectMap): raise Exception('CorrectMap.update called with invalid argument %s' % other_cmap) self.cmap.update(other_cmap.get_dict()) - - - diff --git a/common/lib/capa/capa/customrender.py b/common/lib/capa/capa/customrender.py index ef1044e8b1f..a925a5970d0 100644 --- a/common/lib/capa/capa/customrender.py +++ b/common/lib/capa/capa/customrender.py @@ -22,6 +22,8 @@ log = logging.getLogger('mitx.' + __name__) registry = TagRegistry() #----------------------------------------------------------------------------- + + class MathRenderer(object): tags = ['math'] @@ -77,6 +79,7 @@ registry.register(MathRenderer) #----------------------------------------------------------------------------- + class SolutionRenderer(object): ''' A solution is just a <span>...</span> which is given an ID, that is used for displaying an @@ -97,4 +100,3 @@ class SolutionRenderer(object): return etree.XML(html) registry.register(SolutionRenderer) - diff --git a/common/lib/capa/capa/inputtypes.py b/common/lib/capa/capa/inputtypes.py index 0b0e86ce662..83c79a72472 100644 --- a/common/lib/capa/capa/inputtypes.py +++ b/common/lib/capa/capa/inputtypes.py @@ -54,6 +54,7 @@ log = logging.getLogger('mitx.' + __name__) registry = TagRegistry() + class Attribute(object): """ Allows specifying required and optional attributes for input types. @@ -413,7 +414,7 @@ class JavascriptInput(InputTypeBase): return [Attribute('params', None), Attribute('problem_state', None), Attribute('display_class', None), - Attribute('display_file', None),] + Attribute('display_file', None), ] def setup(self): @@ -477,12 +478,13 @@ class TextLine(InputTypeBase): def _extra_context(self): return {'do_math': self.do_math, - 'preprocessor': self.preprocessor,} + 'preprocessor': self.preprocessor, } registry.register(TextLine) #----------------------------------------------------------------------------- + class FileSubmission(InputTypeBase): """ Upload some files (e.g. for programming assignments) @@ -508,7 +510,7 @@ class FileSubmission(InputTypeBase): Convert the list of allowed files to a convenient format. """ return [Attribute('allowed_files', '[]', transform=cls.parse_files), - Attribute('required_files', '[]', transform=cls.parse_files),] + Attribute('required_files', '[]', transform=cls.parse_files), ] def setup(self): """ @@ -524,7 +526,7 @@ class FileSubmission(InputTypeBase): self.msg = FileSubmission.submitted_msg def _extra_context(self): - return {'queue_len': self.queue_len,} + return {'queue_len': self.queue_len, } return context registry.register(FileSubmission) @@ -582,7 +584,7 @@ class CodeInput(InputTypeBase): def _extra_context(self): """Defined queue_len, add it """ - return {'queue_len': self.queue_len,} + return {'queue_len': self.queue_len, } registry.register(CodeInput) @@ -606,7 +608,7 @@ class Schematic(InputTypeBase): Attribute('parts', None), Attribute('analyses', None), Attribute('initial_value', None), - Attribute('submit_analyses', None),] + Attribute('submit_analyses', None), ] return context @@ -614,6 +616,7 @@ registry.register(Schematic) #----------------------------------------------------------------------------- + class ImageInput(InputTypeBase): """ Clickable image as an input field. Element should specify the image source, height, @@ -635,7 +638,7 @@ class ImageInput(InputTypeBase): """ return [Attribute('src'), Attribute('height'), - Attribute('width'),] + Attribute('width'), ] def setup(self): @@ -660,6 +663,7 @@ registry.register(ImageInput) #----------------------------------------------------------------------------- + class Crystallography(InputTypeBase): """ An input for crystallography -- user selects 3 points on the axes, and we get a plane. @@ -728,18 +732,19 @@ class ChemicalEquationInput(InputTypeBase): """ Can set size of text field. """ - return [Attribute('size', '20'),] + return [Attribute('size', '20'), ] def _extra_context(self): """ TODO (vshnayder): Get rid of this once we have a standard way of requiring js to be loaded. """ - return {'previewer': '/static/js/capa/chemical_equation_preview.js',} + return {'previewer': '/static/js/capa/chemical_equation_preview.js', } registry.register(ChemicalEquationInput) #----------------------------------------------------------------------------- + class DragAndDropInput(InputTypeBase): """ Input for drag and drop problems. Allows student to drag and drop images and diff --git a/common/lib/capa/capa/responsetypes.py b/common/lib/capa/capa/responsetypes.py index 5f0e1639b25..adf5eda416f 100644 --- a/common/lib/capa/capa/responsetypes.py +++ b/common/lib/capa/capa/responsetypes.py @@ -186,9 +186,9 @@ class LoncapaResponse(object): tree = etree.Element('span') # problem author can make this span display:inline - if self.xml.get('inline',''): - tree.set('class','inline') - + if self.xml.get('inline', ''): + tree.set('class', 'inline') + for item in self.xml: # call provided procedure to do the rendering item_xhtml = renderer(item) @@ -1294,7 +1294,7 @@ class CodeResponse(LoncapaResponse): # State associated with the queueing request queuestate = {'key': queuekey, - 'time': qtime,} + 'time': qtime, } cmap = CorrectMap() if error: diff --git a/common/lib/capa/capa/tests/__init__.py b/common/lib/capa/capa/tests/__init__.py index b06975f6ce0..89cb5a5ee90 100644 --- a/common/lib/capa/capa/tests/__init__.py +++ b/common/lib/capa/capa/tests/__init__.py @@ -8,6 +8,7 @@ import xml.sax.saxutils as saxutils TEST_DIR = os.path.dirname(os.path.realpath(__file__)) + def tst_render_template(template, context): """ A test version of render to template. Renders to the repr of the context, completely ignoring @@ -25,7 +26,7 @@ test_system = Mock( user=Mock(), filestore=fs.osfs.OSFS(os.path.join(TEST_DIR, "test_files")), debug=True, - xqueue={'interface':None, 'callback_url':'/', 'default_queuename': 'testqueue', 'waittime': 10}, + xqueue={'interface': None, 'callback_url': '/', 'default_queuename': 'testqueue', 'waittime': 10}, node_path=os.environ.get("NODE_PATH", "/usr/local/lib/node_modules"), - anonymous_student_id = 'student' + anonymous_student_id='student' ) diff --git a/common/lib/capa/capa/tests/test_customrender.py b/common/lib/capa/capa/tests/test_customrender.py index 7208ab2941c..eece275b050 100644 --- a/common/lib/capa/capa/tests/test_customrender.py +++ b/common/lib/capa/capa/tests/test_customrender.py @@ -8,6 +8,7 @@ from capa import customrender # just a handy shortcut lookup_tag = customrender.registry.get_class_for_tag + def extract_context(xml): """ Given an xml element corresponding to the output of test_system.render_template, get back the @@ -15,9 +16,11 @@ def extract_context(xml): """ return eval(xml.text) + def quote_attr(s): return saxutils.quoteattr(s)[1:-1] # don't want the outer quotes + class HelperTest(unittest.TestCase): ''' Make sure that our helper function works! @@ -50,7 +53,7 @@ class SolutionRenderTest(unittest.TestCase): # our test_system "renders" templates to a div with the repr of the context xml = renderer.get_html() context = extract_context(xml) - self.assertEqual(context, {'id' : 'solution_12'}) + self.assertEqual(context, {'id': 'solution_12'}) class MathRenderTest(unittest.TestCase): @@ -65,12 +68,11 @@ class MathRenderTest(unittest.TestCase): renderer = lookup_tag('math')(test_system, element) self.assertEqual(renderer.mathstr, mathjax_out) - + def test_parsing(self): self.check_parse('$abc$', '[mathjaxinline]abc[/mathjaxinline]') self.check_parse('$abc', '$abc') self.check_parse(r'$\displaystyle 2+2$', '[mathjax] 2+2[/mathjax]') - + # NOTE: not testing get_html yet because I don't understand why it's doing what it's doing. - diff --git a/common/lib/capa/capa/tests/test_inputtypes.py b/common/lib/capa/capa/tests/test_inputtypes.py index 6c282baf955..4a5ea5c4293 100644 --- a/common/lib/capa/capa/tests/test_inputtypes.py +++ b/common/lib/capa/capa/tests/test_inputtypes.py @@ -31,6 +31,7 @@ lookup_tag = inputtypes.registry.get_class_for_tag def quote_attr(s): return saxutils.quoteattr(s)[1:-1] # don't want the outer quotes + class OptionInputTest(unittest.TestCase): ''' Make sure option inputs work @@ -100,7 +101,7 @@ class ChoiceGroupTest(unittest.TestCase): 'input_type': expected_input_type, 'choices': [('foil1', '<text>This is foil One.</text>'), ('foil2', '<text>This is foil Two.</text>'), - ('foil3', 'This is foil Three.'),], + ('foil3', 'This is foil Three.'), ], 'name_array_suffix': expected_suffix, # what is this for?? } @@ -137,7 +138,7 @@ class JavascriptInputTest(unittest.TestCase): element = etree.fromstring(xml_str) - state = {'value': '3',} + state = {'value': '3', } the_input = lookup_tag('javascriptinput')(test_system, element, state) context = the_input._get_render_context() @@ -149,7 +150,7 @@ class JavascriptInputTest(unittest.TestCase): 'params': params, 'display_file': display_file, 'display_class': display_class, - 'problem_state': problem_state,} + 'problem_state': problem_state, } self.assertEqual(context, expected) @@ -165,7 +166,7 @@ class TextLineTest(unittest.TestCase): element = etree.fromstring(xml_str) - state = {'value': 'BumbleBee',} + state = {'value': 'BumbleBee', } the_input = lookup_tag('textline')(test_system, element, state) context = the_input._get_render_context() @@ -193,7 +194,7 @@ class TextLineTest(unittest.TestCase): element = etree.fromstring(xml_str) - state = {'value': 'BumbleBee',} + state = {'value': 'BumbleBee', } the_input = lookup_tag('textline')(test_system, element, state) context = the_input._get_render_context() @@ -231,7 +232,7 @@ class FileSubmissionTest(unittest.TestCase): state = {'value': 'BumbleBee.py', 'status': 'incomplete', - 'feedback' : {'message': '3'}, } + 'feedback': {'message': '3'}, } input_class = lookup_tag('filesubmission') the_input = input_class(test_system, element, state) @@ -275,7 +276,7 @@ class CodeInputTest(unittest.TestCase): state = {'value': 'print "good evening"', 'status': 'incomplete', - 'feedback' : {'message': '3'}, } + 'feedback': {'message': '3'}, } input_class = lookup_tag('codeinput') the_input = input_class(test_system, element, state) @@ -488,7 +489,7 @@ class ChemicalEquationTest(unittest.TestCase): element = etree.fromstring(xml_str) - state = {'value': 'H2OYeah',} + state = {'value': 'H2OYeah', } the_input = lookup_tag('chemicalequationinput')(test_system, element, state) context = the_input._get_render_context() diff --git a/common/lib/capa/capa/tests/test_responsetypes.py b/common/lib/capa/capa/tests/test_responsetypes.py index 9eecef3986f..18da338b91e 100644 --- a/common/lib/capa/capa/tests/test_responsetypes.py +++ b/common/lib/capa/capa/tests/test_responsetypes.py @@ -16,6 +16,7 @@ from capa.correctmap import CorrectMap from capa.util import convert_files_to_filenames from capa.xqueue_interface import dateformat + class MultiChoiceTest(unittest.TestCase): def test_MC_grade(self): multichoice_file = os.path.dirname(__file__) + "/test_files/multichoice.xml" @@ -295,16 +296,16 @@ class CodeResponseTest(unittest.TestCase): old_cmap = CorrectMap() for i, answer_id in enumerate(answer_ids): queuekey = 1000 + i - queuestate = CodeResponseTest.make_queuestate(1000+i, datetime.now()) + queuestate = CodeResponseTest.make_queuestate(1000 + i, datetime.now()) old_cmap.update(CorrectMap(answer_id=answer_ids[i], queuestate=queuestate)) # Message format common to external graders - grader_msg = '<span>MESSAGE</span>' # Must be valid XML - correct_score_msg = json.dumps({'correct':True, 'score':1, 'msg': grader_msg}) - incorrect_score_msg = json.dumps({'correct':False, 'score':0, 'msg': grader_msg}) + grader_msg = '<span>MESSAGE</span>' # Must be valid XML + correct_score_msg = json.dumps({'correct': True, 'score': 1, 'msg': grader_msg}) + incorrect_score_msg = json.dumps({'correct': False, 'score': 0, 'msg': grader_msg}) xserver_msgs = {'correct': correct_score_msg, - 'incorrect': incorrect_score_msg,} + 'incorrect': incorrect_score_msg, } # Incorrect queuekey, state should not be updated for correctness in ['correct', 'incorrect']: @@ -325,7 +326,7 @@ class CodeResponseTest(unittest.TestCase): new_cmap = CorrectMap() new_cmap.update(old_cmap) - npoints = 1 if correctness=='correct' else 0 + npoints = 1 if correctness == 'correct' else 0 new_cmap.set(answer_id=answer_id, npoints=npoints, correctness=correctness, msg=grader_msg, queuestate=None) test_lcp.update_score(xserver_msgs[correctness], queuekey=1000 + i) @@ -361,7 +362,7 @@ class CodeResponseTest(unittest.TestCase): for i, answer_id in enumerate(answer_ids): queuekey = 1000 + i latest_timestamp = datetime.now() - queuestate = CodeResponseTest.make_queuestate(1000+i, latest_timestamp) + queuestate = CodeResponseTest.make_queuestate(1000 + i, latest_timestamp) cmap.update(CorrectMap(answer_id=answer_id, queuestate=queuestate)) test_lcp.correct_map.update(cmap) @@ -412,6 +413,7 @@ class ChoiceResponseTest(unittest.TestCase): self.assertEquals(test_lcp.grade_answers(test_answers).get_correctness('1_3_1'), 'incorrect') self.assertEquals(test_lcp.grade_answers(test_answers).get_correctness('1_4_1'), 'correct') + class JavascriptResponseTest(unittest.TestCase): def test_jr_grade(self): @@ -424,4 +426,3 @@ class JavascriptResponseTest(unittest.TestCase): self.assertEquals(test_lcp.grade_answers(incorrect_answers).get_correctness('1_2_1'), 'incorrect') self.assertEquals(test_lcp.grade_answers(correct_answers).get_correctness('1_2_1'), 'correct') - diff --git a/common/lib/capa/capa/util.py b/common/lib/capa/capa/util.py index 0df58c216fc..a0f25c4947e 100644 --- a/common/lib/capa/capa/util.py +++ b/common/lib/capa/capa/util.py @@ -51,15 +51,17 @@ def convert_files_to_filenames(answers): new_answers = dict() for answer_id in answers.keys(): answer = answers[answer_id] - if is_list_of_files(answer): # Files are stored as a list, even if one file + if is_list_of_files(answer): # Files are stored as a list, even if one file new_answers[answer_id] = [f.name for f in answer] else: new_answers[answer_id] = answers[answer_id] return new_answers + def is_list_of_files(files): return isinstance(files, list) and all(is_file(f) for f in files) + def is_file(file_to_test): ''' Duck typing to check if 'file_to_test' is a File object @@ -79,11 +81,10 @@ def find_with_default(node, path, default): Returns: node.find(path).text if the find succeeds, default otherwise. - + """ v = node.find(path) if v is not None: return v.text else: return default - diff --git a/common/lib/capa/capa/xqueue_interface.py b/common/lib/capa/capa/xqueue_interface.py index 798867955b2..8dbe2c84aac 100644 --- a/common/lib/capa/capa/xqueue_interface.py +++ b/common/lib/capa/capa/xqueue_interface.py @@ -10,6 +10,7 @@ import requests log = logging.getLogger('mitx.' + __name__) dateformat = '%Y%m%d%H%M%S' + def make_hashkey(seed): ''' Generate a string key by hashing @@ -29,9 +30,9 @@ def make_xheader(lms_callback_url, lms_key, queue_name): 'queue_name': designate a specific queue within xqueue server, e.g. 'MITx-6.00x' (string) } """ - return json.dumps({ 'lms_callback_url': lms_callback_url, + return json.dumps({'lms_callback_url': lms_callback_url, 'lms_key': lms_key, - 'queue_name': queue_name }) + 'queue_name': queue_name}) def parse_xreply(xreply): @@ -96,18 +97,18 @@ class XQueueInterface(object): def _login(self): - payload = { 'username': self.auth['username'], - 'password': self.auth['password'] } + payload = {'username': self.auth['username'], + 'password': self.auth['password']} return self._http_post(self.url + '/xqueue/login/', payload) def _send_to_queue(self, header, body, files_to_upload): payload = {'xqueue_header': header, - 'xqueue_body' : body} + 'xqueue_body': body} files = {} if files_to_upload is not None: for f in files_to_upload: - files.update({ f.name: f }) + files.update({f.name: f}) return self._http_post(self.url + '/xqueue/submit/', payload, files=files) diff --git a/common/lib/supertrace.py b/common/lib/supertrace.py index e17cd7a8bad..83dfa12031f 100644 --- a/common/lib/supertrace.py +++ b/common/lib/supertrace.py @@ -3,7 +3,8 @@ A handy util to print a django-debug-screen-like stack trace with values of local variables. """ -import sys, traceback +import sys +import traceback from django.utils.encoding import smart_unicode @@ -48,5 +49,3 @@ def supertrace(max_len=160): print s except: print "<ERROR WHILE PRINTING VALUE>" - - diff --git a/common/lib/xmodule/setup.py b/common/lib/xmodule/setup.py index 817af9c10db..3bc8bc51432 100644 --- a/common/lib/xmodule/setup.py +++ b/common/lib/xmodule/setup.py @@ -30,7 +30,7 @@ setup( "peergrading = xmodule.peer_grading_module:PeerGradingDescriptor", "problem = xmodule.capa_module:CapaDescriptor", "problemset = xmodule.seq_module:SequenceDescriptor", - "randomize = xmodule.randomize_module:RandomizeDescriptor", + "randomize = xmodule.randomize_module:RandomizeDescriptor", "section = xmodule.backcompat_module:SemanticSectionDescriptor", "sequential = xmodule.seq_module:SequenceDescriptor", "slides = xmodule.backcompat_module:TranslateCustomTagDescriptor", diff --git a/common/lib/xmodule/xmodule/abtest_module.py b/common/lib/xmodule/xmodule/abtest_module.py index 12456bc7d79..537d8641271 100644 --- a/common/lib/xmodule/xmodule/abtest_module.py +++ b/common/lib/xmodule/xmodule/abtest_module.py @@ -51,7 +51,7 @@ class ABTestModule(XModule): def get_shared_state(self): return json.dumps({'group': self.group}) - + def get_child_descriptors(self): active_locations = set(self.definition['data']['group_content'][self.group]) return [desc for desc in self.descriptor.get_children() if desc.location.url() in active_locations] @@ -171,7 +171,7 @@ class ABTestDescriptor(RawDescriptor, XmlDescriptor): group_elem.append(etree.fromstring(child.export_to_xml(resource_fs))) return xml_object - - + + def has_dynamic_children(self): return True diff --git a/common/lib/xmodule/xmodule/capa_module.py b/common/lib/xmodule/xmodule/capa_module.py index d3c8786f669..d806ec7913e 100644 --- a/common/lib/xmodule/xmodule/capa_module.py +++ b/common/lib/xmodule/xmodule/capa_module.py @@ -29,6 +29,7 @@ TIMEDELTA_REGEX = re.compile(r'^((?P<days>\d+?) day(?:s?))?(\s)?((?P<hours>\d+?) # Generated this many different variants of problems with rerandomize=per_student NUM_RANDOMIZATION_BINS = 20 + def randomization_bin(seed, problem_id): """ Pick a randomization bin for the problem given the user's seed and a problem id. @@ -43,6 +44,7 @@ def randomization_bin(seed, problem_id): # get the first few digits of the hash, convert to an int, then mod. return int(h.hexdigest()[:7], 16) % NUM_RANDOMIZATION_BINS + def only_one(lst, default="", process=lambda x: x): """ If lst is empty, returns default @@ -283,7 +285,7 @@ class CapaModule(XModule): # Next, generate a fresh LoncapaProblem self.lcp = LoncapaProblem(self.definition['data'], self.location.html_id(), - state=None, # Tabula rasa + state=None, # Tabula rasa seed=self.seed, system=self.system) # Prepend a scary warning to the student @@ -302,7 +304,7 @@ class CapaModule(XModule): html = warning try: html += self.lcp.get_html() - except Exception, err: # Couldn't do it. Give up + except Exception, err: # Couldn't do it. Give up log.exception(err) raise @@ -315,7 +317,7 @@ class CapaModule(XModule): # check button is context-specific. # Put a "Check" button if unlimited attempts or still some left - if self.max_attempts is None or self.attempts < self.max_attempts-1: + if self.max_attempts is None or self.attempts < self.max_attempts - 1: check_button = "Check" else: # Will be final check so let user know that @@ -561,9 +563,9 @@ class CapaModule(XModule): current_time = datetime.datetime.now() prev_submit_time = self.lcp.get_recentmost_queuetime() waittime_between_requests = self.system.xqueue['waittime'] - if (current_time-prev_submit_time).total_seconds() < waittime_between_requests: + if (current_time - prev_submit_time).total_seconds() < waittime_between_requests: msg = 'You must wait at least %d seconds between submissions' % waittime_between_requests - return {'success': msg, 'html': ''} # Prompts a modal dialog in ajax callback + return {'success': msg, 'html': ''} # Prompts a modal dialog in ajax callback try: old_state = self.lcp.get_state() @@ -596,7 +598,7 @@ class CapaModule(XModule): event_info['attempts'] = self.attempts self.system.track_function('save_problem_check', event_info) - if hasattr(self.system,'psychometrics_handler'): # update PsychometricsData using callback + if hasattr(self.system, 'psychometrics_handler'): # update PsychometricsData using callback self.system.psychometrics_handler(self.get_instance_state()) # render problem into HTML @@ -707,7 +709,7 @@ class CapaDescriptor(RawDescriptor): @property def editable_metadata_fields(self): """Remove metadata from the editable fields since it has its own editor""" - subset = super(CapaDescriptor,self).editable_metadata_fields + subset = super(CapaDescriptor, self).editable_metadata_fields if 'markdown' in subset: subset.remove('markdown') return subset diff --git a/common/lib/xmodule/xmodule/combined_open_ended_module.py b/common/lib/xmodule/xmodule/combined_open_ended_module.py index 14a59c90047..112a7a68f89 100644 --- a/common/lib/xmodule/xmodule/combined_open_ended_module.py +++ b/common/lib/xmodule/xmodule/combined_open_ended_module.py @@ -50,14 +50,16 @@ ACCEPT_FILE_UPLOAD = False TRUE_DICT = ["True", True, "TRUE", "true"] HUMAN_TASK_TYPE = { - 'selfassessment' : "Self Assessment", - 'openended' : "External Grader", + 'selfassessment': "Self Assessment", + 'openended': "External Grader", } + class IncorrectMaxScoreError(Exception): def __init__(self, msg): self.msg = msg + class CombinedOpenEndedModule(XModule): """ This is a module that encapsulates all open ended grading (self assessment, peer assessment, etc). @@ -700,4 +702,4 @@ class CombinedOpenEndedDescriptor(XmlDescriptor, EditingDescriptor): for child in ['task']: add_child(child) - return elt \ No newline at end of file + return elt diff --git a/common/lib/xmodule/xmodule/combined_open_ended_rubric.py b/common/lib/xmodule/xmodule/combined_open_ended_rubric.py index 6d4a3eebdf2..9a213299cd6 100644 --- a/common/lib/xmodule/xmodule/combined_open_ended_rubric.py +++ b/common/lib/xmodule/xmodule/combined_open_ended_rubric.py @@ -1,12 +1,14 @@ import logging from lxml import etree -log=logging.getLogger(__name__) +log = logging.getLogger(__name__) + class RubricParsingError(Exception): def __init__(self, msg): self.msg = msg + class CombinedOpenEndedRubric(object): def __init__ (self, system, view_only = False): @@ -27,8 +29,8 @@ class CombinedOpenEndedRubric(object): success = False try: rubric_categories = self.extract_categories(rubric_xml) - html = self.system.render_template('open_ended_rubric.html', - {'categories' : rubric_categories, + html = self.system.render_template('open_ended_rubric.html', + {'categories': rubric_categories, 'has_score': self.has_score, 'view_only': self.view_only}) success = True @@ -60,8 +62,8 @@ class CombinedOpenEndedRubric(object): options: [{text: "Option 1 Name", points: 0}, {text:"Option 2 Name", points: 5}] }, { category: "Category 2 Name", - options: [{text: "Option 1 Name", points: 0}, - {text: "Option 2 Name", points: 1}, + options: [{text: "Option 1 Name", points: 0}, + {text: "Option 2 Name", points: 1}, {text: "Option 3 Name", points: 2]}] ''' @@ -77,7 +79,7 @@ class CombinedOpenEndedRubric(object): def extract_category(self, category): - ''' + ''' construct an individual category {category: "Category 1 Name", options: [{text: "Option 1 text", points: 1}, @@ -110,7 +112,7 @@ class CombinedOpenEndedRubric(object): autonumbering = True # parse options for option in optionsxml: - if option.tag != 'option': + if option.tag != 'option': raise RubricParsingError("[extract_category]: expected option tag, got {0} instead".format(option.tag)) else: pointstr = option.get("points") @@ -127,7 +129,7 @@ class CombinedOpenEndedRubric(object): cur_points = cur_points + 1 else: raise Exception("[extract_category]: missing points attribute. Cannot continue to auto-create points values after a points value is explicitly defined.") - + selected = score == points optiontext = option.text options.append({'text': option.text, 'points': points, 'selected': selected}) diff --git a/common/lib/xmodule/xmodule/conditional_module.py b/common/lib/xmodule/xmodule/conditional_module.py index e20681e614c..bcdf0f4738e 100644 --- a/common/lib/xmodule/xmodule/conditional_module.py +++ b/common/lib/xmodule/xmodule/conditional_module.py @@ -9,12 +9,13 @@ from pkg_resources import resource_string log = logging.getLogger('mitx.' + __name__) + class ConditionalModule(XModule): ''' Blocks child module from showing unless certain conditions are met. Example: - + <conditional condition="require_completed" required="tag/url_name1&tag/url_name2"> <video url_name="secret_video" /> </conditional> @@ -37,13 +38,13 @@ class ConditionalModule(XModule): def __init__(self, system, location, definition, descriptor, instance_state=None, shared_state=None, **kwargs): """ In addition to the normal XModule init, provide: - + self.condition = string describing condition required """ XModule.__init__(self, system, location, definition, descriptor, instance_state, shared_state, **kwargs) self.contents = None - self.condition = self.metadata.get('condition','') + self.condition = self.metadata.get('condition', '') #log.debug('conditional module required=%s' % self.required_modules_list) def _get_required_modules(self): @@ -56,7 +57,7 @@ class ConditionalModule(XModule): def is_condition_satisfied(self): self._get_required_modules() - if self.condition=='require_completed': + if self.condition == 'require_completed': # all required modules must be completed, as determined by # the modules .is_completed() method for module in self.required_modules: @@ -70,7 +71,7 @@ class ConditionalModule(XModule): else: log.debug('conditional module: %s IS completed' % module) return True - elif self.condition=='require_attempted': + elif self.condition == 'require_attempted': # all required modules must be attempted, as determined by # the modules .is_attempted() method for module in self.required_modules: @@ -111,9 +112,10 @@ class ConditionalModule(XModule): # for now, just deal with one child html = self.contents[0] - + return json.dumps({'html': html}) + class ConditionalDescriptor(SequenceDescriptor): module_class = ConditionalModule @@ -125,7 +127,7 @@ class ConditionalDescriptor(SequenceDescriptor): def __init__(self, *args, **kwargs): super(ConditionalDescriptor, self).__init__(*args, **kwargs) - required_module_list = [tuple(x.split('/',1)) for x in self.metadata.get('required','').split('&')] + required_module_list = [tuple(x.split('/', 1)) for x in self.metadata.get('required', '').split('&')] self.required_module_locations = [] for (tag, name) in required_module_list: loc = self.location.dict() @@ -133,9 +135,8 @@ class ConditionalDescriptor(SequenceDescriptor): loc['name'] = name self.required_module_locations.append(Location(loc)) log.debug('ConditionalDescriptor required_module_locations=%s' % self.required_module_locations) - + def get_required_module_descriptors(self): """Returns a list of XModuleDescritpor instances upon which this module depends, but are not children of this module""" return [self.system.load_item(loc) for loc in self.required_module_locations] - diff --git a/common/lib/xmodule/xmodule/contentstore/content.py b/common/lib/xmodule/xmodule/contentstore/content.py index 5b10acc0ef0..be33401bc80 100644 --- a/common/lib/xmodule/xmodule/contentstore/content.py +++ b/common/lib/xmodule/xmodule/contentstore/content.py @@ -11,15 +11,16 @@ from xmodule.modulestore import Location from .django import contentstore from PIL import Image + class StaticContent(object): def __init__(self, loc, name, content_type, data, last_modified_at=None, thumbnail_location=None, import_path=None): self.location = loc - self.name = name #a display string which can be edited, and thus not part of the location which needs to be fixed + self.name = name # a display string which can be edited, and thus not part of the location which needs to be fixed self.content_type = content_type self.data = data self.last_modified_at = last_modified_at self.thumbnail_location = Location(thumbnail_location) if thumbnail_location is not None else None - # optional information about where this file was imported from. This is needed to support import/export + # optional information about where this file was imported from. This is needed to support import/export # cycles self.import_path = import_path @@ -29,7 +30,7 @@ class StaticContent(object): @staticmethod def generate_thumbnail_name(original_name): - return ('{0}'+XASSET_THUMBNAIL_TAIL_NAME).format(os.path.splitext(original_name)[0]) + return ('{0}' + XASSET_THUMBNAIL_TAIL_NAME).format(os.path.splitext(original_name)[0]) @staticmethod def compute_location(org, course, name, revision=None, is_thumbnail=False): @@ -41,7 +42,7 @@ class StaticContent(object): def get_url_path(self): return StaticContent.get_url_path_from_location(self.location) - + @staticmethod def get_url_path_from_location(location): if location is not None: @@ -56,15 +57,15 @@ class StaticContent(object): @staticmethod def get_id_from_location(location): - return { 'tag':location.tag, 'org' : location.org, 'course' : location.course, - 'category' : location.category, 'name' : location.name, - 'revision' : location.revision} + return {'tag': location.tag, 'org': location.org, 'course': location.course, + 'category': location.category, 'name': location.name, + 'revision': location.revision} @staticmethod def get_location_from_path(path): # remove leading / character if it is there one if path.startswith('/'): path = path[1:] - + return Location(path.split('/')) @staticmethod @@ -77,7 +78,7 @@ class StaticContent(object): return StaticContent.get_url_path_from_location(loc) - + class ContentStore(object): ''' @@ -95,14 +96,14 @@ class ContentStore(object): [ - {u'displayname': u'profile.jpg', u'chunkSize': 262144, u'length': 85374, - u'uploadDate': datetime.datetime(2012, 10, 3, 5, 41, 54, 183000), u'contentType': u'image/jpeg', - u'_id': {u'category': u'asset', u'name': u'profile.jpg', u'course': u'6.002x', u'tag': u'c4x', - u'org': u'MITx', u'revision': None}, u'md5': u'36dc53519d4b735eb6beba51cd686a0e'}, + {u'displayname': u'profile.jpg', u'chunkSize': 262144, u'length': 85374, + u'uploadDate': datetime.datetime(2012, 10, 3, 5, 41, 54, 183000), u'contentType': u'image/jpeg', + u'_id': {u'category': u'asset', u'name': u'profile.jpg', u'course': u'6.002x', u'tag': u'c4x', + u'org': u'MITx', u'revision': None}, u'md5': u'36dc53519d4b735eb6beba51cd686a0e'}, - {u'displayname': u'profile.thumbnail.jpg', u'chunkSize': 262144, u'length': 4073, - u'uploadDate': datetime.datetime(2012, 10, 3, 5, 41, 54, 196000), u'contentType': u'image/jpeg', - u'_id': {u'category': u'asset', u'name': u'profile.thumbnail.jpg', u'course': u'6.002x', u'tag': u'c4x', + {u'displayname': u'profile.thumbnail.jpg', u'chunkSize': 262144, u'length': 4073, + u'uploadDate': datetime.datetime(2012, 10, 3, 5, 41, 54, 196000), u'contentType': u'image/jpeg', + u'_id': {u'category': u'asset', u'name': u'profile.thumbnail.jpg', u'course': u'6.002x', u'tag': u'c4x', u'org': u'MITx', u'revision': None}, u'md5': u'ff1532598830e3feac91c2449eaa60d6'}, .... @@ -117,7 +118,7 @@ class ContentStore(object): thumbnail_name = StaticContent.generate_thumbnail_name(content.location.name) thumbnail_file_location = StaticContent.compute_location(content.location.org, content.location.course, - thumbnail_name, is_thumbnail = True) + thumbnail_name, is_thumbnail=True) # if we're uploading an image, then let's generate a thumbnail so that we can # serve it up when needed without having to rescale on the fly @@ -129,7 +130,7 @@ class ContentStore(object): # @todo: move the thumbnail size to a configuration setting?!? im = Image.open(StringIO.StringIO(content.data)) - # I've seen some exceptions from the PIL library when trying to save palletted + # I've seen some exceptions from the PIL library when trying to save palletted # PNG files to JPEG. Per the google-universe, they suggest converting to RGB first. im = im.convert('RGB') size = 128, 128 @@ -139,7 +140,7 @@ class ContentStore(object): thumbnail_file.seek(0) # store this thumbnail as any other piece of content - thumbnail_content = StaticContent(thumbnail_file_location, thumbnail_name, + thumbnail_content = StaticContent(thumbnail_file_location, thumbnail_name, 'image/jpeg', thumbnail_file) contentstore().save(thumbnail_content) @@ -149,7 +150,3 @@ class ContentStore(object): logging.exception("Failed to generate thumbnail for {0}. Exception: {1}".format(content.location, str(e))) return thumbnail_content, thumbnail_file_location - - - - diff --git a/common/lib/xmodule/xmodule/contentstore/django.py b/common/lib/xmodule/xmodule/contentstore/django.py index d8b3084135c..ec0397a3489 100644 --- a/common/lib/xmodule/xmodule/contentstore/django.py +++ b/common/lib/xmodule/xmodule/contentstore/django.py @@ -6,6 +6,7 @@ from django.conf import settings _CONTENTSTORE = None + def load_function(path): """ Load a function by name. diff --git a/common/lib/xmodule/xmodule/contentstore/mongo.py b/common/lib/xmodule/xmodule/contentstore/mongo.py index 01f189a9e4b..68cc6d73d32 100644 --- a/common/lib/xmodule/xmodule/contentstore/mongo.py +++ b/common/lib/xmodule/xmodule/contentstore/mongo.py @@ -17,14 +17,14 @@ import os class MongoContentStore(ContentStore): def __init__(self, host, db, port=27017, user=None, password=None, **kwargs): - logging.debug( 'Using MongoDB for static content serving at host={0} db={1}'.format(host,db)) + logging.debug('Using MongoDB for static content serving at host={0} db={1}'.format(host, db)) _db = Connection(host=host, port=port, **kwargs)[db] if user is not None and password is not None: _db.authenticate(user, password) self.fs = gridfs.GridFS(_db) - self.fs_files = _db["fs.files"] # the underlying collection GridFS uses + self.fs_files = _db["fs.files"] # the underlying collection GridFS uses def save(self, content): @@ -33,24 +33,24 @@ class MongoContentStore(ContentStore): # Seems like with the GridFS we can't update existing ID's we have to do a delete/add pair self.delete(id) - with self.fs.new_file(_id = id, filename=content.get_url_path(), content_type=content.content_type, + with self.fs.new_file(_id=id, filename=content.get_url_path(), content_type=content.content_type, displayname=content.name, thumbnail_location=content.thumbnail_location, import_path=content.import_path) as fp: fp.write(content.data) - + return content - + def delete(self, id): - if self.fs.exists({"_id" : id}): + if self.fs.exists({"_id": id}): self.fs.delete(id) def find(self, location): id = StaticContent.get_id_from_location(location) try: with self.fs.get(id) as fp: - return StaticContent(location, fp.displayname, fp.content_type, fp.read(), - fp.uploadDate, thumbnail_location = fp.thumbnail_location if hasattr(fp, 'thumbnail_location') else None, - import_path = fp.import_path if hasattr(fp, 'import_path') else None) + return StaticContent(location, fp.displayname, fp.content_type, fp.read(), + fp.uploadDate, thumbnail_location=fp.thumbnail_location if hasattr(fp, 'thumbnail_location') else None, + import_path=fp.import_path if hasattr(fp, 'import_path') else None) except NoFile: raise NotFoundError() @@ -76,25 +76,25 @@ class MongoContentStore(ContentStore): self.export(asset_location, output_directory) def get_all_content_thumbnails_for_course(self, location): - return self._get_all_content_for_course(location, get_thumbnails = True) + return self._get_all_content_for_course(location, get_thumbnails=True) def get_all_content_for_course(self, location): - return self._get_all_content_for_course(location, get_thumbnails = False) + return self._get_all_content_for_course(location, get_thumbnails=False) - def _get_all_content_for_course(self, location, get_thumbnails = False): + def _get_all_content_for_course(self, location, get_thumbnails=False): ''' Returns a list of all static assets for a course. The return format is a list of dictionary elements. Example: [ - {u'displayname': u'profile.jpg', u'chunkSize': 262144, u'length': 85374, - u'uploadDate': datetime.datetime(2012, 10, 3, 5, 41, 54, 183000), u'contentType': u'image/jpeg', - u'_id': {u'category': u'asset', u'name': u'profile.jpg', u'course': u'6.002x', u'tag': u'c4x', - u'org': u'MITx', u'revision': None}, u'md5': u'36dc53519d4b735eb6beba51cd686a0e'}, + {u'displayname': u'profile.jpg', u'chunkSize': 262144, u'length': 85374, + u'uploadDate': datetime.datetime(2012, 10, 3, 5, 41, 54, 183000), u'contentType': u'image/jpeg', + u'_id': {u'category': u'asset', u'name': u'profile.jpg', u'course': u'6.002x', u'tag': u'c4x', + u'org': u'MITx', u'revision': None}, u'md5': u'36dc53519d4b735eb6beba51cd686a0e'}, - {u'displayname': u'profile.thumbnail.jpg', u'chunkSize': 262144, u'length': 4073, - u'uploadDate': datetime.datetime(2012, 10, 3, 5, 41, 54, 196000), u'contentType': u'image/jpeg', - u'_id': {u'category': u'asset', u'name': u'profile.thumbnail.jpg', u'course': u'6.002x', u'tag': u'c4x', + {u'displayname': u'profile.thumbnail.jpg', u'chunkSize': 262144, u'length': 4073, + u'uploadDate': datetime.datetime(2012, 10, 3, 5, 41, 54, 196000), u'contentType': u'image/jpeg', + u'_id': {u'category': u'asset', u'name': u'profile.thumbnail.jpg', u'course': u'6.002x', u'tag': u'c4x', u'org': u'MITx', u'revision': None}, u'md5': u'ff1532598830e3feac91c2449eaa60d6'}, .... @@ -102,10 +102,7 @@ class MongoContentStore(ContentStore): ] ''' course_filter = Location(XASSET_LOCATION_TAG, category="asset" if not get_thumbnails else "thumbnail", - course=location.course,org=location.org) + course=location.course, org=location.org) # 'borrow' the function 'location_to_query' from the Mongo modulestore implementation items = self.fs_files.find(location_to_query(course_filter)) return list(items) - - - diff --git a/common/lib/xmodule/xmodule/course_module.py b/common/lib/xmodule/xmodule/course_module.py index 6e3e2cfa39a..750c8615a03 100644 --- a/common/lib/xmodule/xmodule/course_module.py +++ b/common/lib/xmodule/xmodule/course_module.py @@ -147,37 +147,37 @@ class CourseDescriptor(SequenceDescriptor): """ Return a dict which is a copy of the default grading policy """ - default = {"GRADER" : [ + default = {"GRADER": [ { - "type" : "Homework", - "min_count" : 12, - "drop_count" : 2, - "short_label" : "HW", - "weight" : 0.15 + "type": "Homework", + "min_count": 12, + "drop_count": 2, + "short_label": "HW", + "weight": 0.15 }, { - "type" : "Lab", - "min_count" : 12, - "drop_count" : 2, - "weight" : 0.15 + "type": "Lab", + "min_count": 12, + "drop_count": 2, + "weight": 0.15 }, { - "type" : "Midterm Exam", - "short_label" : "Midterm", - "min_count" : 1, - "drop_count" : 0, - "weight" : 0.3 + "type": "Midterm Exam", + "short_label": "Midterm", + "min_count": 1, + "drop_count": 0, + "weight": 0.3 }, { - "type" : "Final Exam", - "short_label" : "Final", - "min_count" : 1, - "drop_count" : 0, - "weight" : 0.4 + "type": "Final Exam", + "short_label": "Final", + "min_count": 1, + "drop_count": 0, + "weight": 0.4 } ], - "GRADE_CUTOFFS" : { - "Pass" : 0.5 + "GRADE_CUTOFFS": { + "Pass": 0.5 }} return copy.deepcopy(default) @@ -230,8 +230,8 @@ class CourseDescriptor(SequenceDescriptor): # bleh, have to parse the XML here to just pull out the url_name attribute # I don't think it's stored anywhere in the instance. - course_file = StringIO(xml_data.encode('ascii','ignore')) - xml_obj = etree.parse(course_file,parser=edx_xml_parser).getroot() + course_file = StringIO(xml_data.encode('ascii', 'ignore')) + xml_obj = etree.parse(course_file, parser=edx_xml_parser).getroot() policy_dir = None url_name = xml_obj.get('url_name', xml_obj.get('slug')) @@ -329,7 +329,7 @@ class CourseDescriptor(SequenceDescriptor): def raw_grader(self, value): # NOTE WELL: this change will not update the processed graders. If we need that, this needs to call grader_from_conf self._grading_policy['RAW_GRADER'] = value - self.definition['data'].setdefault('grading_policy',{})['GRADER'] = value + self.definition['data'].setdefault('grading_policy', {})['GRADER'] = value @property def grade_cutoffs(self): @@ -338,7 +338,7 @@ class CourseDescriptor(SequenceDescriptor): @grade_cutoffs.setter def grade_cutoffs(self, value): self._grading_policy['GRADE_CUTOFFS'] = value - self.definition['data'].setdefault('grading_policy',{})['GRADE_CUTOFFS'] = value + self.definition['data'].setdefault('grading_policy', {})['GRADE_CUTOFFS'] = value @property @@ -377,7 +377,7 @@ class CourseDescriptor(SequenceDescriptor): Return list of topic ids defined in course policy. """ topics = self.metadata.get("discussion_topics", {}) - return [d["id"] for d in topics.values()] + return [d["id"] for d in topics.values()] @property @@ -436,10 +436,10 @@ class CourseDescriptor(SequenceDescriptor): scale = 300.0 # about a year if announcement: days = (now - announcement).days - score = -exp(-days/scale) + score = -exp(-days / scale) else: days = (now - start).days - score = exp(days/scale) + score = exp(days / scale) return score def _sorting_dates(self): @@ -501,16 +501,16 @@ class CourseDescriptor(SequenceDescriptor): xmoduledescriptors.append(s) # The xmoduledescriptors included here are only the ones that have scores. - section_description = { 'section_descriptor' : s, 'xmoduledescriptors' : filter(lambda child: child.has_score, xmoduledescriptors) } + section_description = {'section_descriptor': s, 'xmoduledescriptors': filter(lambda child: child.has_score, xmoduledescriptors)} section_format = s.metadata.get('format', "") - graded_sections[ section_format ] = graded_sections.get( section_format, [] ) + [section_description] + graded_sections[section_format] = graded_sections.get(section_format, []) + [section_description] all_descriptors.extend(xmoduledescriptors) all_descriptors.append(s) - return { 'graded_sections' : graded_sections, - 'all_descriptors' : all_descriptors,} + return {'graded_sections': graded_sections, + 'all_descriptors': all_descriptors, } @staticmethod @@ -636,7 +636,7 @@ class CourseDescriptor(SequenceDescriptor): # *end* of the same day, not the same time. It's going to be used as the # end of the exam overall, so we don't want the exam to disappear too soon. # It's also used optionally as the registration end date, so time matters there too. - self.last_eligible_appointment_date = self._try_parse_time('Last_Eligible_Appointment_Date') # or self.first_eligible_appointment_date + self.last_eligible_appointment_date = self._try_parse_time('Last_Eligible_Appointment_Date') # or self.first_eligible_appointment_date if self.last_eligible_appointment_date is None: raise ValueError("Last appointment date must be specified") self.registration_start_date = self._try_parse_time('Registration_Start_Date') or time.gmtime(0) @@ -715,4 +715,3 @@ class CourseDescriptor(SequenceDescriptor): @property def org(self): return self.location.org - diff --git a/common/lib/xmodule/xmodule/discussion_module.py b/common/lib/xmodule/xmodule/discussion_module.py index 57d7780d95b..6ddfcbe6c01 100644 --- a/common/lib/xmodule/xmodule/discussion_module.py +++ b/common/lib/xmodule/xmodule/discussion_module.py @@ -6,6 +6,7 @@ from xmodule.raw_module import RawDescriptor import json + class DiscussionModule(XModule): js = {'coffee': [resource_string(__name__, 'js/src/time.coffee'), @@ -30,6 +31,7 @@ class DiscussionModule(XModule): self.title = xml_data.attrib['for'] self.discussion_category = xml_data.attrib['discussion_category'] + class DiscussionDescriptor(RawDescriptor): module_class = DiscussionModule template_dir_name = "discussion" diff --git a/common/lib/xmodule/xmodule/errortracker.py b/common/lib/xmodule/xmodule/errortracker.py index 6accc8b8a70..80e6d288f8f 100644 --- a/common/lib/xmodule/xmodule/errortracker.py +++ b/common/lib/xmodule/xmodule/errortracker.py @@ -8,12 +8,14 @@ log = logging.getLogger(__name__) ErrorLog = namedtuple('ErrorLog', 'tracker errors') + def exc_info_to_str(exc_info): """Given some exception info, convert it into a string using the traceback.format_exception() function. """ return ''.join(traceback.format_exception(*exc_info)) + def in_exception_handler(): '''Is there an active exception?''' return sys.exc_info() != (None, None, None) @@ -44,6 +46,7 @@ def make_error_tracker(): return ErrorLog(error_tracker, errors) + def null_error_tracker(msg): '''A dummy error tracker that just ignores the messages''' pass diff --git a/common/lib/xmodule/xmodule/graders.py b/common/lib/xmodule/xmodule/graders.py index 3e6d61eb004..35318f4f1ed 100644 --- a/common/lib/xmodule/xmodule/graders.py +++ b/common/lib/xmodule/xmodule/graders.py @@ -49,6 +49,7 @@ def invalid_args(func, argdict): if keywords: return set() # All accepted return set(argdict) - set(args) + def grader_from_conf(conf): """ This creates a CourseGrader from a configuration (such as in course_settings.py). @@ -80,7 +81,7 @@ def grader_from_conf(conf): subgrader_class = SingleSectionGrader else: raise ValueError("Configuration has no appropriate grader class.") - + bad_args = invalid_args(subgrader_class.__init__, subgraderconf) # See note above concerning 'name'. if bad_args.issuperset({name}): @@ -90,7 +91,7 @@ def grader_from_conf(conf): log.warning("Invalid arguments for a subgrader: %s", bad_args) for key in bad_args: del subgraderconf[key] - + subgrader = subgrader_class(**subgraderconf) subgraders.append((subgrader, subgrader.category, weight)) @@ -210,13 +211,13 @@ class SingleSectionGrader(CourseGrader): break if foundScore or generate_random_scores: - if generate_random_scores: # for debugging! - earned = random.randint(2,15) + if generate_random_scores: # for debugging! + earned = random.randint(2, 15) possible = random.randint(earned, 15) - else: # We found the score + else: # We found the score earned = foundScore.earned possible = foundScore.possible - + percent = earned / float(possible) detail = "{name} - {percent:.0%} ({earned:.3n}/{possible:.3n})".format(name=self.name, percent=percent, @@ -245,7 +246,7 @@ class AssignmentFormatGrader(CourseGrader): min_count defines how many assignments are expected throughout the course. Placeholder scores (of 0) will be inserted if the number of matching sections in the course is < min_count. If there number of matching sections in the course is > min_count, min_count will be ignored. - + show_only_average is to suppress the display of each assignment in this grader and instead only show the total score of this grader in the breakdown. @@ -257,7 +258,7 @@ class AssignmentFormatGrader(CourseGrader): short_label is similar to section_type, but shorter. For example, for Homework it would be "HW". - + starting_index is the first number that will appear. For example, starting_index=3 and min_count = 2 would produce the labels "Assignment 3", "Assignment 4" @@ -296,16 +297,16 @@ class AssignmentFormatGrader(CourseGrader): breakdown = [] for i in range(max(self.min_count, len(scores))): if i < len(scores) or generate_random_scores: - if generate_random_scores: # for debugging! - earned = random.randint(2,15) - possible = random.randint(earned, 15) + if generate_random_scores: # for debugging! + earned = random.randint(2, 15) + possible = random.randint(earned, 15) section_name = "Generated" - + else: earned = scores[i].earned possible = scores[i].possible section_name = scores[i].section - + percentage = earned / float(possible) summary = "{section_type} {index} - {name} - {percent:.0%} ({earned:.3n}/{possible:.3n})".format(index=i + self.starting_index, section_type=self.section_type, @@ -318,7 +319,7 @@ class AssignmentFormatGrader(CourseGrader): summary = "{section_type} {index} Unreleased - 0% (?/?)".format(index=i + self.starting_index, section_type=self.section_type) short_label = "{short_label} {index:02d}".format(index=i + self.starting_index, short_label=self.short_label) - + breakdown.append({'percent': percentage, 'label': short_label, 'detail': summary, 'category': self.category}) total_percent, dropped_indices = totalWithDrops(breakdown, self.drop_count) @@ -328,13 +329,13 @@ class AssignmentFormatGrader(CourseGrader): total_detail = "{section_type} Average = {percent:.0%}".format(percent=total_percent, section_type=self.section_type) total_label = "{short_label} Avg".format(short_label=self.short_label) - + if self.show_only_average: breakdown = [] - + if not self.hide_average: breakdown.append({'percent': total_percent, 'label': total_label, 'detail': total_detail, 'category': self.category, 'prominent': True}) - + return {'percent': total_percent, 'section_breakdown': breakdown, #No grade_breakdown here diff --git a/common/lib/xmodule/xmodule/grading_service_module.py b/common/lib/xmodule/xmodule/grading_service_module.py index 7c18731f533..a442f39f340 100644 --- a/common/lib/xmodule/xmodule/grading_service_module.py +++ b/common/lib/xmodule/xmodule/grading_service_module.py @@ -10,9 +10,11 @@ from lxml import etree log = logging.getLogger(__name__) + class GradingServiceError(Exception): pass + class GradingService(object): """ Interface to staff grading backend. @@ -35,7 +37,7 @@ class GradingService(object): """ response = self.session.post(self.login_url, {'username': self.username, - 'password': self.password,}) + 'password': self.password, }) response.raise_for_status() @@ -124,4 +126,4 @@ class GradingService(object): except ValueError: log.exception("Error parsing response: {0}".format(response)) return {'success': False, - 'error': "Error displaying submission"} \ No newline at end of file + 'error': "Error displaying submission"} diff --git a/common/lib/xmodule/xmodule/html_checker.py b/common/lib/xmodule/xmodule/html_checker.py index 5e6b417d284..b30e5163a24 100644 --- a/common/lib/xmodule/xmodule/html_checker.py +++ b/common/lib/xmodule/xmodule/html_checker.py @@ -1,5 +1,6 @@ from lxml import etree + def check_html(html): ''' Check whether the passed in html string can be parsed by lxml. diff --git a/common/lib/xmodule/xmodule/html_module.py b/common/lib/xmodule/xmodule/html_module.py index 612e78ce35e..af1ce0ad80a 100644 --- a/common/lib/xmodule/xmodule/html_module.py +++ b/common/lib/xmodule/xmodule/html_module.py @@ -133,7 +133,7 @@ class HtmlDescriptor(XmlDescriptor, EditingDescriptor): # TODO (ichuang): remove this after migration # for Fall 2012 LMS migration: keep filename (and unmangled filename) - definition['filename'] = [ filepath, filename ] + definition['filename'] = [filepath, filename] return definition @@ -180,6 +180,7 @@ class AboutDescriptor(HtmlDescriptor): """ template_dir_name = "about" + class StaticTabDescriptor(HtmlDescriptor): """ These pieces of course content are treated as HtmlModules but we need to overload where the templates are located @@ -187,6 +188,7 @@ class StaticTabDescriptor(HtmlDescriptor): """ template_dir_name = "statictab" + class CourseInfoDescriptor(HtmlDescriptor): """ These pieces of course content are treated as HtmlModules but we need to overload where the templates are located diff --git a/common/lib/xmodule/xmodule/mako_module.py b/common/lib/xmodule/xmodule/mako_module.py index f5f2fae23b8..dab5d5e85b6 100644 --- a/common/lib/xmodule/xmodule/mako_module.py +++ b/common/lib/xmodule/xmodule/mako_module.py @@ -34,7 +34,7 @@ class MakoModuleDescriptor(XModuleDescriptor): """ return {'module': self, 'metadata': self.metadata, - 'editable_metadata_fields' : self.editable_metadata_fields + 'editable_metadata_fields': self.editable_metadata_fields } def get_html(self): @@ -46,4 +46,3 @@ class MakoModuleDescriptor(XModuleDescriptor): def editable_metadata_fields(self): subset = [name for name in self.metadata.keys() if name not in self.system_metadata_fields] return subset - diff --git a/common/lib/xmodule/xmodule/modulestore/__init__.py b/common/lib/xmodule/xmodule/modulestore/__init__.py index 38915adcb43..a9df6c35041 100644 --- a/common/lib/xmodule/xmodule/modulestore/__init__.py +++ b/common/lib/xmodule/xmodule/modulestore/__init__.py @@ -365,7 +365,7 @@ class ModuleStore(object): raise NotImplementedError def get_parent_locations(self, location, course_id): - '''Find all locations that are the parents of this location in this + '''Find all locations that are the parents of this location in this course. Needed for path_to_location(). returns an iterable of things that can be passed to Location. diff --git a/common/lib/xmodule/xmodule/modulestore/draft.py b/common/lib/xmodule/xmodule/modulestore/draft.py index ef2a848cac8..81f4da2780e 100644 --- a/common/lib/xmodule/xmodule/modulestore/draft.py +++ b/common/lib/xmodule/xmodule/modulestore/draft.py @@ -67,7 +67,7 @@ class DraftModuleStore(ModuleStoreBase): TODO (vshnayder): this may want to live outside the modulestore eventually """ - # cdodge: we're forcing depth=0 here as the Draft store is not handling caching well + # cdodge: we're forcing depth=0 here as the Draft store is not handling caching well try: return wrap_draft(super(DraftModuleStore, self).get_instance(course_id, as_draft(location), depth=0)) except ItemNotFoundError: diff --git a/common/lib/xmodule/xmodule/modulestore/mongo.py b/common/lib/xmodule/xmodule/modulestore/mongo.py index 3b928766731..f4db62ac316 100644 --- a/common/lib/xmodule/xmodule/modulestore/mongo.py +++ b/common/lib/xmodule/xmodule/modulestore/mongo.py @@ -304,7 +304,7 @@ class MongoModuleStore(ModuleStoreBase): if location.category == 'static_tab': course = self.get_course_for_item(item.location) existing_tabs = course.tabs or [] - existing_tabs.append({'type':'static_tab', 'name' : item.metadata.get('display_name'), 'url_slug' : item.location.name}) + existing_tabs.append({'type': 'static_tab', 'name': item.metadata.get('display_name'), 'url_slug': item.location.name}) course.tabs = existing_tabs self.update_metadata(course.location, course.metadata) @@ -423,7 +423,7 @@ class MongoModuleStore(ModuleStoreBase): def get_parent_locations(self, location, course_id): - '''Find all locations that are the parents of this location in this + '''Find all locations that are the parents of this location in this course. Needed for path_to_location(). ''' location = Location.ensure_fully_specified(location) diff --git a/common/lib/xmodule/xmodule/modulestore/search.py b/common/lib/xmodule/xmodule/modulestore/search.py index e53df84bb66..b56b6125928 100644 --- a/common/lib/xmodule/xmodule/modulestore/search.py +++ b/common/lib/xmodule/xmodule/modulestore/search.py @@ -104,14 +104,14 @@ def path_to_location(modulestore, course_id, location): # module nested in more than one positional module will work. if n > 3: position_list = [] - for path_index in range(2, n-1): + for path_index in range(2, n - 1): category = path[path_index].category if category == 'sequential' or category == 'videosequence': section_desc = modulestore.get_instance(course_id, path[path_index]) child_locs = [c.location for c in section_desc.get_children()] # positions are 1-indexed, and should be strings to be consistent with # url parsing. - position_list.append(str(child_locs.index(path[path_index+1]) + 1)) + position_list.append(str(child_locs.index(path[path_index + 1]) + 1)) position = "_".join(position_list) return (course_id, chapter, section, position) diff --git a/common/lib/xmodule/xmodule/modulestore/store_utilities.py b/common/lib/xmodule/xmodule/modulestore/store_utilities.py index af346dbb7e3..192b012befb 100644 --- a/common/lib/xmodule/xmodule/modulestore/store_utilities.py +++ b/common/lib/xmodule/xmodule/modulestore/store_utilities.py @@ -3,6 +3,7 @@ from xmodule.contentstore.content import StaticContent from xmodule.modulestore import Location from xmodule.modulestore.mongo import MongoModuleStore + def clone_course(modulestore, contentstore, source_location, dest_location, delete_original=False): # first check to see if the modulestore is Mongo backed if not isinstance(modulestore, MongoModuleStore): @@ -13,7 +14,7 @@ def clone_course(modulestore, contentstore, source_location, dest_location, dele if not modulestore.has_item(dest_location): raise Exception("An empty course at {0} must have already been created. Aborting...".format(dest_location)) - # verify that the dest_location really is an empty course, which means only one + # verify that the dest_location really is an empty course, which means only one dest_modules = modulestore.get_items([dest_location.tag, dest_location.org, dest_location.course, None, None, None]) if len(dest_modules) != 1: @@ -31,12 +32,12 @@ def clone_course(modulestore, contentstore, source_location, dest_location, dele original_loc = Location(module.location) if original_loc.category != 'course': - module.location = module.location._replace(tag = dest_location.tag, org = dest_location.org, - course = dest_location.course) + module.location = module.location._replace(tag=dest_location.tag, org=dest_location.org, + course=dest_location.course) else: # on the course module we also have to update the module name - module.location = module.location._replace(tag = dest_location.tag, org = dest_location.org, - course = dest_location.course, name=dest_location.name) + module.location = module.location._replace(tag=dest_location.tag, org=dest_location.org, + course=dest_location.course, name=dest_location.name) print "Cloning module {0} to {1}....".format(original_loc, module.location) @@ -48,8 +49,8 @@ def clone_course(modulestore, contentstore, source_location, dest_location, dele new_children = [] for child_loc_url in module.definition['children']: child_loc = Location(child_loc_url) - child_loc = child_loc._replace(tag = dest_location.tag, org = dest_location.org, - course = dest_location.course) + child_loc = child_loc._replace(tag=dest_location.tag, org=dest_location.org, + course=dest_location.course) new_children = new_children + [child_loc.url()] modulestore.update_children(module.location, new_children) @@ -63,8 +64,8 @@ def clone_course(modulestore, contentstore, source_location, dest_location, dele for thumb in thumbs: thumb_loc = Location(thumb["_id"]) content = contentstore.find(thumb_loc) - content.location = content.location._replace(org = dest_location.org, - course = dest_location.course) + content.location = content.location._replace(org=dest_location.org, + course=dest_location.course) print "Cloning thumbnail {0} to {1}".format(thumb_loc, content.location) @@ -76,13 +77,13 @@ def clone_course(modulestore, contentstore, source_location, dest_location, dele for asset in assets: asset_loc = Location(asset["_id"]) content = contentstore.find(asset_loc) - content.location = content.location._replace(org = dest_location.org, - course = dest_location.course) + content.location = content.location._replace(org=dest_location.org, + course=dest_location.course) # be sure to update the pointer to the thumbnail if content.thumbnail_location is not None: - content.thumbnail_location = content.thumbnail_location._replace(org = dest_location.org, - course = dest_location.course) + content.thumbnail_location = content.thumbnail_location._replace(org=dest_location.org, + course=dest_location.course) print "Cloning asset {0} to {1}".format(asset_loc, content.location) @@ -90,6 +91,7 @@ def clone_course(modulestore, contentstore, source_location, dest_location, dele return True + def delete_course(modulestore, contentstore, source_location): # first check to see if the modulestore is Mongo backed if not isinstance(modulestore, MongoModuleStore): @@ -119,7 +121,7 @@ def delete_course(modulestore, contentstore, source_location): modules = modulestore.get_items([source_location.tag, source_location.org, source_location.course, None, None, None]) for module in modules: - if module.category != 'course': # save deleting the course module for last + if module.category != 'course': # save deleting the course module for last print "Deleting {0}...".format(module.location) modulestore.delete_item(module.location) @@ -127,4 +129,4 @@ def delete_course(modulestore, contentstore, source_location): print "Deleting {0}...".format(source_location) modulestore.delete_item(source_location) - return True \ No newline at end of file + return True diff --git a/common/lib/xmodule/xmodule/modulestore/tests/__init__.py b/common/lib/xmodule/xmodule/modulestore/tests/__init__.py index 126f0136e2f..2759f2540c3 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/__init__.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/__init__.py @@ -8,5 +8,3 @@ for i in range(5): TEST_DIR = TEST_DIR / 'test' DATA_DIR = TEST_DIR / 'data' - - diff --git a/common/lib/xmodule/xmodule/modulestore/tests/factories.py b/common/lib/xmodule/xmodule/modulestore/tests/factories.py index b4264b30c94..1259da26907 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/factories.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/factories.py @@ -5,12 +5,15 @@ from xmodule.modulestore import Location from xmodule.modulestore.django import modulestore from xmodule.timeparse import stringify_time -def XMODULE_COURSE_CREATION(class_to_create, **kwargs): + +def XMODULE_COURSE_CREATION(class_to_create, **kwargs): return XModuleCourseFactory._create(class_to_create, **kwargs) + def XMODULE_ITEM_CREATION(class_to_create, **kwargs): return XModuleItemFactory._create(class_to_create, **kwargs) + class XModuleCourseFactory(Factory): """ Factory for XModule courses. @@ -27,7 +30,7 @@ class XModuleCourseFactory(Factory): org = kwargs.get('org') number = kwargs.get('number') display_name = kwargs.get('display_name') - location = Location('i4x', org, number, + location = Location('i4x', org, number, 'course', Location.clean(display_name)) store = modulestore('direct') @@ -42,20 +45,22 @@ class XModuleCourseFactory(Factory): new_course.metadata['data_dir'] = uuid4().hex new_course.metadata['start'] = stringify_time(gmtime()) - new_course.tabs = [{"type": "courseware"}, + new_course.tabs = [{"type": "courseware"}, {"type": "course_info", "name": "Course Info"}, {"type": "discussion", "name": "Discussion"}, {"type": "wiki", "name": "Wiki"}, {"type": "progress", "name": "Progress"}] # Update the data in the mongo datastore - store.update_metadata(new_course.location.url(), new_course.own_metadata) + store.update_metadata(new_course.location.url(), new_course.own_metadata) return new_course + class Course: pass + class CourseFactory(XModuleCourseFactory): FACTORY_FOR = Course @@ -64,6 +69,7 @@ class CourseFactory(XModuleCourseFactory): number = '999' display_name = 'Robot Super Course' + class XModuleItemFactory(Factory): """ Factory for XModule items. @@ -80,7 +86,7 @@ class XModuleItemFactory(Factory): """ DETACHED_CATEGORIES = ['about', 'static_tab', 'course_info'] - + parent_location = Location(kwargs.get('parent_location')) template = Location(kwargs.get('template')) display_name = kwargs.get('display_name') @@ -107,9 +113,11 @@ class XModuleItemFactory(Factory): return new_item + class Item: pass + class ItemFactory(XModuleItemFactory): FACTORY_FOR = Item diff --git a/common/lib/xmodule/xmodule/modulestore/tests/test_location.py b/common/lib/xmodule/xmodule/modulestore/tests/test_location.py index afe5e47d106..0772951884c 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/test_location.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/test_location.py @@ -61,6 +61,7 @@ invalid = ("foo", ["foo"], ["foo", "bar"], invalid_dict, invalid_dict2) + def test_is_valid(): for v in valid: assert_equals(Location.is_valid(v), True) @@ -68,6 +69,7 @@ def test_is_valid(): for v in invalid: assert_equals(Location.is_valid(v), False) + def test_dict(): assert_equals("tag://org/course/category/name", Location(input_dict).url()) assert_equals(dict(revision=None, **input_dict), Location(input_dict).dict()) @@ -76,6 +78,7 @@ def test_dict(): assert_equals("tag://org/course/category/name@revision", Location(input_dict).url()) assert_equals(input_dict, Location(input_dict).dict()) + def test_list(): assert_equals("tag://org/course/category/name", Location(input_list).url()) assert_equals(input_list + [None], Location(input_list).list()) @@ -115,17 +118,18 @@ def test_equality(): ) # All the cleaning functions should do the same thing with these -general_pairs = [ ('',''), +general_pairs = [('', ''), (' ', '_'), ('abc,', 'abc_'), ('ab fg!@//\\aj', 'ab_fg_aj'), (u"ab\xA9", "ab_"), # no unicode allowed for now ] + def test_clean(): pairs = general_pairs + [ ('a:b', 'a_b'), # no colons in non-name components - ('a-b', 'a-b'), # dashes ok + ('a-b', 'a-b'), # dashes ok ('a.b', 'a.b'), # dot ok ] for input, output in pairs: diff --git a/common/lib/xmodule/xmodule/modulestore/tests/test_modulestore.py b/common/lib/xmodule/xmodule/modulestore/tests/test_modulestore.py index 64816581cea..94ea6229077 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/test_modulestore.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/test_modulestore.py @@ -3,6 +3,7 @@ from nose.tools import assert_equals, assert_raises, assert_not_equals, with_set from xmodule.modulestore.exceptions import ItemNotFoundError, NoPathToItem from xmodule.modulestore.search import path_to_location + def check_path_to_location(modulestore): '''Make sure that path_to_location works: should be passed a modulestore with the toy and simple courses loaded.''' @@ -22,4 +23,3 @@ def check_path_to_location(modulestore): ) for location in not_found: assert_raises(ItemNotFoundError, path_to_location, modulestore, course_id, location) - diff --git a/common/lib/xmodule/xmodule/modulestore/tests/test_mongo.py b/common/lib/xmodule/xmodule/modulestore/tests/test_mongo.py index 4c593e391e7..6f6f47ba853 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/test_mongo.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/test_mongo.py @@ -102,4 +102,3 @@ class TestMongoModuleStore(object): def test_path_to_location(self): '''Make sure that path_to_location works''' check_path_to_location(self.store) - diff --git a/common/lib/xmodule/xmodule/modulestore/tests/test_xml.py b/common/lib/xmodule/xmodule/modulestore/tests/test_xml.py index c4446bebb5d..321d98967b3 100644 --- a/common/lib/xmodule/xmodule/modulestore/tests/test_xml.py +++ b/common/lib/xmodule/xmodule/modulestore/tests/test_xml.py @@ -5,6 +5,7 @@ from xmodule.modulestore.xml_importer import import_from_xml from .test_modulestore import check_path_to_location from . import DATA_DIR + class TestXMLModuleStore(object): def test_path_to_location(self): """Make sure that path_to_location works properly""" @@ -12,5 +13,5 @@ class TestXMLModuleStore(object): print "Starting import" modulestore = XMLModuleStore(DATA_DIR, course_dirs=['toy', 'simple']) print "finished import" - + check_path_to_location(modulestore) diff --git a/common/lib/xmodule/xmodule/modulestore/xml.py b/common/lib/xmodule/xmodule/modulestore/xml.py index 17d6f049324..8446162f265 100644 --- a/common/lib/xmodule/xmodule/modulestore/xml.py +++ b/common/lib/xmodule/xmodule/modulestore/xml.py @@ -363,7 +363,7 @@ class XMLModuleStore(ModuleStoreBase): # been imported into the cms from xml course_file = StringIO(clean_out_mako_templating(course_file.read())) - course_data = etree.parse(course_file,parser=edx_xml_parser).getroot() + course_data = etree.parse(course_file, parser=edx_xml_parser).getroot() org = course_data.get('org') @@ -437,7 +437,7 @@ class XMLModuleStore(ModuleStoreBase): self.load_extra_content(system, course_descriptor, 'course_info', self.data_dir / course_dir / 'info', course_dir, url_name) # now import all static tabs which are expected to be stored in - # in <content_dir>/tabs or <content_dir>/tabs/<url_name> + # in <content_dir>/tabs or <content_dir>/tabs/<url_name> self.load_extra_content(system, course_descriptor, 'static_tab', self.data_dir / course_dir / 'tabs', course_dir, url_name) self.load_extra_content(system, course_descriptor, 'custom_tag_template', self.data_dir / course_dir / 'custom_tags', course_dir, url_name) @@ -454,12 +454,12 @@ class XMLModuleStore(ModuleStoreBase): # then look in a override folder based on the course run if os.path.isdir(base_dir / url_name): - self._load_extra_content(system, course_descriptor, category, base_dir / url_name, course_dir) + self._load_extra_content(system, course_descriptor, category, base_dir / url_name, course_dir) def _load_extra_content(self, system, course_descriptor, category, path, course_dir): - for filepath in glob.glob(path/ '*'): + for filepath in glob.glob(path / '*'): if not os.path.isdir(filepath): with open(filepath) as f: try: @@ -467,7 +467,7 @@ class XMLModuleStore(ModuleStoreBase): # tabs are referenced in policy.json through a 'slug' which is just the filename without the .html suffix slug = os.path.splitext(os.path.basename(filepath))[0] loc = Location('i4x', course_descriptor.location.org, course_descriptor.location.course, category, slug) - module = HtmlDescriptor(system, definition={'data' : html}, **{'location' : loc}) + module = HtmlDescriptor(system, definition={'data': html}, **{'location': loc}) # VS[compat]: # Hack because we need to pull in the 'display_name' for static tabs (because we need to edit them) # from the course policy @@ -555,7 +555,7 @@ class XMLModuleStore(ModuleStoreBase): Return a dictionary of course_dir -> [(msg, exception_str)], for each course_dir where course loading failed. """ - return dict( (k, self.errored_courses[k].errors) for k in self.errored_courses) + return dict((k, self.errored_courses[k].errors) for k in self.errored_courses) def update_item(self, location, data): """ @@ -590,7 +590,7 @@ class XMLModuleStore(ModuleStoreBase): raise NotImplementedError("XMLModuleStores are read-only") def get_parent_locations(self, location, course_id): - '''Find all locations that are the parents of this location in this + '''Find all locations that are the parents of this location in this course. Needed for path_to_location(). returns an iterable of things that can be passed to Location. This may diff --git a/common/lib/xmodule/xmodule/modulestore/xml_exporter.py b/common/lib/xmodule/xmodule/modulestore/xml_exporter.py index 3522b45718c..bdbd5a6133a 100644 --- a/common/lib/xmodule/xmodule/modulestore/xml_exporter.py +++ b/common/lib/xmodule/xmodule/modulestore/xml_exporter.py @@ -3,6 +3,7 @@ from xmodule.modulestore import Location from xmodule.modulestore.django import modulestore from fs.osfs import OSFS + def export_to_xml(modulestore, contentstore, course_location, root_dir, course_dir): course = modulestore.get_item(course_location) @@ -27,7 +28,7 @@ def export_to_xml(modulestore, contentstore, course_location, root_dir, course_d export_extra_content(export_fs, modulestore, course_location, 'course_info', 'info', '.html') -def export_extra_content(export_fs, modulestore, course_location, category_type, dirname, file_suffix = ''): +def export_extra_content(export_fs, modulestore, course_location, category_type, dirname, file_suffix=''): query_loc = Location('i4x', course_location.org, course_location.course, category_type, None) items = modulestore.get_items(query_loc) @@ -36,7 +37,3 @@ def export_extra_content(export_fs, modulestore, course_location, category_type, for item in items: with item_dir.open(item.location.name + file_suffix, 'w') as item_file: item_file.write(item.definition['data'].encode('utf8')) - - - - \ No newline at end of file diff --git a/common/lib/xmodule/xmodule/modulestore/xml_importer.py b/common/lib/xmodule/xmodule/modulestore/xml_importer.py index 7658d699d48..0b77900ae98 100644 --- a/common/lib/xmodule/xmodule/modulestore/xml_importer.py +++ b/common/lib/xmodule/xmodule/modulestore/xml_importer.py @@ -11,9 +11,10 @@ from xmodule.contentstore.content import StaticContent, XASSET_SRCREF_PREFIX log = logging.getLogger(__name__) -def import_static_content(modules, course_loc, course_data_path, static_content_store, target_location_namespace, - subpath = 'static', verbose=False): - + +def import_static_content(modules, course_loc, course_data_path, static_content_store, target_location_namespace, + subpath='static', verbose=False): + remap_dict = {} # now import all static assets @@ -36,7 +37,7 @@ def import_static_content(modules, course_loc, course_data_path, static_content_ with open(content_path, 'rb') as f: data = f.read() - content = StaticContent(content_loc, filename, mime_type, data, import_path = fullname_with_subpath) + content = StaticContent(content_loc, filename, mime_type, data, import_path=fullname_with_subpath) # first let's save a thumbnail so we can get back a thumbnail location (thumbnail_content, thumbnail_location) = static_content_store.generate_thumbnail(content) @@ -50,11 +51,12 @@ def import_static_content(modules, course_loc, course_data_path, static_content_ #store the remapping information which will be needed to subsitute in the module data remap_dict[fullname_with_subpath] = content_loc.name except: - raise + raise return remap_dict -def verify_content_links(module, base_dir, static_content_store, link, remap_dict = None): + +def verify_content_links(module, base_dir, static_content_store, link, remap_dict=None): if link.startswith('/static/'): # yes, then parse out the name path = link[len('/static/'):] @@ -70,7 +72,7 @@ def verify_content_links(module, base_dir, static_content_store, link, remap_dic with open(static_pathname, 'rb') as f: data = f.read() - content = StaticContent(content_loc, filename, mime_type, data, import_path = path) + content = StaticContent(content_loc, filename, mime_type, data, import_path=path) # first let's save a thumbnail so we can get back a thumbnail location (thumbnail_content, thumbnail_location) = static_content_store.generate_thumbnail(content) @@ -79,20 +81,21 @@ def verify_content_links(module, base_dir, static_content_store, link, remap_dic content.thumbnail_location = thumbnail_location #then commit the content - static_content_store.save(content) + static_content_store.save(content) - new_link = StaticContent.get_url_path_from_location(content_loc) + new_link = StaticContent.get_url_path_from_location(content_loc) if remap_dict is not None: remap_dict[link] = new_link - return new_link + return new_link except Exception, e: logging.exception('Skipping failed content load from {0}. Exception: {1}'.format(path, e)) return link -def import_from_xml(store, data_dir, course_dirs=None, + +def import_from_xml(store, data_dir, course_dirs=None, default_class='xmodule.raw_module.RawDescriptor', load_error_modules=True, static_content_store=None, target_location_namespace=None, verbose=False): """ @@ -108,7 +111,7 @@ def import_from_xml(store, data_dir, course_dirs=None, the policy.json. so we need to keep the original url_name during import """ - + module_store = XMLModuleStore( data_dir, default_class=default_class, @@ -137,12 +140,12 @@ def import_from_xml(store, data_dir, course_dirs=None, module = remap_namespace(module, target_location_namespace) - # cdodge: more hacks (what else). Seems like we have a problem when importing a course (like 6.002) which - # does not have any tabs defined in the policy file. The import goes fine and then displays fine in LMS, - # but if someone tries to add a new tab in the CMS, then the LMS barfs because it expects that - + # cdodge: more hacks (what else). Seems like we have a problem when importing a course (like 6.002) which + # does not have any tabs defined in the policy file. The import goes fine and then displays fine in LMS, + # but if someone tries to add a new tab in the CMS, then the LMS barfs because it expects that - # if there is *any* tabs - then there at least needs to be some predefined ones if module.tabs is None or len(module.tabs) == 0: - module.tabs = [{"type": "courseware"}, + module.tabs = [{"type": "courseware"}, {"type": "course_info", "name": "Course Info"}, {"type": "discussion", "name": "Discussion"}, {"type": "wiki", "name": "Wiki"}] # note, add 'progress' when we can support it on Edge @@ -159,13 +162,13 @@ def import_from_xml(store, data_dir, course_dirs=None, course_items.append(module) - + # then import all the static content if static_content_store is not None: _namespace_rename = target_location_namespace if target_location_namespace is not None else course_location - + # first pass to find everything in /static/ - import_static_content(module_store.modules[course_id], course_location, course_data_path, static_content_store, + import_static_content(module_store.modules[course_id], course_location, course_data_path, static_content_store, _namespace_rename, subpath='static', verbose=verbose) # finally loop through all the modules @@ -188,18 +191,18 @@ def import_from_xml(store, data_dir, course_dirs=None, # cdodge: now go through any link references to '/static/' and make sure we've imported # it as a StaticContent asset - try: + try: remap_dict = {} # use the rewrite_links as a utility means to enumerate through all links # in the module data. We use that to load that reference into our asset store # IMPORTANT: There appears to be a bug in lxml.rewrite_link which makes us not be able to # do the rewrites natively in that code. - # For example, what I'm seeing is <img src='foo.jpg' /> -> <img src='bar.jpg'> + # For example, what I'm seeing is <img src='foo.jpg' /> -> <img src='bar.jpg'> # Note the dropped element closing tag. This causes the LMS to fail when rendering modules - that's # no good, so we have to do this kludge if isinstance(module_data, str) or isinstance(module_data, unicode): # some module 'data' fields are non strings which blows up the link traversal code - lxml_rewrite_links(module_data, lambda link: verify_content_links(module, course_data_path, + lxml_rewrite_links(module_data, lambda link: verify_content_links(module, course_data_path, static_content_store, link, remap_dict)) for key in remap_dict.keys(): @@ -219,17 +222,18 @@ def import_from_xml(store, data_dir, course_dirs=None, return module_store, course_items + def remap_namespace(module, target_location_namespace): if target_location_namespace is None: return module - + # This looks a bit wonky as we need to also change the 'name' of the imported course to be what # the caller passed in if module.location.category != 'course': - module.location = module.location._replace(tag=target_location_namespace.tag, org=target_location_namespace.org, + module.location = module.location._replace(tag=target_location_namespace.tag, org=target_location_namespace.org, course=target_location_namespace.course) else: - module.location = module.location._replace(tag=target_location_namespace.tag, org=target_location_namespace.org, + module.location = module.location._replace(tag=target_location_namespace.tag, org=target_location_namespace.org, course=target_location_namespace.course, name=target_location_namespace.name) # then remap children pointers since they too will be re-namespaced @@ -238,15 +242,16 @@ def remap_namespace(module, target_location_namespace): new_locs = [] for child in children_locs: child_loc = Location(child) - new_child_loc = child_loc._replace(tag=target_location_namespace.tag, org=target_location_namespace.org, + new_child_loc = child_loc._replace(tag=target_location_namespace.tag, org=target_location_namespace.org, course=target_location_namespace.course) new_locs.append(new_child_loc.url()) - module.definition['children'] = new_locs + module.definition['children'] = new_locs return module + def validate_category_hierarchy(module_store, course_id, parent_category, expected_child_category): err_cnt = 0 @@ -265,7 +270,8 @@ def validate_category_hierarchy(module_store, course_id, parent_category, expect return err_cnt -def validate_data_source_path_existence(path, is_err = True, extra_msg = None): + +def validate_data_source_path_existence(path, is_err=True, extra_msg=None): _cnt = 0 if not os.path.exists(path): print ("{0}: Expected folder at {1}. {2}".format('ERROR' if is_err == True else 'WARNING', path, extra_msg if @@ -273,18 +279,19 @@ def validate_data_source_path_existence(path, is_err = True, extra_msg = None): _cnt = 1 return _cnt + def validate_data_source_paths(data_dir, course_dir): # check that there is a '/static/' directory course_path = data_dir / course_dir err_cnt = 0 warn_cnt = 0 err_cnt += validate_data_source_path_existence(course_path / 'static') - warn_cnt += validate_data_source_path_existence(course_path / 'static/subs', is_err = False, - extra_msg = 'Video captions (if they are used) will not work unless they are static/subs.') + warn_cnt += validate_data_source_path_existence(course_path / 'static/subs', is_err=False, + extra_msg='Video captions (if they are used) will not work unless they are static/subs.') return err_cnt, warn_cnt -def perform_xlint(data_dir, course_dirs, +def perform_xlint(data_dir, course_dirs, default_class='xmodule.raw_module.RawDescriptor', load_error_modules=True): err_cnt = 0 @@ -308,9 +315,9 @@ def perform_xlint(data_dir, course_dirs, for err_log_entry in err_log.errors: msg = err_log_entry[0] if msg.startswith('ERROR:'): - err_cnt+=1 + err_cnt += 1 else: - warn_cnt+=1 + warn_cnt += 1 # then count outright all courses that failed to load at all for err_log in module_store.errored_courses.itervalues(): @@ -318,9 +325,9 @@ def perform_xlint(data_dir, course_dirs, msg = err_log_entry[0] print msg if msg.startswith('ERROR:'): - err_cnt+=1 + err_cnt += 1 else: - warn_cnt+=1 + warn_cnt += 1 for course_id in module_store.modules.keys(): # constrain that courses only have 'chapter' children @@ -345,6 +352,3 @@ def perform_xlint(data_dir, course_dirs, print "This course can be imported, but some errors may occur during the run of the course. It is recommend that you fix your courseware before importing" else: print "This course can be imported successfully." - - - diff --git a/common/lib/xmodule/xmodule/open_ended_image_submission.py b/common/lib/xmodule/xmodule/open_ended_image_submission.py index 8fa4d721d3e..10b1a20a7ca 100644 --- a/common/lib/xmodule/xmodule/open_ended_image_submission.py +++ b/common/lib/xmodule/xmodule/open_ended_image_submission.py @@ -51,6 +51,7 @@ MAX_COLORS_TO_COUNT = 16 #Maximum number of colors allowed in an uploaded image MAX_COLORS = 400 + class ImageProperties(object): """ Class to check properties of an image and to validate if they are allowed. @@ -187,6 +188,7 @@ class URLProperties(object): return success return success + def run_url_tests(url_string): """ Creates a URLProperties object and runs all tests @@ -244,7 +246,7 @@ def upload_to_s3(file_to_upload, keyname): #k.set_metadata("Content-Type", 'images/png') k.set_acl("public-read") - public_url = k.generate_url(60 * 60 * 24 * 365) # URL timeout in seconds. + public_url = k.generate_url(60 * 60 * 24 * 365) # URL timeout in seconds. return True, public_url except: @@ -260,6 +262,3 @@ def get_from_s3(s3_public_url): r = requests.get(s3_public_url, timeout=2) data = r.text return data - - - diff --git a/common/lib/xmodule/xmodule/open_ended_module.py b/common/lib/xmodule/xmodule/open_ended_module.py index 94d45d96e3d..072a7153fb7 100644 --- a/common/lib/xmodule/xmodule/open_ended_module.py +++ b/common/lib/xmodule/xmodule/open_ended_module.py @@ -38,6 +38,7 @@ from combined_open_ended_rubric import CombinedOpenEndedRubric log = logging.getLogger("mitx.courseware") + class OpenEndedModule(openendedchild.OpenEndedChild): """ The open ended module supports all external open ended grader problems. @@ -300,7 +301,7 @@ class OpenEndedModule(openendedchild.OpenEndedChild): # We want to display available feedback in a particular order. # This dictionary specifies which goes first--lower first. - priorities = {# These go at the start of the feedback + priorities = { # These go at the start of the feedback 'spelling': 0, 'grammar': 1, # needs to be after all the other feedback @@ -674,5 +675,3 @@ class OpenEndedDescriptor(XmlDescriptor, EditingDescriptor): add_child(child) return elt - - diff --git a/common/lib/xmodule/xmodule/openendedchild.py b/common/lib/xmodule/xmodule/openendedchild.py index 7151ac07231..a56937270be 100644 --- a/common/lib/xmodule/xmodule/openendedchild.py +++ b/common/lib/xmodule/xmodule/openendedchild.py @@ -38,6 +38,7 @@ MAX_ATTEMPTS = 1 # Overriden by max_score specified in xml. MAX_SCORE = 1 + class OpenEndedChild(object): """ States: @@ -375,18 +376,13 @@ class OpenEndedChild(object): """ success = False links = re.findall(r'(https?://\S+)', string) - if len(links)>0: + if len(links) > 0: for link in links: success = open_ended_image_submission.run_url_tests(link) if not success: string = re.sub(link, '', string) else: - string = re.sub(link, self.generate_image_tag_from_url(link,link), string) + string = re.sub(link, self.generate_image_tag_from_url(link, link), string) success = True return success, string - - - - - diff --git a/common/lib/xmodule/xmodule/peer_grading_module.py b/common/lib/xmodule/xmodule/peer_grading_module.py index e853160f4a6..20f71f3b3ca 100644 --- a/common/lib/xmodule/xmodule/peer_grading_module.py +++ b/common/lib/xmodule/xmodule/peer_grading_module.py @@ -43,6 +43,7 @@ TRUE_DICT = [True, "True", "true", "TRUE"] MAX_SCORE = 1 IS_GRADED = True + class PeerGradingModule(XModule): _VERSION = 1 @@ -80,7 +81,7 @@ class PeerGradingModule(XModule): self.is_graded = (self.is_graded in TRUE_DICT) self.link_to_location = self.metadata.get('link_to_location', USE_FOR_SINGLE_LOCATION) - if self.use_for_single_location ==True: + if self.use_for_single_location == True: #This will raise an exception if the location is invalid link_to_location_object = Location(self.link_to_location) @@ -116,7 +117,7 @@ class PeerGradingModule(XModule): if not self.use_for_single_location: return self.peer_grading() else: - return self.peer_grading_problem({'location' : self.link_to_location})['html'] + return self.peer_grading_problem({'location': self.link_to_location})['html'] def handle_ajax(self, dispatch, get): """ @@ -128,8 +129,8 @@ class PeerGradingModule(XModule): 'show_calibration_essay': self.show_calibration_essay, 'is_student_calibrated': self.is_student_calibrated, 'save_grade': self.save_grade, - 'save_calibration_essay' : self.save_calibration_essay, - 'problem' : self.peer_grading_problem, + 'save_calibration_essay': self.save_calibration_essay, + 'problem': self.peer_grading_problem, } if dispatch not in handlers: @@ -175,11 +176,11 @@ class PeerGradingModule(XModule): return None count_graded = response['count_graded'] count_required = response['count_required'] - if count_required>0 and count_graded>=count_required: + if count_required > 0 and count_graded >= count_required: self.student_data_for_location = response score_dict = { - 'score': int(count_graded>=count_required), + 'score': int(count_graded >= count_required), 'total': self.max_grade, } @@ -399,7 +400,7 @@ class PeerGradingModule(XModule): log.exception("Error saving calibration grade, location: {0}, submission_id: {1}, submission_key: {2}, grader_id: {3}".format(location, submission_id, submission_key, grader_id)) return self._err_response('Could not connect to grading service') - def peer_grading(self, get = None): + def peer_grading(self, get=None): ''' Show a peer grading interface ''' @@ -434,19 +435,19 @@ class PeerGradingModule(XModule): 'error_text': error_text, # Checked above 'staff_access': False, - 'use_single_location' : self.use_for_single_location, + 'use_single_location': self.use_for_single_location, }) return html - def peer_grading_problem(self, get = None): + def peer_grading_problem(self, get=None): ''' Show individual problem interface ''' - if get == None or get.get('location')==None: + if get == None or get.get('location') == None: if not self.use_for_single_location: #This is an error case, because it must be set to use a single location to be called without get parameters - return {'html' : "", 'success' : False} + return {'html': "", 'success': False} problem_location = self.link_to_location elif get.get('location') is not None: @@ -460,10 +461,10 @@ class PeerGradingModule(XModule): 'ajax_url': ajax_url, # Checked above 'staff_access': False, - 'use_single_location' : self.use_for_single_location, + 'use_single_location': self.use_for_single_location, }) - return {'html' : html, 'success' : True} + return {'html': html, 'success': True} def get_instance_state(self): """ @@ -473,11 +474,12 @@ class PeerGradingModule(XModule): """ state = { - 'student_data_for_location' : self.student_data_for_location, + 'student_data_for_location': self.student_data_for_location, } return json.dumps(state) + class PeerGradingDescriptor(XmlDescriptor, EditingDescriptor): """ Module for adding combined open ended questions @@ -534,4 +536,4 @@ class PeerGradingDescriptor(XmlDescriptor, EditingDescriptor): for child in ['task']: add_child(child) - return elt \ No newline at end of file + return elt diff --git a/common/lib/xmodule/xmodule/peer_grading_service.py b/common/lib/xmodule/xmodule/peer_grading_service.py index 6b30f4e0434..8c50b6ff0a6 100644 --- a/common/lib/xmodule/xmodule/peer_grading_service.py +++ b/common/lib/xmodule/xmodule/peer_grading_service.py @@ -14,11 +14,13 @@ from combined_open_ended_rubric import CombinedOpenEndedRubric, RubricParsingErr from lxml import etree from grading_service_module import GradingService, GradingServiceError -log=logging.getLogger(__name__) +log = logging.getLogger(__name__) + class GradingServiceError(Exception): pass + class PeerGradingService(GradingService): """ Interface with the grading controller for peer grading @@ -47,23 +49,23 @@ class PeerGradingService(GradingService): return self.try_to_decode(self._render_rubric(response)) def save_grade(self, location, grader_id, submission_id, score, feedback, submission_key, rubric_scores, submission_flagged): - data = {'grader_id' : grader_id, - 'submission_id' : submission_id, - 'score' : score, - 'feedback' : feedback, + data = {'grader_id': grader_id, + 'submission_id': submission_id, + 'score': score, + 'feedback': feedback, 'submission_key': submission_key, 'location': location, 'rubric_scores': rubric_scores, 'rubric_scores_complete': True, - 'submission_flagged' : submission_flagged} + 'submission_flagged': submission_flagged} return self.try_to_decode(self.post(self.save_grade_url, data)) def is_student_calibrated(self, problem_location, grader_id): - params = {'problem_id' : problem_location, 'student_id': grader_id} + params = {'problem_id': problem_location, 'student_id': grader_id} return self.try_to_decode(self.get(self.is_student_calibrated_url, params)) def show_calibration_essay(self, problem_location, grader_id): - params = {'problem_id' : problem_location, 'student_id': grader_id} + params = {'problem_id': problem_location, 'student_id': grader_id} response = self.get(self.show_calibration_essay_url, params) return self.try_to_decode(self._render_rubric(response)) @@ -100,10 +102,12 @@ class PeerGradingService(GradingService): This is a mock peer grading service that can be used for unit tests without making actual service calls to the grading controller """ + + class MockPeerGradingService(object): def get_next_submission(self, problem_location, grader_id): return json.dumps({'success': True, - 'submission_id':1, + 'submission_id': 1, 'submission_key': "", 'student_response': 'fake student response', 'prompt': 'fake submission prompt', @@ -119,7 +123,7 @@ class MockPeerGradingService(object): def show_calibration_essay(self, problem_location, grader_id): return json.dumps({'success': True, - 'submission_id':1, + 'submission_id': 1, 'submission_key': '', 'student_response': 'fake student response', 'prompt': 'fake submission prompt', @@ -140,6 +144,8 @@ class MockPeerGradingService(object): ]}) _service = None + + def peer_grading_service(system): """ Return a peer grading service instance--if settings.MOCK_PEER_GRADING is True, diff --git a/common/lib/xmodule/xmodule/randomize_module.py b/common/lib/xmodule/xmodule/randomize_module.py index f88cdc5efb5..b3367891935 100644 --- a/common/lib/xmodule/xmodule/randomize_module.py +++ b/common/lib/xmodule/xmodule/randomize_module.py @@ -12,6 +12,7 @@ from pkg_resources import resource_string log = logging.getLogger('mitx.' + __name__) + class RandomizeModule(XModule): """ Chooses a random child module. Chooses the same one every time for each student. @@ -96,7 +97,7 @@ class RandomizeModule(XModule): def get_icon_class(self): return self.child.get_icon_class() if self.child else 'other' - + class RandomizeDescriptor(SequenceDescriptor): # the editing interface can be the same as for sequences -- just a container module_class = RandomizeModule @@ -118,4 +119,3 @@ class RandomizeDescriptor(SequenceDescriptor): makes it use module.get_child_descriptors(). """ return True - diff --git a/common/lib/xmodule/xmodule/raw_module.py b/common/lib/xmodule/xmodule/raw_module.py index efdd2e7ba0e..4a2bfbceafd 100644 --- a/common/lib/xmodule/xmodule/raw_module.py +++ b/common/lib/xmodule/xmodule/raw_module.py @@ -6,6 +6,7 @@ import sys log = logging.getLogger(__name__) + class RawDescriptor(XmlDescriptor, XMLEditingDescriptor): """ Module that provides a raw editing view of its data and children. It @@ -13,7 +14,7 @@ class RawDescriptor(XmlDescriptor, XMLEditingDescriptor): """ @classmethod def definition_from_xml(cls, xml_object, system): - return {'data': etree.tostring(xml_object, pretty_print=True,encoding='unicode')} + return {'data': etree.tostring(xml_object, pretty_print=True, encoding='unicode')} def definition_to_xml(self, resource_fs): try: diff --git a/common/lib/xmodule/xmodule/self_assessment_module.py b/common/lib/xmodule/xmodule/self_assessment_module.py index 38a60e11f5a..07cc68a83a3 100644 --- a/common/lib/xmodule/xmodule/self_assessment_module.py +++ b/common/lib/xmodule/xmodule/self_assessment_module.py @@ -25,6 +25,7 @@ from combined_open_ended_rubric import CombinedOpenEndedRubric log = logging.getLogger("mitx.courseware") + class SelfAssessmentModule(openendedchild.OpenEndedChild): """ A Self Assessment module that allows students to write open-ended responses, diff --git a/common/lib/xmodule/xmodule/seq_module.py b/common/lib/xmodule/xmodule/seq_module.py index 6dd92cc8fa4..36011744f55 100644 --- a/common/lib/xmodule/xmodule/seq_module.py +++ b/common/lib/xmodule/xmodule/seq_module.py @@ -88,8 +88,8 @@ class SequenceModule(XModule): 'type': child.get_icon_class(), 'id': child.id, } - if childinfo['title']=='': - childinfo['title'] = child.metadata.get('display_name','') + if childinfo['title'] == '': + childinfo['title'] = child.metadata.get('display_name', '') contents.append(childinfo) params = {'items': contents, @@ -116,7 +116,7 @@ class SequenceDescriptor(MakoModuleDescriptor, XmlDescriptor): mako_template = 'widgets/sequence-edit.html' module_class = SequenceModule - stores_state = True # For remembering where in the sequence the student is + stores_state = True # For remembering where in the sequence the student is js = {'coffee': [resource_string(__name__, 'js/src/sequence/edit.coffee')]} js_module_name = "SequenceDescriptor" @@ -140,4 +140,3 @@ class SequenceDescriptor(MakoModuleDescriptor, XmlDescriptor): xml_object.append( etree.fromstring(child.export_to_xml(resource_fs))) return xml_object - diff --git a/common/lib/xmodule/xmodule/stringify.py b/common/lib/xmodule/xmodule/stringify.py index dab8ff04254..5a640e91b1e 100644 --- a/common/lib/xmodule/xmodule/stringify.py +++ b/common/lib/xmodule/xmodule/stringify.py @@ -1,6 +1,7 @@ from itertools import chain from lxml import etree + def stringify_children(node): ''' Return all contents of an xml tree, without the outside tags. diff --git a/common/lib/xmodule/xmodule/template_module.py b/common/lib/xmodule/xmodule/template_module.py index d7e7ece8973..5f376945ebc 100644 --- a/common/lib/xmodule/xmodule/template_module.py +++ b/common/lib/xmodule/xmodule/template_module.py @@ -5,6 +5,7 @@ from mako.template import Template from xmodule.modulestore.django import modulestore import logging + class CustomTagModule(XModule): """ This module supports tags of the form @@ -81,4 +82,3 @@ class CustomTagDescriptor(RawDescriptor): to export them in a file with yet another layer of indirection. """ return False - diff --git a/common/lib/xmodule/xmodule/tests/__init__.py b/common/lib/xmodule/xmodule/tests/__init__.py index 1f323834a9c..04e7ee19b1f 100644 --- a/common/lib/xmodule/xmodule/tests/__init__.py +++ b/common/lib/xmodule/xmodule/tests/__init__.py @@ -29,9 +29,9 @@ test_system = ModuleSystem( user=Mock(is_staff=False), filestore=Mock(), debug=True, - xqueue={'interface':None, 'callback_url':'/', 'default_queuename': 'testqueue', 'waittime': 10}, + xqueue={'interface': None, 'callback_url': '/', 'default_queuename': 'testqueue', 'waittime': 10}, node_path=os.environ.get("NODE_PATH", "/usr/local/lib/node_modules"), - anonymous_student_id = 'student' + anonymous_student_id='student' ) @@ -85,4 +85,3 @@ class ModelsTest(unittest.TestCase): except: exception_happened = True self.assertTrue(exception_happened) - diff --git a/common/lib/xmodule/xmodule/tests/test_capa_module.py b/common/lib/xmodule/xmodule/tests/test_capa_module.py index e8f639e3c9c..a22fcdb5f6c 100644 --- a/common/lib/xmodule/xmodule/tests/test_capa_module.py +++ b/common/lib/xmodule/xmodule/tests/test_capa_module.py @@ -10,6 +10,7 @@ from lxml import etree from . import test_system + class CapaFactory(object): """ A helper class to create problem modules with various parameters for testing. @@ -58,7 +59,7 @@ class CapaFactory(object): attempts: also added to instance state. Will be converted to an int. """ - definition = {'data': CapaFactory.sample_problem_xml,} + definition = {'data': CapaFactory.sample_problem_xml, } location = Location(["i4x", "edX", "capa_test", "problem", "SampleProblem{0}".format(CapaFactory.next_num())]) metadata = {} @@ -208,8 +209,3 @@ class CapaModuleTest(unittest.TestCase): due=self.yesterday_str, graceperiod=self.two_day_delta_str) self.assertFalse(still_in_grace.answer_available()) - - - - - diff --git a/common/lib/xmodule/xmodule/tests/test_combined_open_ended.py b/common/lib/xmodule/xmodule/tests/test_combined_open_ended.py index c89f5ee8482..690eb7e39ea 100644 --- a/common/lib/xmodule/xmodule/tests/test_combined_open_ended.py +++ b/common/lib/xmodule/xmodule/tests/test_combined_open_ended.py @@ -20,6 +20,7 @@ OpenEndedModule """ + class OpenEndedChildTest(unittest.TestCase): location = Location(["i4x", "edX", "sa_test", "selfassessment", "SampleQuestion"]) @@ -38,17 +39,17 @@ class OpenEndedChildTest(unittest.TestCase): 'max_attempts': 20, 'prompt': prompt, 'rubric': rubric, - 'max_score': max_score, + 'max_score': max_score, 'display_name': 'Name', - 'accept_file_upload' : False, + 'accept_file_upload': False, } definition = Mock() descriptor = Mock() def setUp(self): - self.openendedchild = OpenEndedChild(test_system, self.location, + self.openendedchild = OpenEndedChild(test_system, self.location, self.definition, self.descriptor, self.static_data, self.metadata) - + def test_latest_answer_empty(self): answer = self.openendedchild.latest_answer() @@ -115,12 +116,12 @@ class OpenEndedChildTest(unittest.TestCase): self.assertEqual(score['score'], new_score) self.assertEqual(score['total'], self.static_data['max_score']) - + def test_reset(self): self.openendedchild.reset(test_system) state = json.loads(self.openendedchild.get_instance_state()) self.assertEqual(state['state'], OpenEndedChild.INITIAL) - + def test_is_last_response_correct(self): new_answer = "New Answer" @@ -134,6 +135,7 @@ class OpenEndedChildTest(unittest.TestCase): self.assertEqual(self.openendedchild.is_last_response_correct(), 'incorrect') + class OpenEndedModuleTest(unittest.TestCase): location = Location(["i4x", "edX", "sa_test", "selfassessment", "SampleQuestion"]) @@ -152,7 +154,7 @@ class OpenEndedModuleTest(unittest.TestCase): 'max_attempts': 20, 'prompt': prompt, 'rubric': rubric, - 'max_score': max_score, + 'max_score': max_score, 'display_name': 'Name', 'accept_file_upload': False, } @@ -170,9 +172,9 @@ class OpenEndedModuleTest(unittest.TestCase): def setUp(self): test_system.location = self.location self.mock_xqueue = MagicMock() - self.mock_xqueue.send_to_queue.return_value=(None, "Message") - test_system.xqueue = {'interface':self.mock_xqueue, 'callback_url':'/', 'default_queuename': 'testqueue', 'waittime': 1} - self.openendedmodule = OpenEndedModule(test_system, self.location, + self.mock_xqueue.send_to_queue.return_value = (None, "Message") + test_system.xqueue = {'interface': self.mock_xqueue, 'callback_url': '/', 'default_queuename': 'testqueue', 'waittime': 1} + self.openendedmodule = OpenEndedModule(test_system, self.location, self.definition, self.descriptor, self.static_data, self.metadata) def test_message_post(self): @@ -194,8 +196,8 @@ class OpenEndedModuleTest(unittest.TestCase): result = self.openendedmodule.message_post(get, test_system) self.assertTrue(result['success']) # make sure it's actually sending something we want to the queue - self.mock_xqueue.send_to_queue.assert_called_with(body = json.dumps(contents), header=ANY) - + self.mock_xqueue.send_to_queue.assert_called_with(body=json.dumps(contents), header=ANY) + state = json.loads(self.openendedmodule.get_instance_state()) self.assertIsNotNone(state['state'], OpenEndedModule.DONE) @@ -205,21 +207,21 @@ class OpenEndedModuleTest(unittest.TestCase): student_info = {'anonymous_student_id': test_system.anonymous_student_id, 'submission_time': qtime} contents = self.openendedmodule.payload.copy() - contents.update({ + contents.update({ 'student_info': json.dumps(student_info), - 'student_response': submission, + 'student_response': submission, 'max_score': self.max_score }) result = self.openendedmodule.send_to_grader(submission, test_system) self.assertTrue(result) - self.mock_xqueue.send_to_queue.assert_called_with(body = json.dumps(contents), header=ANY) + self.mock_xqueue.send_to_queue.assert_called_with(body=json.dumps(contents), header=ANY) def update_score_single(self): self.openendedmodule.new_history_entry("New Entry") - score_msg = { + score_msg = { 'correct': True, 'score': 4, - 'msg' : 'Grader Message', + 'msg': 'Grader Message', 'feedback': "Grader Feedback" } get = {'queuekey': "abcd", @@ -232,10 +234,10 @@ class OpenEndedModuleTest(unittest.TestCase): "success": True, "feedback": "Grader Feedback" } - score_msg = { + score_msg = { 'correct': True, 'score': 4, - 'msg' : 'Grader Message', + 'msg': 'Grader Message', 'feedback': json.dumps(feedback), 'grader_type': 'IN', 'grader_id': '1', @@ -261,6 +263,7 @@ class OpenEndedModuleTest(unittest.TestCase): score = self.openendedmodule.latest_score() self.assertEqual(score, 4) + class CombinedOpenEndedModuleTest(unittest.TestCase): location = Location(["i4x", "edX", "open_ended", "combinedopenended", "SampleQuestion"]) @@ -280,7 +283,7 @@ class CombinedOpenEndedModuleTest(unittest.TestCase): 'max_attempts': 20, 'prompt': prompt, 'rubric': rubric, - 'max_score': max_score, + 'max_score': max_score, 'display_name': 'Name' }) @@ -335,5 +338,3 @@ class CombinedOpenEndedModuleTest(unittest.TestCase): changed = self.combinedoe.update_task_states() self.assertTrue(changed) - - diff --git a/common/lib/xmodule/xmodule/tests/test_conditional.py b/common/lib/xmodule/xmodule/tests/test_conditional.py index f889ec71119..1b463eccaf4 100644 --- a/common/lib/xmodule/xmodule/tests/test_conditional.py +++ b/common/lib/xmodule/xmodule/tests/test_conditional.py @@ -17,10 +17,11 @@ from xmodule.modulestore.exceptions import ItemNotFoundError from .test_export import DATA_DIR ORG = 'test_org' -COURSE = 'conditional' # name of directory with course data +COURSE = 'conditional' # name of directory with course data from . import test_system + class DummySystem(ImportSystem): @patch('xmodule.modulestore.xml.OSFS', lambda dir: MemoryFS()) @@ -82,11 +83,11 @@ class ConditionalModuleTest(unittest.TestCase): location = descriptor descriptor = self.modulestore.get_instance(course.id, location, depth=None) location = descriptor.location - instance_state = instance_states.get(location.category,None) + instance_state = instance_states.get(location.category, None) print "inner_get_module, location.category=%s, inst_state=%s" % (location.category, instance_state) return descriptor.xmodule_constructor(test_system)(instance_state, shared_state) - location = Location(["i4x", "edX", "cond_test", "conditional","condone"]) + location = Location(["i4x", "edX", "cond_test", "conditional", "condone"]) module = inner_get_module(location) def replace_urls(text, staticfiles_prefix=None, replace_prefix='/static/', course_namespace=None): @@ -105,15 +106,13 @@ class ConditionalModuleTest(unittest.TestCase): gdi = module.get_display_items() print "gdi=", gdi - ajax = json.loads(module.handle_ajax('','')) + ajax = json.loads(module.handle_ajax('', '')) self.assertTrue('xmodule.conditional_module' in ajax['html']) print "ajax: ", ajax # now change state of the capa problem to make it completed - instance_states['problem'] = json.dumps({'attempts':1}) + instance_states['problem'] = json.dumps({'attempts': 1}) - ajax = json.loads(module.handle_ajax('','')) + ajax = json.loads(module.handle_ajax('', '')) self.assertTrue('This is a secret' in ajax['html']) print "post-attempt ajax: ", ajax - - diff --git a/common/lib/xmodule/xmodule/tests/test_content.py b/common/lib/xmodule/xmodule/tests/test_content.py index 6bd10f22f79..1bcd2f4ebef 100644 --- a/common/lib/xmodule/xmodule/tests/test_content.py +++ b/common/lib/xmodule/xmodule/tests/test_content.py @@ -3,11 +3,13 @@ from xmodule.contentstore.content import StaticContent from xmodule.contentstore.content import ContentStore from xmodule.modulestore import Location + class Content: def __init__(self, location, content_type): self.location = location self.content_type = content_type + class ContentTest(unittest.TestCase): def test_thumbnail_none(self): # We had a bug where a thumbnail location of None was getting transformed into a Location tuple, with @@ -22,4 +24,4 @@ class ContentTest(unittest.TestCase): content = Content(Location(u'c4x', u'mitX', u'800', u'asset', u'monsters.jpg'), None) (thumbnail_content, thumbnail_file_location) = contentStore.generate_thumbnail(content) self.assertIsNone(thumbnail_content) - self.assertEqual(Location(u'c4x', u'mitX', u'800', u'thumbnail', u'monsters.jpg'), thumbnail_file_location) \ No newline at end of file + self.assertEqual(Location(u'c4x', u'mitX', u'800', u'thumbnail', u'monsters.jpg'), thumbnail_file_location) diff --git a/common/lib/xmodule/xmodule/tests/test_export.py b/common/lib/xmodule/xmodule/tests/test_export.py index f92d58db034..da1b04bd941 100644 --- a/common/lib/xmodule/xmodule/tests/test_export.py +++ b/common/lib/xmodule/xmodule/tests/test_export.py @@ -27,6 +27,7 @@ def strip_metadata(descriptor, key): for d in descriptor.get_children(): strip_metadata(d, key) + def strip_filenames(descriptor): """ Recursively strips 'filename' from all children's definitions. @@ -119,12 +120,12 @@ class RoundTripTestCase(unittest.TestCase): def test_selfassessment_roundtrip(self): #Test selfassessment xmodule to see if it exports correctly - self.check_export_roundtrip(DATA_DIR,"self_assessment") + self.check_export_roundtrip(DATA_DIR, "self_assessment") def test_graphicslidertool_roundtrip(self): #Test graphicslidertool xmodule to see if it exports correctly - self.check_export_roundtrip(DATA_DIR,"graphic_slider_tool") + self.check_export_roundtrip(DATA_DIR, "graphic_slider_tool") def test_exam_registration_roundtrip(self): # Test exam_registration xmodule to see if it exports correctly - self.check_export_roundtrip(DATA_DIR,"test_exam_registration") + self.check_export_roundtrip(DATA_DIR, "test_exam_registration") diff --git a/common/lib/xmodule/xmodule/tests/test_graders.py b/common/lib/xmodule/xmodule/tests/test_graders.py index fa0e94d2d57..27416b1d5c5 100644 --- a/common/lib/xmodule/xmodule/tests/test_graders.py +++ b/common/lib/xmodule/xmodule/tests/test_graders.py @@ -4,6 +4,7 @@ import unittest from xmodule import graders from xmodule.graders import Score, aggregate_scores + class GradesheetTest(unittest.TestCase): def test_weighted_grading(self): @@ -217,4 +218,3 @@ class GraderTest(unittest.TestCase): #TODO: How do we test failure cases? The parser only logs an error when #it can't parse something. Maybe it should throw exceptions? - diff --git a/common/lib/xmodule/xmodule/tests/test_import.py b/common/lib/xmodule/xmodule/tests/test_import.py index 7cd91223e3a..42072ffe4d1 100644 --- a/common/lib/xmodule/xmodule/tests/test_import.py +++ b/common/lib/xmodule/xmodule/tests/test_import.py @@ -61,6 +61,7 @@ class BaseCourseTestCase(unittest.TestCase): self.assertEquals(len(courses), 1) return courses[0] + class ImportTestCase(BaseCourseTestCase): def test_fallback(self): @@ -323,7 +324,7 @@ class ImportTestCase(BaseCourseTestCase): self.assertEqual(len(sections), 4) - for i in (2,3): + for i in (2, 3): video = sections[i] # Name should be 'video_{hash}' print "video {0} url_name: {1}".format(i, video.url_name) @@ -379,5 +380,3 @@ class ImportTestCase(BaseCourseTestCase): # and finally... course.metadata['cohort_config'] = {'cohorted': True} self.assertTrue(course.is_cohorted) - - diff --git a/common/lib/xmodule/xmodule/tests/test_progress.py b/common/lib/xmodule/xmodule/tests/test_progress.py index cb011cdc2b6..0114ba4ad36 100644 --- a/common/lib/xmodule/xmodule/tests/test_progress.py +++ b/common/lib/xmodule/xmodule/tests/test_progress.py @@ -7,6 +7,7 @@ from xmodule import x_module from . import test_system + class ProgressTest(unittest.TestCase): ''' Test that basic Progress objects work. A Progress represents a fraction between 0 and 1. diff --git a/common/lib/xmodule/xmodule/tests/test_randomize_module.py b/common/lib/xmodule/xmodule/tests/test_randomize_module.py index 6353245f1a1..456fd379a53 100644 --- a/common/lib/xmodule/xmodule/tests/test_randomize_module.py +++ b/common/lib/xmodule/xmodule/tests/test_randomize_module.py @@ -52,4 +52,3 @@ class RandomizeModuleTestCase(unittest.TestCase): # TODO: add tests that create a module and check. Passing state is a good way to # check that child access works... - diff --git a/common/lib/xmodule/xmodule/tests/test_self_assessment.py b/common/lib/xmodule/xmodule/tests/test_self_assessment.py index c5fb82e4126..78dbb082aca 100644 --- a/common/lib/xmodule/xmodule/tests/test_self_assessment.py +++ b/common/lib/xmodule/xmodule/tests/test_self_assessment.py @@ -8,6 +8,7 @@ from lxml import etree from . import test_system + class SelfAssessmentTest(unittest.TestCase): rubric = '''<rubric><rubric> @@ -44,7 +45,7 @@ class SelfAssessmentTest(unittest.TestCase): 'prompt': self.prompt, 'max_score': 1, 'display_name': "Name", - 'accept_file_upload' : False, + 'accept_file_upload': False, } self.module = SelfAssessmentModule(test_system, self.location, @@ -74,4 +75,3 @@ class SelfAssessmentTest(unittest.TestCase): self.module.save_answer({'student_answer': 'answer 4'}, test_system) self.module.save_assessment({'assessment': '1'}, test_system) self.assertEqual(self.module.state, self.module.DONE) - diff --git a/common/lib/xmodule/xmodule/tests/test_stringify.py b/common/lib/xmodule/xmodule/tests/test_stringify.py index 29e99bef566..e44b93b0b8d 100644 --- a/common/lib/xmodule/xmodule/tests/test_stringify.py +++ b/common/lib/xmodule/xmodule/tests/test_stringify.py @@ -2,6 +2,7 @@ from nose.tools import assert_equals, assert_true, assert_false from lxml import etree from xmodule.stringify import stringify_children + def test_stringify(): text = 'Hi <div x="foo">there <span>Bruce</span><b>!</b></div>' html = '''<html a="b" foo="bar">{0}</html>'''.format(text) @@ -9,6 +10,7 @@ def test_stringify(): out = stringify_children(xml) assert_equals(out, text) + def test_stringify_again(): html = """<html name="Voltage Source Answer" >A voltage source is non-linear! <div align="center"> diff --git a/common/lib/xmodule/xmodule/timeparse.py b/common/lib/xmodule/xmodule/timeparse.py index 36c0f725e52..1c3a780ad83 100644 --- a/common/lib/xmodule/xmodule/timeparse.py +++ b/common/lib/xmodule/xmodule/timeparse.py @@ -5,6 +5,7 @@ import time TIME_FORMAT = "%Y-%m-%dT%H:%M" + def parse_time(time_str): """ Takes a time string in TIME_FORMAT @@ -15,6 +16,7 @@ def parse_time(time_str): """ return time.strptime(time_str, TIME_FORMAT) + def stringify_time(time_struct): """ Convert a time struct to a string diff --git a/common/lib/xmodule/xmodule/util/decorators.py b/common/lib/xmodule/xmodule/util/decorators.py index 81ab747a3e2..0b9b3012449 100644 --- a/common/lib/xmodule/xmodule/util/decorators.py +++ b/common/lib/xmodule/xmodule/util/decorators.py @@ -4,8 +4,8 @@ def lazyproperty(fn): """ Use this decorator for lazy generation of properties that are expensive to compute. From http://stackoverflow.com/a/3013910/86828 - - + + Example: class Test(object): @@ -13,7 +13,7 @@ def lazyproperty(fn): def a(self): print 'generating "a"' return range(5) - + Interactive Session: >>> t = Test() >>> t.__dict__ @@ -26,11 +26,11 @@ def lazyproperty(fn): >>> t.a [0, 1, 2, 3, 4] """ - + attr_name = '_lazy_' + fn.__name__ @property def _lazyprop(self): if not hasattr(self, attr_name): setattr(self, attr_name, fn(self)) return getattr(self, attr_name) - return _lazyprop \ No newline at end of file + return _lazyprop diff --git a/common/lib/xmodule/xmodule/vertical_module.py b/common/lib/xmodule/xmodule/vertical_module.py index 14105b41d01..5827ea96a9b 100644 --- a/common/lib/xmodule/xmodule/vertical_module.py +++ b/common/lib/xmodule/xmodule/vertical_module.py @@ -47,6 +47,6 @@ class VerticalDescriptor(SequenceDescriptor): js = {'coffee': [resource_string(__name__, 'js/src/vertical/edit.coffee')]} js_module_name = "VerticalDescriptor" - + # TODO (victor): Does this need its own definition_to_xml method? Otherwise it looks # like verticals will get exported as sequentials... diff --git a/common/lib/xmodule/xmodule/video_module.py b/common/lib/xmodule/xmodule/video_module.py index f21cd37a377..6d9715cfd6b 100644 --- a/common/lib/xmodule/xmodule/video_module.py +++ b/common/lib/xmodule/xmodule/video_module.py @@ -121,7 +121,7 @@ class VideoModule(XModule): return self.youtube def get_html(self): - if isinstance(modulestore(), XMLModuleStore) : + if isinstance(modulestore(), XMLModuleStore): # VS[compat] # cdodge: filesystem static content support. caption_asset_path = "/static/{0}/subs/".format(self.metadata['data_dir']) diff --git a/common/lib/xmodule/xmodule/x_module.py b/common/lib/xmodule/xmodule/x_module.py index 0e4e8e0f008..87c085b19a0 100644 --- a/common/lib/xmodule/xmodule/x_module.py +++ b/common/lib/xmodule/xmodule/x_module.py @@ -109,16 +109,16 @@ class HTMLSnippet(object): All of these will be loaded onto the page in the CMS """ # cdodge: We've moved the xmodule.coffee script from an outside directory into the xmodule area of common - # this means we need to make sure that all xmodules include this dependency which had been previously implicitly + # this means we need to make sure that all xmodules include this dependency which had been previously implicitly # fulfilled in a different area of code js = cls.js - + if js is None: js = {} if 'coffee' not in js: js['coffee'] = [] - + js['coffee'].append(resource_string(__name__, 'js/src/xmodule.coffee')) return js @@ -538,7 +538,7 @@ class XModuleDescriptor(Plugin, HTMLSnippet, ResourceTemplates): def start(self, value): if isinstance(value, time.struct_time): self.metadata['start'] = stringify_time(value) - + @property def days_early_for_beta(self): """ diff --git a/common/lib/xmodule/xmodule/xml_module.py b/common/lib/xmodule/xmodule/xml_module.py index ed2f6ce9215..64c3aabbccf 100644 --- a/common/lib/xmodule/xmodule/xml_module.py +++ b/common/lib/xmodule/xmodule/xml_module.py @@ -16,6 +16,7 @@ edx_xml_parser = etree.XMLParser(dtd_validation=False, load_dtd=False, remove_comments=True, remove_blank_text=True, encoding='utf-8') + def name_to_pathname(name): """ Convert a location name for use in a path: replace ':' with '/'. @@ -23,6 +24,7 @@ def name_to_pathname(name): """ return name.replace(':', '/') + def is_pointer_tag(xml_obj): """ Check if xml_obj is a pointer tag: <blah url_name="something" />. @@ -46,6 +48,7 @@ def is_pointer_tag(xml_obj): return len(xml_obj) == 0 and actual_attr == expected_attr and not has_text + def get_metadata_from_xml(xml_object, remove=True): meta = xml_object.find('meta') if meta is None: @@ -58,6 +61,7 @@ def get_metadata_from_xml(xml_object, remove=True): _AttrMapBase = namedtuple('_AttrMap', 'from_xml to_xml') + class AttrMap(_AttrMapBase): """ A class that specifies two functions: @@ -93,16 +97,16 @@ class XmlDescriptor(XModuleDescriptor): metadata_attributes = ('format', 'graceperiod', 'showanswer', 'rerandomize', 'start', 'due', 'graded', 'display_name', 'url_name', 'hide_from_toc', 'ispublic', # if True, then course is listed for all users; see - 'xqa_key', # for xqaa server access - # information about testcenter exams is a dict (of dicts), not a string, - # so it cannot be easily exportable as a course element's attribute. + 'xqa_key', # for xqaa server access + # information about testcenter exams is a dict (of dicts), not a string, + # so it cannot be easily exportable as a course element's attribute. 'testcenter_info', # VS[compat] Remove once unused. 'name', 'slug') - metadata_to_strip = ('data_dir', + metadata_to_strip = ('data_dir', # cdodge: @TODO: We need to figure out a way to export out 'tabs' and 'grading_policy' which is on the course - 'tabs', 'grading_policy', 'is_draft', 'published_by', 'published_date', + 'tabs', 'grading_policy', 'is_draft', 'published_by', 'published_date', 'discussion_blackouts', 'testcenter_info', # VS[compat] -- remove the below attrs once everything is in the CMS 'course', 'org', 'url_name', 'filename') @@ -117,7 +121,7 @@ class XmlDescriptor(XModuleDescriptor): bool_map = AttrMap(to_bool, from_bool) to_int = lambda val: int(val) - from_int = lambda val: str(val) + from_int = lambda val: str(val) int_map = AttrMap(to_int, from_int) xml_attribute_map = { # type conversion: want True/False in python, "true"/"false" in xml @@ -125,7 +129,7 @@ class XmlDescriptor(XModuleDescriptor): 'hide_progress_tab': bool_map, 'allow_anonymous': bool_map, 'allow_anonymous_to_peers': bool_map, - 'weight':int_map + 'weight': int_map } @@ -133,8 +137,8 @@ class XmlDescriptor(XModuleDescriptor): # importing 2012 courses. # A set of metadata key conversions that we want to make metadata_translations = { - 'slug' : 'url_name', - 'name' : 'display_name', + 'slug': 'url_name', + 'name': 'display_name', } @classmethod @@ -230,7 +234,7 @@ class XmlDescriptor(XModuleDescriptor): # TODO (ichuang): remove this after migration # for Fall 2012 LMS migration: keep filename (and unmangled filename) - definition['filename'] = [ filepath, filename ] + definition['filename'] = [filepath, filename] return definition @@ -291,9 +295,9 @@ class XmlDescriptor(XModuleDescriptor): filepath = cls._format_filepath(xml_object.tag, name_to_pathname(url_name)) definition_xml = cls.load_file(filepath, system.resources_fs, location) else: - definition_xml = xml_object # this is just a pointer, not the real definition content + definition_xml = xml_object # this is just a pointer, not the real definition content - definition = cls.load_definition(definition_xml, system, location) # note this removes metadata + definition = cls.load_definition(definition_xml, system, location) # note this removes metadata # VS[compat] -- make Ike's github preview links work in both old and # new file layouts if is_pointer_tag(xml_object): @@ -303,13 +307,13 @@ class XmlDescriptor(XModuleDescriptor): metadata = cls.load_metadata(definition_xml) # move definition metadata into dict - dmdata = definition.get('definition_metadata','') + dmdata = definition.get('definition_metadata', '') if dmdata: metadata['definition_metadata_raw'] = dmdata try: metadata.update(json.loads(dmdata)) except Exception as err: - log.debug('Error %s in loading metadata %s' % (err,dmdata)) + log.debug('Error %s in loading metadata %s' % (err, dmdata)) metadata['definition_metadata_err'] = str(err) # Set/override any metadata specified by policy diff --git a/common/static/js/vendor/mathjax-MathJax-c9db6ac/docs/source/conf.py b/common/static/js/vendor/mathjax-MathJax-c9db6ac/docs/source/conf.py index f0a8013eed5..819bff31aab 100644 --- a/common/static/js/vendor/mathjax-MathJax-c9db6ac/docs/source/conf.py +++ b/common/static/js/vendor/mathjax-MathJax-c9db6ac/docs/source/conf.py @@ -11,7 +11,8 @@ # All configuration values have a default; values that are commented out # serve to show the default. -import sys, os +import sys +import os # If extensions (or modules to document with autodoc) are in another directory, # add these directories to sys.path here. If the directory is relative to the diff --git a/common/xml_cleanup.py b/common/xml_cleanup.py index 15890fb99ea..5f2b527063c 100755 --- a/common/xml_cleanup.py +++ b/common/xml_cleanup.py @@ -8,12 +8,16 @@ In particular, the remove-meta option is only intended to be used after pulling using the metadata_to_json management command. """ -import os, fnmatch, re, sys +import os +import fnmatch +import re +import sys from lxml import etree from collections import defaultdict INVALID_CHARS = re.compile(r"[^\w.-]") + def clean(value): """ Return value, made into a form legal for locations @@ -24,6 +28,7 @@ def clean(value): # category -> set of url_names for that category that we've already seen used_names = defaultdict(set) + def clean_unique(category, name): cleaned = clean(name) if cleaned not in used_names[category]: @@ -38,6 +43,7 @@ def clean_unique(category, name): used_names[category].add(cleaned) return cleaned + def cleanup(filepath, remove_meta): # Keys that are exported to the policy file, and so # can be removed from the xml afterward @@ -70,7 +76,7 @@ def cleanup(filepath, remove_meta): print "WARNING: {0} has both slug and url_name".format(node) if ('url_name' in attrs and 'filename' in attrs and - len(attrs)==2 and attrs['url_name'] == attrs['filename']): + len(attrs) == 2 and attrs['url_name'] == attrs['filename']): # This is a pointer tag in disguise. Get rid of the filename. print 'turning {0}.{1} into a pointer tag'.format(node.tag, attrs['url_name']) del attrs['filename'] @@ -108,5 +114,3 @@ def main(args): if __name__ == '__main__': main(sys.argv[1:]) - - diff --git a/lms/djangoapps/certificates/migrations/0008_auto__del_revokedcertificate__del_field_generatedcertificate_name__add.py b/lms/djangoapps/certificates/migrations/0008_auto__del_revokedcertificate__del_field_generatedcertificate_name__add.py index cbec7214c08..872804d2868 100644 --- a/lms/djangoapps/certificates/migrations/0008_auto__del_revokedcertificate__del_field_generatedcertificate_name__add.py +++ b/lms/djangoapps/certificates/migrations/0008_auto__del_revokedcertificate__del_field_generatedcertificate_name__add.py @@ -160,4 +160,4 @@ class Migration(SchemaMigration): } } - complete_apps = ['certificates'] \ No newline at end of file + complete_apps = ['certificates'] diff --git a/lms/djangoapps/certificates/migrations/0009_auto__del_field_generatedcertificate_graded_download_url__del_field_ge.py b/lms/djangoapps/certificates/migrations/0009_auto__del_field_generatedcertificate_graded_download_url__del_field_ge.py index 40637452cca..2ff14343145 100644 --- a/lms/djangoapps/certificates/migrations/0009_auto__del_field_generatedcertificate_graded_download_url__del_field_ge.py +++ b/lms/djangoapps/certificates/migrations/0009_auto__del_field_generatedcertificate_graded_download_url__del_field_ge.py @@ -92,4 +92,4 @@ class Migration(SchemaMigration): } } - complete_apps = ['certificates'] \ No newline at end of file + complete_apps = ['certificates'] diff --git a/lms/djangoapps/certificates/migrations/0010_auto__del_field_generatedcertificate_enabled__add_field_generatedcerti.py b/lms/djangoapps/certificates/migrations/0010_auto__del_field_generatedcertificate_enabled__add_field_generatedcerti.py index 5970c96f6bd..a41e58cc3b8 100644 --- a/lms/djangoapps/certificates/migrations/0010_auto__del_field_generatedcertificate_enabled__add_field_generatedcerti.py +++ b/lms/djangoapps/certificates/migrations/0010_auto__del_field_generatedcertificate_enabled__add_field_generatedcerti.py @@ -78,4 +78,4 @@ class Migration(SchemaMigration): } } - complete_apps = ['certificates'] \ No newline at end of file + complete_apps = ['certificates'] diff --git a/lms/djangoapps/certificates/migrations/0011_auto__del_field_generatedcertificate_certificate_id__add_field_generat.py b/lms/djangoapps/certificates/migrations/0011_auto__del_field_generatedcertificate_certificate_id__add_field_generat.py index 36d6e5d4f3e..155839a82f2 100644 --- a/lms/djangoapps/certificates/migrations/0011_auto__del_field_generatedcertificate_certificate_id__add_field_generat.py +++ b/lms/djangoapps/certificates/migrations/0011_auto__del_field_generatedcertificate_certificate_id__add_field_generat.py @@ -87,4 +87,4 @@ class Migration(SchemaMigration): } } - complete_apps = ['certificates'] \ No newline at end of file + complete_apps = ['certificates'] diff --git a/lms/djangoapps/certificates/migrations/0012_auto__add_field_generatedcertificate_name__add_field_generatedcertific.py b/lms/djangoapps/certificates/migrations/0012_auto__add_field_generatedcertificate_name__add_field_generatedcertific.py index 4195860ca5b..9261654ec88 100644 --- a/lms/djangoapps/certificates/migrations/0012_auto__add_field_generatedcertificate_name__add_field_generatedcertific.py +++ b/lms/djangoapps/certificates/migrations/0012_auto__add_field_generatedcertificate_name__add_field_generatedcertific.py @@ -90,4 +90,4 @@ class Migration(SchemaMigration): } } - complete_apps = ['certificates'] \ No newline at end of file + complete_apps = ['certificates'] diff --git a/lms/djangoapps/certificates/migrations/0013_auto__add_field_generatedcertificate_error_reason.py b/lms/djangoapps/certificates/migrations/0013_auto__add_field_generatedcertificate_error_reason.py index 7f31e6ebd90..6031c8055b6 100644 --- a/lms/djangoapps/certificates/migrations/0013_auto__add_field_generatedcertificate_error_reason.py +++ b/lms/djangoapps/certificates/migrations/0013_auto__add_field_generatedcertificate_error_reason.py @@ -75,4 +75,4 @@ class Migration(SchemaMigration): } } - complete_apps = ['certificates'] \ No newline at end of file + complete_apps = ['certificates'] diff --git a/lms/djangoapps/certificates/migrations/0014_adding_whitelist.py b/lms/djangoapps/certificates/migrations/0014_adding_whitelist.py index 1bd4d994cf3..0aafea40678 100644 --- a/lms/djangoapps/certificates/migrations/0014_adding_whitelist.py +++ b/lms/djangoapps/certificates/migrations/0014_adding_whitelist.py @@ -86,4 +86,4 @@ class Migration(SchemaMigration): } } - complete_apps = ['certificates'] \ No newline at end of file + complete_apps = ['certificates'] diff --git a/lms/djangoapps/certificates/models.py b/lms/djangoapps/certificates/models.py index 0e68e3cfe70..dc438b805a5 100644 --- a/lms/djangoapps/certificates/models.py +++ b/lms/djangoapps/certificates/models.py @@ -62,6 +62,7 @@ class CertificateStatuses(object): restricted = 'restricted' error = 'error' + class CertificateWhitelist(models.Model): """ Tracks students who are whitelisted, all users @@ -74,6 +75,7 @@ class CertificateWhitelist(models.Model): course_id = models.CharField(max_length=255, blank=True, default='') whitelist = models.BooleanField(default=0) + class GeneratedCertificate(models.Model): user = models.ForeignKey(User) course_id = models.CharField(max_length=255, blank=True, default='') diff --git a/lms/djangoapps/certificates/queue.py b/lms/djangoapps/certificates/queue.py index d926035efd5..a7eb4e3c818 100644 --- a/lms/djangoapps/certificates/queue.py +++ b/lms/djangoapps/certificates/queue.py @@ -142,7 +142,7 @@ class XQueueCertInterface(object): """ - VALID_STATUSES = [ status.generating, + VALID_STATUSES = [status.generating, status.unavailable, status.deleted, status.error, status.notpassing] diff --git a/lms/djangoapps/course_wiki/course_nav.py b/lms/djangoapps/course_wiki/course_nav.py index 122f9ebb54a..434860a0f7a 100644 --- a/lms/djangoapps/course_wiki/course_nav.py +++ b/lms/djangoapps/course_wiki/course_nav.py @@ -11,6 +11,7 @@ from courseware.courses import get_course_with_access IN_COURSE_WIKI_REGEX = r'/courses/(?P<course_id>[^/]+/[^/]+/[^/]+)/wiki/(?P<wiki_path>.*|)$' + class Middleware(object): """ This middleware is to keep the course nav bar above the wiki while @@ -18,68 +19,68 @@ class Middleware(object): If it intercepts a request for /wiki/.. that has a referrer in the form /courses/course_id/... it will redirect the user to the page /courses/course_id/wiki/... - + It is also possible that someone followed a link leading to a course that they don't have access to. In this case, we redirect them to the same page on the regular wiki. - + If we return a redirect, this middleware makes sure that the redirect keeps the student in the course. - + Finally, if the student is in the course viewing a wiki, we change the reverse() function to resolve wiki urls as a course wiki url by setting the _transform_url attribute on wiki.models.reverse. - + Forgive me Father, for I have hacked. """ - + def __init__(self): self.redirected = False - + def process_request(self, request): self.redirected = False wiki_reverse._transform_url = lambda url: url - + referer = request.META.get('HTTP_REFERER') destination = request.path - - + + if request.method == 'GET': new_destination = self.get_redirected_url(request.user, referer, destination) - + if new_destination != destination: # We mark that we generated this redirection, so we don't modify it again self.redirected = True return redirect(new_destination) - + course_match = re.match(IN_COURSE_WIKI_REGEX, destination) if course_match: course_id = course_match.group('course_id') prepend_string = '/courses/' + course_match.group('course_id') wiki_reverse._transform_url = lambda url: prepend_string + url - + return None - - + + def process_response(self, request, response): """ If this is a redirect response going to /wiki/*, then we might need to change it to be a redirect going to /courses/*/wiki*. """ - if not self.redirected and response.status_code == 302: #This is a redirect + if not self.redirected and response.status_code == 302: # This is a redirect referer = request.META.get('HTTP_REFERER') destination_url = response['LOCATION'] destination = urlparse(destination_url).path - + new_destination = self.get_redirected_url(request.user, referer, destination) - + if new_destination != destination: new_url = destination_url.replace(destination, new_destination) response['LOCATION'] = new_url - + return response - - + + def get_redirected_url(self, user, referer, destination): """ Returns None if the destination shouldn't be changed. @@ -87,14 +88,14 @@ class Middleware(object): if not referer: return destination referer_path = urlparse(referer).path - + path_match = re.match(r'^/wiki/(?P<wiki_path>.*|)$', destination) if path_match: # We are going to the wiki. Check if we came from a course course_match = re.match(r'/courses/(?P<course_id>[^/]+/[^/]+/[^/]+)/.*', referer_path) if course_match: course_id = course_match.group('course_id') - + # See if we are able to view the course. If we are, redirect to it try: course = get_course_with_access(user, course_id, 'load') @@ -102,9 +103,9 @@ class Middleware(object): except Http404: # Even though we came from the course, we can't see it. So don't worry about it. pass - + else: - # It is also possible we are going to a course wiki view, but we + # It is also possible we are going to a course wiki view, but we # don't have permission to see the course! course_match = re.match(IN_COURSE_WIKI_REGEX, destination) if course_match: @@ -117,9 +118,9 @@ class Middleware(object): except Http404: # We can't see the course, so redirect to the regular wiki return "/wiki/" + course_match.group('wiki_path') - + return destination - + def context_processor(request): """ @@ -129,21 +130,20 @@ def context_processor(request): then we add 'course' to the context. This allows the course nav bar to be shown. """ - + match = re.match(IN_COURSE_WIKI_REGEX, request.path) if match: course_id = match.group('course_id') - + try: course = get_course_with_access(request.user, course_id, 'load') staff_access = has_access(request.user, course, 'staff') - return {'course' : course, + return {'course': course, 'staff_access': staff_access} except Http404: # We couldn't access the course for whatever reason. It is too late to change # the URL here, so we just leave the course context. The middleware shouldn't # let this happen pass - + return {} - \ No newline at end of file diff --git a/lms/djangoapps/course_wiki/editors.py b/lms/djangoapps/course_wiki/editors.py index c1f5b733adb..2ca8260bfe5 100644 --- a/lms/djangoapps/course_wiki/editors.py +++ b/lms/djangoapps/course_wiki/editors.py @@ -14,11 +14,11 @@ class CodeMirrorWidget(forms.Widget): def __init__(self, attrs=None): # The 'rows' and 'cols' attributes are required for HTML correctness. default_attrs = {'class': 'markItUp', - 'rows': '10', 'cols': '40',} + 'rows': '10', 'cols': '40', } if attrs: default_attrs.update(attrs) super(CodeMirrorWidget, self).__init__(default_attrs) - + def render(self, name, value, attrs=None): if value is None: value = '' @@ -34,10 +34,10 @@ class CodeMirrorWidget(forms.Widget): class CodeMirror(BaseEditor): editor_id = 'codemirror' - + def get_admin_widget(self, instance=None): return MarkItUpAdminWidget() - + def get_widget(self, instance=None): return CodeMirrorWidget() @@ -61,4 +61,3 @@ class CodeMirror(BaseEditor): "js/wiki/CodeMirror.init.js", "js/wiki/cheatsheet.js", ) - diff --git a/lms/djangoapps/course_wiki/plugins/markdownedx/__init__.py b/lms/djangoapps/course_wiki/plugins/markdownedx/__init__.py index 8845471eda6..53fd3d4d3b8 100644 --- a/lms/djangoapps/course_wiki/plugins/markdownedx/__init__.py +++ b/lms/djangoapps/course_wiki/plugins/markdownedx/__init__.py @@ -1,2 +1,2 @@ # Make sure wiki_plugin.py gets run. -from course_wiki.plugins.markdownedx.wiki_plugin import ExtendMarkdownPlugin \ No newline at end of file +from course_wiki.plugins.markdownedx.wiki_plugin import ExtendMarkdownPlugin diff --git a/lms/djangoapps/course_wiki/plugins/markdownedx/mdx_mathjax.py b/lms/djangoapps/course_wiki/plugins/markdownedx/mdx_mathjax.py index a9148511e32..b14803744b7 100644 --- a/lms/djangoapps/course_wiki/plugins/markdownedx/mdx_mathjax.py +++ b/lms/djangoapps/course_wiki/plugins/markdownedx/mdx_mathjax.py @@ -28,4 +28,3 @@ class MathJaxExtension(markdown.Extension): def makeExtension(configs=None): return MathJaxExtension(configs) - diff --git a/lms/djangoapps/course_wiki/plugins/markdownedx/wiki_plugin.py b/lms/djangoapps/course_wiki/plugins/markdownedx/wiki_plugin.py index ffbe7b0a52b..765ee574226 100644 --- a/lms/djangoapps/course_wiki/plugins/markdownedx/wiki_plugin.py +++ b/lms/djangoapps/course_wiki/plugins/markdownedx/wiki_plugin.py @@ -7,15 +7,15 @@ from wiki.core.plugins import registry as plugin_registry from course_wiki.plugins.markdownedx import mdx_circuit, mdx_mathjax, mdx_video + class ExtendMarkdownPlugin(BasePlugin): """ This plugin simply loads all of the markdown extensions we use in edX. """ - + markdown_extensions = [mdx_circuit.CircuitExtension(configs={}), #mdx_image.ImageExtension() , #This one doesn't work. Tries to import simplewiki.settings - mdx_mathjax.MathJaxExtension(configs={}) , + mdx_mathjax.MathJaxExtension(configs={}), mdx_video.VideoExtension(configs={})] plugin_registry.register(ExtendMarkdownPlugin) - diff --git a/lms/djangoapps/course_wiki/tests/__init__.py b/lms/djangoapps/course_wiki/tests/__init__.py index 8b137891791..e69de29bb2d 100644 --- a/lms/djangoapps/course_wiki/tests/__init__.py +++ b/lms/djangoapps/course_wiki/tests/__init__.py @@ -1 +0,0 @@ - diff --git a/lms/djangoapps/course_wiki/tests/tests.py b/lms/djangoapps/course_wiki/tests/tests.py index f1c4fa4810f..99f138f0bc6 100644 --- a/lms/djangoapps/course_wiki/tests/tests.py +++ b/lms/djangoapps/course_wiki/tests/tests.py @@ -13,10 +13,10 @@ class WikiRedirectTestCase(PageLoader): def setUp(self): xmodule.modulestore.django._MODULESTORES = {} courses = modulestore().get_courses() - + def find_course(name): """Assumes the course is present""" - return [c for c in courses if c.location.course==name][0] + return [c for c in courses if c.location.course == name][0] self.full = find_course("full") self.toy = find_course("toy") @@ -29,91 +29,89 @@ class WikiRedirectTestCase(PageLoader): self.create_account('u2', self.instructor, self.password) self.activate_user(self.student) self.activate_user(self.instructor) - - - + + + def test_wiki_redirect(self): """ Test that requesting wiki URLs redirect properly to or out of classes. - - An enrolled in student going from /courses/edX/toy/2012_Fall/progress - to /wiki/some/fake/wiki/page/ will redirect to + + An enrolled in student going from /courses/edX/toy/2012_Fall/progress + to /wiki/some/fake/wiki/page/ will redirect to /courses/edX/toy/2012_Fall/wiki/some/fake/wiki/page/ - + An unenrolled student going to /courses/edX/toy/2012_Fall/wiki/some/fake/wiki/page/ will be redirected to /wiki/some/fake/wiki/page/ - + """ self.login(self.student, self.password) - + self.enroll(self.toy) - - referer = reverse("progress", kwargs={ 'course_id' : self.toy.id }) + + referer = reverse("progress", kwargs={'course_id': self.toy.id}) destination = reverse("wiki:get", kwargs={'path': 'some/fake/wiki/page/'}) - + redirected_to = referer.replace("progress", "wiki/some/fake/wiki/page/") - - resp = self.client.get( destination, HTTP_REFERER=referer) - self.assertEqual(resp.status_code, 302 ) - - self.assertEqual(resp['Location'], 'http://testserver' + redirected_to ) - - + + resp = self.client.get(destination, HTTP_REFERER=referer) + self.assertEqual(resp.status_code, 302) + + self.assertEqual(resp['Location'], 'http://testserver' + redirected_to) + + # Now we test that the student will be redirected away from that page if the course doesn't exist # We do this in the same test because we want to make sure the redirected_to is constructed correctly - + # This is a location like /courses/*/wiki/* , but with an invalid course ID - bad_course_wiki_page = redirected_to.replace( self.toy.location.course, "bad_course" ) - - resp = self.client.get( bad_course_wiki_page, HTTP_REFERER=referer) + bad_course_wiki_page = redirected_to.replace(self.toy.location.course, "bad_course") + + resp = self.client.get(bad_course_wiki_page, HTTP_REFERER=referer) self.assertEqual(resp.status_code, 302) - self.assertEqual(resp['Location'], 'http://testserver' + destination ) - - + self.assertEqual(resp['Location'], 'http://testserver' + destination) + + def create_course_page(self, course): """ Test that loading the course wiki page creates the wiki page. The user must be enrolled in the course to see the page. """ - - course_wiki_home = reverse('course_wiki', kwargs={'course_id' : course.id}) - referer = reverse("progress", kwargs={ 'course_id' : self.toy.id }) - + + course_wiki_home = reverse('course_wiki', kwargs={'course_id': course.id}) + referer = reverse("progress", kwargs={'course_id': self.toy.id}) + resp = self.client.get(course_wiki_home, follow=True, HTTP_REFERER=referer) - + course_wiki_page = referer.replace('progress', 'wiki/' + self.toy.wiki_slug + "/") - + ending_location = resp.redirect_chain[-1][0] ending_status = resp.redirect_chain[-1][1] - - self.assertEquals(ending_location, 'http://testserver' + course_wiki_page ) + + self.assertEquals(ending_location, 'http://testserver' + course_wiki_page) self.assertEquals(resp.status_code, 200) - + self.has_course_navigator(resp) - + def has_course_navigator(self, resp): """ Ensure that the response has the course navigator. """ - self.assertTrue( "course info" in resp.content.lower() ) - self.assertTrue( "courseware" in resp.content.lower() ) - - + self.assertTrue("course info" in resp.content.lower()) + self.assertTrue("courseware" in resp.content.lower()) + + def test_course_navigator(self): """" Test that going from a course page to a wiki page contains the course navigator. """ - + self.login(self.student, self.password) self.enroll(self.toy) self.create_course_page(self.toy) - - - course_wiki_page = reverse('wiki:get', kwargs={'path' : self.toy.wiki_slug + '/'}) - referer = reverse("courseware", kwargs={ 'course_id' : self.toy.id }) - - resp = self.client.get(course_wiki_page, follow=True, HTTP_REFERER=referer) - - self.has_course_navigator(resp) + course_wiki_page = reverse('wiki:get', kwargs={'path': self.toy.wiki_slug + '/'}) + referer = reverse("courseware", kwargs={'course_id': self.toy.id}) + + resp = self.client.get(course_wiki_page, follow=True, HTTP_REFERER=referer) + + self.has_course_navigator(resp) diff --git a/lms/djangoapps/course_wiki/views.py b/lms/djangoapps/course_wiki/views.py index 47112fc1d30..a088f8fc14c 100644 --- a/lms/djangoapps/course_wiki/views.py +++ b/lms/djangoapps/course_wiki/views.py @@ -12,6 +12,7 @@ from courseware.courses import get_course_by_id log = logging.getLogger(__name__) + def root_create(request): """ In the edX wiki, we don't show the root_create view. Instead, we @@ -28,7 +29,7 @@ def course_wiki_redirect(request, course_id): example, "/6.002x") to keep things simple. """ course = get_course_by_id(course_id) - + course_slug = course.wiki_slug @@ -43,7 +44,7 @@ def course_wiki_redirect(request, course_id): except: pass - + valid_slug = True if not course_slug: log.exception("This course is improperly configured. The slug cannot be empty.") @@ -54,8 +55,8 @@ def course_wiki_redirect(request, course_id): if not valid_slug: return redirect("wiki:get", path="") - - + + # The wiki needs a Site object created. We make sure it exists here try: site = Site.objects.get_current() @@ -66,30 +67,30 @@ def course_wiki_redirect(request, course_id): new_site.save() if str(new_site.id) != str(settings.SITE_ID): raise ImproperlyConfigured("No site object was created and the SITE_ID doesn't match the newly created one. " + str(new_site.id) + "!=" + str(settings.SITE_ID)) - + try: urlpath = URLPath.get_by_path(course_slug, select_related=True) - - results = list( Article.objects.filter( id = urlpath.article.id ) ) + + results = list(Article.objects.filter(id=urlpath.article.id)) if results: article = results[0] else: article = None - + except (NoRootURL, URLPath.DoesNotExist): # We will create it in the next block urlpath = None article = None - + if not article: # create it root = get_or_create_root() - + if urlpath: # Somehow we got a urlpath without an article. Just delete it and # recerate it. urlpath.delete() - + urlpath = URLPath.create_article( root, course_slug, @@ -105,9 +106,9 @@ def course_wiki_redirect(request, course_id): 'other_read': True, 'other_write': True, }) - + return redirect("wiki:get", path=urlpath.path) - + def get_or_create_root(): """ @@ -121,12 +122,12 @@ def get_or_create_root(): return root except NoRootURL: pass - + starting_content = "\n".join(( "Welcome to the edX Wiki", "===", "Visit a course wiki to add an article.")) - + root = URLPath.create_root(title="Wiki", content=starting_content) article = root.article @@ -136,6 +137,5 @@ def get_or_create_root(): article.other_read = True article.other_write = False article.save() - + return root - diff --git a/lms/djangoapps/courseware/access.py b/lms/djangoapps/courseware/access.py index 475a7082547..b41d231011f 100644 --- a/lms/djangoapps/courseware/access.py +++ b/lms/djangoapps/courseware/access.py @@ -186,9 +186,9 @@ def _get_access_group_name_course_desc(course, action): ''' Return name of group which gives staff access to course. Only understands action = 'staff' and 'instructor' ''' - if action=='staff': + if action == 'staff': return _course_staff_group_name(course.location) - elif action=='instructor': + elif action == 'instructor': return _course_instructor_group_name(course.location) return [] @@ -367,6 +367,7 @@ def _course_staff_group_name(location, course_context=None): return 'staff_%s' % course_id + def course_beta_test_group_name(location): """ Get the name of the beta tester group for a location. Right now, that's @@ -462,6 +463,7 @@ def _adjust_start_date_for_beta_testers(user, descriptor): return descriptor.start + def _has_instructor_access_to_location(user, location, course_context=None): return _has_access_to_location(user, location, 'instructor', course_context) diff --git a/lms/djangoapps/courseware/admin.py b/lms/djangoapps/courseware/admin.py index f7e54d18004..9ef4c1de206 100644 --- a/lms/djangoapps/courseware/admin.py +++ b/lms/djangoapps/courseware/admin.py @@ -11,4 +11,3 @@ admin.site.register(StudentModule) admin.site.register(OfflineComputedGrade) admin.site.register(OfflineComputedGradeLog) - diff --git a/lms/djangoapps/courseware/courses.py b/lms/djangoapps/courseware/courses.py index ce29d697841..52346d7583e 100644 --- a/lms/djangoapps/courseware/courses.py +++ b/lms/djangoapps/courseware/courses.py @@ -27,6 +27,7 @@ from xmodule.modulestore.exceptions import ItemNotFoundError log = logging.getLogger(__name__) + def get_request_for_thread(): """Walk up the stack, return the nearest first argument named "request".""" frame = None @@ -152,7 +153,7 @@ def get_course_about_section(course, section_key): request = get_request_for_thread() loc = course.location._replace(category='about', name=section_key) - course_module = get_module(request.user, request, loc, None, course.id, not_found_ok = True, wrap_xmodule_display = False) + course_module = get_module(request.user, request, loc, None, course.id, not_found_ok=True, wrap_xmodule_display=False) html = '' @@ -190,7 +191,7 @@ def get_course_info_section(request, cache, course, section_key): loc = Location(course.location.tag, course.location.org, course.location.course, 'course_info', section_key) - course_module = get_module(request.user, request, loc, cache, course.id, wrap_xmodule_display = False) + course_module = get_module(request.user, request, loc, cache, course.id, wrap_xmodule_display=False) html = '' if course_module is not None: @@ -259,7 +260,7 @@ def get_courses(user, domain=None): courses = branding.get_visible_courses(domain) courses = [c for c in courses if has_access(user, c, 'see_exists')] - courses = sorted(courses, key=lambda course:course.number) + courses = sorted(courses, key=lambda course: course.number) return courses diff --git a/lms/djangoapps/courseware/features/courses.py b/lms/djangoapps/courseware/features/courses.py index aecaa139ff8..9b1316b00d7 100644 --- a/lms/djangoapps/courseware/features/courses.py +++ b/lms/djangoapps/courseware/features/courses.py @@ -8,6 +8,8 @@ from logging import getLogger logger = getLogger(__name__) ## support functions + + def get_courses(): ''' Returns dict of lists of courses available, keyed by course.org (ie university). @@ -41,6 +43,7 @@ def get_courses(): # courseware = [ {'chapter_name':c.display_name, 'sections':[s.display_name for s in c.get_children()]} for c in chapters] # return courseware + def get_courseware_with_tabs(course_id): """ Given a course_id (string), return a courseware array of dictionaries for the @@ -101,18 +104,19 @@ def get_courseware_with_tabs(course_id): """ course = get_course_by_id(course_id) - chapters = [ chapter for chapter in course.get_children() if chapter.metadata.get('hide_from_toc','false').lower() != 'true' ] - courseware = [{'chapter_name':c.display_name, - 'sections':[{'section_name':s.display_name, - 'clickable_tab_count':len(s.get_children()) if (type(s)==seq_module.SequenceDescriptor) else 0, - 'tabs':[{'children_count':len(t.get_children()) if (type(t)==vertical_module.VerticalDescriptor) else 0, - 'class':t.__class__.__name__ } - for t in s.get_children() ]} + chapters = [chapter for chapter in course.get_children() if chapter.metadata.get('hide_from_toc', 'false').lower() != 'true'] + courseware = [{'chapter_name': c.display_name, + 'sections': [{'section_name': s.display_name, + 'clickable_tab_count': len(s.get_children()) if (type(s) == seq_module.SequenceDescriptor) else 0, + 'tabs': [{'children_count': len(t.get_children()) if (type(t) == vertical_module.VerticalDescriptor) else 0, + 'class': t.__class__.__name__} + for t in s.get_children()]} for s in c.get_children() if s.metadata.get('hide_from_toc', 'false').lower() != 'true']} - for c in chapters ] + for c in chapters] return courseware + def process_section(element, num_tabs=0): ''' Process section reads through whatever is in 'course-content' and classifies it according to sequence module type. diff --git a/lms/djangoapps/courseware/features/courseware.py b/lms/djangoapps/courseware/features/courseware.py index 05ecd63f4b4..7e99cc9f55f 100644 --- a/lms/djangoapps/courseware/features/courseware.py +++ b/lms/djangoapps/courseware/features/courseware.py @@ -1,7 +1,8 @@ from lettuce import world, step from lettuce.django import django_url + @step('I visit the courseware URL$') def i_visit_the_course_info_url(step): url = django_url('/courses/MITx/6.002x/2012_Fall/courseware') - world.browser.visit(url) \ No newline at end of file + world.browser.visit(url) diff --git a/lms/djangoapps/courseware/features/courseware_common.py b/lms/djangoapps/courseware/features/courseware_common.py index 8850c88feff..5ee21da9062 100644 --- a/lms/djangoapps/courseware/features/courseware_common.py +++ b/lms/djangoapps/courseware/features/courseware_common.py @@ -1,36 +1,43 @@ from lettuce import world, step from lettuce.django import django_url + @step('I click on View Courseware') def i_click_on_view_courseware(step): css = 'p.enter-course' world.browser.find_by_css(css).first.click() + @step('I click on the "([^"]*)" tab$') def i_click_on_the_tab(step, tab): world.browser.find_link_by_text(tab).first.click() world.save_the_html() + @step('I visit the courseware URL$') def i_visit_the_course_info_url(step): url = django_url('/courses/MITx/6.002x/2012_Fall/courseware') world.browser.visit(url) + @step(u'I do not see "([^"]*)" anywhere on the page') def i_do_not_see_text_anywhere_on_the_page(step, text): - assert world.browser.is_text_not_present(text) + assert world.browser.is_text_not_present(text) + @step(u'I am on the dashboard page$') def i_am_on_the_dashboard_page(step): assert world.browser.is_element_present_by_css('section.courses') assert world.browser.url == django_url('/dashboard') + @step('the "([^"]*)" tab is active$') def the_tab_is_active(step, tab): css = '.course-tabs a.active' active_tab = world.browser.find_by_css(css) assert (active_tab.text == tab) + @step('the login dialog is visible$') def login_dialog_visible(step): css = 'form#login_form.login_form' diff --git a/lms/djangoapps/courseware/features/openended.py b/lms/djangoapps/courseware/features/openended.py index d37f9a0fae5..0725a051fff 100644 --- a/lms/djangoapps/courseware/features/openended.py +++ b/lms/djangoapps/courseware/features/openended.py @@ -4,29 +4,33 @@ from nose.tools import assert_equals, assert_in from logging import getLogger logger = getLogger(__name__) + @step('I navigate to an openended question$') def navigate_to_an_openended_question(step): world.register_by_course_id('MITx/3.091x/2012_Fall') - world.log_in('robot@edx.org','test') + world.log_in('robot@edx.org', 'test') problem = '/courses/MITx/3.091x/2012_Fall/courseware/Week_10/Polymer_Synthesis/' world.browser.visit(django_url(problem)) tab_css = 'ol#sequence-list > li > a[data-element="5"]' world.browser.find_by_css(tab_css).click() + @step('I navigate to an openended question as staff$') def navigate_to_an_openended_question_as_staff(step): world.register_by_course_id('MITx/3.091x/2012_Fall', True) - world.log_in('robot@edx.org','test') + world.log_in('robot@edx.org', 'test') problem = '/courses/MITx/3.091x/2012_Fall/courseware/Week_10/Polymer_Synthesis/' world.browser.visit(django_url(problem)) tab_css = 'ol#sequence-list > li > a[data-element="5"]' world.browser.find_by_css(tab_css).click() + @step(u'I enter the answer "([^"]*)"$') def enter_the_answer_text(step, text): textarea_css = 'textarea' world.browser.find_by_css(textarea_css).first.fill(text) + @step(u'I submit the answer "([^"]*)"$') def i_submit_the_answer_text(step, text): textarea_css = 'textarea' @@ -34,53 +38,62 @@ def i_submit_the_answer_text(step, text): check_css = 'input.check' world.browser.find_by_css(check_css).click() + @step('I click the link for full output$') def click_full_output_link(step): link_css = 'a.full' world.browser.find_by_css(link_css).first.click() + @step(u'I visit the staff grading page$') def i_visit_the_staff_grading_page(step): # course_u = '/courses/MITx/3.091x/2012_Fall' # sg_url = '%s/staff_grading' % course_u world.browser.click_link_by_text('Instructor') - world.browser.click_link_by_text('Staff grading') + world.browser.click_link_by_text('Staff grading') # world.browser.visit(django_url(sg_url)) + @step(u'I see the grader message "([^"]*)"$') def see_grader_message(step, msg): message_css = 'div.external-grader-message' grader_msg = world.browser.find_by_css(message_css).text assert_in(msg, grader_msg) + @step(u'I see the grader status "([^"]*)"$') def see_the_grader_status(step, status): status_css = 'div.grader-status' grader_status = world.browser.find_by_css(status_css).text assert_equals(status, grader_status) + @step('I see the red X$') def see_the_red_x(step): x_css = 'div.grader-status > span.incorrect' assert world.browser.find_by_css(x_css) + @step(u'I see the grader score "([^"]*)"$') def see_the_grader_score(step, score): score_css = 'div.result-output > p' score_text = world.browser.find_by_css(score_css).text assert_equals(score_text, 'Score: %s' % score) + @step('I see the link for full output$') def see_full_output_link(step): link_css = 'a.full' assert world.browser.find_by_css(link_css) + @step('I see the spelling grading message "([^"]*)"$') def see_spelling_msg(step, msg): spelling_css = 'div.spelling' - spelling_msg = world.browser.find_by_css(spelling_css).text + spelling_msg = world.browser.find_by_css(spelling_css).text assert_equals('Spelling: %s' % msg, spelling_msg) + @step(u'my answer is queued for instructor grading$') def answer_is_queued_for_instructor_grading(step): list_css = 'ul.problem-list > li > a' diff --git a/lms/djangoapps/courseware/features/smart-accordion.py b/lms/djangoapps/courseware/features/smart-accordion.py index 95d3396f577..7c4770d632a 100644 --- a/lms/djangoapps/courseware/features/smart-accordion.py +++ b/lms/djangoapps/courseware/features/smart-accordion.py @@ -7,6 +7,7 @@ from courses import * from logging import getLogger logger = getLogger(__name__) + def check_for_errors(): e = world.browser.find_by_css('.outside-app') if len(e) > 0: @@ -14,6 +15,7 @@ def check_for_errors(): else: assert True + @step(u'I verify all the content of each course') def i_verify_all_the_content_of_each_course(step): all_possible_courses = get_courses() @@ -34,11 +36,11 @@ def i_verify_all_the_content_of_each_course(step): check_for_errors() # Get the course. E.g. 'MITx/6.002x/2012_Fall' - current_course = sub('/info','', sub('.*/courses/', '', world.browser.url)) - validate_course(current_course,ids) + current_course = sub('/info', '', sub('.*/courses/', '', world.browser.url)) + validate_course(current_course, ids) world.browser.find_link_by_text('Courseware').click() - assert world.browser.is_element_present_by_id('accordion',wait_time=2) + assert world.browser.is_element_present_by_id('accordion', wait_time=2) check_for_errors() browse_course(current_course) @@ -46,6 +48,7 @@ def i_verify_all_the_content_of_each_course(step): world.browser.find_by_css('.user-link').click() check_for_errors() + def browse_course(course_id): ## count chapters from xml and page and compare @@ -91,7 +94,7 @@ def browse_course(course_id): world.browser.find_by_css('#accordion > nav > div')[chapter_it].find_by_tag('li')[section_it].find_by_tag('a').click() ## sometimes the course-content takes a long time to load - assert world.browser.is_element_present_by_css('.course-content',wait_time=5) + assert world.browser.is_element_present_by_css('.course-content', wait_time=5) ## look for server error div check_for_errors() @@ -108,7 +111,7 @@ def browse_course(course_id): rendered_tabs = 0 num_rendered_tabs = 0 - msg = ('%d tabs expected, %d tabs found, %s - %d - %s' % + msg = ('%d tabs expected, %d tabs found, %s - %d - %s' % (num_tabs, num_rendered_tabs, course_id, section_it, sections[section_it]['section_name'])) #logger.debug(msg) @@ -132,7 +135,7 @@ def browse_course(course_id): tab_class = tabs[tab_it]['class'] if tab_children != 0: rendered_items = world.browser.find_by_css('div#seq_content > section > ol > li > section') - num_rendered_items = len(rendered_items) + num_rendered_items = len(rendered_items) msg = ('%d items expected, %d items found, %s - %d - %s - tab %d' % (tab_children, num_rendered_items, course_id, section_it, sections[section_it]['section_name'], tab_it)) #logger.debug(msg) diff --git a/lms/djangoapps/courseware/grades.py b/lms/djangoapps/courseware/grades.py index f532e6c530b..62dd656fe1f 100644 --- a/lms/djangoapps/courseware/grades.py +++ b/lms/djangoapps/courseware/grades.py @@ -18,15 +18,17 @@ from models import StudentModule log = logging.getLogger("mitx.courseware") + def yield_module_descendents(module): stack = module.get_display_items() stack.reverse() while len(stack) > 0: next_module = stack.pop() - stack.extend( next_module.get_display_items() ) + stack.extend(next_module.get_display_items()) yield next_module + def yield_dynamic_descriptor_descendents(descriptor, module_creator): """ This returns all of the descendants of a descriptor. If the descriptor @@ -39,15 +41,15 @@ def yield_dynamic_descriptor_descendents(descriptor, module_creator): return module.get_child_descriptors() else: return descriptor.get_children() - - + + stack = [descriptor] while len(stack) > 0: next_descriptor = stack.pop() - stack.extend( get_dynamic_descriptor_children(next_descriptor) ) + stack.extend(get_dynamic_descriptor_children(next_descriptor)) yield next_descriptor - + def yield_problems(request, course, student): """ @@ -88,6 +90,7 @@ def yield_problems(request, course, student): if isinstance(problem, CapaModule): yield problem + def answer_distributions(request, course): """ Given a course_descriptor, compute frequencies of answers for each problem: @@ -150,7 +153,7 @@ def grade(student, request, course, student_module_cache=None, keep_raw_scores=F section_name = section_descriptor.metadata.get('display_name') should_grade_section = False - # If we haven't seen a single problem in the section, we don't have to grade it at all! We can assume 0% + # If we haven't seen a single problem in the section, we don't have to grade it at all! We can assume 0% for moduledescriptor in section['xmoduledescriptors']: if student_module_cache.lookup( course.id, moduledescriptor.category, moduledescriptor.location.url()): @@ -159,20 +162,20 @@ def grade(student, request, course, student_module_cache=None, keep_raw_scores=F if should_grade_section: scores = [] - + def create_module(descriptor): # TODO: We need the request to pass into here. If we could forgo that, our arguments # would be simpler - return get_module(student, request, descriptor.location, + return get_module(student, request, descriptor.location, student_module_cache, course.id) - + for module_descriptor in yield_dynamic_descriptor_descendents(section_descriptor, create_module): - + (correct, total) = get_score(course.id, student, module_descriptor, create_module, student_module_cache) if correct is None and total is None: continue - if settings.GENERATE_PROFILE_SCORES: # for debugging! + if settings.GENERATE_PROFILE_SCORES: # for debugging! if total > 1: correct = random.randrange(max(total - 2, 1), total + 1) else: @@ -208,12 +211,13 @@ def grade(student, request, course, student_module_cache=None, keep_raw_scores=F letter_grade = grade_for_percentage(course.grade_cutoffs, grade_summary['percent']) grade_summary['grade'] = letter_grade - grade_summary['totaled_scores'] = totaled_scores # make this available, eg for instructor download & debugging + grade_summary['totaled_scores'] = totaled_scores # make this available, eg for instructor download & debugging if keep_raw_scores: grade_summary['raw_scores'] = raw_scores # way to get all RAW scores out to instructor # so grader can be double-checked return grade_summary + def grade_for_percentage(grade_cutoffs, percentage): """ Returns a letter grade as defined in grading_policy (e.g. 'A' 'B' 'C' for 6.002x) or None. @@ -225,7 +229,7 @@ def grade_for_percentage(grade_cutoffs, percentage): """ letter_grade = None - + # Possible grades, sorted in descending order of score descending_grades = sorted(grade_cutoffs, key=lambda x: grade_cutoffs[x], reverse=True) for possible_grade in descending_grades: @@ -255,13 +259,13 @@ def progress_summary(student, request, course, student_module_cache): course: A Descriptor containing the course to grade student_module_cache: A StudentModuleCache initialized with all instance_modules for the student - + If the student does not have access to load the course module, this function will return None. - + """ - - + + # TODO: We need the request to pass into here. If we could forgo that, our arguments # would be simpler course_module = get_module(student, request, @@ -270,30 +274,30 @@ def progress_summary(student, request, course, student_module_cache): if not course_module: # This student must not have access to the course. return None - + chapters = [] # Don't include chapters that aren't displayable (e.g. due to error) for chapter_module in course_module.get_display_items(): # Skip if the chapter is hidden - hidden = chapter_module.metadata.get('hide_from_toc','false') + hidden = chapter_module.metadata.get('hide_from_toc', 'false') if hidden.lower() == 'true': continue - + sections = [] for section_module in chapter_module.get_display_items(): # Skip if the section is hidden - hidden = section_module.metadata.get('hide_from_toc','false') + hidden = section_module.metadata.get('hide_from_toc', 'false') if hidden.lower() == 'true': continue - + # Same for sections graded = section_module.metadata.get('graded', False) scores = [] - + module_creator = section_module.system.get_module - + for module_descriptor in yield_dynamic_descriptor_descendents(section_module.descriptor, module_creator): - + course_id = course.id (correct, total) = get_score(course_id, student, module_descriptor, module_creator, student_module_cache) if correct is None and total is None: diff --git a/lms/djangoapps/courseware/management/commands/clean_xml.py b/lms/djangoapps/courseware/management/commands/clean_xml.py index 425dd156c1b..1989361b85a 100644 --- a/lms/djangoapps/courseware/management/commands/clean_xml.py +++ b/lms/djangoapps/courseware/management/commands/clean_xml.py @@ -12,6 +12,7 @@ from django.core.management.base import BaseCommand from xmodule.modulestore.xml import XMLModuleStore from xmodule.errortracker import make_error_tracker + def traverse_tree(course): '''Load every descriptor in course. Return bool success value.''' queue = [course] diff --git a/lms/djangoapps/courseware/management/commands/metadata_to_json.py b/lms/djangoapps/courseware/management/commands/metadata_to_json.py index 8ef0dee7b3b..b80736f693f 100644 --- a/lms/djangoapps/courseware/management/commands/metadata_to_json.py +++ b/lms/djangoapps/courseware/management/commands/metadata_to_json.py @@ -14,6 +14,7 @@ from django.core.management.base import BaseCommand from xmodule.modulestore.xml import XMLModuleStore from xmodule.x_module import policy_key + def import_course(course_dir, verbose=True): course_dir = path(course_dir) data_dir = course_dir.dirname() @@ -44,6 +45,7 @@ def import_course(course_dir, verbose=True): return course + def node_metadata(node): # make a copy to_export = ('format', 'display_name', @@ -55,6 +57,7 @@ def node_metadata(node): d = {k: orig[k] for k in to_export if k in orig} return d + def get_metadata(course): d = OrderedDict({}) queue = [course] diff --git a/lms/djangoapps/courseware/migrations/0005_auto__add_offlinecomputedgrade__add_unique_offlinecomputedgrade_user_c.py b/lms/djangoapps/courseware/migrations/0005_auto__add_offlinecomputedgrade__add_unique_offlinecomputedgrade_user_c.py index 674f97cec88..4c21d229376 100644 --- a/lms/djangoapps/courseware/migrations/0005_auto__add_offlinecomputedgrade__add_unique_offlinecomputedgrade_user_c.py +++ b/lms/djangoapps/courseware/migrations/0005_auto__add_offlinecomputedgrade__add_unique_offlinecomputedgrade_user_c.py @@ -114,4 +114,4 @@ class Migration(SchemaMigration): } } - complete_apps = ['courseware'] \ No newline at end of file + complete_apps = ['courseware'] diff --git a/lms/djangoapps/courseware/models.py b/lms/djangoapps/courseware/models.py index bd01318f63d..93a4beaafdf 100644 --- a/lms/djangoapps/courseware/models.py +++ b/lms/djangoapps/courseware/models.py @@ -113,7 +113,7 @@ class StudentModuleCache(object): descriptor_filter=lambda descriptor: True, select_for_update=False): """ - obtain and return cache for descriptor descendents (ie children) AND modules required by the descriptor, + obtain and return cache for descriptor descendents (ie children) AND modules required by the descriptor, but which are not children of the module course_id: the course in the context of which we want StudentModules. @@ -212,7 +212,7 @@ class OfflineComputedGradeLog(models.Model): course_id = models.CharField(max_length=255, db_index=True) created = models.DateTimeField(auto_now_add=True, null=True, db_index=True) - seconds = models.IntegerField(default=0) # seconds elapsed for computation + seconds = models.IntegerField(default=0) # seconds elapsed for computation nstudents = models.IntegerField(default=0) def __unicode__(self): diff --git a/lms/djangoapps/courseware/module_render.py b/lms/djangoapps/courseware/module_render.py index b19796b357e..ded84a971e4 100644 --- a/lms/djangoapps/courseware/module_render.py +++ b/lms/djangoapps/courseware/module_render.py @@ -91,7 +91,7 @@ def toc_for_course(user, request, course, active_chapter, active_section): chapters = list() for chapter in course_module.get_display_items(): - hide_from_toc = chapter.metadata.get('hide_from_toc','false').lower() == 'true' + hide_from_toc = chapter.metadata.get('hide_from_toc', 'false').lower() == 'true' if hide_from_toc: continue @@ -166,6 +166,7 @@ def get_module_for_descriptor(user, request, descriptor, student_module_cache, c return _get_module(user, request, descriptor, student_module_cache, course_id, position=position, wrap_xmodule_display=wrap_xmodule_display) + def _get_module(user, request, descriptor, student_module_cache, course_id, position=None, wrap_xmodule_display=True): """ @@ -287,7 +288,7 @@ def _get_module(user, request, descriptor, student_module_cache, course_id, module.get_html = replace_static_urls( _get_html, module.metadata.get('data_dir', ''), - course_namespace = module.location._replace(category=None, name=None)) + course_namespace=module.location._replace(category=None, name=None)) # Allow URLs of the form '/course/' refer to the root of multicourse directory # hierarchy of this course @@ -300,6 +301,8 @@ def _get_module(user, request, descriptor, student_module_cache, course_id, return module # TODO (vshnayder): Rename this? It's very confusing. + + def get_instance_module(course_id, user, module, student_module_cache): """ Returns the StudentModule specific to this module for this student, @@ -329,6 +332,7 @@ def get_instance_module(course_id, user, module, student_module_cache): else: return None + def get_shared_instance_module(course_id, user, module, student_module_cache): """ Return shared_module is a StudentModule specific to all modules with the same @@ -359,6 +363,7 @@ def get_shared_instance_module(course_id, user, module, student_module_cache): else: return None + @csrf_exempt def xqueue_callback(request, course_id, userid, id, dispatch): ''' @@ -415,8 +420,8 @@ def xqueue_callback(request, course_id, userid, id, dispatch): instance_module.save() #Bin score into range and increment stats - score_bucket=get_score_bucket(instance_module.grade, instance_module.max_grade) - org, course_num, run=course_id.split("/") + score_bucket = get_score_bucket(instance_module.grade, instance_module.max_grade) + org, course_num, run = course_id.split("/") statsd.increment("lms.courseware.question_answered", tags=["org:{0}".format(org), "course:{0}".format(course_num), @@ -456,9 +461,9 @@ def modx_dispatch(request, dispatch, location, course_id): return HttpResponse(json.dumps({'success': too_many_files_msg})) for inputfile in inputfiles: - if inputfile.size > settings.STUDENT_FILEUPLOAD_MAX_SIZE: # Bytes + if inputfile.size > settings.STUDENT_FILEUPLOAD_MAX_SIZE: # Bytes file_too_big_msg = 'Submission aborted! Your file "%s" is too large (max size: %d MB)' %\ - (inputfile.name, settings.STUDENT_FILEUPLOAD_MAX_SIZE/(1000**2)) + (inputfile.name, settings.STUDENT_FILEUPLOAD_MAX_SIZE / (1000 ** 2)) return HttpResponse(json.dumps({'success': file_too_big_msg})) p[fileinput_id] = inputfiles @@ -499,7 +504,7 @@ def modx_dispatch(request, dispatch, location, course_id): # Don't track state for anonymous users (who don't have student modules) if instance_module is not None: instance_module.state = instance.get_instance_state() - instance_module.max_grade=instance.max_score() + instance_module.max_grade = instance.max_score() if instance.get_score(): instance_module.grade = instance.get_score()['score'] if (instance_module.grade != oldgrade or @@ -508,8 +513,8 @@ def modx_dispatch(request, dispatch, location, course_id): instance_module.save() #Bin score into range and increment stats - score_bucket=get_score_bucket(instance_module.grade, instance_module.max_grade) - org, course_num, run=course_id.split("/") + score_bucket = get_score_bucket(instance_module.grade, instance_module.max_grade) + org, course_num, run = course_id.split("/") statsd.increment("lms.courseware.question_answered", tags=["org:{0}".format(org), "course:{0}".format(course_num), @@ -526,6 +531,7 @@ def modx_dispatch(request, dispatch, location, course_id): # Return whatever the module wanted to return to the client/caller return HttpResponse(ajax_return) + def preview_chemcalc(request): """ Render an html preview of a chemical formula or equation. The fact that @@ -544,7 +550,7 @@ def preview_chemcalc(request): raise Http404 result = {'preview': '', - 'error': '' } + 'error': ''} formula = request.GET.get('formula') if formula is None: result['error'] = "No formula specified." @@ -563,17 +569,15 @@ def preview_chemcalc(request): return HttpResponse(json.dumps(result)) -def get_score_bucket(grade,max_grade): +def get_score_bucket(grade, max_grade): """ Function to split arbitrary score ranges into 3 buckets. Used with statsd tracking. """ - score_bucket="incorrect" - if(grade>0 and grade<max_grade): - score_bucket="partial" - elif(grade==max_grade): - score_bucket="correct" + score_bucket = "incorrect" + if(grade > 0 and grade < max_grade): + score_bucket = "partial" + elif(grade == max_grade): + score_bucket = "correct" return score_bucket - - diff --git a/lms/djangoapps/courseware/tabs.py b/lms/djangoapps/courseware/tabs.py index 4f5a881d977..a47141b1837 100644 --- a/lms/djangoapps/courseware/tabs.py +++ b/lms/djangoapps/courseware/tabs.py @@ -33,6 +33,7 @@ from open_ended_grading import open_ended_notifications log = logging.getLogger(__name__) + class InvalidTabsException(Exception): """ A complaint about invalid tabs. @@ -41,6 +42,7 @@ class InvalidTabsException(Exception): CourseTabBase = namedtuple('CourseTab', 'name link is_active has_img img') + def CourseTab(name, link, is_active, has_img=False, img=""): return CourseTabBase(name, link, is_active, has_img, img) @@ -64,22 +66,26 @@ def _courseware(tab, user, course, active_page): link = reverse('courseware', args=[course.id]) return [CourseTab('Courseware', link, active_page == "courseware")] + def _course_info(tab, user, course, active_page): link = reverse('info', args=[course.id]) return [CourseTab(tab['name'], link, active_page == "info")] + def _progress(tab, user, course, active_page): if user.is_authenticated(): link = reverse('progress', args=[course.id]) return [CourseTab(tab['name'], link, active_page == "progress")] return [] + def _wiki(tab, user, course, active_page): if settings.WIKI_ENABLED: link = reverse('course_wiki', args=[course.id]) return [CourseTab(tab['name'], link, active_page == 'wiki')] return [] + def _discussion(tab, user, course, active_page): """ This tab format only supports the new Berkeley discussion forums. @@ -87,17 +93,19 @@ def _discussion(tab, user, course, active_page): if settings.MITX_FEATURES.get('ENABLE_DISCUSSION_SERVICE'): link = reverse('django_comment_client.forum.views.forum_form_discussion', args=[course.id]) - return [CourseTab(tab['name'], link, active_page=='discussion')] + return [CourseTab(tab['name'], link, active_page == 'discussion')] return [] + def _external_link(tab, user, course, active_page): # external links are never active return [CourseTab(tab['name'], tab['link'], False)] + def _static_tab(tab, user, course, active_page): link = reverse('static_tab', args=[course.id, tab['url_slug']]) active_str = 'static_tab_{0}'.format(tab['url_slug']) - return [CourseTab(tab['name'], link, active_page==active_str)] + return [CourseTab(tab['name'], link, active_page == active_str)] def _textbooks(tab, user, course, active_page): @@ -107,7 +115,7 @@ def _textbooks(tab, user, course, active_page): if user.is_authenticated() and settings.MITX_FEATURES.get('ENABLE_TEXTBOOK'): # since there can be more than one textbook, active_page is e.g. "book/0". return [CourseTab(textbook.title, reverse('book', args=[course.id, index]), - active_page=="textbook/{0}".format(index)) + active_page == "textbook/{0}".format(index)) for index, textbook in enumerate(course.textbooks)] return [] @@ -126,6 +134,7 @@ def _staff_grading(tab, user, course, active_page): return tab return [] + def _peer_grading(tab, user, course, active_page): if user.is_authenticated(): @@ -140,6 +149,7 @@ def _peer_grading(tab, user, course, active_page): return tab return [] + def _combined_open_ended_grading(tab, user, course, active_page): if user.is_authenticated(): link = reverse('open_ended_notifications', args=[course.id]) @@ -171,6 +181,7 @@ def key_checker(expected_keys): need_name = key_checker(['name']) + def null_validator(d): """ Don't check anything--use for tabs that don't need any params. (e.g. textbook) @@ -235,7 +246,7 @@ def get_course_tabs(user, course, active_page): """ Return the tabs to show a particular user, as a list of CourseTab items. """ - if not hasattr(course,'tabs') or not course.tabs: + if not hasattr(course, 'tabs') or not course.tabs: return get_default_tabs(user, course, active_page) # TODO (vshnayder): There needs to be a place to call this right after course @@ -269,7 +280,7 @@ def get_default_tabs(user, course, active_page): if hasattr(course, 'syllabus_present') and course.syllabus_present: link = reverse('syllabus', args=[course.id]) - tabs.append(CourseTab('Syllabus', link, active_page=='syllabus')) + tabs.append(CourseTab('Syllabus', link, active_page == 'syllabus')) tabs.extend(_textbooks({}, user, course, active_page)) @@ -290,10 +301,11 @@ def get_default_tabs(user, course, active_page): if has_access(user, course, 'staff'): link = reverse('instructor_dashboard', args=[course.id]) - tabs.append(CourseTab('Instructor', link, active_page=='instructor')) + tabs.append(CourseTab('Instructor', link, active_page == 'instructor')) return tabs + def get_static_tab_by_slug(course, tab_slug): """ Look for a tab with type 'static_tab' and the specified 'tab_slug'. Returns @@ -308,6 +320,7 @@ def get_static_tab_by_slug(course, tab_slug): return None + def get_static_tab_contents(request, cache, course, tab): loc = Location(course.location.tag, course.location.org, course.location.course, 'static_tab', tab['url_slug']) diff --git a/lms/djangoapps/courseware/tests/__init__.py b/lms/djangoapps/courseware/tests/__init__.py index 8b137891791..e69de29bb2d 100644 --- a/lms/djangoapps/courseware/tests/__init__.py +++ b/lms/djangoapps/courseware/tests/__init__.py @@ -1 +0,0 @@ - diff --git a/lms/djangoapps/courseware/tests/factories.py b/lms/djangoapps/courseware/tests/factories.py index 6950e285654..a84b2b8475f 100644 --- a/lms/djangoapps/courseware/tests/factories.py +++ b/lms/djangoapps/courseware/tests/factories.py @@ -5,6 +5,7 @@ from django.contrib.auth.models import Group from datetime import datetime import uuid + class UserProfileFactory(factory.Factory): FACTORY_FOR = UserProfile @@ -12,12 +13,14 @@ class UserProfileFactory(factory.Factory): name = 'Robot Studio' courseware = 'course.xml' + class RegistrationFactory(factory.Factory): FACTORY_FOR = Registration user = None activation_key = uuid.uuid4().hex + class UserFactory(factory.Factory): FACTORY_FOR = User @@ -32,11 +35,13 @@ class UserFactory(factory.Factory): last_login = datetime.now() date_joined = datetime.now() + class GroupFactory(factory.Factory): FACTORY_FOR = Group name = 'test_group' + class CourseEnrollmentAllowedFactory(factory.Factory): FACTORY_FOR = CourseEnrollmentAllowed diff --git a/lms/djangoapps/courseware/tests/test_access.py b/lms/djangoapps/courseware/tests/test_access.py index ed9335d3823..c0b28e7803e 100644 --- a/lms/djangoapps/courseware/tests/test_access.py +++ b/lms/djangoapps/courseware/tests/test_access.py @@ -3,10 +3,11 @@ import time from mock import Mock from django.test import TestCase -from xmodule.modulestore import Location +from xmodule.modulestore import Location from factories import CourseEnrollmentAllowedFactory import courseware.access as access + class AccessTestCase(TestCase): def test__has_global_staff_access(self): u = Mock(is_staff=False) @@ -44,13 +45,13 @@ class AccessTestCase(TestCase): self.assertTrue(access._has_access_to_location(u, location, 'instructor', None)) - # A user does not have staff access if they are + # A user does not have staff access if they are # not in either the staff or the the instructor group g.name = 'student_only' self.assertFalse(access._has_access_to_location(u, location, 'staff', None)) - # A user does not have instructor access if they are + # A user does not have instructor access if they are # not in the instructor group g.name = 'student_only' self.assertFalse(access._has_access_to_location(u, location, @@ -69,7 +70,7 @@ class AccessTestCase(TestCase): # TODO: override DISABLE_START_DATES and test the start date branch of the method u = Mock() d = Mock() - d.start = time.gmtime(time.time() - 86400) # make sure the start time is in the past + d.start = time.gmtime(time.time() - 86400) # make sure the start time is in the past # Always returns true because DISABLE_START_DATES is set in test.py self.assertTrue(access._has_access_descriptor(u, d, 'load')) @@ -105,5 +106,5 @@ class AccessTestCase(TestCase): c.metadata.get = 'is_public' self.assertTrue(access._has_access_course_desc(u, c, 'enroll')) - # TODO: + # TODO: # Non-staff cannot enroll outside the open enrollment period if not specifically allowed diff --git a/lms/djangoapps/courseware/tests/tests.py b/lms/djangoapps/courseware/tests/tests.py index 6c41cbac14e..efa5ad823ee 100644 --- a/lms/djangoapps/courseware/tests/tests.py +++ b/lms/djangoapps/courseware/tests/tests.py @@ -31,6 +31,7 @@ from xmodule.modulestore.xml_importer import import_from_xml from xmodule.modulestore.xml import XMLModuleStore from xmodule.timeparse import stringify_time + def parse_json(response): """Parse response, which is assumed to be json""" return json.loads(response.content) @@ -49,6 +50,7 @@ def registration(email): # jump_to works with the xmlmodulestore or we have an even better solution # NOTE: this means this test requires mongo to be running. + def mongo_store_config(data_dir): return { 'default': { @@ -64,6 +66,7 @@ def mongo_store_config(data_dir): } } + def draft_mongo_store_config(data_dir): return { 'default': { @@ -79,6 +82,7 @@ def draft_mongo_store_config(data_dir): } } + def xml_store_config(data_dir): return { 'default': { @@ -95,6 +99,7 @@ TEST_DATA_XML_MODULESTORE = xml_store_config(TEST_DATA_DIR) TEST_DATA_MONGO_MODULESTORE = mongo_store_config(TEST_DATA_DIR) TEST_DATA_DRAFT_MONGO_MODULESTORE = draft_mongo_store_config(TEST_DATA_DIR) + class ActivateLoginTestCase(TestCase): '''Check that we can activate and log in''' @@ -286,13 +291,13 @@ class PageLoader(ActivateLoginTestCase): all_ok = False num_bad += 1 elif descriptor.location.category == 'static_tab': - resp = self.client.get(reverse('static_tab', kwargs={'course_id': course_id, 'tab_slug' : descriptor.location.name})) + resp = self.client.get(reverse('static_tab', kwargs={'course_id': course_id, 'tab_slug': descriptor.location.name})) msg = str(resp.status_code) if resp.status_code != 200: msg = "ERROR " + msg all_ok = False - num_bad += 1 + num_bad += 1 elif descriptor.location.category == 'course_info': resp = self.client.get(reverse('info', kwargs={'course_id': course_id})) msg = str(resp.status_code) @@ -300,7 +305,7 @@ class PageLoader(ActivateLoginTestCase): if resp.status_code != 200: msg = "ERROR " + msg all_ok = False - num_bad += 1 + num_bad += 1 elif descriptor.location.category == 'custom_tag_template': pass else: @@ -321,7 +326,7 @@ class PageLoader(ActivateLoginTestCase): # check content to make sure there were no rendering failures content = resp.content - if content.find("this module is temporarily unavailable")>=0: + if content.find("this module is temporarily unavailable") >= 0: msg = "ERROR unavailable module " all_ok = False num_bad += 1 @@ -335,7 +340,7 @@ class PageLoader(ActivateLoginTestCase): self.assertTrue(all_ok) # fail fast print "{0}/{1} good".format(n - num_bad, n) - log.info( "{0}/{1} good".format(n - num_bad, n)) + log.info("{0}/{1} good".format(n - num_bad, n)) self.assertTrue(all_ok) @@ -347,7 +352,7 @@ class TestCoursesLoadTestCase_XmlModulestore(PageLoader): def setUp(self): ActivateLoginTestCase.setUp(self) xmodule.modulestore.django._MODULESTORES = {} - + def test_toy_course_loads(self): module_store = XMLModuleStore( TEST_DATA_DIR, @@ -376,7 +381,7 @@ class TestCoursesLoadTestCase_MongoModulestore(PageLoader): ActivateLoginTestCase.setUp(self) xmodule.modulestore.django._MODULESTORES = {} modulestore().collection.drop() - + def test_toy_course_loads(self): module_store = modulestore() import_from_xml(module_store, TEST_DATA_DIR, ['toy']) @@ -431,7 +436,7 @@ class TestNavigation(PageLoader): # Now we directly navigate to a section in a different chapter self.check_for_get_code(200, reverse('courseware_section', kwargs={'course_id': self.toy.id, - 'chapter':'secret:magic', 'section':'toyvideo'})) + 'chapter': 'secret:magic', 'section': 'toyvideo'})) # And now hitting the courseware tab should redirect to 'secret:magic' resp = self.client.get(reverse('courseware', kwargs={'course_id': self.toy.id})) @@ -565,7 +570,7 @@ class TestViewAuth(PageLoader): """Actually do the test, relying on settings to be right.""" # Make courses start in the future - tomorrow = time.time() + 24*3600 + tomorrow = time.time() + 24 * 3600 self.toy.metadata['start'] = stringify_time(time.gmtime(tomorrow)) self.full.metadata['start'] = stringify_time(time.gmtime(tomorrow)) @@ -603,7 +608,7 @@ class TestViewAuth(PageLoader): def instructor_urls(course): """list of urls that only instructors/staff should be able to see""" - urls = reverse_urls(['instructor_dashboard','gradebook','grade_summary'], + urls = reverse_urls(['instructor_dashboard', 'gradebook', 'grade_summary'], course) return urls @@ -770,7 +775,7 @@ class TestCourseGrader(PageLoader): def find_course(course_id): """Assumes the course is present""" - return [c for c in courses if c.id==course_id][0] + return [c for c in courses if c.id == course_id][0] self.graded_course = find_course("edX/graded/2012_Fall") @@ -825,17 +830,17 @@ class TestCourseGrader(PageLoader): modx_url = reverse('modx_dispatch', kwargs={ - 'course_id' : self.graded_course.id, - 'location' : problem_location, - 'dispatch' : 'problem_check', } + 'course_id': self.graded_course.id, + 'location': problem_location, + 'dispatch': 'problem_check', } ) resp = self.client.post(modx_url, { 'input_i4x-edX-graded-problem-{0}_2_1'.format(problem_url_name): responses[0], 'input_i4x-edX-graded-problem-{0}_2_2'.format(problem_url_name): responses[1], }) - print "modx_url" , modx_url, "responses" , responses - print "resp" , resp + print "modx_url", modx_url, "responses", responses + print "resp", resp return resp @@ -847,9 +852,9 @@ class TestCourseGrader(PageLoader): modx_url = reverse('modx_dispatch', kwargs={ - 'course_id' : self.graded_course.id, - 'location' : problem_location, - 'dispatch' : 'problem_reset', } + 'course_id': self.graded_course.id, + 'location': problem_location, + 'dispatch': 'problem_reset', } ) resp = self.client.post(modx_url) @@ -873,7 +878,7 @@ class TestCourseGrader(PageLoader): # Only get half of the first problem correct self.submit_question_answer('H1P1', ['Correct', 'Incorrect']) self.check_grade_percent(0.06) - self.assertEqual(earned_hw_scores(), [1.0, 0, 0]) # Order matters + self.assertEqual(earned_hw_scores(), [1.0, 0, 0]) # Order matters self.assertEqual(score_for_hw('Homework1'), [1.0, 0.0]) # Get both parts of the first problem correct @@ -905,14 +910,13 @@ class TestCourseGrader(PageLoader): # Third homework self.submit_question_answer('H3P1', ['Correct', 'Correct']) - self.check_grade_percent(0.42) # Score didn't change + self.check_grade_percent(0.42) # Score didn't change self.assertEqual(earned_hw_scores(), [4.0, 4.0, 2.0]) self.submit_question_answer('H3P2', ['Correct', 'Correct']) - self.check_grade_percent(0.5) # Now homework2 dropped. Score changes + self.check_grade_percent(0.5) # Now homework2 dropped. Score changes self.assertEqual(earned_hw_scores(), [4.0, 4.0, 4.0]) # Now we answer the final question (worth half of the grade) self.submit_question_answer('FinalQuestion', ['Correct', 'Correct']) - self.check_grade_percent(1.0) # Hooray! We got 100% - + self.check_grade_percent(1.0) # Hooray! We got 100% diff --git a/lms/djangoapps/courseware/views.py b/lms/djangoapps/courseware/views.py index 5d65d7c632f..02a4b5f5f22 100644 --- a/lms/djangoapps/courseware/views.py +++ b/lms/djangoapps/courseware/views.py @@ -137,6 +137,7 @@ def redirect_to_course_position(course_module, first_time): 'chapter': chapter.url_name, 'section': section.url_name})) + def save_child_position(seq_module, child_name, instance_module): """ child_name: url_name of the child @@ -152,6 +153,7 @@ def save_child_position(seq_module, child_name, instance_module): instance_module.state = seq_module.get_instance_state() instance_module.save() + @login_required @ensure_csrf_cookie @cache_control(no_cache=True, no_store=True, must_revalidate=True) @@ -184,7 +186,7 @@ def index(request, course_id, chapter=None, section=None, registered = registered_for_course(course, request.user) if not registered: # TODO (vshnayder): do course instructors need to be registered to see course? - log.debug('User %s tried to view course %s but is not enrolled' % (request.user,course.location.url())) + log.debug('User %s tried to view course %s but is not enrolled' % (request.user, course.location.url())) return redirect(reverse('about_course', args=[course.id])) try: @@ -212,7 +214,7 @@ def index(request, course_id, chapter=None, section=None, 'init': '', 'content': '', 'staff_access': staff_access, - 'xqa_server': settings.MITX_FEATURES.get('USE_XQA_SERVER','http://xqa:server@content-qa.mitx.mit.edu/xqa') + 'xqa_server': settings.MITX_FEATURES.get('USE_XQA_SERVER', 'http://xqa:server@content-qa.mitx.mit.edu/xqa') } chapter_descriptor = course.get_child_by(lambda m: m.url_name == chapter) @@ -288,7 +290,7 @@ def index(request, course_id, chapter=None, section=None, try: result = render_to_response('courseware/courseware-error.html', {'staff_access': staff_access, - 'course' : course}) + 'course': course}) except: # Let the exception propagate, relying on global config to at # at least return a nice error message @@ -297,6 +299,7 @@ def index(request, course_id, chapter=None, section=None, return result + @ensure_csrf_cookie def jump_to(request, course_id, location): ''' @@ -333,6 +336,7 @@ def jump_to(request, course_id, location): else: return redirect('courseware_position', course_id=course_id, chapter=chapter, section=section, position=position) + @ensure_csrf_cookie def course_info(request, course_id): """ @@ -343,9 +347,10 @@ def course_info(request, course_id): course = get_course_with_access(request.user, course_id, 'load') staff_access = has_access(request.user, course, 'staff') - return render_to_response('courseware/info.html', {'request' : request, 'course_id' : course_id, 'cache' : None, + return render_to_response('courseware/info.html', {'request': request, 'course_id': course_id, 'cache': None, 'course': course, 'staff_access': staff_access}) + @ensure_csrf_cookie def static_tab(request, course_id, tab_slug): """ @@ -368,9 +373,11 @@ def static_tab(request, course_id, tab_slug): {'course': course, 'tab': tab, 'tab_contents': contents, - 'staff_access': staff_access,}) + 'staff_access': staff_access, }) # TODO arjun: remove when custom tabs in place, see courseware/syllabus.py + + @ensure_csrf_cookie def syllabus(request, course_id): """ @@ -382,7 +389,7 @@ def syllabus(request, course_id): staff_access = has_access(request.user, course, 'staff') return render_to_response('courseware/syllabus.html', {'course': course, - 'staff_access': staff_access,}) + 'staff_access': staff_access, }) def registered_for_course(course, user): @@ -394,6 +401,7 @@ def registered_for_course(course, user): else: return False + @ensure_csrf_cookie @cache_if_anonymous def course_about(request, course_id): @@ -412,7 +420,7 @@ def course_about(request, course_id): {'course': course, 'registered': registered, 'course_target': course_target, - 'show_courseware_link' : show_courseware_link}) + 'show_courseware_link': show_courseware_link}) @ensure_csrf_cookie @@ -425,6 +433,7 @@ def static_university_profile(request, org_id): context = dict(courses=[], org_id=org_id) return render_to_response(template_file, context) + @ensure_csrf_cookie @cache_if_anonymous def university_profile(request, org_id): @@ -446,6 +455,7 @@ def university_profile(request, org_id): return render_to_response(template_file, context) + def render_notifications(request, course, notifications): context = { 'notifications': notifications, @@ -454,6 +464,7 @@ def render_notifications(request, course, notifications): } return render_to_string('courseware/notifications.html', context) + @login_required def news(request, course_id): course = get_course_with_access(request.user, course_id, 'load') @@ -467,6 +478,7 @@ def news(request, course_id): return render_to_response('courseware/news.html', context) + @login_required @cache_control(no_cache=True, no_store=True, must_revalidate=True) def progress(request, course_id, student_id=None): diff --git a/lms/djangoapps/dashboard/views.py b/lms/djangoapps/dashboard/views.py index 964b3fac4a0..f5929f241b8 100644 --- a/lms/djangoapps/dashboard/views.py +++ b/lms/djangoapps/dashboard/views.py @@ -3,6 +3,7 @@ import json from datetime import datetime from django.http import HttpResponse, Http404 + def dictfetchall(cursor): '''Returns all rows from a cursor as a dict. Borrowed from Django documentation''' @@ -12,23 +13,24 @@ def dictfetchall(cursor): for row in cursor.fetchall() ] + def dashboard(request): """ Quick hack to show staff enrollment numbers. This should be replaced with a real dashboard later. This version is a short-term - bandaid for the next couple weeks. + bandaid for the next couple weeks. """ if not request.user.is_staff: raise Http404 - queries=[] + queries = [] queries.append("select count(user_id) as students, course_id from student_courseenrollment group by course_id order by students desc;") queries.append("select count(distinct user_id) as unique_students from student_courseenrollment;") queries.append("select registrations, count(registrations) from (select count(user_id) as registrations from student_courseenrollment group by user_id) as registrations_per_user group by registrations;") - + from django.db import connection cursor = connection.cursor() - results =[] + results = [] for query in queries: cursor.execute(query) diff --git a/lms/djangoapps/django_comment_client/base/urls.py b/lms/djangoapps/django_comment_client/base/urls.py index 23f2afa037f..d8fd4927fb2 100644 --- a/lms/djangoapps/django_comment_client/base/urls.py +++ b/lms/djangoapps/django_comment_client/base/urls.py @@ -23,7 +23,7 @@ urlpatterns = patterns('django_comment_client.base.views', url(r'comments/(?P<comment_id>[\w\-]+)/upvote$', 'vote_for_comment', {'value': 'up'}, name='upvote_comment'), url(r'comments/(?P<comment_id>[\w\-]+)/downvote$', 'vote_for_comment', {'value': 'down'}, name='downvote_comment'), url(r'comments/(?P<comment_id>[\w\-]+)/unvote$', 'undo_vote_for_comment', name='undo_vote_for_comment'), - + url(r'^(?P<commentable_id>[\w\-.]+)/threads/create$', 'create_thread', name='create_thread'), # TODO should we search within the board? url(r'^(?P<commentable_id>[\w\-.]+)/threads/search_similar$', 'search_similar_threads', name='search_similar_threads'), diff --git a/lms/djangoapps/django_comment_client/base/views.py b/lms/djangoapps/django_comment_client/base/views.py index 777c7bafceb..7ca00cb37c3 100644 --- a/lms/djangoapps/django_comment_client/base/views.py +++ b/lms/djangoapps/django_comment_client/base/views.py @@ -30,6 +30,7 @@ from django_comment_client.models import Role log = logging.getLogger(__name__) + def permitted(fn): @functools.wraps(fn) def wrapper(request, *args, **kwargs): @@ -47,6 +48,7 @@ def permitted(fn): return JsonError("unauthorized", status=401) return wrapper + def ajax_content_response(request, course_id, content, template_name): context = { 'course_id': course_id, @@ -82,11 +84,11 @@ def create_thread(request, course_id, commentable_id): thread = cc.Thread(**extract(post, ['body', 'title', 'tags'])) thread.update_attributes(**{ - 'anonymous' : anonymous, - 'anonymous_to_peers' : anonymous_to_peers, - 'commentable_id' : commentable_id, - 'course_id' : course_id, - 'user_id' : request.user.id, + 'anonymous': anonymous, + 'anonymous_to_peers': anonymous_to_peers, + 'commentable_id': commentable_id, + 'course_id': course_id, + 'user_id': request.user.id, }) @@ -118,6 +120,7 @@ def create_thread(request, course_id, commentable_id): else: return JsonResponse(utils.safe_content(data)) + @require_POST @login_required @permitted @@ -130,6 +133,7 @@ def update_thread(request, course_id, thread_id): else: return JsonResponse(utils.safe_content(thread.to_dict())) + def _create_comment(request, course_id, thread_id=None, parent_id=None): post = request.POST comment = cc.Comment(**extract(post, ['body'])) @@ -146,12 +150,12 @@ def _create_comment(request, course_id, thread_id=None, parent_id=None): anonymous_to_peers = False comment.update_attributes(**{ - 'anonymous' : anonymous, - 'anonymous_to_peers' : anonymous_to_peers, - 'user_id' : request.user.id, - 'course_id' : course_id, - 'thread_id' : thread_id, - 'parent_id' : parent_id, + 'anonymous': anonymous, + 'anonymous_to_peers': anonymous_to_peers, + 'user_id': request.user.id, + 'course_id': course_id, + 'thread_id': thread_id, + 'parent_id': parent_id, }) comment.save() if post.get('auto_subscribe', 'false').lower() == 'true': @@ -162,6 +166,7 @@ def _create_comment(request, course_id, thread_id=None, parent_id=None): else: return JsonResponse(utils.safe_content(comment.to_dict())) + @require_POST @login_required @permitted @@ -171,6 +176,7 @@ def create_comment(request, course_id, thread_id): return JsonError("Comment level too deep") return _create_comment(request, course_id, thread_id=thread_id) + @require_POST @login_required @permitted @@ -179,6 +185,7 @@ def delete_thread(request, course_id, thread_id): thread.delete() return JsonResponse(utils.safe_content(thread.to_dict())) + @require_POST @login_required @permitted @@ -191,6 +198,7 @@ def update_comment(request, course_id, comment_id): else: return JsonResponse(utils.safe_content(comment.to_dict())) + @require_POST @login_required @permitted @@ -200,6 +208,7 @@ def endorse_comment(request, course_id, comment_id): comment.save() return JsonResponse(utils.safe_content(comment.to_dict())) + @require_POST @login_required @permitted @@ -213,6 +222,7 @@ def openclose_thread(request, course_id, thread_id): 'ability': utils.get_ability(course_id, thread, request.user), }) + @require_POST @login_required @permitted @@ -222,6 +232,7 @@ def create_sub_comment(request, course_id, comment_id): return JsonError("Comment level too deep") return _create_comment(request, course_id, parent_id=comment_id) + @require_POST @login_required @permitted @@ -230,6 +241,7 @@ def delete_comment(request, course_id, comment_id): comment.delete() return JsonResponse(utils.safe_content(comment.to_dict())) + @require_POST @login_required @permitted @@ -239,6 +251,7 @@ def vote_for_comment(request, course_id, comment_id, value): user.vote(comment, value) return JsonResponse(utils.safe_content(comment.to_dict())) + @require_POST @login_required @permitted @@ -248,6 +261,7 @@ def undo_vote_for_comment(request, course_id, comment_id): user.unvote(comment) return JsonResponse(utils.safe_content(comment.to_dict())) + @require_POST @login_required @permitted @@ -257,6 +271,7 @@ def vote_for_thread(request, course_id, thread_id, value): user.vote(thread, value) return JsonResponse(utils.safe_content(thread.to_dict())) + @require_POST @login_required @permitted @@ -276,6 +291,7 @@ def follow_thread(request, course_id, thread_id): user.follow(thread) return JsonResponse({}) + @require_POST @login_required @permitted @@ -285,6 +301,7 @@ def follow_commentable(request, course_id, commentable_id): user.follow(commentable) return JsonResponse({}) + @require_POST @login_required @permitted @@ -294,6 +311,7 @@ def follow_user(request, course_id, followed_user_id): user.follow(followed_user) return JsonResponse({}) + @require_POST @login_required @permitted @@ -303,6 +321,7 @@ def unfollow_thread(request, course_id, thread_id): user.unfollow(thread) return JsonResponse({}) + @require_POST @login_required @permitted @@ -312,6 +331,7 @@ def unfollow_commentable(request, course_id, commentable_id): user.unfollow(commentable) return JsonResponse({}) + @require_POST @login_required @permitted @@ -321,6 +341,7 @@ def unfollow_user(request, course_id, followed_user_id): user.unfollow(followed_user) return JsonResponse({}) + @require_POST @login_required @permitted @@ -351,6 +372,7 @@ def update_moderator_status(request, course_id, user_id): else: return JsonResponse({}) + @require_GET def search_similar_threads(request, course_id, commentable_id): text = request.GET.get('text', None) @@ -362,11 +384,12 @@ def search_similar_threads(request, course_id, commentable_id): threads = cc.search_similar_threads(course_id, recursive=False, query_params=query_params) else: theads = [] - context = { 'threads': map(utils.extend_content, threads) } + context = {'threads': map(utils.extend_content, threads)} return JsonResponse({ 'html': render_to_string('discussion/_similar_posts.html', context) }) + @require_GET def tags_autocomplete(request, course_id): value = request.GET.get('q', None) @@ -375,10 +398,11 @@ def tags_autocomplete(request, course_id): results = cc.tags_autocomplete(value) return JsonResponse(results) + @require_POST @login_required @csrf.csrf_exempt -def upload(request, course_id):#ajax upload file to a question or answer +def upload(request, course_id): # ajax upload file to a question or answer """view that handles file upload via Ajax """ @@ -409,7 +433,7 @@ def upload(request, course_id):#ajax upload file to a question or answer time.time() ).replace( '.', - str(random.randint(0,100000)) + str(random.randint(0, 100000)) ) + file_extension file_storage = get_storage_class()() diff --git a/lms/djangoapps/django_comment_client/forum/views.py b/lms/djangoapps/django_comment_client/forum/views.py index 2c1d3c68d55..70d9f40fcf5 100644 --- a/lms/djangoapps/django_comment_client/forum/views.py +++ b/lms/djangoapps/django_comment_client/forum/views.py @@ -30,6 +30,7 @@ PAGES_NEARBY_DELTA = 2 escapedict = {'"': '"'} log = logging.getLogger("edx.discussions") + def get_threads(request, course_id, discussion_id=None, per_page=THREADS_PER_PAGE): """ This may raise cc.utils.CommentClientError or @@ -78,6 +79,7 @@ def get_threads(request, course_id, discussion_id=None, per_page=THREADS_PER_PAG return threads, query_params + def inline_discussion(request, course_id, discussion_id): """ Renders JSON for DiscussionModules @@ -111,6 +113,7 @@ def inline_discussion(request, course_id, discussion_id): 'allow_anonymous': allow_anonymous, }) + @login_required def forum_form_discussion(request, course_id): """ @@ -136,7 +139,7 @@ def forum_form_discussion(request, course_id): thread.update(courseware_context) if request.is_ajax(): return utils.JsonResponse({ - 'discussion_data': threads, # TODO: Standardize on 'discussion_data' vs 'threads' + 'discussion_data': threads, # TODO: Standardize on 'discussion_data' vs 'threads' 'annotated_content_info': annotated_content_info, 'num_pages': query_params['num_pages'], 'page': query_params['page'], @@ -157,11 +160,11 @@ def forum_form_discussion(request, course_id): 'course': course, #'recent_active_threads': recent_active_threads, #'trending_tags': trending_tags, - 'staff_access' : has_access(request.user, course, 'staff'), - 'threads': saxutils.escape(json.dumps(threads),escapedict), + 'staff_access': has_access(request.user, course, 'staff'), + 'threads': saxutils.escape(json.dumps(threads), escapedict), 'thread_pages': query_params['num_pages'], - 'user_info': saxutils.escape(json.dumps(user_info),escapedict), - 'annotated_content_info': saxutils.escape(json.dumps(annotated_content_info),escapedict), + 'user_info': saxutils.escape(json.dumps(user_info), escapedict), + 'annotated_content_info': saxutils.escape(json.dumps(annotated_content_info), escapedict), 'course_id': course.id, 'category_map': category_map, 'roles': saxutils.escape(json.dumps(utils.get_role_ids(course_id)), escapedict), @@ -169,6 +172,7 @@ def forum_form_discussion(request, course_id): # print "start rendering.." return render_to_response('discussion/index.html', context) + @login_required def single_thread(request, course_id, discussion_id, thread_id): @@ -234,13 +238,13 @@ def single_thread(request, course_id, discussion_id, thread_id): context = { 'discussion_id': discussion_id, 'csrf': csrf(request)['csrf_token'], - 'init': '', #TODO: What is this? - 'user_info': saxutils.escape(json.dumps(user_info),escapedict), + 'init': '', # TODO: What is this? + 'user_info': saxutils.escape(json.dumps(user_info), escapedict), 'annotated_content_info': saxutils.escape(json.dumps(annotated_content_info), escapedict), 'course': course, #'recent_active_threads': recent_active_threads, #'trending_tags': trending_tags, - 'course_id': course.id, #TODO: Why pass both course and course.id to template? + 'course_id': course.id, # TODO: Why pass both course and course.id to template? 'thread_id': thread_id, 'threads': saxutils.escape(json.dumps(threads), escapedict), 'category_map': category_map, @@ -250,6 +254,7 @@ def single_thread(request, course_id, discussion_id, thread_id): return render_to_response('discussion/single_thread.html', context) + @login_required def user_profile(request, course_id, user_id): #TODO: Allow sorting? @@ -259,7 +264,7 @@ def user_profile(request, course_id, user_id): query_params = { 'page': request.GET.get('page', 1), - 'per_page': THREADS_PER_PAGE, # more than threads_per_page to show more activities + 'per_page': THREADS_PER_PAGE, # more than threads_per_page to show more activities } threads, page, num_pages = profiled_user.active_threads(query_params) @@ -274,7 +279,7 @@ def user_profile(request, course_id, user_id): 'discussion_data': map(utils.safe_content, threads), 'page': query_params['page'], 'num_pages': query_params['num_pages'], - 'annotated_content_info': saxutils.escape(json.dumps(annotated_content_info),escapedict), + 'annotated_content_info': saxutils.escape(json.dumps(annotated_content_info), escapedict), }) else: @@ -285,8 +290,8 @@ def user_profile(request, course_id, user_id): 'django_user': User.objects.get(id=user_id), 'profiled_user': profiled_user.to_dict(), 'threads': saxutils.escape(json.dumps(threads), escapedict), - 'user_info': saxutils.escape(json.dumps(user_info),escapedict), - 'annotated_content_info': saxutils.escape(json.dumps(annotated_content_info),escapedict), + 'user_info': saxutils.escape(json.dumps(user_info), escapedict), + 'annotated_content_info': saxutils.escape(json.dumps(annotated_content_info), escapedict), # 'content': content, } @@ -302,7 +307,7 @@ def followed_threads(request, course_id, user_id): query_params = { 'page': request.GET.get('page', 1), - 'per_page': THREADS_PER_PAGE, # more than threads_per_page to show more activities + 'per_page': THREADS_PER_PAGE, # more than threads_per_page to show more activities 'sort_key': request.GET.get('sort_key', 'date'), 'sort_order': request.GET.get('sort_order', 'desc'), } @@ -328,8 +333,8 @@ def followed_threads(request, course_id, user_id): 'django_user': User.objects.get(id=user_id), 'profiled_user': profiled_user.to_dict(), 'threads': saxutils.escape(json.dumps(threads), escapedict), - 'user_info': saxutils.escape(json.dumps(user_info),escapedict), - 'annotated_content_info': saxutils.escape(json.dumps(annotated_content_info),escapedict), + 'user_info': saxutils.escape(json.dumps(user_info), escapedict), + 'annotated_content_info': saxutils.escape(json.dumps(annotated_content_info), escapedict), # 'content': content, } diff --git a/lms/djangoapps/django_comment_client/helpers.py b/lms/djangoapps/django_comment_client/helpers.py index 0a1e8639ef2..733856e2a9f 100644 --- a/lms/djangoapps/django_comment_client/helpers.py +++ b/lms/djangoapps/django_comment_client/helpers.py @@ -15,12 +15,16 @@ import os # This method is used to pluralize the words "discussion" and "comment" # when referring to how many discussion threads or comments the user # has contributed to. + + def pluralize(singular_term, count): if int(count) >= 2 or int(count) == 0: return singular_term + 's' return singular_term # TODO there should be a better way to handle this + + def include_mustache_templates(): mustache_dir = settings.PROJECT_ROOT / 'templates' / 'discussion' / 'mustache' valid_file_name = lambda file_name: file_name.endswith('.mustache') @@ -31,6 +35,7 @@ def include_mustache_templates(): file_contents = map(read_file, filter(valid_file_name, os.listdir(mustache_dir))) return '\n'.join(map(wrap_in_tag, map(strip_file_name, file_contents))) + def render_content(content, additional_context={}): context = { diff --git a/lms/djangoapps/django_comment_client/management/commands/assign_roles_for_course.py b/lms/djangoapps/django_comment_client/management/commands/assign_roles_for_course.py index 82f2290bc79..304907cdaec 100644 --- a/lms/djangoapps/django_comment_client/management/commands/assign_roles_for_course.py +++ b/lms/djangoapps/django_comment_client/management/commands/assign_roles_for_course.py @@ -8,6 +8,7 @@ from django.core.management.base import BaseCommand, CommandError from student.models import CourseEnrollment, assign_default_role + class Command(BaseCommand): args = 'course_id' help = 'Add roles for all users in a course' diff --git a/lms/djangoapps/django_comment_client/management/commands/create_roles_for_existing.py b/lms/djangoapps/django_comment_client/management/commands/create_roles_for_existing.py index d1244a6690b..638d59f5fee 100644 --- a/lms/djangoapps/django_comment_client/management/commands/create_roles_for_existing.py +++ b/lms/djangoapps/django_comment_client/management/commands/create_roles_for_existing.py @@ -8,6 +8,7 @@ from django.core.management.base import BaseCommand, CommandError from student.models import CourseEnrollment, assign_default_role + class Command(BaseCommand): args = 'course_id' help = 'Seed default permisssions and roles' diff --git a/lms/djangoapps/django_comment_client/management/commands/seed_permissions_roles.py b/lms/djangoapps/django_comment_client/management/commands/seed_permissions_roles.py index 958b67cdb38..6a31e73af3e 100644 --- a/lms/djangoapps/django_comment_client/management/commands/seed_permissions_roles.py +++ b/lms/djangoapps/django_comment_client/management/commands/seed_permissions_roles.py @@ -18,7 +18,7 @@ class Command(BaseCommand): student_role = Role.objects.get_or_create(name="Student", course_id=course_id)[0] for per in ["vote", "update_thread", "follow_thread", "unfollow_thread", - "update_comment", "create_sub_comment", "unvote" , "create_thread", + "update_comment", "create_sub_comment", "unvote", "create_thread", "follow_commentable", "unfollow_commentable", "create_comment", ]: student_role.add_permission(per) diff --git a/lms/djangoapps/django_comment_client/middleware.py b/lms/djangoapps/django_comment_client/middleware.py index 08e20b02967..abf2d40cabe 100644 --- a/lms/djangoapps/django_comment_client/middleware.py +++ b/lms/djangoapps/django_comment_client/middleware.py @@ -2,7 +2,8 @@ from comment_client import CommentClientError from django_comment_client.utils import JsonError import json -class AjaxExceptionMiddleware(object): + +class AjaxExceptionMiddleware(object): def process_exception(self, request, exception): if isinstance(exception, CommentClientError) and request.is_ajax(): return JsonError(json.loads(exception.message)) diff --git a/lms/djangoapps/django_comment_client/migrations/0001_initial.py b/lms/djangoapps/django_comment_client/migrations/0001_initial.py index 4993984d748..0b5f88e2f2a 100644 --- a/lms/djangoapps/django_comment_client/migrations/0001_initial.py +++ b/lms/djangoapps/django_comment_client/migrations/0001_initial.py @@ -129,4 +129,4 @@ class Migration(SchemaMigration): } } - complete_apps = ['django_comment_client'] \ No newline at end of file + complete_apps = ['django_comment_client'] diff --git a/lms/djangoapps/django_comment_client/models.py b/lms/djangoapps/django_comment_client/models.py index 10c05c75e99..023b355a29e 100644 --- a/lms/djangoapps/django_comment_client/models.py +++ b/lms/djangoapps/django_comment_client/models.py @@ -35,7 +35,7 @@ class Role(models.Model): def __unicode__(self): return self.name + " for " + (self.course_id if self.course_id else "all courses") - def inherit_permissions(self, role): # TODO the name of this method is a little bit confusing, + def inherit_permissions(self, role): # TODO the name of this method is a little bit confusing, # since it's one-off and doesn't handle inheritance later if role.course_id and role.course_id != self.course_id: logging.warning("{0} cannot inherit permissions from {1} due to course_id inconsistency", \ @@ -52,7 +52,7 @@ class Role(models.Model): (permission.startswith('edit') or permission.startswith('update') or permission.startswith('create')) and \ (not course.forum_posts_allowed): return False - + return self.permissions.filter(name=permission).exists() diff --git a/lms/djangoapps/django_comment_client/mustache_helpers.py b/lms/djangoapps/django_comment_client/mustache_helpers.py index 9756294696f..5743dba9cbc 100644 --- a/lms/djangoapps/django_comment_client/mustache_helpers.py +++ b/lms/djangoapps/django_comment_client/mustache_helpers.py @@ -7,6 +7,8 @@ import inspect # This method is used to pluralize the words "discussion" and "comment" # which is why you need to tack on an "s" for the case of 0 or two or more. + + def pluralize(content, text): num, word = text.split(' ') num = int(num or '0') @@ -15,12 +17,15 @@ def pluralize(content, text): else: return word + def url_for_user(content, user_id): return urlresolvers.reverse('django_comment_client.forum.views.user_profile', args=[content['course_id'], user_id]) -def url_for_tags(content, tags): # assume that attribute 'tags' is in the format u'a, b, c' + +def url_for_tags(content, tags): # assume that attribute 'tags' is in the format u'a, b, c' return _url_for_tags(content['course_id'], tags) + def close_thread_text(content): if content.get('closed'): return 'Re-open thread' diff --git a/lms/djangoapps/django_comment_client/permissions.py b/lms/djangoapps/django_comment_client/permissions.py index b95a890dda6..dfdcd3e7baa 100644 --- a/lms/djangoapps/django_comment_client/permissions.py +++ b/lms/djangoapps/django_comment_client/permissions.py @@ -8,6 +8,7 @@ from util.cache import cache from django.core import cache cache = cache.get_cache('default') + def cached_has_permission(user, permission, course_id=None): """ Call has_permission if it's not cached. A change in a user's role or @@ -21,6 +22,7 @@ def cached_has_permission(user, permission, course_id=None): cache.set(key, val, CACHE_LIFESPAN) return val + def has_permission(user, permission, course_id=None): for role in user.roles.filter(course_id=course_id): if role.has_permission(permission): @@ -29,6 +31,8 @@ def has_permission(user, permission, course_id=None): CONDITIONS = ['is_open', 'is_author'] + + def check_condition(user, condition, course_id, data): def check_open(user, condition, course_id, data): try: @@ -43,8 +47,8 @@ def check_condition(user, condition, course_id, data): return False handlers = { - 'is_open' : check_open, - 'is_author' : check_author, + 'is_open': check_open, + 'is_author': check_author, } return handlers[condition](user, condition, course_id, data) @@ -93,7 +97,7 @@ VIEW_PERMISSIONS = { 'unfollow_commentable': ['unfollow_commentable'], 'unfollow_user' : ['unfollow_user'], 'create_thread' : ['create_thread'], - 'update_moderator_status' : ['manage_moderator'], + 'update_moderator_status': ['manage_moderator'], } diff --git a/lms/djangoapps/django_comment_client/settings.py b/lms/djangoapps/django_comment_client/settings.py index 3234c324789..b9a8d180818 100644 --- a/lms/djangoapps/django_comment_client/settings.py +++ b/lms/djangoapps/django_comment_client/settings.py @@ -1,7 +1,7 @@ from django.conf import settings MAX_COMMENT_DEPTH = None -MAX_UPLOAD_FILE_SIZE = 1024 * 1024 #result in bytes +MAX_UPLOAD_FILE_SIZE = 1024 * 1024 # result in bytes ALLOWED_UPLOAD_FILE_TYPES = ('.jpg', '.jpeg', '.gif', '.bmp', '.png', '.tiff') if hasattr(settings, 'DISCUSSION_SETTINGS'): diff --git a/lms/djangoapps/django_comment_client/tests/test_helpers.py b/lms/djangoapps/django_comment_client/tests/test_helpers.py index bd67830841f..e2c074231fd 100644 --- a/lms/djangoapps/django_comment_client/tests/test_helpers.py +++ b/lms/djangoapps/django_comment_client/tests/test_helpers.py @@ -2,10 +2,11 @@ import string import random import collections -from django.test import TestCase +from django.test import TestCase from django_comment_client.helpers import pluralize + class PluralizeTestCase(TestCase): def testPluralize(self): diff --git a/lms/djangoapps/django_comment_client/tests/test_middleware.py b/lms/djangoapps/django_comment_client/tests/test_middleware.py index e3249551b37..55e4c72c753 100644 --- a/lms/djangoapps/django_comment_client/tests/test_middleware.py +++ b/lms/djangoapps/django_comment_client/tests/test_middleware.py @@ -2,27 +2,28 @@ import string import random import collections -from django.test import TestCase +from django.test import TestCase import comment_client import django.http import django_comment_client.middleware as middleware + class AjaxExceptionTestCase(TestCase): -# TODO: check whether the correct error message is produced. +# TODO: check whether the correct error message is produced. # The error message should be the same as the argument to CommentClientError - def setUp(self): - self.a = middleware.AjaxExceptionMiddleware() - self.request1 = django.http.HttpRequest() - self.request0 = django.http.HttpRequest() - self.exception1 = comment_client.CommentClientError('{}') - self.exception0 = ValueError() - self.request1.META['HTTP_X_REQUESTED_WITH'] = "XMLHttpRequest" - self.request0.META['HTTP_X_REQUESTED_WITH'] = "SHADOWFAX" - - def test_process_exception(self): - self.assertIsInstance(self.a.process_exception(self.request1, self.exception1), middleware.JsonError) - self.assertIsNone(self.a.process_exception(self.request1, self.exception0)) - self.assertIsNone(self.a.process_exception(self.request0, self.exception1)) - self.assertIsNone(self.a.process_exception(self.request0, self.exception0)) + def setUp(self): + self.a = middleware.AjaxExceptionMiddleware() + self.request1 = django.http.HttpRequest() + self.request0 = django.http.HttpRequest() + self.exception1 = comment_client.CommentClientError('{}') + self.exception0 = ValueError() + self.request1.META['HTTP_X_REQUESTED_WITH'] = "XMLHttpRequest" + self.request0.META['HTTP_X_REQUESTED_WITH'] = "SHADOWFAX" + + def test_process_exception(self): + self.assertIsInstance(self.a.process_exception(self.request1, self.exception1), middleware.JsonError) + self.assertIsNone(self.a.process_exception(self.request1, self.exception0)) + self.assertIsNone(self.a.process_exception(self.request0, self.exception1)) + self.assertIsNone(self.a.process_exception(self.request0, self.exception0)) diff --git a/lms/djangoapps/django_comment_client/tests/test_mustache_helpers.py b/lms/djangoapps/django_comment_client/tests/test_mustache_helpers.py index 8638aba67ed..5b788b3cc47 100644 --- a/lms/djangoapps/django_comment_client/tests/test_mustache_helpers.py +++ b/lms/djangoapps/django_comment_client/tests/test_mustache_helpers.py @@ -2,25 +2,27 @@ import string import random import collections -from django.test import TestCase +from django.test import TestCase import django_comment_client.mustache_helpers as mustache_helpers + class PluralizeTestCase(TestCase): - def test_pluralize(self): - self.text1 = '0 goat' - self.text2 = '1 goat' - self.text3 = '7 goat' - self.content = 'unused argument' - self.assertEqual(mustache_helpers.pluralize(self.content, self.text1), 'goats') - self.assertEqual(mustache_helpers.pluralize(self.content, self.text2), 'goat') - self.assertEqual(mustache_helpers.pluralize(self.content, self.text3), 'goats') + def test_pluralize(self): + self.text1 = '0 goat' + self.text2 = '1 goat' + self.text3 = '7 goat' + self.content = 'unused argument' + self.assertEqual(mustache_helpers.pluralize(self.content, self.text1), 'goats') + self.assertEqual(mustache_helpers.pluralize(self.content, self.text2), 'goat') + self.assertEqual(mustache_helpers.pluralize(self.content, self.text3), 'goats') + class CloseThreadTextTestCase(TestCase): - - def test_close_thread_text(self): - self.contentClosed = {'closed': True} - self.contentOpen = {'closed': False} - self.assertEqual(mustache_helpers.close_thread_text(self.contentClosed), 'Re-open thread') - self.assertEqual(mustache_helpers.close_thread_text(self.contentOpen), 'Close thread') + + def test_close_thread_text(self): + self.contentClosed = {'closed': True} + self.contentOpen = {'closed': False} + self.assertEqual(mustache_helpers.close_thread_text(self.contentClosed), 'Re-open thread') + self.assertEqual(mustache_helpers.close_thread_text(self.contentOpen), 'Close thread') diff --git a/lms/djangoapps/django_comment_client/tests/test_utils.py b/lms/djangoapps/django_comment_client/tests/test_utils.py index 2e24cbd8377..cec006e6301 100644 --- a/lms/djangoapps/django_comment_client/tests/test_utils.py +++ b/lms/djangoapps/django_comment_client/tests/test_utils.py @@ -2,7 +2,7 @@ import string import random import collections -from django.test import TestCase +from django.test import TestCase import factory from django.contrib.auth.models import User @@ -14,6 +14,7 @@ import django_comment_client.utils as utils import xmodule.modulestore.django as django + class UserFactory(factory.Factory): FACTORY_FOR = User username = 'robot' @@ -22,20 +23,24 @@ class UserFactory(factory.Factory): is_active = True is_staff = False + class CourseEnrollmentFactory(factory.Factory): - FACTORY_FOR = CourseEnrollment + FACTORY_FOR = CourseEnrollment user = factory.SubFactory(UserFactory) course_id = 'edX/toy/2012_Fall' + class RoleFactory(factory.Factory): FACTORY_FOR = Role name = 'Student' course_id = 'edX/toy/2012_Fall' + class PermissionFactory(factory.Factory): - FACTORY_FOR = Permission + FACTORY_FOR = Permission name = 'create_comment' + class DictionaryTestCase(TestCase): def test_extract(self): d = {'cats': 'meow', 'dogs': 'woof'} @@ -54,11 +59,12 @@ class DictionaryTestCase(TestCase): self.assertEqual(utils.strip_blank(d), expected) def test_merge_dict(self): - d1 ={'cats': 'meow', 'dogs': 'woof'} - d2 ={'lions': 'roar','ducks': 'quack'} - expected ={'cats': 'meow', 'dogs': 'woof','lions': 'roar','ducks': 'quack'} + d1 = {'cats': 'meow', 'dogs': 'woof'} + d2 = {'lions': 'roar', 'ducks': 'quack'} + expected = {'cats': 'meow', 'dogs': 'woof', 'lions': 'roar', 'ducks': 'quack'} self.assertEqual(utils.merge_dict(d1, d2), expected) + class AccessUtilsTestCase(TestCase): def setUp(self): self.course_id = 'edX/toy/2012_Fall' diff --git a/lms/djangoapps/django_comment_client/utils.py b/lms/djangoapps/django_comment_client/utils.py index b58e3b30e6e..1f1a80e2b43 100644 --- a/lms/djangoapps/django_comment_client/utils.py +++ b/lms/djangoapps/django_comment_client/utils.py @@ -24,21 +24,27 @@ log = logging.getLogger(__name__) _FULLMODULES = None _DISCUSSIONINFO = defaultdict(dict) + def extract(dic, keys): return {k: dic.get(k) for k in keys} + def strip_none(dic): return dict([(k, v) for k, v in dic.iteritems() if v is not None]) + def strip_blank(dic): def _is_blank(v): return isinstance(v, str) and len(v.strip()) == 0 return dict([(k, v) for k, v in dic.iteritems() if not _is_blank(v)]) # TODO should we be checking if d1 and d2 have the same keys with different values? + + def merge_dict(dic1, dic2): return dict(dic1.items() + dic2.items()) + def get_role_ids(course_id): roles = Role.objects.filter(course_id=course_id) staff = list(User.objects.filter(is_staff=True).values_list('id', flat=True)) @@ -47,6 +53,7 @@ def get_role_ids(course_id): roles_with_ids[role.name] = list(role.users.values_list('id', flat=True)) return roles_with_ids + def has_forum_access(uname, course_id, rolename): try: role = Role.objects.get(name=rolename, course_id=course_id) @@ -54,12 +61,14 @@ def has_forum_access(uname, course_id, rolename): return False return role.users.filter(username=uname).exists() + def get_full_modules(): global _FULLMODULES if not _FULLMODULES: _FULLMODULES = modulestore().modules return _FULLMODULES + def get_discussion_id_map(course): """ return a dict of the form {category: modules} @@ -68,18 +77,21 @@ def get_discussion_id_map(course): initialize_discussion_info(course) return _DISCUSSIONINFO[course.id]['id_map'] + def get_discussion_title(course, discussion_id): global _DISCUSSIONINFO initialize_discussion_info(course) title = _DISCUSSIONINFO[course.id]['id_map'].get(discussion_id, {}).get('title', '(no title)') return title + def get_discussion_category_map(course): global _DISCUSSIONINFO initialize_discussion_info(course) return filter_unstarted_categories(_DISCUSSIONINFO[course.id]['category_map']) + def filter_unstarted_categories(category_map): now = time.gmtime() @@ -117,6 +129,7 @@ def filter_unstarted_categories(category_map): return result_map + def sort_map_entries(category_map): things = [] for title, entry in category_map["entries"].items(): @@ -211,7 +224,7 @@ def initialize_discussion_info(course): # TODO. BUG! : course location is not unique across multiple course runs! # (I think Kevin already noticed this) Need to send course_id with requests, store it # in the backend. - default_topics = {'General': {'id' :course.location.html_id()}} + default_topics = {'General': {'id': course.location.html_id()}} discussion_topics = course.metadata.get('discussion_topics', default_topics) for topic, entry in discussion_topics.items(): category_map['entries'][topic] = {"id": entry["id"], @@ -223,12 +236,14 @@ def initialize_discussion_info(course): _DISCUSSIONINFO[course.id]['category_map'] = category_map _DISCUSSIONINFO[course.id]['timestamp'] = datetime.now() + class JsonResponse(HttpResponse): def __init__(self, data=None): content = simplejson.dumps(data) super(JsonResponse, self).__init__(content, mimetype='application/json; charset=utf-8') + class JsonError(HttpResponse): def __init__(self, error_messages=[], status=400): if isinstance(error_messages, str): @@ -239,14 +254,17 @@ class JsonError(HttpResponse): super(JsonError, self).__init__(content, mimetype='application/json; charset=utf-8', status=status) + class HtmlResponse(HttpResponse): def __init__(self, html=''): super(HtmlResponse, self).__init__(html, content_type='text/plain') + class ViewNameMiddleware(object): def process_view(self, request, view_func, view_args, view_kwargs): request.view_name = view_func.__name__ + class QueryCountDebugMiddleware(object): """ This middleware will log the number of queries run @@ -272,6 +290,7 @@ class QueryCountDebugMiddleware(object): log.info('%s queries run, total %s seconds' % (len(connection.queries), total_time)) return response + def get_ability(course_id, content, user): return { 'editable': check_permissions_by_view(user, course_id, content, "update_thread" if content['type'] == 'thread' else "update_comment"), @@ -283,6 +302,8 @@ def get_ability(course_id, content, user): } #TODO: RENAME + + def get_annotated_content_info(course_id, content, user, user_info): """ Get metadata for an individual content (thread or comment) @@ -299,6 +320,8 @@ def get_annotated_content_info(course_id, content, user, user_info): } #TODO: RENAME + + def get_annotated_content_infos(course_id, thread, user, user_info): """ Get metadata for a thread and its children @@ -311,6 +334,7 @@ def get_annotated_content_infos(course_id, thread, user, user_info): annotate(thread) return infos + def get_metadata_for_threads(course_id, threads, user, user_info): def infogetter(thread): return get_annotated_content_infos(course_id, thread, user, user_info) @@ -319,13 +343,17 @@ def get_metadata_for_threads(course_id, threads, user, user_info): return metadata # put this method in utils.py to avoid circular import dependency between helpers and mustache_helpers + + def url_for_tags(course_id, tags): return reverse('django_comment_client.forum.views.forum_form_discussion', args=[course_id]) + '?' + urllib.urlencode({'tags': tags}) + def render_mustache(template_name, dictionary, *args, **kwargs): template = middleware.lookup['main'].get_template(template_name).source return pystache.render(template, dictionary) + def permalink(content): if content['type'] == 'thread': return reverse('django_comment_client.forum.views.single_thread', @@ -334,6 +362,7 @@ def permalink(content): return reverse('django_comment_client.forum.views.single_thread', args=[content['course_id'], content['commentable_id'], content['thread_id']]) + '#' + content['id'] + def extend_content(content): roles = {} if content.get('user_id'): @@ -349,10 +378,11 @@ def extend_content(content): 'raw_tags': ','.join(content.get('tags', [])), 'permalink': permalink(content), 'roles': roles, - 'updated': content['created_at']!=content['updated_at'], + 'updated': content['created_at'] != content['updated_at'], } return merge_dict(content, content_info) + def get_courseware_context(content, course): id_map = get_discussion_id_map(course) id = content['commentable_id'] @@ -361,13 +391,14 @@ def get_courseware_context(content, course): location = id_map[id]["location"].url() title = id_map[id]["title"] (course_id, chapter, section, position) = path_to_location(modulestore(), course.id, location) - url = reverse('courseware_position', kwargs={"course_id":course_id, - "chapter":chapter, - "section":section, - "position":position}) + url = reverse('courseware_position', kwargs={"course_id": course_id, + "chapter": chapter, + "section": section, + "position": position}) content_info = {"courseware_url": url, "courseware_title": title} return content_info + def safe_content(content): fields = [ 'id', 'title', 'body', 'course_id', 'anonymous', 'anonymous_to_peers', diff --git a/lms/djangoapps/instructor/management/commands/compute_grades.py b/lms/djangoapps/instructor/management/commands/compute_grades.py index 462833ba3c9..92db04f09ab 100644 --- a/lms/djangoapps/instructor/management/commands/compute_grades.py +++ b/lms/djangoapps/instructor/management/commands/compute_grades.py @@ -3,7 +3,9 @@ # django management command: dump grades to csv files # for use by batch processes -import os, sys, string +import os +import sys +import string import datetime import json @@ -15,6 +17,7 @@ from xmodule.modulestore.django import modulestore from django.conf import settings from django.core.management.base import BaseCommand + class Command(BaseCommand): help = "Compute grades for all students in a course, and store result in DB.\n" help += "Usage: compute_grades course_id_or_dir \n" @@ -25,7 +28,7 @@ class Command(BaseCommand): print "args = ", args - if len(args)>0: + if len(args) > 0: course_id = args[0] else: print self.help @@ -46,7 +49,3 @@ class Command(BaseCommand): print "Computing grades for %s" % (course.id) offline_grade_calculation(course.id) - - - - diff --git a/lms/djangoapps/instructor/management/commands/dump_grades.py b/lms/djangoapps/instructor/management/commands/dump_grades.py index 65825271f37..13f86c0e0f0 100644 --- a/lms/djangoapps/instructor/management/commands/dump_grades.py +++ b/lms/djangoapps/instructor/management/commands/dump_grades.py @@ -3,7 +3,9 @@ # django management command: dump grades to csv files # for use by batch processes -import os, sys, string +import os +import sys +import string import datetime import json @@ -14,6 +16,7 @@ from xmodule.modulestore.django import modulestore from django.conf import settings from django.core.management.base import BaseCommand + class Command(BaseCommand): help = "dump grades to CSV file. Usage: dump_grades course_id_or_dir filename dump_type\n" help += " course_id_or_dir: either course_id or course_dir\n" @@ -32,12 +35,12 @@ class Command(BaseCommand): fn = "grades.csv" get_raw_scores = False - if len(args)>0: + if len(args) > 0: course_id = args[0] - if len(args)>1: + if len(args) > 1: fn = args[1] - if len(args)>2: - get_raw_scores = args[2].lower()=='raw' + if len(args) > 2: + get_raw_scores = args[2].lower() == 'raw' request = self.DummyRequest() try: @@ -54,15 +57,15 @@ class Command(BaseCommand): print "-----------------------------------------------------------------------------" print "Dumping grades from %s to file %s (get_raw_scores=%s)" % (course.id, fn, get_raw_scores) datatable = get_student_grade_summary_data(request, course, course.id, get_raw_scores=get_raw_scores) - - fp = open(fn,'w') - + + fp = open(fn, 'w') + writer = csv.writer(fp, dialect='excel', quotechar='"', quoting=csv.QUOTE_ALL) writer.writerow(datatable['header']) for datarow in datatable['data']: encoded_row = [unicode(s).encode('utf-8') for s in datarow] writer.writerow(encoded_row) - + fp.close() print "Done: %d records dumped" % len(datatable['data']) @@ -74,6 +77,3 @@ class Command(BaseCommand): return 'edx.mit.edu' def is_secure(self): return False - - - diff --git a/lms/djangoapps/instructor/offline_gradecalc.py b/lms/djangoapps/instructor/offline_gradecalc.py index 7c102805b46..8182c4e58ae 100644 --- a/lms/djangoapps/instructor/offline_gradecalc.py +++ b/lms/djangoapps/instructor/offline_gradecalc.py @@ -31,7 +31,7 @@ class MyEncoder(JSONEncoder): def offline_grade_calculation(course_id): ''' - Compute grades for all students for a specified course, and save results to the DB. + Compute grades for all students for a specified course, and save results to the DB. ''' tstart = time.time() @@ -59,16 +59,16 @@ def offline_grade_calculation(course_id): ocg, created = models.OfflineComputedGrade.objects.get_or_create(user=student, course_id=course_id) ocg.gradeset = gs ocg.save() - print "%s done" % student # print statement used because this is run by a management command + print "%s done" % student # print statement used because this is run by a management command tend = time.time() dt = tend - tstart - + ocgl = models.OfflineComputedGradeLog(course_id=course_id, seconds=dt, nstudents=len(enrolled_students)) ocgl.save() print ocgl print "All Done!" - + def offline_grades_available(course_id): ''' @@ -80,7 +80,7 @@ def offline_grades_available(course_id): return False return ocgl.latest('created') - + def student_grades(student, request, course, keep_raw_scores=False, use_offline=False): ''' This is the main interface to get grades. It has the same parameters as grades.grade, as well @@ -89,15 +89,11 @@ def student_grades(student, request, course, keep_raw_scores=False, use_offline= if not use_offline: return grades.grade(student, request, course, keep_raw_scores=keep_raw_scores) - + try: ocg = models.OfflineComputedGrade.objects.get(user=student, course_id=course.id) except models.OfflineComputedGrade.DoesNotExist: - return dict(raw_scores=[], section_breakdown=[], + return dict(raw_scores=[], section_breakdown=[], msg='Error: no offline gradeset available for %s, %s' % (student, course.id)) - - return json.loads(ocg.gradeset) - - - + return json.loads(ocg.gradeset) diff --git a/lms/djangoapps/instructor/tests.py b/lms/djangoapps/instructor/tests.py index e2ee8780218..2610e57422f 100644 --- a/lms/djangoapps/instructor/tests.py +++ b/lms/djangoapps/instructor/tests.py @@ -71,13 +71,13 @@ class TestInstructorDashboardGradeDownloadCSV(ct.PageLoader): response = self.client.post(url, {'action': 'Download CSV of all student grades for this course'}) msg += "instructor dashboard download csv grades: response = '{0}'\n".format(response) - self.assertEqual(response['Content-Type'],'text/csv',msg) + self.assertEqual(response['Content-Type'], 'text/csv', msg) cdisp = response['Content-Disposition'] msg += "Content-Disposition = '%s'\n" % cdisp self.assertEqual(cdisp, 'attachment; filename=grades_{0}.csv'.format(course.id), msg) - body = response.content.replace('\r','') + body = response.content.replace('\r', '') msg += "body = '{0}'\n".format(body) # All the not-actually-in-the-course hw and labs come from the @@ -89,9 +89,10 @@ class TestInstructorDashboardGradeDownloadCSV(ct.PageLoader): self.assertEqual(body, expected_body, msg) -FORUM_ROLES = [ FORUM_ROLE_ADMINISTRATOR, FORUM_ROLE_MODERATOR, FORUM_ROLE_COMMUNITY_TA ] -FORUM_ADMIN_ACTION_SUFFIX = { FORUM_ROLE_ADMINISTRATOR : 'admin', FORUM_ROLE_MODERATOR : 'moderator', FORUM_ROLE_COMMUNITY_TA : 'community TA'} -FORUM_ADMIN_USER = { FORUM_ROLE_ADMINISTRATOR : 'forumadmin', FORUM_ROLE_MODERATOR : 'forummoderator', FORUM_ROLE_COMMUNITY_TA : 'forummoderator'} +FORUM_ROLES = [FORUM_ROLE_ADMINISTRATOR, FORUM_ROLE_MODERATOR, FORUM_ROLE_COMMUNITY_TA] +FORUM_ADMIN_ACTION_SUFFIX = {FORUM_ROLE_ADMINISTRATOR: 'admin', FORUM_ROLE_MODERATOR: 'moderator', FORUM_ROLE_COMMUNITY_TA: 'community TA'} +FORUM_ADMIN_USER = {FORUM_ROLE_ADMINISTRATOR: 'forumadmin', FORUM_ROLE_MODERATOR: 'forummoderator', FORUM_ROLE_COMMUNITY_TA: 'forummoderator'} + def action_name(operation, rolename): if operation == 'List': @@ -146,7 +147,7 @@ class TestInstructorDashboardForumAdmin(ct.PageLoader): for action in ['Add', 'Remove']: for rolename in FORUM_ROLES: response = self.client.post(url, {'action': action_name(action, rolename), FORUM_ADMIN_USER[rolename]: username}) - self.assertTrue(response.content.find('Error: unknown username "{0}"'.format(username))>=0) + self.assertTrue(response.content.find('Error: unknown username "{0}"'.format(username)) >= 0) def test_add_forum_admin_users_for_missing_roles(self): course = self.toy @@ -155,7 +156,7 @@ class TestInstructorDashboardForumAdmin(ct.PageLoader): for action in ['Add', 'Remove']: for rolename in FORUM_ROLES: response = self.client.post(url, {'action': action_name(action, rolename), FORUM_ADMIN_USER[rolename]: username}) - self.assertTrue(response.content.find('Error: unknown rolename "{0}"'.format(rolename))>=0) + self.assertTrue(response.content.find('Error: unknown rolename "{0}"'.format(rolename)) >= 0) def test_remove_forum_admin_users_for_missing_users(self): course = self.toy @@ -165,7 +166,7 @@ class TestInstructorDashboardForumAdmin(ct.PageLoader): action = 'Remove' for rolename in FORUM_ROLES: response = self.client.post(url, {'action': action_name(action, rolename), FORUM_ADMIN_USER[rolename]: username}) - self.assertTrue(response.content.find('Error: user "{0}" does not have rolename "{1}"'.format(username, rolename))>=0) + self.assertTrue(response.content.find('Error: user "{0}" does not have rolename "{1}"'.format(username, rolename)) >= 0) def test_add_and_remove_forum_admin_users(self): course = self.toy @@ -174,10 +175,10 @@ class TestInstructorDashboardForumAdmin(ct.PageLoader): username = 'u2' for rolename in FORUM_ROLES: response = self.client.post(url, {'action': action_name('Add', rolename), FORUM_ADMIN_USER[rolename]: username}) - self.assertTrue(response.content.find('Added "{0}" to "{1}" forum role = "{2}"'.format(username, course.id, rolename))>=0) + self.assertTrue(response.content.find('Added "{0}" to "{1}" forum role = "{2}"'.format(username, course.id, rolename)) >= 0) self.assertTrue(has_forum_access(username, course.id, rolename)) response = self.client.post(url, {'action': action_name('Remove', rolename), FORUM_ADMIN_USER[rolename]: username}) - self.assertTrue(response.content.find('Removed "{0}" from "{1}" forum role = "{2}"'.format(username, course.id, rolename))>=0) + self.assertTrue(response.content.find('Removed "{0}" from "{1}" forum role = "{2}"'.format(username, course.id, rolename)) >= 0) self.assertFalse(has_forum_access(username, course.id, rolename)) def test_add_and_read_forum_admin_users(self): @@ -189,7 +190,7 @@ class TestInstructorDashboardForumAdmin(ct.PageLoader): # perform an add, and follow with a second identical add: self.client.post(url, {'action': action_name('Add', rolename), FORUM_ADMIN_USER[rolename]: username}) response = self.client.post(url, {'action': action_name('Add', rolename), FORUM_ADMIN_USER[rolename]: username}) - self.assertTrue(response.content.find('Error: user "{0}" already has rolename "{1}", cannot add'.format(username, rolename))>=0) + self.assertTrue(response.content.find('Error: user "{0}" already has rolename "{1}", cannot add'.format(username, rolename)) >= 0) self.assertTrue(has_forum_access(username, course.id, rolename)) def test_add_nonstaff_forum_admin_users(self): @@ -199,7 +200,7 @@ class TestInstructorDashboardForumAdmin(ct.PageLoader): username = 'u1' rolename = FORUM_ROLE_ADMINISTRATOR response = self.client.post(url, {'action': action_name('Add', rolename), FORUM_ADMIN_USER[rolename]: username}) - self.assertTrue(response.content.find('Error: user "{0}" should first be added as staff'.format(username))>=0) + self.assertTrue(response.content.find('Error: user "{0}" should first be added as staff'.format(username)) >= 0) def test_list_forum_admin_users(self): course = self.toy @@ -213,12 +214,10 @@ class TestInstructorDashboardForumAdmin(ct.PageLoader): self.assertTrue(has_forum_access(username, course.id, rolename)) response = self.client.post(url, {'action': action_name('List', rolename), FORUM_ADMIN_USER[rolename]: username}) for header in ['Username', 'Full name', 'Roles']: - self.assertTrue(response.content.find('<th>{0}</th>'.format(header))>0) - self.assertTrue(response.content.find('<td>{0}</td>'.format(username))>=0) + self.assertTrue(response.content.find('<th>{0}</th>'.format(header)) > 0) + self.assertTrue(response.content.find('<td>{0}</td>'.format(username)) >= 0) # concatenate all roles for user, in sorted order: added_roles.append(rolename) added_roles.sort() roles = ', '.join(added_roles) - self.assertTrue(response.content.find('<td>{0}</td>'.format(roles))>=0, 'not finding roles "{0}"'.format(roles)) - - + self.assertTrue(response.content.find('<td>{0}</td>'.format(roles)) >= 0, 'not finding roles "{0}"'.format(roles)) diff --git a/lms/djangoapps/instructor/views.py b/lms/djangoapps/instructor/views.py index a7075060458..4faf814bc98 100644 --- a/lms/djangoapps/instructor/views.py +++ b/lms/djangoapps/instructor/views.py @@ -50,9 +50,11 @@ template_imports = {'urllib': urllib} FORUM_ROLE_ADD = 'add' FORUM_ROLE_REMOVE = 'remove' + def split_by_comma_and_whitespace(s): return re.split(r'[\s,]', s) + @ensure_csrf_cookie @cache_control(no_cache=True, no_store=True, must_revalidate=True) def instructor_dashboard(request, course_id): @@ -69,11 +71,11 @@ def instructor_dashboard(request, course_id): # the instructor dashboard page is modal: grades, psychometrics, admin # keep that state in request.session (defaults to grades mode) - idash_mode = request.POST.get('idash_mode','') + idash_mode = request.POST.get('idash_mode', '') if idash_mode: request.session['idash_mode'] = idash_mode else: - idash_mode = request.session.get('idash_mode','Grades') + idash_mode = request.session.get('idash_mode', 'Grades') def escape(s): """escape HTML special characters in string""" @@ -130,7 +132,7 @@ def instructor_dashboard(request, course_id): # process actions from form POST action = request.POST.get('action', '') - use_offline = request.POST.get('use_offline_grades',False) + use_offline = request.POST.get('use_offline_grades', False) if settings.MITX_FEATURES['ENABLE_MANUAL_GIT_RELOAD']: if 'GIT pull' in action: @@ -155,12 +157,12 @@ def instructor_dashboard(request, course_id): course_errors = modulestore().get_item_errors(course.location) msg += '<ul>' for cmsg, cerr in course_errors: - msg += "<li>{0}: <pre>{1}</pre>".format(cmsg,escape(cerr)) + msg += "<li>{0}: <pre>{1}</pre>".format(cmsg, escape(cerr)) msg += '</ul>' except Exception as err: msg += '<br/><p>Error: {0}</p>'.format(escape(err)) - if action == 'Dump list of enrolled students' or action=='List enrolled students': + if action == 'Dump list of enrolled students' or action == 'List enrolled students': log.debug(action) datatable = get_student_grade_summary_data(request, course, course_id, get_grades=False, use_offline=use_offline) datatable['title'] = 'List of students enrolled in {0}'.format(course_id) @@ -195,44 +197,44 @@ def instructor_dashboard(request, course_id): elif "Reset student's attempts" in action: # get the form data - unique_student_identifier=request.POST.get('unique_student_identifier','') - problem_to_reset=request.POST.get('problem_to_reset','') + unique_student_identifier = request.POST.get('unique_student_identifier', '') + problem_to_reset = request.POST.get('problem_to_reset', '') - if problem_to_reset[-4:]==".xml": - problem_to_reset=problem_to_reset[:-4] + if problem_to_reset[-4:] == ".xml": + problem_to_reset = problem_to_reset[:-4] # try to uniquely id student by email address or username try: if "@" in unique_student_identifier: - student_to_reset=User.objects.get(email=unique_student_identifier) + student_to_reset = User.objects.get(email=unique_student_identifier) else: - student_to_reset=User.objects.get(username=unique_student_identifier) - msg+="Found a single student to reset. " + student_to_reset = User.objects.get(username=unique_student_identifier) + msg += "Found a single student to reset. " except: - student_to_reset=None - msg+="<font color='red'>Couldn't find student with that email or username. </font>" + student_to_reset = None + msg += "<font color='red'>Couldn't find student with that email or username. </font>" if student_to_reset is not None: # find the module in question try: - (org, course_name, run)=course_id.split("/") - module_state_key="i4x://"+org+"/"+course_name+"/problem/"+problem_to_reset - module_to_reset=StudentModule.objects.get(student_id=student_to_reset.id, + (org, course_name, run) = course_id.split("/") + module_state_key = "i4x://" + org + "/" + course_name + "/problem/" + problem_to_reset + module_to_reset = StudentModule.objects.get(student_id=student_to_reset.id, course_id=course_id, module_state_key=module_state_key) - msg+="Found module to reset. " + msg += "Found module to reset. " except Exception as e: - msg+="<font color='red'>Couldn't find module with that urlname. </font>" + msg += "<font color='red'>Couldn't find module with that urlname. </font>" # modify the problem's state try: # load the state json - problem_state=json.loads(module_to_reset.state) - old_number_of_attempts=problem_state["attempts"] - problem_state["attempts"]=0 + problem_state = json.loads(module_to_reset.state) + old_number_of_attempts = problem_state["attempts"] + problem_state["attempts"] = 0 # save - module_to_reset.state=json.dumps(problem_state) + module_to_reset.state = json.dumps(problem_state) module_to_reset.save() track.views.server_track(request, '{instructor} reset attempts from {old_attempts} to 0 for {student} on problem {problem} in {course}'.format( @@ -243,19 +245,19 @@ def instructor_dashboard(request, course_id): course=course_id), {}, page='idashboard') - msg+="<font color='green'>Module state successfully reset!</font>" + msg += "<font color='green'>Module state successfully reset!</font>" except: - msg+="<font color='red'>Couldn't reset module state. </font>" + msg += "<font color='red'>Couldn't reset module state. </font>" elif "Get link to student's progress page" in action: - unique_student_identifier=request.POST.get('unique_student_identifier','') + unique_student_identifier = request.POST.get('unique_student_identifier', '') try: if "@" in unique_student_identifier: - student_to_reset=User.objects.get(email=unique_student_identifier) + student_to_reset = User.objects.get(email=unique_student_identifier) else: - student_to_reset=User.objects.get(username=unique_student_identifier) - progress_url=reverse('student_progress',kwargs={'course_id':course_id,'student_id': student_to_reset.id}) + student_to_reset = User.objects.get(username=unique_student_identifier) + progress_url = reverse('student_progress', kwargs={'course_id': course_id, 'student_id': student_to_reset.id}) track.views.server_track(request, '{instructor} requested progress page for {student} in {course}'.format( student=student_to_reset, @@ -263,18 +265,18 @@ def instructor_dashboard(request, course_id): course=course_id), {}, page='idashboard') - msg+="<a href='{0}' target='_blank'> Progress page for username: {1} with email address: {2}</a>.".format(progress_url,student_to_reset.username,student_to_reset.email) + msg += "<a href='{0}' target='_blank'> Progress page for username: {1} with email address: {2}</a>.".format(progress_url, student_to_reset.username, student_to_reset.email) except: - msg+="<font color='red'>Couldn't find student with that username. </font>" + msg += "<font color='red'>Couldn't find student with that username. </font>" #---------------------------------------- # export grades to remote gradebook - elif action=='List assignments available in remote gradebook': + elif action == 'List assignments available in remote gradebook': msg2, datatable = _do_remote_gradebook(request.user, course, 'get-assignments') msg += msg2 - elif action=='List assignments available for this course': + elif action == 'List assignments available for this course': log.debug(action) allgrades = get_student_grade_summary_data(request, course, course_id, get_grades=True, use_offline=use_offline) @@ -285,11 +287,11 @@ def instructor_dashboard(request, course_id): msg += 'assignments=<pre>%s</pre>' % assignments - elif action=='List enrolled students matching remote gradebook': + elif action == 'List enrolled students matching remote gradebook': stud_data = get_student_grade_summary_data(request, course, course_id, get_grades=False, use_offline=use_offline) msg2, rg_stud_data = _do_remote_gradebook(request.user, course, 'get-membership') datatable = {'header': ['Student email', 'Match?']} - rg_students = [ x['email'] for x in rg_stud_data['retdata'] ] + rg_students = [x['email'] for x in rg_stud_data['retdata']] def domatch(x): return '<font color="green">yes</font>' if x.email in rg_students else '<font color="red">No</font>' datatable['data'] = [[x.email, domatch(x)] for x in stud_data['students']] @@ -300,7 +302,7 @@ def instructor_dashboard(request, course_id): log.debug(action) datatable = {} - aname = request.POST.get('assignment_name','') + aname = request.POST.get('assignment_name', '') if not aname: msg += "<font color='red'>Please enter an assignment name</font>" else: @@ -476,13 +478,13 @@ def instructor_dashboard(request, course_id): elif action == 'Enroll student': - student = request.POST.get('enstudent','') + student = request.POST.get('enstudent', '') ret = _do_enroll_students(course, course_id, student) datatable = ret['datatable'] elif action == 'Un-enroll student': - student = request.POST.get('enstudent','') + student = request.POST.get('enstudent', '') datatable = {} isok = False cea = CourseEnrollmentAllowed.objects.filter(course_id=course_id, email=student) @@ -506,7 +508,7 @@ def instructor_dashboard(request, course_id): elif action == 'Enroll multiple students': - students = request.POST.get('enroll_multiple','') + students = request.POST.get('enroll_multiple', '') ret = _do_enroll_students(course, course_id, students) datatable = ret['datatable'] @@ -519,8 +521,8 @@ def instructor_dashboard(request, course_id): 'Overload enrollment list using remote gradebook', 'Merge enrollment list with remote gradebook']: - section = request.POST.get('gradebook_section','') - msg2, datatable = _do_remote_gradebook(request.user, course, 'get-membership', dict(section=section) ) + section = request.POST.get('gradebook_section', '') + msg2, datatable = _do_remote_gradebook(request.user, course, 'get-membership', dict(section=section)) msg += msg2 if not 'List' in action: @@ -539,7 +541,7 @@ def instructor_dashboard(request, course_id): msg += nmsg track.views.server_track(request, 'psychometrics {0}'.format(problem), {}, page='idashboard') - if idash_mode=='Psychometrics': + if idash_mode == 'Psychometrics': problems = psychoanalyze.problems_with_psychometric_data(course_id) @@ -563,10 +565,10 @@ def instructor_dashboard(request, course_id): 'problems': problems, # psychometrics 'plots': plots, # psychometrics 'course_errors': modulestore().get_item_errors(course.location), - 'djangopid' : os.getpid(), - 'mitx_version' : getattr(settings,'MITX_VERSION_STRING',''), - 'offline_grade_log' : offline_grades_available(course_id), - 'cohorts_ajax_url' : reverse('cohorts', kwargs={'course_id': course_id}), + 'djangopid': os.getpid(), + 'mitx_version': getattr(settings, 'MITX_VERSION_STRING', ''), + 'offline_grade_log': offline_grades_available(course_id), + 'cohorts_ajax_url': reverse('cohorts', kwargs={'course_id': course_id}), } return render_to_response('courseware/instructor_dashboard.html', context) @@ -576,17 +578,17 @@ def _do_remote_gradebook(user, course, action, args=None, files=None): ''' Perform remote gradebook action. Returns msg, datatable. ''' - rg = course.metadata.get('remote_gradebook','') + rg = course.metadata.get('remote_gradebook', '') if not rg: msg = "No remote gradebook defined in course metadata" return msg, {} - rgurl = settings.MITX_FEATURES.get('REMOTE_GRADEBOOK_URL','') + rgurl = settings.MITX_FEATURES.get('REMOTE_GRADEBOOK_URL', '') if not rgurl: msg = "No remote gradebook url defined in settings.MITX_FEATURES" return msg, {} - rgname = rg.get('name','') + rgname = rg.get('name', '') if not rgname: msg = "No gradebook name defined in course remote_gradebook metadata" return msg, {} @@ -606,8 +608,8 @@ def _do_remote_gradebook(user, course, action, args=None, files=None): msg += "<br/>data=%s" % data return msg, {} - msg = '<pre>%s</pre>' % retdict['msg'].replace('\n','<br/>') - retdata = retdict['data'] # a list of dicts + msg = '<pre>%s</pre>' % retdict['msg'].replace('\n', '<br/>') + retdata = retdict['data'] # a list of dicts if retdata: datatable = {'header': retdata[0].keys()} @@ -619,6 +621,7 @@ def _do_remote_gradebook(user, course, action, args=None, files=None): return msg, datatable + def _list_course_forum_members(course_id, rolename, datatable): """ Fills in datatable with forum membership information, for a given role, @@ -672,7 +675,7 @@ def _update_forum_role_membership(uname, course, rolename, add_or_remove): log.debug('rolename={0}'.format(rolename)) if add_or_remove == FORUM_ROLE_REMOVE: if not alreadyexists: - msg ='<font color="red">Error: user "{0}" does not have rolename "{1}", cannot remove</font>'.format(uname, rolename) + msg = '<font color="red">Error: user "{0}" does not have rolename "{1}", cannot remove</font>'.format(uname, rolename) else: user.roles.remove(role) msg = '<font color="green">Removed "{0}" from "{1}" forum role = "{2}"</font>'.format(user, course.id, rolename) @@ -688,6 +691,7 @@ def _update_forum_role_membership(uname, course, rolename, add_or_remove): return msg + def _group_members_table(group, title, course_id): """ Return a data table of usernames and names of users in group_name. @@ -756,6 +760,7 @@ def add_user_to_group(request, username_or_email, group, group_title, event_name """ return _add_or_remove_user_group(request, username_or_email, group, group_title, event_name, True) + def remove_user_from_group(request, username_or_email, group, group_title, event_name): """ Look up the given user by username (if no '@') or email (otherwise), and remove them from group. @@ -809,22 +814,22 @@ def get_student_grade_summary_data(request, course, course_id, get_grades=True, data = [] for student in enrolled_students: - datarow = [ student.id, student.username, student.profile.name, student.email ] + datarow = [student.id, student.username, student.profile.name, student.email] try: datarow.append(student.externalauthmap.external_email) - except: # ExternalAuthMap.DoesNotExist + except: # ExternalAuthMap.DoesNotExist datarow.append('') if get_grades: gradeset = student_grades(student, request, course, keep_raw_scores=get_raw_scores, use_offline=use_offline) - log.debug('student={0}, gradeset={1}'.format(student,gradeset)) + log.debug('student={0}, gradeset={1}'.format(student, gradeset)) if get_raw_scores: # TODO (ichuang) encode Score as dict instead of as list, so score[0] -> score['earned'] - sgrades = [(getattr(score,'earned','') or score[0]) for score in gradeset['raw_scores']] + sgrades = [(getattr(score, 'earned', '') or score[0]) for score in gradeset['raw_scores']] else: sgrades = [x['percent'] for x in gradeset['section_breakdown']] datarow += sgrades - student.grades = sgrades # store in student object + student.grades = sgrades # store in student object data.append(datarow) datatable['data'] = data @@ -832,6 +837,7 @@ def get_student_grade_summary_data(request, course, course_id, get_grades=True, #----------------------------------------------------------------------------- + @cache_control(no_cache=True, no_store=True, must_revalidate=True) def gradebook(request, course_id): """ @@ -886,9 +892,9 @@ def _do_enroll_students(course, course_id, students, overload=False): if '' in new_students: new_students.remove('') - status = dict([x,'unprocessed'] for x in new_students) + status = dict([x, 'unprocessed'] for x in new_students) - if overload: # delete all but staff + if overload: # delete all but staff todelete = CourseEnrollment.objects.filter(course_id=course_id) for ce in todelete: if not has_access(ce.user, course, 'staff') and ce.user.email.lower() not in new_students_lc: @@ -903,7 +909,7 @@ def _do_enroll_students(course, course_id, students, overload=False): for student in new_students: try: - user=User.objects.get(email=student) + user = User.objects.get(email=student) except User.DoesNotExist: # user not signed up yet, put in pending enrollment allowed table if CourseEnrollmentAllowed.objects.filter(email=student, course_id=course_id): @@ -928,9 +934,9 @@ def _do_enroll_students(course, course_id, students, overload=False): datatable['data'] = [[x, status[x]] for x in status] datatable['title'] = 'Enrollment of students' - def sf(stat): return [x for x in status if status[x]==stat] + def sf(stat): return [x for x in status if status[x] == stat] - data = dict(added=sf('added'), rejected=sf('rejected')+sf('exists'), + data = dict(added=sf('added'), rejected=sf('rejected') + sf('exists'), deleted=sf('deleted'), datatable=datatable) return data @@ -1013,5 +1019,5 @@ def compute_course_stats(course): walk(c) walk(course) - stats = dict(counts) # number of each kind of module + stats = dict(counts) # number of each kind of module return stats diff --git a/lms/djangoapps/licenses/migrations/0001_initial.py b/lms/djangoapps/licenses/migrations/0001_initial.py index bdc1d3ead4f..365046272d5 100644 --- a/lms/djangoapps/licenses/migrations/0001_initial.py +++ b/lms/djangoapps/licenses/migrations/0001_initial.py @@ -115,4 +115,4 @@ class Migration(SchemaMigration): } } - complete_apps = ['licenses'] \ No newline at end of file + complete_apps = ['licenses'] diff --git a/lms/djangoapps/lms_migration/management/commands/create_groups.py b/lms/djangoapps/lms_migration/management/commands/create_groups.py index 7b52795606e..95c9e4238b9 100644 --- a/lms/djangoapps/lms_migration/management/commands/create_groups.py +++ b/lms/djangoapps/lms_migration/management/commands/create_groups.py @@ -4,7 +4,10 @@ # # Create all staff_* groups for classes in data directory. -import os, sys, string, re +import os +import sys +import string +import re from django.core.management.base import BaseCommand from django.conf import settings @@ -12,6 +15,7 @@ from django.contrib.auth.models import User, Group from path import path from lxml import etree + def create_groups(): ''' Create staff and instructor groups for all classes in the data_dir @@ -26,7 +30,7 @@ def create_groups(): continue if not os.path.isdir(path(data_dir) / course_dir): continue - + cxfn = path(data_dir) / course_dir / 'course.xml' try: coursexml = etree.parse(cxfn) @@ -38,11 +42,12 @@ def create_groups(): if course is None: print "oops, can't get course id for %s" % course_dir continue - print "course=%s for course_dir=%s" % (course,course_dir) - + print "course=%s for course_dir=%s" % (course, course_dir) + create_group('staff_%s' % course) # staff group create_group('instructor_%s' % course) # instructor group (can manage staff group list) + def create_group(gname): if Group.objects.filter(name=gname): print " group exists for %s" % gname @@ -51,6 +56,7 @@ def create_group(gname): g.save() print " created group %s" % gname + class Command(BaseCommand): help = "Create groups associated with all courses in data_dir." diff --git a/lms/djangoapps/lms_migration/management/commands/create_user.py b/lms/djangoapps/lms_migration/management/commands/create_user.py index 7d39accc44b..86b355e5719 100644 --- a/lms/djangoapps/lms_migration/management/commands/create_user.py +++ b/lms/djangoapps/lms_migration/management/commands/create_user.py @@ -4,7 +4,10 @@ # # Create user. Prompt for groups and ExternalAuthMap -import os, sys, string, re +import os +import sys +import string +import re import datetime from getpass import getpass import json @@ -16,6 +19,7 @@ from student.models import UserProfile, Registration from external_auth.models import ExternalAuthMap from django.contrib.auth.models import User, Group + class MyCompleter(object): # Custom completer def __init__(self, options): @@ -24,23 +28,25 @@ class MyCompleter(object): # Custom completer def complete(self, text, state): if state == 0: # on first trigger, build possible matches if text: # cache matches (entries that start with entered text) - self.matches = [s for s in self.options + self.matches = [s for s in self.options if s and s.startswith(text)] else: # no text entered, all matches possible self.matches = self.options[:] # return match indexed by state - try: + try: return self.matches[state] except IndexError: return None + def GenPasswd(length=8, chars=string.letters + string.digits): return ''.join([choice(chars) for i in range(length)]) #----------------------------------------------------------------------------- # main command + class Command(BaseCommand): help = "Create user, interactively; can add ExternalAuthMap for MIT user if email@MIT.EDU resolves properly." @@ -52,27 +58,27 @@ class Command(BaseCommand): print "username %s already taken" % uname else: break - + make_eamap = False - if raw_input('Create MIT ExternalAuth? [n] ').lower()=='y': + if raw_input('Create MIT ExternalAuth? [n] ').lower() == 'y': email = '%s@MIT.EDU' % uname if not email.endswith('@MIT.EDU'): print "Failed - email must be @MIT.EDU" sys.exit(-1) mit_domain = 'ssl:MIT' - if ExternalAuthMap.objects.filter(external_id = email, external_domain = mit_domain): + if ExternalAuthMap.objects.filter(external_id=email, external_domain=mit_domain): print "Failed - email %s already exists as external_id" % email sys.exit(-1) make_eamap = True password = GenPasswd(12) - + # get name from kerberos try: kname = os.popen("finger %s | grep 'name:'" % email).read().strip().split('name: ')[1].strip() except: kname = '' name = raw_input('Full name: [%s] ' % kname).strip() - if name=='': + if name == '': name = kname print "name = %s" % name else: @@ -82,17 +88,17 @@ class Command(BaseCommand): if password == password2: break print "Oops, passwords do not match, please retry" - + while True: email = raw_input('email: ') if User.objects.filter(email=email): print "email %s already taken" % email else: break - + name = raw_input('Full name: ') - - + + user = User(username=uname, email=email, is_active=True) user.set_password(password) try: @@ -100,41 +106,41 @@ class Command(BaseCommand): except IntegrityError: print "Oops, failed to create user %s, IntegrityError" % user raise - + r = Registration() r.register(user) - + up = UserProfile(user=user) up.name = name up.save() - + if make_eamap: - credentials = "/C=US/ST=Massachusetts/O=Massachusetts Institute of Technology/OU=Client CA v1/CN=%s/emailAddress=%s" % (name,email) - eamap = ExternalAuthMap(external_id = email, - external_email = email, - external_domain = mit_domain, - external_name = name, - internal_password = password, - external_credentials = json.dumps(credentials), + credentials = "/C=US/ST=Massachusetts/O=Massachusetts Institute of Technology/OU=Client CA v1/CN=%s/emailAddress=%s" % (name, email) + eamap = ExternalAuthMap(external_id=email, + external_email=email, + external_domain=mit_domain, + external_name=name, + internal_password=password, + external_credentials=json.dumps(credentials), ) eamap.user = user eamap.dtsignup = datetime.datetime.now() eamap.save() - + print "User %s created successfully!" % user - - if not raw_input('Add user %s to any groups? [n] ' % user).lower()=='y': + + if not raw_input('Add user %s to any groups? [n] ' % user).lower() == 'y': sys.exit(0) - + print "Here are the groups available:" - + groups = [str(g.name) for g in Group.objects.all()] print groups - + completer = MyCompleter(groups) readline.set_completer(completer.complete) readline.parse_and_bind('tab: complete') - + while True: gname = raw_input("Add group (tab to autocomplete, empty line to end): ") if not gname: @@ -144,6 +150,6 @@ class Command(BaseCommand): continue g = Group.objects.get(name=gname) user.groups.add(g) - print "Added %s to group %s" % (user,g) - + print "Added %s to group %s" % (user, g) + print "Done!" diff --git a/lms/djangoapps/lms_migration/management/commands/manage_course_groups.py b/lms/djangoapps/lms_migration/management/commands/manage_course_groups.py index f3a39db5ca3..b63ef7859b6 100644 --- a/lms/djangoapps/lms_migration/management/commands/manage_course_groups.py +++ b/lms/djangoapps/lms_migration/management/commands/manage_course_groups.py @@ -4,7 +4,10 @@ # # interactively list and edit membership in course staff and instructor groups -import os, sys, string, re +import os +import sys +import string +import re import datetime from getpass import getpass import json @@ -17,26 +20,27 @@ from django.contrib.auth.models import User, Group #----------------------------------------------------------------------------- # get all staff groups + class Command(BaseCommand): help = "Manage course group membership, interactively." def handle(self, *args, **options): gset = Group.objects.all() - + print "Groups:" - for cnt,g in zip(range(len(gset)), gset): - print "%d. %s" % (cnt,g) - + for cnt, g in zip(range(len(gset)), gset): + print "%d. %s" % (cnt, g) + gnum = int(raw_input('Choose group to manage (enter #): ')) - + group = gset[gnum] - + #----------------------------------------------------------------------------- # users in group - + uall = User.objects.all() - if uall.count()<50: + if uall.count() < 50: print "----" print "List of All Users: %s" % [str(x.username) for x in uall] print "----" @@ -44,24 +48,24 @@ class Command(BaseCommand): print "----" print "There are %d users, which is too many to list" % uall.count() print "----" - + while True: - + print "Users in the group:" - + uset = group.user_set.all() for cnt, u in zip(range(len(uset)), uset): print "%d. %s" % (cnt, u) - + action = raw_input('Choose user to delete (enter #) or enter usernames (comma delim) to add: ') - - m = re.match('^[0-9]+$',action) + + m = re.match('^[0-9]+$', action) if m: unum = int(action) u = uset[unum] print "Deleting user %s" % u u.groups.remove(group) - + else: for uname in action.split(','): try: @@ -71,6 +75,3 @@ class Command(BaseCommand): continue print "adding %s to group %s" % (user, group) user.groups.add(group) - - - diff --git a/lms/djangoapps/lms_migration/migrate.py b/lms/djangoapps/lms_migration/migrate.py index ecde31d6ddd..9cdc783bb96 100644 --- a/lms/djangoapps/lms_migration/migrate.py +++ b/lms/djangoapps/lms_migration/migrate.py @@ -22,29 +22,32 @@ log = logging.getLogger("mitx.lms_migrate") LOCAL_DEBUG = True ALLOWED_IPS = settings.LMS_MIGRATION_ALLOWED_IPS + def escape(s): """escape HTML special characters in string""" - return str(s).replace('<','<').replace('>','>') + return str(s).replace('<', '<').replace('>', '>') + def getip(request): ''' Extract IP address of requester from header, even if behind proxy ''' - ip = request.META.get('HTTP_X_REAL_IP','') # nginx reverse proxy + ip = request.META.get('HTTP_X_REAL_IP', '') # nginx reverse proxy if not ip: - ip = request.META.get('REMOTE_ADDR','None') + ip = request.META.get('REMOTE_ADDR', 'None') return ip def get_commit_id(course): - return course.metadata.get('GIT_COMMIT_ID','No commit id') + return course.metadata.get('GIT_COMMIT_ID', 'No commit id') # getattr(def_ms.courses[reload_dir], 'GIT_COMMIT_ID','No commit id') -def set_commit_id(course,commit_id): +def set_commit_id(course, commit_id): course.metadata['GIT_COMMIT_ID'] = commit_id # setattr(def_ms.courses[reload_dir], 'GIT_COMMIT_ID', new_commit_id) + def manage_modulestores(request, reload_dir=None, commit_id=None): ''' Manage the static in-memory modulestores. @@ -65,7 +68,7 @@ def manage_modulestores(request, reload_dir=None, commit_id=None): html += '<h3>IP address: %s <h3>' % ip html += '<h3>User: %s </h3>' % request.user html += '<h3>My pid: %s</h3>' % os.getpid() - log.debug('request from ip=%s, user=%s' % (ip,request.user)) + log.debug('request from ip=%s, user=%s' % (ip, request.user)) if not (ip in ALLOWED_IPS or 'any' in ALLOWED_IPS): if request.user and request.user.is_staff: @@ -89,7 +92,7 @@ def manage_modulestores(request, reload_dir=None, commit_id=None): log.debug('commit_id="%s"' % commit_id) log.debug('current_commit_id="%s"' % current_commit_id) - if (commit_id is not None) and (commit_id==current_commit_id): + if (commit_id is not None) and (commit_id == current_commit_id): html += "<h2>Already at commit id %s for %s</h2>" % (commit_id, reload_dir) track.views.server_track(request, 'reload %s skipped already at %s (pid=%s)' % (reload_dir, @@ -100,7 +103,7 @@ def manage_modulestores(request, reload_dir=None, commit_id=None): else: html += '<h2>Reloaded course directory "%s"</h2>' % reload_dir def_ms.try_load_course(reload_dir) - gdir = settings.DATA_DIR / reload_dir + gdir = settings.DATA_DIR / reload_dir new_commit_id = os.popen('cd %s; git log -n 1 | head -1' % gdir).read().strip().split(' ')[1] set_commit_id(def_ms.courses[reload_dir], new_commit_id) html += '<p>commit_id=%s</p>' % new_commit_id @@ -121,21 +124,21 @@ def manage_modulestores(request, reload_dir=None, commit_id=None): #---------------------------------------- - dumpfields = ['definition','location','metadata'] + dumpfields = ['definition', 'location', 'metadata'] for cdir, course in def_ms.courses.items(): html += '<hr width="100%"/>' - html += '<h2>Course: %s (%s)</h2>' % (course.display_name,cdir) + html += '<h2>Course: %s (%s)</h2>' % (course.display_name, cdir) html += '<p>commit_id=%s</p>' % get_commit_id(course) for field in dumpfields: - data = getattr(course,field) + data = getattr(course, field) html += '<h3>%s</h3>' % field - if type(data)==dict: + if type(data) == dict: html += '<ul>' - for k,v in data.items(): - html += '<li>%s:%s</li>' % (escape(k),escape(v)) + for k, v in data.items(): + html += '<li>%s:%s</li>' % (escape(k), escape(v)) html += '</ul>' else: html += '<ul><li>%s</li></ul>' % escape(data) @@ -159,6 +162,7 @@ def manage_modulestores(request, reload_dir=None, commit_id=None): html += "</body></html>" return HttpResponse(html) + @csrf_exempt def gitreload(request, reload_dir=None): ''' @@ -172,8 +176,8 @@ def gitreload(request, reload_dir=None): html += '<h3>IP address: %s ' % ip html += '<h3>User: %s ' % request.user - ALLOWED_IPS = [] # allow none by default - if hasattr(settings,'ALLOWED_GITRELOAD_IPS'): # allow override in settings + ALLOWED_IPS = [] # allow none by default + if hasattr(settings, 'ALLOWED_GITRELOAD_IPS'): # allow override in settings ALLOWED_IPS = settings.ALLOWED_GITRELOAD_IPS if not (ip in ALLOWED_IPS or 'any' in ALLOWED_IPS): @@ -182,9 +186,9 @@ def gitreload(request, reload_dir=None): else: html += 'Permission denied' html += "</body></html>" - log.debug('request denied from %s, ALLOWED_IPS=%s' % (ip,ALLOWED_IPS)) - return HttpResponse(html) - + log.debug('request denied from %s, ALLOWED_IPS=%s' % (ip, ALLOWED_IPS)) + return HttpResponse(html) + #---------------------------------------- # see if request is from github (POST with JSON) @@ -195,19 +199,19 @@ def gitreload(request, reload_dir=None): log.debug("gitargs=%s" % gitargs) reload_dir = gitargs['repository']['name'] log.debug("github reload_dir=%s" % reload_dir) - gdir = settings.DATA_DIR / reload_dir + gdir = settings.DATA_DIR / reload_dir if not os.path.exists(gdir): log.debug("====> ERROR in gitreload - no such directory %s" % reload_dir) return HttpResponse('Error') cmd = "cd %s; git reset --hard HEAD; git clean -f -d; git pull origin; chmod g+w course.xml" % gdir log.debug(os.popen(cmd).read()) - if hasattr(settings,'GITRELOAD_HOOK'): # hit this hook after reload, if set + if hasattr(settings, 'GITRELOAD_HOOK'): # hit this hook after reload, if set gh = settings.GITRELOAD_HOOK if gh: - ghurl = '%s/%s' % (gh,reload_dir) + ghurl = '%s/%s' % (gh, reload_dir) r = requests.get(ghurl) log.debug("GITRELOAD_HOOK to %s: %s" % (ghurl, r.text)) - + #---------------------------------------- # reload course if specified @@ -220,4 +224,4 @@ def gitreload(request, reload_dir=None): def_ms.try_load_course(reload_dir) track.views.server_track(request, 'reloaded %s' % reload_dir, {}, page='migrate') - return HttpResponse(html) + return HttpResponse(html) diff --git a/lms/djangoapps/open_ended_grading/controller_query_service.py b/lms/djangoapps/open_ended_grading/controller_query_service.py index 5d2c40b6ce7..83d5617bd20 100644 --- a/lms/djangoapps/open_ended_grading/controller_query_service.py +++ b/lms/djangoapps/open_ended_grading/controller_query_service.py @@ -12,12 +12,13 @@ from mitxmako.shortcuts import render_to_string log = logging.getLogger(__name__) + class ControllerQueryService(GradingService): """ Interface to staff grading backend. """ def __init__(self, config): - config['system'] = ModuleSystem(None,None,None,render_to_string,None) + config['system'] = ModuleSystem(None, None, None, render_to_string, None) super(ControllerQueryService, self).__init__(config) self.check_eta_url = self.url + '/get_submission_eta/' self.is_unique_url = self.url + '/is_name_unique/' @@ -29,34 +30,34 @@ class ControllerQueryService(GradingService): def check_if_name_is_unique(self, location, problem_id, course_id): params = { 'course_id': course_id, - 'location' : location, - 'problem_id' : problem_id + 'location': location, + 'problem_id': problem_id } response = self.get(self.is_unique_url, params) return response def check_for_eta(self, location): params = { - 'location' : location, + 'location': location, } response = self.get(self.check_eta_url, params) return response def check_combined_notifications(self, course_id, student_id, user_is_staff, last_time_viewed): - params= { - 'student_id' : student_id, - 'course_id' : course_id, - 'user_is_staff' : user_is_staff, - 'last_time_viewed' : last_time_viewed, + params = { + 'student_id': student_id, + 'course_id': course_id, + 'user_is_staff': user_is_staff, + 'last_time_viewed': last_time_viewed, } log.debug(self.combined_notifications_url) - response = self.get(self.combined_notifications_url,params) + response = self.get(self.combined_notifications_url, params) return response def get_grading_status_list(self, course_id, student_id): params = { - 'student_id' : student_id, - 'course_id' : course_id, + 'student_id': student_id, + 'course_id': course_id, } response = self.get(self.grading_status_list_url, params) @@ -64,7 +65,7 @@ class ControllerQueryService(GradingService): def get_flagged_problem_list(self, course_id): params = { - 'course_id' : course_id, + 'course_id': course_id, } response = self.get(self.flagged_problem_list_url, params) @@ -72,12 +73,11 @@ class ControllerQueryService(GradingService): def take_action_on_flags(self, course_id, student_id, submission_id, action_type): params = { - 'course_id' : course_id, - 'student_id' : student_id, - 'submission_id' : submission_id, - 'action_type' : action_type + 'course_id': course_id, + 'student_id': student_id, + 'submission_id': submission_id, + 'action_type': action_type } response = self.post(self.take_action_on_flags_url, params) return response - diff --git a/lms/djangoapps/open_ended_grading/open_ended_notifications.py b/lms/djangoapps/open_ended_grading/open_ended_notifications.py index b8b323acad2..f79013e3963 100644 --- a/lms/djangoapps/open_ended_grading/open_ended_notifications.py +++ b/lms/djangoapps/open_ended_grading/open_ended_notifications.py @@ -14,7 +14,7 @@ from xmodule import peer_grading_service from xmodule.x_module import ModuleSystem from mitxmako.shortcuts import render_to_string -log=logging.getLogger(__name__) +log = logging.getLogger(__name__) NOTIFICATION_CACHE_TIME = 300 KEY_PREFIX = "open_ended_" @@ -26,10 +26,11 @@ NOTIFICATION_TYPES = ( ('flagged_submissions_exist', 'open_ended_flagged_problems', 'Flagged Submissions') ) + def staff_grading_notifications(course, user): staff_gs = StaffGradingService(settings.STAFF_GRADING_INTERFACE) - pending_grading=False - img_path= "" + pending_grading = False + img_path = "" course_id = course.id student_id = unique_id_for_user(user) notification_type = "staff" @@ -42,7 +43,7 @@ def staff_grading_notifications(course, user): notifications = json.loads(staff_gs.get_notifications(course_id)) if notifications['success']: if notifications['staff_needs_to_grade']: - pending_grading=True + pending_grading = True except: #Non catastrophic error, so no real action notifications = {} @@ -51,17 +52,18 @@ def staff_grading_notifications(course, user): if pending_grading: img_path = "/static/images/slider-handle.png" - notification_dict = {'pending_grading' : pending_grading, 'img_path' : img_path, 'response' : notifications} + notification_dict = {'pending_grading': pending_grading, 'img_path': img_path, 'response': notifications} set_value_in_cache(student_id, course_id, notification_type, notification_dict) return notification_dict + def peer_grading_notifications(course, user): - system = ModuleSystem(None,None,None,render_to_string,None) + system = ModuleSystem(None, None, None, render_to_string, None) peer_gs = peer_grading_service.PeerGradingService(settings.PEER_GRADING_INTERFACE, system) - pending_grading=False - img_path= "" + pending_grading = False + img_path = "" course_id = course.id student_id = unique_id_for_user(user) notification_type = "peer" @@ -71,10 +73,10 @@ def peer_grading_notifications(course, user): return notification_dict try: - notifications = json.loads(peer_gs.get_notifications(course_id,student_id)) + notifications = json.loads(peer_gs.get_notifications(course_id, student_id)) if notifications['success']: if notifications['student_needs_to_peer_grade']: - pending_grading=True + pending_grading = True except: #Non catastrophic error, so no real action notifications = {} @@ -83,12 +85,13 @@ def peer_grading_notifications(course, user): if pending_grading: img_path = "/static/images/slider-handle.png" - notification_dict = {'pending_grading' : pending_grading, 'img_path' : img_path, 'response' : notifications} + notification_dict = {'pending_grading': pending_grading, 'img_path': img_path, 'response': notifications} set_value_in_cache(student_id, course_id, notification_type, notification_dict) return notification_dict + def combined_notifications(course, user): controller_url = open_ended_util.get_controller_url() controller_qs = ControllerQueryService(controller_url) @@ -102,24 +105,24 @@ def combined_notifications(course, user): return notification_dict min_time_to_query = user.last_login - last_module_seen = StudentModule.objects.filter(student=user, course_id = course_id, modified__gt=min_time_to_query).values('modified').order_by('-modified') + last_module_seen = StudentModule.objects.filter(student=user, course_id=course_id, modified__gt=min_time_to_query).values('modified').order_by('-modified') last_module_seen_count = last_module_seen.count() - if last_module_seen_count>0: + if last_module_seen_count > 0: last_time_viewed = last_module_seen[0]['modified'] - datetime.timedelta(seconds=(NOTIFICATION_CACHE_TIME + 60)) else: last_time_viewed = user.last_login - pending_grading= False + pending_grading = False - img_path= "" + img_path = "" try: - controller_response = controller_qs.check_combined_notifications(course.id,student_id, user_is_staff, last_time_viewed) + controller_response = controller_qs.check_combined_notifications(course.id, student_id, user_is_staff, last_time_viewed) log.debug(controller_response) notifications = json.loads(controller_response) if notifications['success']: if notifications['overall_need_to_check']: - pending_grading=True + pending_grading = True except: #Non catastrophic error, so no real action notifications = {} @@ -128,36 +131,41 @@ def combined_notifications(course, user): if pending_grading: img_path = "/static/images/slider-handle.png" - notification_dict = {'pending_grading' : pending_grading, 'img_path' : img_path, 'response' : notifications} + notification_dict = {'pending_grading': pending_grading, 'img_path': img_path, 'response': notifications} set_value_in_cache(student_id, course_id, notification_type, notification_dict) return notification_dict + def get_value_from_cache(student_id, course_id, notification_type): key_name = create_key_name(student_id, course_id, notification_type) success, value = _get_value_from_cache(key_name) return success, value + def set_value_in_cache(student_id, course_id, notification_type, value): key_name = create_key_name(student_id, course_id, notification_type) _set_value_in_cache(key_name, value) + def create_key_name(student_id, course_id, notification_type): key_name = "{prefix}{type}_{course}_{student}".format(prefix=KEY_PREFIX, type=notification_type, course=course_id, student=student_id) return key_name + def _get_value_from_cache(key_name): value = cache.get(key_name) success = False if value is None: - return success , value + return success, value try: value = json.loads(value) success = True except: pass - return success , value + return success, value + def _set_value_in_cache(key_name, value): - cache.set(key_name, json.dumps(value), NOTIFICATION_CACHE_TIME) \ No newline at end of file + cache.set(key_name, json.dumps(value), NOTIFICATION_CACHE_TIME) diff --git a/lms/djangoapps/open_ended_grading/open_ended_util.py b/lms/djangoapps/open_ended_grading/open_ended_util.py index 07744d7d2ce..1aa0f1ba700 100644 --- a/lms/djangoapps/open_ended_grading/open_ended_util.py +++ b/lms/djangoapps/open_ended_grading/open_ended_util.py @@ -1,12 +1,13 @@ from django.conf import settings import logging -log=logging.getLogger(__name__) +log = logging.getLogger(__name__) + def get_controller_url(): peer_grading_url = settings.PEER_GRADING_INTERFACE['url'] split_url = peer_grading_url.split("/") controller_url = "http://" + split_url[2] + "/grading_controller" - controller_settings=settings.PEER_GRADING_INTERFACE.copy() + controller_settings = settings.PEER_GRADING_INTERFACE.copy() controller_settings['url'] = controller_url return controller_settings diff --git a/lms/djangoapps/open_ended_grading/staff_grading.py b/lms/djangoapps/open_ended_grading/staff_grading.py index 7a48b25a499..e39b26da561 100644 --- a/lms/djangoapps/open_ended_grading/staff_grading.py +++ b/lms/djangoapps/open_ended_grading/staff_grading.py @@ -22,4 +22,3 @@ class StaffGrading(object): return "<b>Instructor grading!</b>" # context = {} # return render_to_string('courseware/instructor_grading_view.html', context) - diff --git a/lms/djangoapps/open_ended_grading/staff_grading_service.py b/lms/djangoapps/open_ended_grading/staff_grading_service.py index d8bee99ac75..dfadacb7248 100644 --- a/lms/djangoapps/open_ended_grading/staff_grading_service.py +++ b/lms/djangoapps/open_ended_grading/staff_grading_service.py @@ -21,6 +21,7 @@ from mitxmako.shortcuts import render_to_string log = logging.getLogger(__name__) + class MockStaffGradingService(object): """ A simple mockup of a staff grading service, testing. @@ -28,7 +29,7 @@ class MockStaffGradingService(object): def __init__(self): self.cnt = 0 - def get_next(self,course_id, location, grader_id): + def get_next(self, course_id, location, grader_id): self.cnt += 1 return json.dumps({'success': True, 'submission_id': self.cnt, @@ -61,7 +62,7 @@ class StaffGradingService(GradingService): Interface to staff grading backend. """ def __init__(self, config): - config['system'] = ModuleSystem(None,None,None,render_to_string,None) + config['system'] = ModuleSystem(None, None, None, render_to_string, None) super(StaffGradingService, self).__init__(config) self.get_next_url = self.url + '/get_next_submission/' self.save_grade_url = self.url + '/save_grade/' @@ -85,7 +86,7 @@ class StaffGradingService(GradingService): Raises: GradingServiceError: something went wrong with the connection. """ - params = {'course_id': course_id,'grader_id': grader_id} + params = {'course_id': course_id, 'grader_id': grader_id} return self.get(self.get_problem_list_url, params) @@ -166,6 +167,7 @@ def staff_grading_service(): return _service + def _err_response(msg): """ Return a HttpResponse with a json dump with success=False, and the given error message. @@ -329,4 +331,3 @@ def save_grade(request, course_id): # Ok, save_grade seemed to work. Get the next submission to grade. return HttpResponse(_get_next(course_id, grader_id, location), mimetype="application/json") - diff --git a/lms/djangoapps/open_ended_grading/tests.py b/lms/djangoapps/open_ended_grading/tests.py index 3ee8352c5c9..4d220d4baad 100644 --- a/lms/djangoapps/open_ended_grading/tests.py +++ b/lms/djangoapps/open_ended_grading/tests.py @@ -44,7 +44,7 @@ class TestStaffGradingService(ct.PageLoader): self.create_account('u2', self.instructor, self.password) self.activate_user(self.student) self.activate_user(self.instructor) - + self.course_id = "edX/toy/2012_Fall" self.toy = modulestore().get_course(self.course_id) def make_instructor(course): @@ -118,7 +118,7 @@ class TestStaffGradingService(ct.PageLoader): self.assertTrue(d['success'], str(d)) self.assertIsNotNone(d['problem_list']) - + @override_settings(MODULESTORE=ct.TEST_DATA_XML_MODULESTORE) class TestPeerGradingService(ct.PageLoader): ''' @@ -137,7 +137,7 @@ class TestPeerGradingService(ct.PageLoader): self.create_account('u2', self.instructor, self.password) self.activate_user(self.student) self.activate_user(self.instructor) - + self.course_id = "edX/toy/2012_Fall" self.toy = modulestore().get_course(self.course_id) location = "i4x://edX/toy/peergrading/init" @@ -146,7 +146,7 @@ class TestPeerGradingService(ct.PageLoader): self.system = ModuleSystem(location, None, None, render_to_string, None) self.descriptor = peer_grading_module.PeerGradingDescriptor(self.system) - self.peer_module = peer_grading_module.PeerGradingModule(self.system,location,"<peergrading/>",self.descriptor) + self.peer_module = peer_grading_module.PeerGradingModule(self.system, location, "<peergrading/>", self.descriptor) self.peer_module.peer_gs = self.mock_service self.logout() @@ -171,7 +171,7 @@ class TestPeerGradingService(ct.PageLoader): def test_save_grade_success(self): raise SkipTest() data = 'rubric_scores[]=1|rubric_scores[]=2|location=' + self.location + '|submission_id=1|submission_key=fake key|score=2|feedback=feedback|submission_flagged=False' - qdict = QueryDict(data.replace("|","&")) + qdict = QueryDict(data.replace("|", "&")) r = self.peer_module.save_grade(qdict) d = r self.assertTrue(d['success']) @@ -222,7 +222,7 @@ class TestPeerGradingService(ct.PageLoader): def test_save_calibration_essay_success(self): raise SkipTest() data = 'rubric_scores[]=1|rubric_scores[]=2|location=' + self.location + '|submission_id=1|submission_key=fake key|score=2|feedback=feedback|submission_flagged=False' - qdict = QueryDict(data.replace("|","&")) + qdict = QueryDict(data.replace("|", "&")) r = self.peer_module.save_calibration_essay(qdict) d = r self.assertTrue(d['success']) @@ -235,4 +235,3 @@ class TestPeerGradingService(ct.PageLoader): self.assertFalse(d['success']) self.assertTrue(d['error'].find('Missing required keys:') > -1) self.assertFalse('actual_score' in d) - diff --git a/lms/djangoapps/open_ended_grading/views.py b/lms/djangoapps/open_ended_grading/views.py index af7f930207c..f2e2a4513e4 100644 --- a/lms/djangoapps/open_ended_grading/views.py +++ b/lms/djangoapps/open_ended_grading/views.py @@ -10,7 +10,7 @@ from mitxmako.shortcuts import render_to_response from django.core.urlresolvers import reverse from student.models import unique_id_for_user -from courseware.courses import get_course_with_access +from courseware.courses import get_course_with_access from controller_query_service import ControllerQueryService from xmodule.grading_service_module import GradingServiceError @@ -38,12 +38,15 @@ Reverses the URL from the name and the course id, and then adds a trailing slash it does not exist yet """ + + def _reverse_with_slash(url_name, course_id): ajax_url = _reverse_without_slash(url_name, course_id) if not ajax_url.endswith('/'): ajax_url += '/' return ajax_url + def _reverse_without_slash(url_name, course_id): ajax_url = reverse(url_name, kwargs={'course_id': course_id}) return ajax_url @@ -52,14 +55,16 @@ DESCRIPTION_DICT = { 'Peer Grading': "View all problems that require peer assessment in this particular course.", 'Staff Grading': "View ungraded submissions submitted by students for the open ended problems in the course.", 'Problems you have submitted': "View open ended problems that you have previously submitted for grading.", - 'Flagged Submissions' : "View submissions that have been flagged by students as inappropriate." + 'Flagged Submissions': "View submissions that have been flagged by students as inappropriate." } ALERT_DICT = { 'Peer Grading': "New submissions to grade", 'Staff Grading': "New submissions to grade", 'Problems you have submitted': "New grades have been returned", - 'Flagged Submissions' : "Submissions have been flagged for review" + 'Flagged Submissions': "Submissions have been flagged for review" } + + @cache_control(no_cache=True, no_store=True, must_revalidate=True) def staff_grading(request, course_id): """ @@ -68,7 +73,7 @@ def staff_grading(request, course_id): course = get_course_with_access(request.user, course_id, 'staff') ajax_url = _reverse_with_slash('staff_grading', course_id) - + return render_to_response('instructor/staff_grading.html', { 'course': course, 'course_id': course_id, @@ -76,6 +81,7 @@ def staff_grading(request, course_id): # Checked above 'staff_access': True, }) + @cache_control(no_cache=True, no_store=True, must_revalidate=True) def peer_grading(request, course_id): ''' @@ -98,6 +104,7 @@ def peer_grading(request, course_id): log.exception(error_message + "Current course is: {0}".format(course_id)) return HttpResponse(error_message) + def generate_problem_url(problem_url_parts, base_course_url): """ From a list of problem url parts generated by search.path_to_location and a base course url, generates a url to a problem @@ -106,10 +113,10 @@ def generate_problem_url(problem_url_parts, base_course_url): @return: A path to the problem """ problem_url = base_course_url + "/" - for z in xrange(0,len(problem_url_parts)): + for z in xrange(0, len(problem_url_parts)): part = problem_url_parts[z] if part is not None: - if z==1: + if z == 1: problem_url += "courseware/" problem_url += part + "/" return problem_url @@ -139,10 +146,10 @@ def student_problem_list(request, course_id): else: problem_list = problem_list_dict['problem_list'] - for i in xrange(0,len(problem_list)): + for i in xrange(0, len(problem_list)): problem_url_parts = search.path_to_location(modulestore(), course.id, problem_list[i]['location']) problem_url = generate_problem_url(problem_url_parts, base_course_url) - problem_list[i].update({'actual_url' : problem_url}) + problem_list[i].update({'actual_url': problem_url}) """ except GradingServiceError: @@ -166,6 +173,7 @@ def student_problem_list(request, course_id): # Checked above 'staff_access': False, }) + @cache_control(no_cache=True, no_store=True, must_revalidate=True) def flagged_problem_list(request, course_id): ''' @@ -186,7 +194,7 @@ def flagged_problem_list(request, course_id): success = problem_list_dict['success'] if 'error' in problem_list_dict: error_text = problem_list_dict['error'] - problem_list=[] + problem_list = [] else: problem_list = problem_list_dict['flagged_submissions'] @@ -211,6 +219,7 @@ def flagged_problem_list(request, course_id): } return render_to_response('open_ended_problems/open_ended_flagged_problems.html', context) + @cache_control(no_cache=True, no_store=True, must_revalidate=True) def combined_notifications(request, course_id): """ @@ -220,11 +229,11 @@ def combined_notifications(request, course_id): user = request.user notifications = open_ended_notifications.combined_notifications(course, user) response = notifications['response'] - notification_tuples=open_ended_notifications.NOTIFICATION_TYPES + notification_tuples = open_ended_notifications.NOTIFICATION_TYPES notification_list = [] - for response_num in xrange(0,len(notification_tuples)): - tag=notification_tuples[response_num][0] + for response_num in xrange(0, len(notification_tuples)): + tag = notification_tuples[response_num][0] if tag in response: url_name = notification_tuples[response_num][1] human_name = notification_tuples[response_num][2] @@ -241,11 +250,11 @@ def combined_notifications(request, course_id): alert_message = ALERT_DICT[human_name] else: alert_message = "" - + notification_item = { - 'url' : url, - 'name' : human_name, - 'alert' : has_img, + 'url': url, + 'name': human_name, + 'alert': has_img, 'description': description, 'alert_message': alert_message } @@ -253,17 +262,18 @@ def combined_notifications(request, course_id): ajax_url = _reverse_with_slash('open_ended_notifications', course_id) combined_dict = { - 'error_text' : "", - 'notification_list' : notification_list, - 'course' : course, - 'success' : True, - 'ajax_url' : ajax_url, + 'error_text': "", + 'notification_list': notification_list, + 'course': course, + 'success': True, + 'ajax_url': ajax_url, } return render_to_response('open_ended_problems/combined_notifications.html', combined_dict ) + @cache_control(no_cache=True, no_store=True, must_revalidate=True) def take_action_on_flags(request, course_id): """ @@ -293,5 +303,3 @@ def take_action_on_flags(request, course_id): except GradingServiceError: log.exception("Error saving calibration grade, submission_id: {0}, submission_key: {1}, grader_id: {2}".format(submission_id, submission_key, grader_id)) return _err_response('Could not connect to grading service') - - diff --git a/lms/djangoapps/portal/features/common.py b/lms/djangoapps/portal/features/common.py index 20c2ab56b88..8bfb5483674 100644 --- a/lms/djangoapps/portal/features/common.py +++ b/lms/djangoapps/portal/features/common.py @@ -1,4 +1,4 @@ -from lettuce import world, step#, before, after +from lettuce import world, step # , before, after from factories import * from django.core.management import call_command from nose.tools import assert_equals, assert_in @@ -11,74 +11,90 @@ import time from logging import getLogger logger = getLogger(__name__) + @step(u'I wait (?:for )?"(\d+)" seconds?$') def wait(step, seconds): time.sleep(float(seconds)) + @step('I (?:visit|access|open) the homepage$') def i_visit_the_homepage(step): world.browser.visit(django_url('/')) assert world.browser.is_element_present_by_css('header.global', 10) + @step(u'I (?:visit|access|open) the dashboard$') def i_visit_the_dashboard(step): world.browser.visit(django_url('/dashboard')) assert world.browser.is_element_present_by_css('section.container.dashboard', 5) + @step(r'click (?:the|a) link (?:called|with the text) "([^"]*)"$') def click_the_link_called(step, text): world.browser.find_link_by_text(text).click() + @step('I should be on the dashboard page$') def i_should_be_on_the_dashboard(step): assert world.browser.is_element_present_by_css('section.container.dashboard', 5) assert world.browser.title == 'Dashboard' + @step(u'I (?:visit|access|open) the courses page$') def i_am_on_the_courses_page(step): world.browser.visit(django_url('/courses')) assert world.browser.is_element_present_by_css('section.courses') + @step('I should see that the path is "([^"]*)"$') def i_should_see_that_the_path_is(step, path): assert world.browser.url == django_url(path) + @step(u'the page title should be "([^"]*)"$') def the_page_title_should_be(step, title): assert world.browser.title == title + @step(r'should see that the url is "([^"]*)"$') def should_have_the_url(step, url): assert_equals(world.browser.url, url) + @step(r'should see (?:the|a) link (?:called|with the text) "([^"]*)"$') def should_see_a_link_called(step, text): assert len(world.browser.find_link_by_text(text)) > 0 + @step(r'should see "(.*)" (?:somewhere|anywhere) in (?:the|this) page') def should_see_in_the_page(step, text): assert_in(text, world.browser.html) + @step('I am logged in$') def i_am_logged_in(step): world.create_user('robot') world.log_in('robot@edx.org', 'test') + @step('I am not logged in$') def i_am_not_logged_in(step): world.browser.cookies.delete() + @step(u'I am registered for a course$') def i_am_registered_for_a_course(step): world.create_user('robot') u = User.objects.get(username='robot') CourseEnrollment.objects.create(user=u, course_id='MITx/6.002x/2012_Fall') world.log_in('robot@edx.org', 'test') - + + @step(u'I am an edX user$') def i_am_an_edx_user(step): world.create_user('robot') + @step(u'User "([^"]*)" is an edX user$') def registered_edx_user(step, uname): world.create_user(uname) diff --git a/lms/djangoapps/portal/features/factories.py b/lms/djangoapps/portal/features/factories.py index 07b615f4680..71781ea3d6e 100644 --- a/lms/djangoapps/portal/features/factories.py +++ b/lms/djangoapps/portal/features/factories.py @@ -3,6 +3,7 @@ from student.models import User, UserProfile, Registration from datetime import datetime import uuid + class UserProfileFactory(factory.Factory): FACTORY_FOR = UserProfile @@ -13,12 +14,14 @@ class UserProfileFactory(factory.Factory): mailing_address = None goals = 'World domination' + class RegistrationFactory(factory.Factory): FACTORY_FOR = Registration user = None activation_key = uuid.uuid4().hex + class UserFactory(factory.Factory): FACTORY_FOR = User diff --git a/lms/djangoapps/portal/features/homepage.py b/lms/djangoapps/portal/features/homepage.py index 638d65077c0..442098c1611 100644 --- a/lms/djangoapps/portal/features/homepage.py +++ b/lms/djangoapps/portal/features/homepage.py @@ -1,6 +1,7 @@ from lettuce import world, step from nose.tools import assert_in + @step('I should see "([^"]*)" in the Partners section$') def i_should_see_partner(step, partner): partners = world.browser.find_by_css(".partner .name span") diff --git a/lms/djangoapps/portal/features/login.py b/lms/djangoapps/portal/features/login.py index 5f200eb259d..094db078ca2 100644 --- a/lms/djangoapps/portal/features/login.py +++ b/lms/djangoapps/portal/features/login.py @@ -1,26 +1,31 @@ from lettuce import step, world from django.contrib.auth.models import User + @step('I am an unactivated user$') def i_am_an_unactivated_user(step): user_is_an_unactivated_user('robot') + @step('I am an activated user$') def i_am_an_activated_user(step): user_is_an_activated_user('robot') + @step('I submit my credentials on the login form') def i_submit_my_credentials_on_the_login_form(step): fill_in_the_login_form('email', 'robot@edx.org') fill_in_the_login_form('password', 'test') login_form = world.browser.find_by_css('form#login_form') login_form.find_by_value('Access My Courses').click() - + + @step(u'I should see the login error message "([^"]*)"$') def i_should_see_the_login_error_message(step, msg): login_error_div = world.browser.find_by_css('form#login_form #login_error') assert (msg in login_error_div.text) + @step(u'click the dropdown arrow$') def click_the_dropdown(step): css = ".dropdown" @@ -29,16 +34,19 @@ def click_the_dropdown(step): #### helper functions + def user_is_an_unactivated_user(uname): u = User.objects.get(username=uname) u.is_active = False u.save() + def user_is_an_activated_user(uname): u = User.objects.get(username=uname) u.is_active = True u.save() + def fill_in_the_login_form(field, value): login_form = world.browser.find_by_css('form#login_form') form_field = login_form.find_by_name(field) diff --git a/lms/djangoapps/portal/features/registration.py b/lms/djangoapps/portal/features/registration.py index 124bed4923c..b2b4c4bd8d2 100644 --- a/lms/djangoapps/portal/features/registration.py +++ b/lms/djangoapps/portal/features/registration.py @@ -1,5 +1,6 @@ from lettuce import world, step + @step('I register for the course numbered "([^"]*)"$') def i_register_for_the_course(step, course): courses_section = world.browser.find_by_css('section.courses') @@ -13,11 +14,13 @@ def i_register_for_the_course(step, course): assert world.browser.is_element_present_by_css('section.container.dashboard') + @step(u'I should see the course numbered "([^"]*)" in my dashboard$') def i_should_see_that_course_in_my_dashboard(step, course): course_link_css = 'section.my-courses a[href*="%s"]' % course assert world.browser.is_element_present_by_css(course_link_css) + @step(u'I press the "([^"]*)" button in the Unenroll dialog') def i_press_the_button_in_the_unenroll_dialog(step, value): button_css = 'section#unenroll-modal input[value="%s"]' % value diff --git a/lms/djangoapps/portal/features/signup.py b/lms/djangoapps/portal/features/signup.py index afde72b5898..3a697a6102f 100644 --- a/lms/djangoapps/portal/features/signup.py +++ b/lms/djangoapps/portal/features/signup.py @@ -1,22 +1,25 @@ from lettuce import world, step + @step('I fill in "([^"]*)" on the registration form with "([^"]*)"$') def when_i_fill_in_field_on_the_registration_form_with_value(step, field, value): register_form = world.browser.find_by_css('form#register_form') form_field = register_form.find_by_name(field) form_field.fill(value) + @step('I press the "([^"]*)" button on the registration form$') def i_press_the_button_on_the_registration_form(step, button): register_form = world.browser.find_by_css('form#register_form') register_form.find_by_value(button).click() + @step('I check the checkbox named "([^"]*)"$') def i_check_checkbox(step, checkbox): world.browser.find_by_name(checkbox).check() + @step('I should see "([^"]*)" in the dashboard banner$') def i_should_see_text_in_the_dashboard_banner_section(step, text): css_selector = "section.dashboard-banner h2" assert (text in world.browser.find_by_css(css_selector).text) - \ No newline at end of file diff --git a/lms/djangoapps/psychometrics/management/commands/init_psychometrics.py b/lms/djangoapps/psychometrics/management/commands/init_psychometrics.py index 5e782df5955..53f6e17e9d8 100644 --- a/lms/djangoapps/psychometrics/management/commands/init_psychometrics.py +++ b/lms/djangoapps/psychometrics/management/commands/init_psychometrics.py @@ -2,7 +2,9 @@ # # generate pyschometrics data from tracking logs and student module data -import os, sys, string +import os +import sys +import string import datetime import json @@ -17,7 +19,7 @@ from django.core.management.base import BaseCommand #db = "ocwtutor" # for debugging #db = "default" -db = getattr(settings,'DATABASE_FOR_PSYCHOMETRICS','default') +db = getattr(settings, 'DATABASE_FOR_PSYCHOMETRICS', 'default') class Command(BaseCommand): @@ -32,39 +34,39 @@ class Command(BaseCommand): #PsychometricData.objects.using(db).all().delete() smset = StudentModule.objects.using(db).exclude(max_grade=None) - + for sm in smset: url = sm.module_state_key location = Location(url) - if not location.category=="problem": + if not location.category == "problem": continue try: state = json.loads(sm.state) done = state['done'] except: - print "Oops, failed to eval state for %s (state=%s)" % (sm,sm.state) + print "Oops, failed to eval state for %s (state=%s)" % (sm, sm.state) continue - + if done: # only keep if problem completed try: pmd = PsychometricData.objects.using(db).get(studentmodule=sm) except PsychometricData.DoesNotExist: pmd = PsychometricData(studentmodule=sm) - + pmd.done = done pmd.attempts = state['attempts'] - + # get attempt times from tracking log uname = sm.student.username - tset = TrackingLog.objects.using(db).filter(username=uname, event_type__contains='save_problem_check') + tset = TrackingLog.objects.using(db).filter(username=uname, event_type__contains='save_problem_check') tset = tset.filter(event_source='server') tset = tset.filter(event__contains="'%s'" % url) checktimes = [x.dtcreated for x in tset] pmd.checktimes = checktimes - if not len(checktimes)==pmd.attempts: + if not len(checktimes) == pmd.attempts: print "Oops, mismatch in number of attempts and check times for %s" % pmd - + #print pmd pmd.save(using=db) - + print "%d PMD entries" % PsychometricData.objects.using(db).all().count() diff --git a/lms/djangoapps/psychometrics/models.py b/lms/djangoapps/psychometrics/models.py index 4ffdf591205..60455f01b87 100644 --- a/lms/djangoapps/psychometrics/models.py +++ b/lms/djangoapps/psychometrics/models.py @@ -7,6 +7,7 @@ from django.db import models from courseware.models import StudentModule + class PsychometricData(models.Model): """ This data is a table linking student, module, and module performance, @@ -25,7 +26,7 @@ class PsychometricData(models.Model): done = models.BooleanField(default=False) attempts = models.IntegerField(default=0) # extracted from studentmodule.state - checktimes = models.TextField(null=True, blank=True) # internally stored as list of datetime objects + checktimes = models.TextField(null=True, blank=True) # internally stored as list of datetime objects # keep in mind # grade = studentmodule.grade @@ -33,7 +34,7 @@ class PsychometricData(models.Model): # student = studentmodule.student # course_id = studentmodule.course_id # location = studentmodule.module_state_key - + def __unicode__(self): sm = self.studentmodule return "[PsychometricData] %s url=%s, grade=%s, max=%s, attempts=%s, ct=%s" % (sm.student, @@ -42,4 +43,3 @@ class PsychometricData(models.Model): sm.max_grade, self.attempts, self.checktimes) - diff --git a/lms/djangoapps/psychometrics/psychoanalyze.py b/lms/djangoapps/psychometrics/psychoanalyze.py index dd7d3282781..28a5c4437cc 100644 --- a/lms/djangoapps/psychometrics/psychoanalyze.py +++ b/lms/djangoapps/psychometrics/psychoanalyze.py @@ -66,7 +66,7 @@ class StatVar(object): if x > self.max: self.max = x self.sum += x - self.sum2 += x**2 + self.sum2 += x ** 2 self.cnt += 1 def avg(self): @@ -77,11 +77,11 @@ class StatVar(object): def var(self): if self.cnt is None: return 0 - return (self.sum2 / 1.0 / self.cnt / (self.unit**2)) - (self.avg()**2) + return (self.sum2 / 1.0 / self.cnt / (self.unit ** 2)) - (self.avg() ** 2) def sdv(self): v = self.var() - if v>0: + if v > 0: return math.sqrt(v) else: return 0 @@ -112,7 +112,7 @@ def make_histogram(ydata, bins=None): hist = dict(zip(bins, [0] * nbins)) for y in ydata: for b in bins[::-1]: # in reverse order - if y>b: + if y > b: hist[b] += 1 break # hist['bins'] = bins @@ -128,7 +128,7 @@ def problems_with_psychometric_data(course_id): ''' pmdset = PsychometricData.objects.using(db).filter(studentmodule__course_id=course_id) plist = [p['studentmodule__module_state_key'] for p in pmdset.values('studentmodule__module_state_key').distinct()] - problems = dict( (p, pmdset.filter(studentmodule__module_state_key=p).count()) for p in plist ) + problems = dict((p, pmdset.filter(studentmodule__module_state_key=p).count()) for p in plist) return problems @@ -241,7 +241,7 @@ def generate_plots_for_problem(problem): ylast = 0 for x in xdat: y = gset.filter(attempts=x).count() / ngset - ydat.append( y + ylast ) + ydat.append(y + ylast) ylast = y + ylast yset['ydat'] = ydat diff --git a/lms/djangoapps/simplewiki/mdx_mathjax.py b/lms/djangoapps/simplewiki/mdx_mathjax.py index a9148511e32..b14803744b7 100644 --- a/lms/djangoapps/simplewiki/mdx_mathjax.py +++ b/lms/djangoapps/simplewiki/mdx_mathjax.py @@ -28,4 +28,3 @@ class MathJaxExtension(markdown.Extension): def makeExtension(configs=None): return MathJaxExtension(configs) - diff --git a/lms/djangoapps/simplewiki/views.py b/lms/djangoapps/simplewiki/views.py index ef0928709fe..38c367dec9e 100644 --- a/lms/djangoapps/simplewiki/views.py +++ b/lms/djangoapps/simplewiki/views.py @@ -55,6 +55,7 @@ def update_template_dictionary(dictionary, request=None, course=None, article=No else: dictionary['staff_access'] = False + def view(request, article_path, course_id=None): course = get_opt_course_with_access(request.user, course_id, 'load') diff --git a/lms/djangoapps/static_template_view/views.py b/lms/djangoapps/static_template_view/views.py index 8ab6216eda5..022f12b1487 100644 --- a/lms/djangoapps/static_template_view/views.py +++ b/lms/djangoapps/static_template_view/views.py @@ -46,4 +46,3 @@ def render_404(request): def render_500(request): return HttpResponseServerError(render_to_string('static_templates/server-error.html', {})) - diff --git a/lms/djangoapps/staticbook/views.py b/lms/djangoapps/staticbook/views.py index fabb8b861c0..6750d151ce6 100644 --- a/lms/djangoapps/staticbook/views.py +++ b/lms/djangoapps/staticbook/views.py @@ -6,6 +6,7 @@ from courseware.access import has_access from courseware.courses import get_course_with_access from lxml import etree + @login_required def index(request, course_id, book_index, page=None): course = get_course_with_access(request.user, course_id, 'load') @@ -22,9 +23,10 @@ def index(request, course_id, book_index, page=None): {'book_index': book_index, 'page': int(page), 'course': course, 'book_url': textbook.book_url, 'table_of_contents': table_of_contents, - 'start_page' : textbook.start_page, - 'end_page' : textbook.end_page, + 'start_page': textbook.start_page, + 'end_page': textbook.end_page, 'staff_access': staff_access}) + def index_shifted(request, course_id, page): return index(request, course_id=course_id, page=int(page) + 24) diff --git a/lms/djangoapps/terrain/__init__.py b/lms/djangoapps/terrain/__init__.py index dd6869e7fd4..3445a01d176 100644 --- a/lms/djangoapps/terrain/__init__.py +++ b/lms/djangoapps/terrain/__init__.py @@ -3,4 +3,4 @@ # See https://groups.google.com/forum/?fromgroups=#!msg/lettuce-users/5VyU9B4HcX8/USgbGIJdS5QJ from terrain.browser import * from terrain.steps import * -from terrain.factories import * \ No newline at end of file +from terrain.factories import * diff --git a/lms/djangoapps/terrain/browser.py b/lms/djangoapps/terrain/browser.py index 1c2d401680f..e1925bde0be 100644 --- a/lms/djangoapps/terrain/browser.py +++ b/lms/djangoapps/terrain/browser.py @@ -8,20 +8,23 @@ logger.info("Loading the lettuce acceptance testing terrain file...") from django.core.management import call_command + @before.harvest def initial_setup(server): # Launch firefox world.browser = Browser('chrome') + @before.each_scenario def reset_data(scenario): - # Clean out the django test database defined in the + # Clean out the django test database defined in the # envs/acceptance.py file: mitx_all/db/test_mitx.db logger.debug("Flushing the test database...") call_command('flush', interactive=False) + @after.all def teardown_browser(total): # Quit firefox world.browser.quit() - pass \ No newline at end of file + pass diff --git a/lms/djangoapps/terrain/factories.py b/lms/djangoapps/terrain/factories.py index 377ce54d563..896f115df58 100644 --- a/lms/djangoapps/terrain/factories.py +++ b/lms/djangoapps/terrain/factories.py @@ -7,6 +7,7 @@ from time import gmtime from uuid import uuid4 from xmodule.timeparse import stringify_time + class UserProfileFactory(Factory): FACTORY_FOR = UserProfile @@ -17,12 +18,14 @@ class UserProfileFactory(Factory): mailing_address = None goals = 'World domination' + class RegistrationFactory(Factory): FACTORY_FOR = Registration user = None activation_key = uuid4().hex + class UserFactory(Factory): FACTORY_FOR = User @@ -37,12 +40,15 @@ class UserFactory(Factory): last_login = datetime(2012, 1, 1) date_joined = datetime(2011, 1, 1) -def XMODULE_COURSE_CREATION(class_to_create, **kwargs): + +def XMODULE_COURSE_CREATION(class_to_create, **kwargs): return XModuleCourseFactory._create(class_to_create, **kwargs) + def XMODULE_ITEM_CREATION(class_to_create, **kwargs): return XModuleItemFactory._create(class_to_create, **kwargs) + class XModuleCourseFactory(Factory): """ Factory for XModule courses. @@ -58,7 +64,7 @@ class XModuleCourseFactory(Factory): org = kwargs.get('org') number = kwargs.get('number') display_name = kwargs.get('display_name') - location = Location('i4x', org, number, + location = Location('i4x', org, number, 'course', Location.clean(display_name)) store = modulestore('direct') @@ -72,20 +78,22 @@ class XModuleCourseFactory(Factory): new_course.metadata['data_dir'] = uuid4().hex new_course.metadata['start'] = stringify_time(gmtime()) - new_course.tabs = [{"type": "courseware"}, + new_course.tabs = [{"type": "courseware"}, {"type": "course_info", "name": "Course Info"}, {"type": "discussion", "name": "Discussion"}, {"type": "wiki", "name": "Wiki"}, {"type": "progress", "name": "Progress"}] # Update the data in the mongo datastore - store.update_metadata(new_course.location.url(), new_course.own_metadata) + store.update_metadata(new_course.location.url(), new_course.own_metadata) return new_course + class Course: pass + class CourseFactory(XModuleCourseFactory): FACTORY_FOR = Course @@ -94,6 +102,7 @@ class CourseFactory(XModuleCourseFactory): number = '999' display_name = 'Robot Super Course' + class XModuleItemFactory(Factory): """ Factory for XModule items. @@ -110,7 +119,7 @@ class XModuleItemFactory(Factory): """ DETACHED_CATEGORIES = ['about', 'static_tab', 'course_info'] - + parent_location = Location(kwargs.get('parent_location')) template = Location(kwargs.get('template')) display_name = kwargs.get('display_name') @@ -137,12 +146,14 @@ class XModuleItemFactory(Factory): return new_item + class Item: pass + class ItemFactory(XModuleItemFactory): FACTORY_FOR = Item parent_location = 'i4x://MITx/999/course/Robot_Super_Course' template = 'i4x://edx/templates/chapter/Empty' - display_name = 'Section One' \ No newline at end of file + display_name = 'Section One' diff --git a/lms/djangoapps/terrain/steps.py b/lms/djangoapps/terrain/steps.py index 6824fa16ce7..6b2a813d8d2 100644 --- a/lms/djangoapps/terrain/steps.py +++ b/lms/djangoapps/terrain/steps.py @@ -15,79 +15,97 @@ import os.path from logging import getLogger logger = getLogger(__name__) + @step(u'I wait (?:for )?"(\d+)" seconds?$') def wait(step, seconds): time.sleep(float(seconds)) + @step('I (?:visit|access|open) the homepage$') def i_visit_the_homepage(step): world.browser.visit(django_url('/')) assert world.browser.is_element_present_by_css('header.global', 10) + @step(u'I (?:visit|access|open) the dashboard$') def i_visit_the_dashboard(step): world.browser.visit(django_url('/dashboard')) assert world.browser.is_element_present_by_css('section.container.dashboard', 5) + @step('I should be on the dashboard page$') def i_should_be_on_the_dashboard(step): assert world.browser.is_element_present_by_css('section.container.dashboard', 5) assert world.browser.title == 'Dashboard' + @step(u'I (?:visit|access|open) the courses page$') def i_am_on_the_courses_page(step): world.browser.visit(django_url('/courses')) assert world.browser.is_element_present_by_css('section.courses') + @step(u'I press the "([^"]*)" button$') def and_i_press_the_button(step, value): button_css = 'input[value="%s"]' % value world.browser.find_by_css(button_css).first.click() + @step(u'I click the link with the text "([^"]*)"$') def click_the_link_with_the_text_group1(step, linktext): world.browser.find_link_by_text(linktext).first.click() + @step('I should see that the path is "([^"]*)"$') def i_should_see_that_the_path_is(step, path): assert world.browser.url == django_url(path) + @step(u'the page title should be "([^"]*)"$') def the_page_title_should_be(step, title): assert_equals(world.browser.title, title) + @step('I am a logged in user$') def i_am_logged_in_user(step): create_user('robot') - log_in('robot@edx.org','test') + log_in('robot@edx.org', 'test') + @step('I am not logged in$') def i_am_not_logged_in(step): world.browser.cookies.delete() + @step('I am registered for a course$') def i_am_registered_for_a_course(step): create_user('robot') u = User.objects.get(username='robot') CourseEnrollment.objects.get_or_create(user=u, course_id='MITx/6.002x/2012_Fall') + @step('I am registered for course "([^"]*)"$') def i_am_registered_for_course_by_id(step, course_id): register_by_course_id(course_id) + @step('I am staff for course "([^"]*)"$') def i_am_staff_for_course_by_id(step, course_id): register_by_course_id(course_id, True) + @step('I log in$') def i_log_in(step): - log_in('robot@edx.org','test') + log_in('robot@edx.org', 'test') + @step(u'I am an edX user$') def i_am_an_edx_user(step): create_user('robot') #### helper functions + + @world.absorb def create_user(uname): portal_user = UserFactory.build(username=uname, email=uname + '@edx.org') @@ -100,11 +118,12 @@ def create_user(uname): user_profile = UserProfileFactory(user=portal_user) + @world.absorb -def log_in(email, password): +def log_in(email, password): world.browser.cookies.delete() world.browser.visit(django_url('/')) - world.browser.is_element_present_by_css('header.global', 10) + world.browser.is_element_present_by_css('header.global', 10) world.browser.click_link_by_href('#login-modal') login_form = world.browser.find_by_css('form#login_form') login_form.find_by_name('email').fill(email) @@ -114,15 +133,17 @@ def log_in(email, password): # wait for the page to redraw assert world.browser.is_element_present_by_css('.content-wrapper', 10) + @world.absorb def register_by_course_id(course_id, is_staff=False): create_user('robot') u = User.objects.get(username='robot') if is_staff: - u.is_staff=True - u.save() + u.is_staff = True + u.save() CourseEnrollment.objects.get_or_create(user=u, course_id=course_id) + @world.absorb def save_the_html(path='/tmp'): u = world.browser.url @@ -132,6 +153,7 @@ def save_the_html(path='/tmp'): f.write(html) f.close + @world.absorb def save_the_course_content(path='/tmp'): html = world.browser.html.encode('ascii', 'ignore') @@ -140,12 +162,12 @@ def save_the_course_content(path='/tmp'): # get rid of the header, we only want to compare the body soup.head.decompose() - # for now, remove the data-id attributes, because they are + # for now, remove the data-id attributes, because they are # causing mismatches between cms-master and master for item in soup.find_all(attrs={'data-id': re.compile('.*')}): del item['data-id'] - # we also need to remove them from unrendered problems, + # we also need to remove them from unrendered problems, # where they are contained in the text of divs instead of # in attributes of tags # Be careful of whether or not it was the last attribute @@ -164,7 +186,7 @@ def save_the_course_content(path='/tmp'): # use string slicing to grab everything after 'courseware/' in the URL u = world.browser.url - section_url = u[u.find('courseware/')+11:] + section_url = u[u.find('courseware/') + 11:] if not os.path.exists(path): os.makedirs(path) diff --git a/lms/envs/acceptance.py b/lms/envs/acceptance.py index e0857a43926..412815a4024 100644 --- a/lms/envs/acceptance.py +++ b/lms/envs/acceptance.py @@ -1,5 +1,5 @@ """ -This config file extends the test environment configuration +This config file extends the test environment configuration so that we can run the lettuce acceptance tests. """ from .test import * @@ -21,14 +21,14 @@ MODULESTORE = { } } -# Set this up so that rake lms[acceptance] and running the +# Set this up so that rake lms[acceptance] and running the # harvest command both use the same (test) database # which they can flush without messing up your dev db DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': ENV_ROOT / "db" / "test_mitx.db", - 'TEST_NAME': ENV_ROOT / "db" / "test_mitx.db", + 'TEST_NAME': ENV_ROOT / "db" / "test_mitx.db", } } diff --git a/lms/envs/common.py b/lms/envs/common.py index 614c68b361c..10947a97351 100644 --- a/lms/envs/common.py +++ b/lms/envs/common.py @@ -40,11 +40,11 @@ DISCUSSION_SETTINGS = { # Features MITX_FEATURES = { - 'SAMPLE' : False, - 'USE_DJANGO_PIPELINE' : True, - 'DISPLAY_HISTOGRAMS_TO_STAFF' : True, - 'REROUTE_ACTIVATION_EMAIL' : False, # nonempty string = address for all activation emails - 'DEBUG_LEVEL' : 0, # 0 = lowest level, least verbose, 255 = max level, most verbose + 'SAMPLE': False, + 'USE_DJANGO_PIPELINE': True, + 'DISPLAY_HISTOGRAMS_TO_STAFF': True, + 'REROUTE_ACTIVATION_EMAIL': False, # nonempty string = address for all activation emails + 'DEBUG_LEVEL': 0, # 0 = lowest level, least verbose, 255 = max level, most verbose ## DO NOT SET TO True IN THIS FILE ## Doing so will cause all courses to be released on production @@ -53,20 +53,20 @@ MITX_FEATURES = { # When True, will only publicly list courses by the subdomain. Expects you # to define COURSE_LISTINGS, a dictionary mapping subdomains to lists of # course_ids (see dev_int.py for an example) - 'SUBDOMAIN_COURSE_LISTINGS' : False, + 'SUBDOMAIN_COURSE_LISTINGS': False, # When True, will override certain branding with university specific values # Expects a SUBDOMAIN_BRANDING dictionary that maps the subdomain to the # university to use for branding purposes 'SUBDOMAIN_BRANDING': False, - 'FORCE_UNIVERSITY_DOMAIN': False, # set this to the university domain to use, as an override to HTTP_HOST + 'FORCE_UNIVERSITY_DOMAIN': False, # set this to the university domain to use, as an override to HTTP_HOST # set to None to do no university selection - 'ENABLE_TEXTBOOK' : True, + 'ENABLE_TEXTBOOK': True, 'ENABLE_DISCUSSION_SERVICE': True, - 'ENABLE_PSYCHOMETRICS': False, # real-time psychometrics (eg item response theory analysis in instructor dashboard) + 'ENABLE_PSYCHOMETRICS': False, # real-time psychometrics (eg item response theory analysis in instructor dashboard) 'ENABLE_SQL_TRACKING_LOGS': False, 'ENABLE_LMS_MIGRATION': False, @@ -74,12 +74,12 @@ MITX_FEATURES = { 'DISABLE_LOGIN_BUTTON': False, # used in systems where login is automatic, eg MIT SSL - 'STUB_VIDEO_FOR_TESTING': False, # do not display video when running automated acceptance tests + 'STUB_VIDEO_FOR_TESTING': False, # do not display video when running automated acceptance tests # extrernal access methods 'ACCESS_REQUIRE_STAFF_FOR_COURSE': False, 'AUTH_USE_OPENID': False, - 'AUTH_USE_MIT_CERTIFICATES' : False, + 'AUTH_USE_MIT_CERTIFICATES': False, 'AUTH_USE_OPENID_PROVIDER': False, } @@ -90,7 +90,7 @@ DEFAULT_GROUPS = [] GENERATE_PROFILE_SCORES = False # Used with XQueue -XQUEUE_WAITTIME_BETWEEN_REQUESTS = 5 # seconds +XQUEUE_WAITTIME_BETWEEN_REQUESTS = 5 # seconds ############################# SET PATH INFORMATION ############################# @@ -151,8 +151,8 @@ TEMPLATE_CONTEXT_PROCESSORS = ( 'django.core.context_processors.static', 'django.contrib.messages.context_processors.messages', #'django.core.context_processors.i18n', - 'django.contrib.auth.context_processors.auth', #this is required for admin - 'django.core.context_processors.csrf', #necessary for csrf protection + 'django.contrib.auth.context_processors.auth', # this is required for admin + 'django.core.context_processors.csrf', # necessary for csrf protection # Added for django-wiki 'django.core.context_processors.media', @@ -162,7 +162,7 @@ TEMPLATE_CONTEXT_PROCESSORS = ( 'course_wiki.course_nav.context_processor', ) -STUDENT_FILEUPLOAD_MAX_SIZE = 4*1000*1000 # 4 MB +STUDENT_FILEUPLOAD_MAX_SIZE = 4 * 1000 * 1000 # 4 MB MAX_FILEUPLOADS_PER_INPUT = 20 # FIXME: @@ -172,7 +172,7 @@ LIB_URL = '/static/js/' # Dev machines shouldn't need the book # BOOK_URL = '/static/book/' -BOOK_URL = 'https://mitxstatic.s3.amazonaws.com/book_images/' # For AWS deploys +BOOK_URL = 'https://mitxstatic.s3.amazonaws.com/book_images/' # For AWS deploys # RSS_URL = r'lms/templates/feed.rss' # PRESS_URL = r'' RSS_TIMEOUT = 600 @@ -268,8 +268,8 @@ STATICFILES_DIRS = [ ] # Locale/Internationalization -TIME_ZONE = 'America/New_York' # http://en.wikipedia.org/wiki/List_of_tz_zones_by_name -LANGUAGE_CODE = 'en' # http://www.i18nguy.com/unicode/language-identifiers.html +TIME_ZONE = 'America/New_York' # http://en.wikipedia.org/wiki/List_of_tz_zones_by_name +LANGUAGE_CODE = 'en' # http://www.i18nguy.com/unicode/language-identifiers.html USE_I18N = True USE_L10N = True @@ -290,7 +290,7 @@ ALLOWED_GITRELOAD_IPS = ['207.97.227.253', '50.57.128.197', '108.171.174.178'] # setting is, I'm just bumping the expiration time to something absurd (100 # years). This is only used if DEFAULT_FILE_STORAGE is overriden to use S3 # in the global settings.py -AWS_QUERYSTRING_EXPIRE = 10 * 365 * 24 * 60 * 60 # 10 years +AWS_QUERYSTRING_EXPIRE = 10 * 365 * 24 * 60 * 60 # 10 years ################################# SIMPLEWIKI ################################### SIMPLE_WIKI_REQUIRE_LOGIN_EDIT = True @@ -299,8 +299,8 @@ SIMPLE_WIKI_REQUIRE_LOGIN_VIEW = False ################################# WIKI ################################### WIKI_ACCOUNT_HANDLING = False WIKI_EDITOR = 'course_wiki.editors.CodeMirror' -WIKI_SHOW_MAX_CHILDREN = 0 # We don't use the little menu that shows children of an article in the breadcrumb -WIKI_ANONYMOUS = False # Don't allow anonymous access until the styling is figured out +WIKI_SHOW_MAX_CHILDREN = 0 # We don't use the little menu that shows children of an article in the breadcrumb +WIKI_ANONYMOUS = False # Don't allow anonymous access until the styling is figured out WIKI_CAN_CHANGE_PERMISSIONS = lambda article, user: user.is_staff or user.is_superuser WIKI_CAN_ASSIGN = lambda article, user: user.is_staff or user.is_superuser @@ -419,7 +419,7 @@ main_vendor_js = [ discussion_js = sorted(rooted_glob(PROJECT_ROOT / 'static', 'coffee/src/discussion/**/*.coffee')) staff_grading_js = sorted(rooted_glob(PROJECT_ROOT / 'static', 'coffee/src/staff_grading/**/*.coffee')) -open_ended_js = sorted(rooted_glob(PROJECT_ROOT / 'static','coffee/src/open_ended/**/*.coffee')) +open_ended_js = sorted(rooted_glob(PROJECT_ROOT / 'static', 'coffee/src/open_ended/**/*.coffee')) PIPELINE_CSS = { 'application': { @@ -476,11 +476,11 @@ PIPELINE_JS = { 'source_filenames': discussion_js, 'output_filename': 'js/discussion.js' }, - 'staff_grading' : { + 'staff_grading': { 'source_filenames': staff_grading_js, 'output_filename': 'js/staff_grading.js' }, - 'open_ended' : { + 'open_ended': { 'source_filenames': open_ended_js, 'output_filename': 'js/open_ended.js' } @@ -563,9 +563,9 @@ INSTALLED_APPS = ( 'course_groups', #For the wiki - 'wiki', # The new django-wiki from benjaoming + 'wiki', # The new django-wiki from benjaoming 'django_notify', - 'course_wiki', # Our customizations + 'course_wiki', # Our customizations 'mptt', 'sekizai', #'wiki.plugins.attachments', diff --git a/lms/envs/content.py b/lms/envs/content.py index 2584dca969c..f6991538954 100644 --- a/lms/envs/content.py +++ b/lms/envs/content.py @@ -10,7 +10,7 @@ TEMPLATE_DEBUG = True EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' ################################ DEBUG TOOLBAR ################################# -INSTALLED_APPS += ('debug_toolbar',) +INSTALLED_APPS += ('debug_toolbar',) MIDDLEWARE_CLASSES += ('debug_toolbar.middleware.DebugToolbarMiddleware',) DEBUG_TOOLBAR_PANELS = ( @@ -24,8 +24,8 @@ DEBUG_TOOLBAR_PANELS = ( 'debug_toolbar.panels.logger.LoggingPanel', # Enabling the profiler has a weird bug as of django-debug-toolbar==0.9.4 and -# Django=1.3.1/1.4 where requests to views get duplicated (your method gets -# hit twice). So you can uncomment when you need to diagnose performance +# Django=1.3.1/1.4 where requests to views get duplicated (your method gets +# hit twice). So you can uncomment when you need to diagnose performance # problems, but you shouldn't leave it on. # 'debug_toolbar.panels.profiling.ProfilingDebugPanel', ) diff --git a/lms/envs/dev.py b/lms/envs/dev.py index 338a31f641d..47bcee1b7e6 100644 --- a/lms/envs/dev.py +++ b/lms/envs/dev.py @@ -165,7 +165,7 @@ INSTALLED_APPS += ('django_openid_auth',) OPENID_CREATE_USERS = False OPENID_UPDATE_DETAILS_FROM_SREG = True -OPENID_SSO_SERVER_URL = 'https://www.google.com/accounts/o8/id' # TODO: accept more endpoints +OPENID_SSO_SERVER_URL = 'https://www.google.com/accounts/o8/id' # TODO: accept more endpoints OPENID_USE_AS_ADMIN_LOGIN = False OPENID_PROVIDER_TRUSTED_ROOTS = ['*'] diff --git a/lms/envs/dev_edx4edx.py b/lms/envs/dev_edx4edx.py index 0212d8b5501..c138ed81ae5 100644 --- a/lms/envs/dev_edx4edx.py +++ b/lms/envs/dev_edx4edx.py @@ -36,7 +36,7 @@ DEBUG = True ENABLE_MULTICOURSE = True # set to False to disable multicourse display (see lib.util.views.mitxhome) QUICKEDIT = True -MAKO_TEMPLATES['course'] = [DATA_DIR, EDX4EDX_ROOT ] +MAKO_TEMPLATES['course'] = [DATA_DIR, EDX4EDX_ROOT] #MITX_FEATURES['USE_DJANGO_PIPELINE'] = False MITX_FEATURES['DISPLAY_HISTOGRAMS_TO_STAFF'] = False @@ -49,12 +49,12 @@ COURSE_TITLE = "edx4edx: edX Author Course" SITE_NAME = "ichuang.mitx.mit.edu" COURSE_SETTINGS = {'edx4edx': {'number' : 'edX.01', - 'title' : 'edx4edx: edX Author Course', + 'title': 'edx4edx: edX Author Course', 'xmlpath': '/edx4edx/', 'github_url': 'https://github.com/MITx/edx4edx', - 'active' : True, - 'default_chapter' : 'Introduction', - 'default_section' : 'edx4edx_Course', + 'active': True, + 'default_chapter': 'Introduction', + 'default_section': 'edx4edx_Course', }, } diff --git a/lms/envs/dev_ike.py b/lms/envs/dev_ike.py index a17622a81ab..639d1869893 100644 --- a/lms/envs/dev_ike.py +++ b/lms/envs/dev_ike.py @@ -24,25 +24,25 @@ MITX_FEATURES['DISABLE_START_DATES'] = True myhost = socket.gethostname() if ('edxvm' in myhost) or ('ocw' in myhost): - MITX_FEATURES['DISABLE_LOGIN_BUTTON'] = True # auto-login with MIT certificate - MITX_FEATURES['USE_XQA_SERVER'] = 'https://qisx.mit.edu/xqa' # needs to be ssl or browser blocks it - MITX_FEATURES['USE_DJANGO_PIPELINE']=False # don't recompile scss + MITX_FEATURES['DISABLE_LOGIN_BUTTON'] = True # auto-login with MIT certificate + MITX_FEATURES['USE_XQA_SERVER'] = 'https://qisx.mit.edu/xqa' # needs to be ssl or browser blocks it + MITX_FEATURES['USE_DJANGO_PIPELINE'] = False # don't recompile scss if ('ocw' in myhost): MITX_FEATURES['ACCESS_REQUIRE_STAFF_FOR_COURSE'] = False if ('domU' in myhost): EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' - MITX_FEATURES['REROUTE_ACTIVATION_EMAIL'] = 'ichuang@mitx.mit.edu' # nonempty string = address for all activation emails - MITX_FEATURES['USE_DJANGO_PIPELINE']=False # don't recompile scss + MITX_FEATURES['REROUTE_ACTIVATION_EMAIL'] = 'ichuang@mitx.mit.edu' # nonempty string = address for all activation emails + MITX_FEATURES['USE_DJANGO_PIPELINE'] = False # don't recompile scss -SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTOCOL', 'https') # django 1.4 for nginx ssl proxy +SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTOCOL', 'https') # django 1.4 for nginx ssl proxy #----------------------------------------------------------------------------- # disable django debug toolbars -INSTALLED_APPS = tuple([ app for app in INSTALLED_APPS if not app.startswith('debug_toolbar') ]) -MIDDLEWARE_CLASSES = tuple([ mcl for mcl in MIDDLEWARE_CLASSES if not mcl.startswith('debug_toolbar') ]) +INSTALLED_APPS = tuple([app for app in INSTALLED_APPS if not app.startswith('debug_toolbar')]) +MIDDLEWARE_CLASSES = tuple([mcl for mcl in MIDDLEWARE_CLASSES if not mcl.startswith('debug_toolbar')]) #TEMPLATE_LOADERS = tuple([ app for app in TEMPLATE_LOADERS if not app.startswith('mitxmako') ]) TEMPLATE_LOADERS = ( 'django.template.loaders.filesystem.Loader', diff --git a/lms/envs/dev_int.py b/lms/envs/dev_int.py index 12123e12d4e..21c52c8abc9 100644 --- a/lms/envs/dev_int.py +++ b/lms/envs/dev_int.py @@ -14,7 +14,7 @@ from .dev import * MITX_FEATURES['SUBDOMAIN_COURSE_LISTINGS'] = True COURSE_LISTINGS = { - 'default' : ['BerkeleyX/CS169.1x/2012_Fall', + 'default': ['BerkeleyX/CS169.1x/2012_Fall', 'BerkeleyX/CS188.1x/2012_Fall', 'HarvardX/CS50x/2012', 'HarvardX/PH207x/2012_Fall', @@ -25,8 +25,8 @@ COURSE_LISTINGS = { 'berkeley': ['BerkeleyX/CS169.1x/2012_Fall', 'BerkeleyX/CS188.1x/2012_Fall'], - 'harvard' : ['HarvardX/CS50x/2012'], + 'harvard': ['HarvardX/CS50x/2012'], - 'mit' : ['MITx/3.091x/2012_Fall', + 'mit': ['MITx/3.091x/2012_Fall', 'MITx/6.00x/2012_Fall'] } diff --git a/lms/envs/devgroups/courses.py b/lms/envs/devgroups/courses.py index e9ed28a09df..c44717c451b 100644 --- a/lms/envs/devgroups/courses.py +++ b/lms/envs/devgroups/courses.py @@ -1,13 +1,13 @@ from ..dev import * CLASSES_TO_DBS = { - 'BerkeleyX/CS169.1x/2012_Fall' : "cs169.db", - 'BerkeleyX/CS188.1x/2012_Fall' : "cs188_1.db", - 'HarvardX/CS50x/2012' : "cs50.db", - 'HarvardX/PH207x/2012_Fall' : "ph207.db", - 'MITx/3.091x/2012_Fall' : "3091.db", - 'MITx/6.002x/2012_Fall' : "6002.db", - 'MITx/6.00x/2012_Fall' : "600.db", + 'BerkeleyX/CS169.1x/2012_Fall': "cs169.db", + 'BerkeleyX/CS188.1x/2012_Fall': "cs188_1.db", + 'HarvardX/CS50x/2012': "cs50.db", + 'HarvardX/PH207x/2012_Fall': "ph207.db", + 'MITx/3.091x/2012_Fall': "3091.db", + 'MITx/6.002x/2012_Fall': "6002.db", + 'MITx/6.00x/2012_Fall': "600.db", } @@ -20,8 +20,8 @@ CACHES = { 'general': { 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', 'LOCATION': '127.0.0.1:11211', - 'KEY_PREFIX' : 'general', - 'VERSION' : 5, + 'KEY_PREFIX': 'general', + 'VERSION': 5, 'KEY_FUNCTION': 'util.memcache.safe_key', } } @@ -32,11 +32,12 @@ SESSION_ENGINE = 'django.contrib.sessions.backends.cache' def path_for_db(db_name): return ENV_ROOT / "db" / db_name + def course_db_for(course_id): db_name = CLASSES_TO_DBS[course_id] return { - 'default' : { - 'ENGINE' : 'django.db.backends.sqlite3', - 'NAME' : path_for_db(db_name) + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': path_for_db(db_name) } } diff --git a/lms/envs/devgroups/portal.py b/lms/envs/devgroups/portal.py index b6742185715..35808d56fad 100644 --- a/lms/envs/devgroups/portal.py +++ b/lms/envs/devgroups/portal.py @@ -10,4 +10,3 @@ for class_id, db_name in CLASSES_TO_DBS.items(): 'ENGINE': 'django.db.backends.sqlite3', 'NAME': path_for_db(db_name) } - diff --git a/lms/envs/devplus.py b/lms/envs/devplus.py index 5c79304c0a4..ea6590291c7 100644 --- a/lms/envs/devplus.py +++ b/lms/envs/devplus.py @@ -37,8 +37,8 @@ CACHES = { 'general': { 'BACKEND': 'django.core.cache.backends.memcached.MemcachedCache', 'LOCATION': '127.0.0.1:11211', - 'KEY_PREFIX' : 'general', - 'VERSION' : 5, + 'KEY_PREFIX': 'general', + 'VERSION': 5, 'KEY_FUNCTION': 'util.memcache.safe_key', } } @@ -47,7 +47,7 @@ SESSION_ENGINE = 'django.contrib.sessions.backends.cache' ################################ DEBUG TOOLBAR ################################# -INSTALLED_APPS += ('debug_toolbar',) +INSTALLED_APPS += ('debug_toolbar',) MIDDLEWARE_CLASSES += ('debug_toolbar.middleware.DebugToolbarMiddleware',) INTERNAL_IPS = ('127.0.0.1',) @@ -62,8 +62,8 @@ DEBUG_TOOLBAR_PANELS = ( 'debug_toolbar.panels.logger.LoggingPanel', # Enabling the profiler has a weird bug as of django-debug-toolbar==0.9.4 and -# Django=1.3.1/1.4 where requests to views get duplicated (your method gets -# hit twice). So you can uncomment when you need to diagnose performance +# Django=1.3.1/1.4 where requests to views get duplicated (your method gets +# hit twice). So you can uncomment when you need to diagnose performance # problems, but you shouldn't leave it on. 'debug_toolbar.panels.profiling.ProfilingDebugPanel', ) diff --git a/lms/envs/edx4edx_aws.py b/lms/envs/edx4edx_aws.py index adc2c6e1ce8..de377c0b57d 100644 --- a/lms/envs/edx4edx_aws.py +++ b/lms/envs/edx4edx_aws.py @@ -5,21 +5,21 @@ COURSE_NUMBER = "edX.01" COURSE_TITLE = "edx4edx: edX Author Course" EDX4EDX_ROOT = ENV_ROOT / "data/edx4edx" -### Dark code. Should be enabled in local settings for devel. +### Dark code. Should be enabled in local settings for devel. QUICKEDIT = True -ENABLE_MULTICOURSE = True # set to False to disable multicourse display (see lib.util.views.mitxhome) +ENABLE_MULTICOURSE = True # set to False to disable multicourse display (see lib.util.views.mitxhome) ### PIPELINE_CSS_COMPRESSOR = None PIPELINE_JS_COMPRESSOR = None COURSE_DEFAULT = 'edx4edx' COURSE_SETTINGS = {'edx4edx': {'number' : 'edX.01', - 'title' : 'edx4edx: edX Author Course', + 'title': 'edx4edx: edX Author Course', 'xmlpath': '/edx4edx/', 'github_url': 'https://github.com/MITx/edx4edx', - 'active' : True, - 'default_chapter' : 'Introduction', - 'default_section' : 'edx4edx_Course', + 'active': True, + 'default_chapter': 'Introduction', + 'default_section': 'edx4edx_Course', }, } @@ -34,4 +34,4 @@ STATICFILES_DIRS = [ # ("book", ENV_ROOT / "book_images") ] -MAKO_TEMPLATES['course'] = [DATA_DIR, EDX4EDX_ROOT ] +MAKO_TEMPLATES['course'] = [DATA_DIR, EDX4EDX_ROOT] diff --git a/lms/envs/static.py b/lms/envs/static.py index f233571a9e3..23e735c7470 100644 --- a/lms/envs/static.py +++ b/lms/envs/static.py @@ -12,7 +12,7 @@ from logsettings import get_logger_config STATIC_GRAB = True -LOGGING = get_logger_config(ENV_ROOT / "log", +LOGGING = get_logger_config(ENV_ROOT / "log", logging_env="dev", tracking_filename="tracking.log", debug=False) @@ -25,7 +25,7 @@ DATABASES = { } CACHES = { - # This is the cache used for most things. + # This is the cache used for most things. # In staging/prod envs, the sessions also live here. 'default': { 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', diff --git a/lms/envs/test.py b/lms/envs/test.py index 8b546549eb5..6cad6416d05 100644 --- a/lms/envs/test.py +++ b/lms/envs/test.py @@ -23,7 +23,7 @@ MITX_FEATURES['ENABLE_DISCUSSION_SERVICE'] = False WIKI_ENABLED = True # Makes the tests run much faster... -SOUTH_TESTS_MIGRATE = False # To disable migrations and use syncdb instead +SOUTH_TESTS_MIGRATE = False # To disable migrations and use syncdb instead # Nose Test Runner INSTALLED_APPS += ('django_nose',) @@ -57,7 +57,7 @@ XQUEUE_INTERFACE = { }, "basic_auth": ('anant', 'agarwal'), } -XQUEUE_WAITTIME_BETWEEN_REQUESTS = 5 # seconds +XQUEUE_WAITTIME_BETWEEN_REQUESTS = 5 # seconds # Don't rely on a real staff grading backend diff --git a/lms/envs/test_ike.py b/lms/envs/test_ike.py index b162d9e2bb3..907b7eeadfd 100644 --- a/lms/envs/test_ike.py +++ b/lms/envs/test_ike.py @@ -34,11 +34,11 @@ DATA_DIR = COURSES_ROOT MAKO_TEMPLATES['course'] = [DATA_DIR] MAKO_TEMPLATES['sections'] = [DATA_DIR / 'sections'] MAKO_TEMPLATES['custom_tags'] = [DATA_DIR / 'custom_tags'] -MAKO_TEMPLATES['main'] = [PROJECT_ROOT / 'templates', +MAKO_TEMPLATES['main'] = [PROJECT_ROOT / 'templates', DATA_DIR / 'info', DATA_DIR / 'problems'] -LOGGING = get_logger_config(TEST_ROOT / "log", +LOGGING = get_logger_config(TEST_ROOT / "log", logging_env="dev", tracking_filename="tracking.log", debug=True) @@ -51,7 +51,7 @@ DATABASES = { } CACHES = { - # This is the cache used for most things. + # This is the cache used for most things. # In staging/prod envs, the sessions also live here. 'default': { 'BACKEND': 'django.core.cache.backends.locmem.LocMemCache', diff --git a/lms/lib/comment_client/comment.py b/lms/lib/comment_client/comment.py index 6d0bafee02a..0d817615124 100644 --- a/lms/lib/comment_client/comment.py +++ b/lms/lib/comment_client/comment.py @@ -4,6 +4,7 @@ from thread import Thread import models import settings + class Comment(models.Model): accessible_fields = [ @@ -41,8 +42,10 @@ class Comment(models.Model): else: return super(Comment, cls).url(action, params) + def _url_for_thread_comments(thread_id): return "{prefix}/threads/{thread_id}/comments".format(prefix=settings.PREFIX, thread_id=thread_id) + def _url_for_comment(comment_id): return "{prefix}/comments/{comment_id}".format(prefix=settings.PREFIX, comment_id=comment_id) diff --git a/lms/lib/comment_client/comment_client.py b/lms/lib/comment_client/comment_client.py index fe485433d61..d7c8f05485f 100644 --- a/lms/lib/comment_client/comment_client.py +++ b/lms/lib/comment_client/comment_client.py @@ -7,32 +7,40 @@ from utils import * import settings + def search_similar_threads(course_id, recursive=False, query_params={}, *args, **kwargs): default_params = {'course_id': course_id, 'recursive': recursive} attributes = dict(default_params.items() + query_params.items()) return perform_request('get', _url_for_search_similar_threads(), attributes, *args, **kwargs) + def search_recent_active_threads(course_id, recursive=False, query_params={}, *args, **kwargs): default_params = {'course_id': course_id, 'recursive': recursive} attributes = dict(default_params.items() + query_params.items()) return perform_request('get', _url_for_search_recent_active_threads(), attributes, *args, **kwargs) + def search_trending_tags(course_id, query_params={}, *args, **kwargs): default_params = {'course_id': course_id} attributes = dict(default_params.items() + query_params.items()) return perform_request('get', _url_for_search_trending_tags(), attributes, *args, **kwargs) + def tags_autocomplete(value, *args, **kwargs): return perform_request('get', _url_for_threads_tags_autocomplete(), {'value': value}, *args, **kwargs) + def _url_for_search_similar_threads(): return "{prefix}/search/threads/more_like_this".format(prefix=settings.PREFIX) + def _url_for_search_recent_active_threads(): return "{prefix}/search/threads/recent_active".format(prefix=settings.PREFIX) + def _url_for_search_trending_tags(): return "{prefix}/search/tags/trending".format(prefix=settings.PREFIX) + def _url_for_threads_tags_autocomplete(): return "{prefix}/threads/tags/autocomplete".format(prefix=settings.PREFIX) diff --git a/lms/lib/comment_client/commentable.py b/lms/lib/comment_client/commentable.py index 8f91bfc93d8..85c357ef81d 100644 --- a/lms/lib/comment_client/commentable.py +++ b/lms/lib/comment_client/commentable.py @@ -3,6 +3,7 @@ from utils import * import models import settings + class Commentable(models.Model): base_url = "{prefix}/commentables".format(prefix=settings.PREFIX) diff --git a/lms/lib/comment_client/legacy.py b/lms/lib/comment_client/legacy.py index fc87bcaf4f1..fbf66a09fda 100644 --- a/lms/lib/comment_client/legacy.py +++ b/lms/lib/comment_client/legacy.py @@ -1,6 +1,7 @@ def delete_threads(commentable_id, *args, **kwargs): return _perform_request('delete', _url_for_commentable_threads(commentable_id), *args, **kwargs) + def get_threads(commentable_id, recursive=False, query_params={}, *args, **kwargs): default_params = {'page': 1, 'per_page': 20, 'recursive': recursive} attributes = dict(default_params.items() + query_params.items()) @@ -8,6 +9,7 @@ def get_threads(commentable_id, recursive=False, query_params={}, *args, **kwarg attributes, *args, **kwargs) return response.get('collection', []), response.get('page', 1), response.get('num_pages', 1) + def search_threads(course_id, recursive=False, query_params={}, *args, **kwargs): default_params = {'page': 1, 'per_page': 20, 'course_id': course_id, 'recursive': recursive} attributes = dict(default_params.items() + query_params.items()) @@ -15,106 +17,137 @@ def search_threads(course_id, recursive=False, query_params={}, *args, **kwargs) attributes, *args, **kwargs) return response.get('collection', []), response.get('page', 1), response.get('num_pages', 1) + def search_similar_threads(course_id, recursive=False, query_params={}, *args, **kwargs): default_params = {'course_id': course_id, 'recursive': recursive} attributes = dict(default_params.items() + query_params.items()) return _perform_request('get', _url_for_search_similar_threads(), attributes, *args, **kwargs) + def search_recent_active_threads(course_id, recursive=False, query_params={}, *args, **kwargs): default_params = {'course_id': course_id, 'recursive': recursive} attributes = dict(default_params.items() + query_params.items()) return _perform_request('get', _url_for_search_recent_active_threads(), attributes, *args, **kwargs) + def search_trending_tags(course_id, query_params={}, *args, **kwargs): default_params = {'course_id': course_id} attributes = dict(default_params.items() + query_params.items()) return _perform_request('get', _url_for_search_trending_tags(), attributes, *args, **kwargs) + def create_user(attributes, *args, **kwargs): return _perform_request('post', _url_for_users(), attributes, *args, **kwargs) + def update_user(user_id, attributes, *args, **kwargs): return _perform_request('put', _url_for_user(user_id), attributes, *args, **kwargs) + def get_threads_tags(*args, **kwargs): return _perform_request('get', _url_for_threads_tags(), {}, *args, **kwargs) + def tags_autocomplete(value, *args, **kwargs): return _perform_request('get', _url_for_threads_tags_autocomplete(), {'value': value}, *args, **kwargs) + def create_thread(commentable_id, attributes, *args, **kwargs): return _perform_request('post', _url_for_threads(commentable_id), attributes, *args, **kwargs) - + + def get_thread(thread_id, recursive=False, *args, **kwargs): return _perform_request('get', _url_for_thread(thread_id), {'recursive': recursive}, *args, **kwargs) + def update_thread(thread_id, attributes, *args, **kwargs): return _perform_request('put', _url_for_thread(thread_id), attributes, *args, **kwargs) + def create_comment(thread_id, attributes, *args, **kwargs): return _perform_request('post', _url_for_thread_comments(thread_id), attributes, *args, **kwargs) + def delete_thread(thread_id, *args, **kwargs): return _perform_request('delete', _url_for_thread(thread_id), *args, **kwargs) + def get_comment(comment_id, recursive=False, *args, **kwargs): return _perform_request('get', _url_for_comment(comment_id), {'recursive': recursive}, *args, **kwargs) + def update_comment(comment_id, attributes, *args, **kwargs): return _perform_request('put', _url_for_comment(comment_id), attributes, *args, **kwargs) + def create_sub_comment(comment_id, attributes, *args, **kwargs): return _perform_request('post', _url_for_comment(comment_id), attributes, *args, **kwargs) + def delete_comment(comment_id, *args, **kwargs): return _perform_request('delete', _url_for_comment(comment_id), *args, **kwargs) + def vote_for_comment(comment_id, user_id, value, *args, **kwargs): return _perform_request('put', _url_for_vote_comment(comment_id), {'user_id': user_id, 'value': value}, *args, **kwargs) + def undo_vote_for_comment(comment_id, user_id, *args, **kwargs): return _perform_request('delete', _url_for_vote_comment(comment_id), {'user_id': user_id}, *args, **kwargs) + def vote_for_thread(thread_id, user_id, value, *args, **kwargs): return _perform_request('put', _url_for_vote_thread(thread_id), {'user_id': user_id, 'value': value}, *args, **kwargs) + def undo_vote_for_thread(thread_id, user_id, *args, **kwargs): return _perform_request('delete', _url_for_vote_thread(thread_id), {'user_id': user_id}, *args, **kwargs) + def get_notifications(user_id, *args, **kwargs): return _perform_request('get', _url_for_notifications(user_id), *args, **kwargs) + def get_user_info(user_id, complete=True, *args, **kwargs): return _perform_request('get', _url_for_user(user_id), {'complete': complete}, *args, **kwargs) + def subscribe(user_id, subscription_detail, *args, **kwargs): return _perform_request('post', _url_for_subscription(user_id), subscription_detail, *args, **kwargs) + def subscribe_user(user_id, followed_user_id, *args, **kwargs): return subscribe(user_id, {'source_type': 'user', 'source_id': followed_user_id}) follow = subscribe_user + def subscribe_thread(user_id, thread_id, *args, **kwargs): return subscribe(user_id, {'source_type': 'thread', 'source_id': thread_id}) + def subscribe_commentable(user_id, commentable_id, *args, **kwargs): return subscribe(user_id, {'source_type': 'other', 'source_id': commentable_id}) + def unsubscribe(user_id, subscription_detail, *args, **kwargs): return _perform_request('delete', _url_for_subscription(user_id), subscription_detail, *args, **kwargs) + def unsubscribe_user(user_id, followed_user_id, *args, **kwargs): return unsubscribe(user_id, {'source_type': 'user', 'source_id': followed_user_id}) unfollow = unsubscribe_user + def unsubscribe_thread(user_id, thread_id, *args, **kwargs): return unsubscribe(user_id, {'source_type': 'thread', 'source_id': thread_id}) + def unsubscribe_commentable(user_id, commentable_id, *args, **kwargs): return unsubscribe(user_id, {'source_type': 'other', 'source_id': commentable_id}) + def _perform_request(method, url, data_or_params=None, *args, **kwargs): if method in ['post', 'put', 'patch']: response = requests.request(method, url, data=data_or_params) @@ -130,51 +163,66 @@ def _perform_request(method, url, data_or_params=None, *args, **kwargs): else: return json.loads(response.text) + def _url_for_threads(commentable_id): return "{prefix}/{commentable_id}/threads".format(prefix=PREFIX, commentable_id=commentable_id) + def _url_for_thread(thread_id): return "{prefix}/threads/{thread_id}".format(prefix=PREFIX, thread_id=thread_id) + def _url_for_thread_comments(thread_id): return "{prefix}/threads/{thread_id}/comments".format(prefix=PREFIX, thread_id=thread_id) + def _url_for_comment(comment_id): return "{prefix}/comments/{comment_id}".format(prefix=PREFIX, comment_id=comment_id) + def _url_for_vote_comment(comment_id): return "{prefix}/comments/{comment_id}/votes".format(prefix=PREFIX, comment_id=comment_id) + def _url_for_vote_thread(thread_id): return "{prefix}/threads/{thread_id}/votes".format(prefix=PREFIX, thread_id=thread_id) + def _url_for_notifications(user_id): return "{prefix}/users/{user_id}/notifications".format(prefix=PREFIX, user_id=user_id) + def _url_for_subscription(user_id): return "{prefix}/users/{user_id}/subscriptions".format(prefix=PREFIX, user_id=user_id) + def _url_for_user(user_id): return "{prefix}/users/{user_id}".format(prefix=PREFIX, user_id=user_id) + def _url_for_search_threads(): return "{prefix}/search/threads".format(prefix=PREFIX) + def _url_for_search_similar_threads(): return "{prefix}/search/threads/more_like_this".format(prefix=PREFIX) + def _url_for_search_recent_active_threads(): return "{prefix}/search/threads/recent_active".format(prefix=PREFIX) + def _url_for_search_trending_tags(): return "{prefix}/search/tags/trending".format(prefix=PREFIX) + def _url_for_threads_tags(): return "{prefix}/threads/tags".format(prefix=PREFIX) + def _url_for_threads_tags_autocomplete(): return "{prefix}/threads/tags/autocomplete".format(prefix=PREFIX) + def _url_for_users(): return "{prefix}/users".format(prefix=PREFIX) - diff --git a/lms/lib/comment_client/models.py b/lms/lib/comment_client/models.py index 3ce3858d2d2..8676d3af333 100644 --- a/lms/lib/comment_client/models.py +++ b/lms/lib/comment_client/models.py @@ -1,5 +1,6 @@ from utils import * + class Model(object): accessible_fields = ['id'] @@ -26,7 +27,7 @@ class Model(object): raise AttributeError("Field {0} does not exist".format(name)) self.retrieve() return self.__getattr__(name) - + def __setattr__(self, name, value): if name == 'attributes' or name not in self.accessible_fields: super(Model, self).__setattr__(name, value) @@ -80,7 +81,7 @@ class Model(object): def initializable_attributes(self): return extract(self.attributes, self.initializable_fields) - + @classmethod def before_save(cls, instance): pass @@ -91,10 +92,10 @@ class Model(object): def save(self): self.__class__.before_save(self) - if self.id: # if we have id already, treat this as an update + if self.id: # if we have id already, treat this as an update url = self.url(action='put', params=self.attributes) response = perform_request('put', url, self.updatable_attributes()) - else: # otherwise, treat this as an insert + else: # otherwise, treat this as an insert url = self.url(action='post', params=self.attributes) response = perform_request('post', url, self.initializable_attributes()) self.retrieved = True @@ -126,5 +127,5 @@ class Model(object): return cls.url_with_id(params) except KeyError: raise CommentClientError("Cannot perform action {0} without id".format(action)) - else: # action must be in DEFAULT_ACTIONS_WITHOUT_ID now + else: # action must be in DEFAULT_ACTIONS_WITHOUT_ID now return cls.url_without_id() diff --git a/lms/lib/comment_client/thread.py b/lms/lib/comment_client/thread.py index 6fd31b08230..912ae1af18c 100644 --- a/lms/lib/comment_client/thread.py +++ b/lms/lib/comment_client/thread.py @@ -3,6 +3,7 @@ from utils import * import models import settings + class Thread(models.Model): accessible_fields = [ @@ -66,7 +67,7 @@ class Thread(models.Model): def _retrieve(self, *args, **kwargs): url = self.url(action='get', params=self.attributes) - request_params = { + request_params = { 'recursive': kwargs.get('recursive'), 'user_id': kwargs.get('user_id'), 'mark_as_read': kwargs.get('mark_as_read', True), diff --git a/lms/lib/comment_client/user.py b/lms/lib/comment_client/user.py index 546b27556c1..c3ba84175ed 100644 --- a/lms/lib/comment_client/user.py +++ b/lms/lib/comment_client/user.py @@ -3,6 +3,7 @@ from utils import * import models import settings + class User(models.Model): accessible_fields = ['username', 'email', 'follower_ids', 'upvoted_ids', 'downvoted_ids', @@ -82,17 +83,22 @@ class User(models.Model): response = perform_request('get', url, retrieve_params) self.update_attributes(**response) + def _url_for_vote_comment(comment_id): return "{prefix}/comments/{comment_id}/votes".format(prefix=settings.PREFIX, comment_id=comment_id) + def _url_for_vote_thread(thread_id): return "{prefix}/threads/{thread_id}/votes".format(prefix=settings.PREFIX, thread_id=thread_id) + def _url_for_subscription(user_id): return "{prefix}/users/{user_id}/subscriptions".format(prefix=settings.PREFIX, user_id=user_id) + def _url_for_user_active_threads(user_id): return "{prefix}/users/{user_id}/active_threads".format(prefix=settings.PREFIX, user_id=user_id) + def _url_for_user_subscribed_threads(user_id): return "{prefix}/users/{user_id}/subscribed_threads".format(prefix=settings.PREFIX, user_id=user_id) diff --git a/lms/lib/comment_client/utils.py b/lms/lib/comment_client/utils.py index f50797d5e0d..e053fea6c0a 100644 --- a/lms/lib/comment_client/utils.py +++ b/lms/lib/comment_client/utils.py @@ -5,23 +5,28 @@ import settings log = logging.getLogger('mitx.' + __name__) + def strip_none(dic): return dict([(k, v) for k, v in dic.iteritems() if v is not None]) + def strip_blank(dic): def _is_blank(v): return isinstance(v, str) and len(v.strip()) == 0 return dict([(k, v) for k, v in dic.iteritems() if not _is_blank(v)]) + def extract(dic, keys): if isinstance(keys, str): return strip_none({keys: dic.get(keys)}) else: return strip_none({k: dic.get(k) for k in keys}) + def merge_dict(dic1, dic2): return dict(dic1.items() + dic2.items()) + def perform_request(method, url, data_or_params=None, *args, **kwargs): if data_or_params is None: data_or_params = {} @@ -34,7 +39,7 @@ def perform_request(method, url, data_or_params=None, *args, **kwargs): except Exception as err: log.exception("Trying to call {method} on {url} with params {params}".format( method=method, url=url, params=data_or_params)) - # Reraise with a single exception type + # Reraise with a single exception type raise CommentClientError(str(err)) if 200 < response.status_code < 500: @@ -47,6 +52,7 @@ def perform_request(method, url, data_or_params=None, *args, **kwargs): else: return json.loads(response.text) + class CommentClientError(Exception): def __init__(self, msg): self.message = msg @@ -54,5 +60,6 @@ class CommentClientError(Exception): def __str__(self): return repr(self.message) + class CommentClientUnknownError(CommentClientError): pass diff --git a/lms/lib/dogfood/check.py b/lms/lib/dogfood/check.py index 0a1da38529a..070d3f9262e 100644 --- a/lms/lib/dogfood/check.py +++ b/lms/lib/dogfood/check.py @@ -59,5 +59,3 @@ def check_problem_code(ans, the_lcp, correct_answers, false_answers): 'msg': msg + endmsg, } return ret - - diff --git a/lms/lib/loncapa/loncapa_check.py b/lms/lib/loncapa/loncapa_check.py index 0fd998e00e8..2cd591520e9 100644 --- a/lms/lib/loncapa/loncapa_check.py +++ b/lms/lib/loncapa/loncapa_check.py @@ -33,5 +33,3 @@ def lc_choose(index, *args): deg2rad = math.pi / 180.0 rad2deg = 180.0 / math.pi - - diff --git a/lms/lib/symmath/symmath_check.py b/lms/lib/symmath/symmath_check.py index 3cc4fd7d3c4..a3dec4aae50 100644 --- a/lms/lib/symmath/symmath_check.py +++ b/lms/lib/symmath/symmath_check.py @@ -143,6 +143,7 @@ def check(expect, given, numerical=False, matrix=False, normphase=False, abcsym= #----------------------------------------------------------------------------- # helper function to convert all <p> to <span class='inline-error'> + def make_error_message(msg): # msg = msg.replace('<p>','<p><span class="inline-error">').replace('</p>','</span></p>') msg = '<div class="capa_alert">%s</div>' % msg @@ -153,6 +154,7 @@ def make_error_message(msg): # # This is one of the main entry points to call. + def symmath_check(expect, ans, dynamath=None, options=None, debug=None, xml=None): ''' Check a symbolic mathematical expression using sympy. @@ -183,12 +185,12 @@ def symmath_check(expect, ans, dynamath=None, options=None, debug=None, xml=None # msg += '<p/>abname=%s' % abname # msg += '<p/>adict=%s' % (repr(adict).replace('<','<')) - threshold = 1.0e-3 # for numerical comparison (also with matrices) + threshold = 1.0e-3 # for numerical comparison (also with matrices) DEBUG = debug if xml is not None: - DEBUG = xml.get('debug',False) # override debug flag using attribute in symbolicmath xml - if DEBUG in ['0','False']: + DEBUG = xml.get('debug', False) # override debug flag using attribute in symbolicmath xml + if DEBUG in ['0', 'False']: DEBUG = False # options diff --git a/lms/static/admin/js/compress.py b/lms/static/admin/js/compress.py index 8d2caa28ea0..a23b431750c 100644 --- a/lms/static/admin/js/compress.py +++ b/lms/static/admin/js/compress.py @@ -6,6 +6,7 @@ import sys here = os.path.dirname(__file__) + def main(): usage = "usage: %prog [file1..fileN]" description = """With no file paths given this script will automatically diff --git a/lms/urls.py b/lms/urls.py index e9746c23389..b25c4d259ec 100644 --- a/lms/urls.py +++ b/lms/urls.py @@ -13,7 +13,7 @@ urlpatterns = ('', # certificate view url(r'^update_certificate$', 'certificates.views.update_certificate'), - url(r'^$', 'branding.views.index', name="root"), # Main marketing page, or redirect to courseware + url(r'^$', 'branding.views.index', name="root"), # Main marketing page, or redirect to courseware url(r'^dashboard$', 'student.views.dashboard', name="dashboard"), url(r'^admin_dashboard$', 'dashboard.views.dashboard'), @@ -35,7 +35,7 @@ urlpatterns = ('', # url(r'^testcenter/logout$', 'student.test_center_views.logout'), url(r'^event$', 'track.views.user_track'), - url(r'^t/(?P<template>[^/]*)$', 'static_template_view.views.index'), # TODO: Is this used anymore? What is STATIC_GRAB? + url(r'^t/(?P<template>[^/]*)$', 'static_template_view.views.index'), # TODO: Is this used anymore? What is STATIC_GRAB? url(r'^accounts/login$', 'student.views.accounts_login', name="accounts_login"), @@ -65,9 +65,9 @@ urlpatterns = ('', url(r'^heartbeat$', include('heartbeat.urls')), - url(r'^university_profile/UTx$', 'courseware.views.static_university_profile', name="static_university_profile", kwargs={'org_id':'UTx'}), - url(r'^university_profile/WellesleyX$', 'courseware.views.static_university_profile', name="static_university_profile", kwargs={'org_id':'WellesleyX'}), - url(r'^university_profile/GeorgetownX$', 'courseware.views.static_university_profile', name="static_university_profile", kwargs={'org_id':'GeorgetownX'}), + url(r'^university_profile/UTx$', 'courseware.views.static_university_profile', name="static_university_profile", kwargs={'org_id': 'UTx'}), + url(r'^university_profile/WellesleyX$', 'courseware.views.static_university_profile', name="static_university_profile", kwargs={'org_id': 'WellesleyX'}), + url(r'^university_profile/GeorgetownX$', 'courseware.views.static_university_profile', name="static_university_profile", kwargs={'org_id': 'GeorgetownX'}), url(r'^university_profile/(?P<org_id>[^/]+)$', 'courseware.views.university_profile', name="university_profile"), #Semi-static views (these need to be rendered and have the login bar, but don't change) @@ -143,7 +143,7 @@ urlpatterns = ('', ) if settings.PERFSTATS: - urlpatterns += (url(r'^reprofile$','perfstats.views.end_profile'),) + urlpatterns += (url(r'^reprofile$', 'perfstats.views.end_profile'),) @@ -226,7 +226,7 @@ if settings.COURSEWARE_ENABLED: url(r'^courses/(?P<course_id>[^/]+/[^/]+/[^/]+)/info$', 'courseware.views.course_info', name="info"), url(r'^courses/(?P<course_id>[^/]+/[^/]+/[^/]+)/syllabus$', - 'courseware.views.syllabus', name="syllabus"), # TODO arjun remove when custom tabs in place, see courseware/courses.py + 'courseware.views.syllabus', name="syllabus"), # TODO arjun remove when custom tabs in place, see courseware/courses.py url(r'^courses/(?P<course_id>[^/]+/[^/]+/[^/]+)/book/(?P<book_index>[^/]*)/$', 'staticbook.views.index', name="book"), url(r'^courses/(?P<course_id>[^/]+/[^/]+/[^/]+)/book/(?P<book_index>[^/]*)/(?P<page>[^/]*)$', @@ -277,7 +277,7 @@ if settings.COURSEWARE_ENABLED: 'open_ended_grading.views.flagged_problem_list', name='open_ended_flagged_problems'), url(r'^courses/(?P<course_id>[^/]+/[^/]+/[^/]+)/open_ended_flagged_problems/take_action_on_flags$', 'open_ended_grading.views.take_action_on_flags', name='open_ended_flagged_problems_take_action'), - + # Cohorts management url(r'^courses/(?P<course_id>[^/]+/[^/]+/[^/]+)/cohorts$', 'course_groups.views.list_cohorts', name="cohorts"), -- GitLab