Commit 3dcc3c4a by Sven Marnach

Clean up occurrences of SlashSeparatedCourseKey in the django_comment_client app.

The preferred form is to use CourseKey.from_string() instead of
SlashSeparatedCourseKey.from_deprecated_string().
parent 8f2ea979
...@@ -13,7 +13,7 @@ from django.core.urlresolvers import reverse ...@@ -13,7 +13,7 @@ from django.core.urlresolvers import reverse
from request_cache.middleware import RequestCache from request_cache.middleware import RequestCache
from mock import patch, ANY, Mock from mock import patch, ANY, Mock
from nose.tools import assert_true, assert_equal # pylint: disable=no-name-in-module from nose.tools import assert_true, assert_equal # pylint: disable=no-name-in-module
from opaque_keys.edx.locations import SlashSeparatedCourseKey from opaque_keys.edx.keys import CourseKey
from lms.lib.comment_client import Thread from lms.lib.comment_client import Thread
from common.test.utils import MockSignalHandlerMixin, disable_signal from common.test.utils import MockSignalHandlerMixin, disable_signal
...@@ -1733,7 +1733,7 @@ class UsersEndpointTestCase(ModuleStoreTestCase, MockRequestSetupMixin): ...@@ -1733,7 +1733,7 @@ class UsersEndpointTestCase(ModuleStoreTestCase, MockRequestSetupMixin):
self.assertNotIn("users", content) self.assertNotIn("users", content)
def test_course_does_not_exist(self): def test_course_does_not_exist(self):
course_id = SlashSeparatedCourseKey.from_deprecated_string("does/not/exist") course_id = CourseKey.from_string("does/not/exist")
response = self.make_request(course_id=course_id, username="other") response = self.make_request(course_id=course_id, username="other")
self.assertEqual(response.status_code, 404) self.assertEqual(response.status_code, 404)
......
...@@ -12,7 +12,6 @@ from django.utils.translation import ugettext as _ ...@@ -12,7 +12,6 @@ from django.utils.translation import ugettext as _
from django.views.decorators import csrf from django.views.decorators import csrf
from django.views.decorators.http import require_GET, require_POST from django.views.decorators.http import require_GET, require_POST
from opaque_keys.edx.keys import CourseKey from opaque_keys.edx.keys import CourseKey
from opaque_keys.edx.locations import SlashSeparatedCourseKey
from courseware.access import has_access from courseware.access import has_access
from util.file import store_uploaded_file from util.file import store_uploaded_file
...@@ -171,7 +170,7 @@ def permitted(func): ...@@ -171,7 +170,7 @@ def permitted(func):
else: else:
content = None content = None
return content return content
course_key = SlashSeparatedCourseKey.from_deprecated_string(kwargs['course_id']) course_key = CourseKey.from_string(kwargs['course_id'])
if check_permissions_by_view(request.user, course_key, fetch_content(), request.view_name): if check_permissions_by_view(request.user, course_key, fetch_content(), request.view_name):
return func(request, *args, **kwargs) return func(request, *args, **kwargs)
else: else:
...@@ -200,7 +199,7 @@ def create_thread(request, course_id, commentable_id): ...@@ -200,7 +199,7 @@ def create_thread(request, course_id, commentable_id):
""" """
log.debug("Creating new thread in %r, id %r", course_id, commentable_id) log.debug("Creating new thread in %r, id %r", course_id, commentable_id)
course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id) course_key = CourseKey.from_string(course_id)
course = get_course_with_access(request.user, 'load', course_key) course = get_course_with_access(request.user, 'load', course_key)
post = request.POST post = request.POST
user = request.user user = request.user
...@@ -285,7 +284,7 @@ def update_thread(request, course_id, thread_id): ...@@ -285,7 +284,7 @@ def update_thread(request, course_id, thread_id):
if 'body' not in request.POST or not request.POST['body'].strip(): if 'body' not in request.POST or not request.POST['body'].strip():
return JsonError(_("Body can't be empty")) return JsonError(_("Body can't be empty"))
course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id) course_key = CourseKey.from_string(course_id)
thread = cc.Thread.find(thread_id) thread = cc.Thread.find(thread_id)
# Get thread context first in order to be safe from reseting the values of thread object later # Get thread context first in order to be safe from reseting the values of thread object later
thread_context = getattr(thread, "context", "course") thread_context = getattr(thread, "context", "course")
...@@ -374,7 +373,7 @@ def create_comment(request, course_id, thread_id): ...@@ -374,7 +373,7 @@ def create_comment(request, course_id, thread_id):
""" """
if is_comment_too_deep(parent=None): if is_comment_too_deep(parent=None):
return JsonError(_("Comment level too deep")) return JsonError(_("Comment level too deep"))
return _create_comment(request, SlashSeparatedCourseKey.from_deprecated_string(course_id), thread_id=thread_id) return _create_comment(request, CourseKey.from_string(course_id), thread_id=thread_id)
@require_POST @require_POST
...@@ -385,7 +384,7 @@ def delete_thread(request, course_id, thread_id): # pylint: disable=unused-argu ...@@ -385,7 +384,7 @@ def delete_thread(request, course_id, thread_id): # pylint: disable=unused-argu
given a course_id and thread_id, delete this thread given a course_id and thread_id, delete this thread
this is ajax only this is ajax only
""" """
course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id) course_key = CourseKey.from_string(course_id)
thread = cc.Thread.find(thread_id) thread = cc.Thread.find(thread_id)
thread.delete() thread.delete()
thread_deleted.send(sender=None, user=request.user, post=thread) thread_deleted.send(sender=None, user=request.user, post=thread)
...@@ -400,7 +399,7 @@ def update_comment(request, course_id, comment_id): ...@@ -400,7 +399,7 @@ def update_comment(request, course_id, comment_id):
given a course_id and comment_id, update the comment with payload attributes given a course_id and comment_id, update the comment with payload attributes
handles static and ajax submissions handles static and ajax submissions
""" """
course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id) course_key = CourseKey.from_string(course_id)
comment = cc.Comment.find(comment_id) comment = cc.Comment.find(comment_id)
if 'body' not in request.POST or not request.POST['body'].strip(): if 'body' not in request.POST or not request.POST['body'].strip():
return JsonError(_("Body can't be empty")) return JsonError(_("Body can't be empty"))
...@@ -423,7 +422,7 @@ def endorse_comment(request, course_id, comment_id): ...@@ -423,7 +422,7 @@ def endorse_comment(request, course_id, comment_id):
given a course_id and comment_id, toggle the endorsement of this comment, given a course_id and comment_id, toggle the endorsement of this comment,
ajax only ajax only
""" """
course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id) course_key = CourseKey.from_string(course_id)
comment = cc.Comment.find(comment_id) comment = cc.Comment.find(comment_id)
user = request.user user = request.user
comment.endorsed = request.POST.get('endorsed', 'false').lower() == 'true' comment.endorsed = request.POST.get('endorsed', 'false').lower() == 'true'
...@@ -441,7 +440,7 @@ def openclose_thread(request, course_id, thread_id): ...@@ -441,7 +440,7 @@ def openclose_thread(request, course_id, thread_id):
given a course_id and thread_id, toggle the status of this thread given a course_id and thread_id, toggle the status of this thread
ajax only ajax only
""" """
course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id) course_key = CourseKey.from_string(course_id)
thread = cc.Thread.find(thread_id) thread = cc.Thread.find(thread_id)
thread.closed = request.POST.get('closed', 'false').lower() == 'true' thread.closed = request.POST.get('closed', 'false').lower() == 'true'
thread.save() thread.save()
...@@ -462,7 +461,7 @@ def create_sub_comment(request, course_id, comment_id): ...@@ -462,7 +461,7 @@ def create_sub_comment(request, course_id, comment_id):
""" """
if is_comment_too_deep(parent=cc.Comment(comment_id)): if is_comment_too_deep(parent=cc.Comment(comment_id)):
return JsonError(_("Comment level too deep")) return JsonError(_("Comment level too deep"))
return _create_comment(request, SlashSeparatedCourseKey.from_deprecated_string(course_id), parent_id=comment_id) return _create_comment(request, CourseKey.from_string(course_id), parent_id=comment_id)
@require_POST @require_POST
...@@ -473,7 +472,7 @@ def delete_comment(request, course_id, comment_id): ...@@ -473,7 +472,7 @@ def delete_comment(request, course_id, comment_id):
given a course_id and comment_id delete this comment given a course_id and comment_id delete this comment
ajax only ajax only
""" """
course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id) course_key = CourseKey.from_string(course_id)
comment = cc.Comment.find(comment_id) comment = cc.Comment.find(comment_id)
comment.delete() comment.delete()
comment_deleted.send(sender=None, user=request.user, post=comment) comment_deleted.send(sender=None, user=request.user, post=comment)
...@@ -484,7 +483,7 @@ def _vote_or_unvote(request, course_id, obj, value='up', undo_vote=False): ...@@ -484,7 +483,7 @@ def _vote_or_unvote(request, course_id, obj, value='up', undo_vote=False):
""" """
Vote or unvote for a thread or a response. Vote or unvote for a thread or a response.
""" """
course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id) course_key = CourseKey.from_string(course_id)
course = get_course_with_access(request.user, 'load', course_key) course = get_course_with_access(request.user, 'load', course_key)
user = cc.User.from_django_user(request.user) user = cc.User.from_django_user(request.user)
if undo_vote: if undo_vote:
...@@ -555,7 +554,7 @@ def flag_abuse_for_thread(request, course_id, thread_id): ...@@ -555,7 +554,7 @@ def flag_abuse_for_thread(request, course_id, thread_id):
given a course_id and thread_id flag this thread for abuse given a course_id and thread_id flag this thread for abuse
ajax only ajax only
""" """
course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id) course_key = CourseKey.from_string(course_id)
user = cc.User.from_django_user(request.user) user = cc.User.from_django_user(request.user)
thread = cc.Thread.find(thread_id) thread = cc.Thread.find(thread_id)
thread.flagAbuse(user, thread) thread.flagAbuse(user, thread)
...@@ -572,7 +571,7 @@ def un_flag_abuse_for_thread(request, course_id, thread_id): ...@@ -572,7 +571,7 @@ def un_flag_abuse_for_thread(request, course_id, thread_id):
ajax only ajax only
""" """
user = cc.User.from_django_user(request.user) user = cc.User.from_django_user(request.user)
course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id) course_key = CourseKey.from_string(course_id)
course = get_course_by_id(course_key) course = get_course_by_id(course_key)
thread = cc.Thread.find(thread_id) thread = cc.Thread.find(thread_id)
remove_all = bool( remove_all = bool(
...@@ -592,7 +591,7 @@ def flag_abuse_for_comment(request, course_id, comment_id): ...@@ -592,7 +591,7 @@ def flag_abuse_for_comment(request, course_id, comment_id):
given a course and comment id, flag comment for abuse given a course and comment id, flag comment for abuse
ajax only ajax only
""" """
course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id) course_key = CourseKey.from_string(course_id)
user = cc.User.from_django_user(request.user) user = cc.User.from_django_user(request.user)
comment = cc.Comment.find(comment_id) comment = cc.Comment.find(comment_id)
comment.flagAbuse(user, comment) comment.flagAbuse(user, comment)
...@@ -608,7 +607,7 @@ def un_flag_abuse_for_comment(request, course_id, comment_id): ...@@ -608,7 +607,7 @@ def un_flag_abuse_for_comment(request, course_id, comment_id):
ajax only ajax only
""" """
user = cc.User.from_django_user(request.user) user = cc.User.from_django_user(request.user)
course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id) course_key = CourseKey.from_string(course_id)
course = get_course_by_id(course_key) course = get_course_by_id(course_key)
remove_all = bool( remove_all = bool(
has_permission(request.user, 'openclose_thread', course_key) or has_permission(request.user, 'openclose_thread', course_key) or
...@@ -627,7 +626,7 @@ def pin_thread(request, course_id, thread_id): ...@@ -627,7 +626,7 @@ def pin_thread(request, course_id, thread_id):
given a course id and thread id, pin this thread given a course id and thread id, pin this thread
ajax only ajax only
""" """
course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id) course_key = CourseKey.from_string(course_id)
user = cc.User.from_django_user(request.user) user = cc.User.from_django_user(request.user)
thread = cc.Thread.find(thread_id) thread = cc.Thread.find(thread_id)
thread.pin(user, thread_id) thread.pin(user, thread_id)
...@@ -643,7 +642,7 @@ def un_pin_thread(request, course_id, thread_id): ...@@ -643,7 +642,7 @@ def un_pin_thread(request, course_id, thread_id):
given a course id and thread id, remove pin from this thread given a course id and thread id, remove pin from this thread
ajax only ajax only
""" """
course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id) course_key = CourseKey.from_string(course_id)
user = cc.User.from_django_user(request.user) user = cc.User.from_django_user(request.user)
thread = cc.Thread.find(thread_id) thread = cc.Thread.find(thread_id)
thread.un_pin(user, thread_id) thread.un_pin(user, thread_id)
...@@ -769,7 +768,7 @@ def users(request, course_id): ...@@ -769,7 +768,7 @@ def users(request, course_id):
Only exact matches are supported here, so the length of the result set will either be 0 or 1. Only exact matches are supported here, so the length of the result set will either be 0 or 1.
""" """
course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id) course_key = CourseKey.from_string(course_id)
try: try:
get_course_with_access(request.user, 'load', course_key, check_if_enrolled=True) get_course_with_access(request.user, 'load', course_key, check_if_enrolled=True)
except Http404: except Http404:
......
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
from opaque_keys import InvalidKeyError from opaque_keys import InvalidKeyError
from opaque_keys.edx.keys import CourseKey from opaque_keys.edx.keys import CourseKey
from opaque_keys.edx.locations import SlashSeparatedCourseKey
from courseware.courses import get_course from courseware.courses import get_course
...@@ -16,10 +15,7 @@ class Command(BaseCommand): ...@@ -16,10 +15,7 @@ class Command(BaseCommand):
raise CommandError("Only one course id may be specifiied") raise CommandError("Only one course id may be specifiied")
course_id = args[0] course_id = args[0]
try: course_key = CourseKey.from_string(course_id)
course_key = CourseKey.from_string(course_id)
except InvalidKeyError:
course_key = SlashSeparatedCourseKey.from_deprecated_string(course_id)
course = get_course(course_key) course = get_course(course_key)
if not course: if not course:
......
...@@ -3,7 +3,7 @@ Management command to seed default permissions and roles. ...@@ -3,7 +3,7 @@ Management command to seed default permissions and roles.
""" """
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
from django_comment_common.utils import seed_permissions_roles from django_comment_common.utils import seed_permissions_roles
from opaque_keys.edx.locations import SlashSeparatedCourseKey from opaque_keys.edx.keys import CourseKey
class Command(BaseCommand): class Command(BaseCommand):
...@@ -15,6 +15,6 @@ class Command(BaseCommand): ...@@ -15,6 +15,6 @@ class Command(BaseCommand):
raise CommandError("Please provide a course id") raise CommandError("Please provide a course id")
if len(args) > 1: if len(args) > 1:
raise CommandError("Too many arguments") raise CommandError("Too many arguments")
course_id = SlashSeparatedCourseKey.from_deprecated_string(args[0]) course_id = CourseKey.from_string(args[0])
seed_permissions_roles(course_id) seed_permissions_roles(course_id)
...@@ -3,7 +3,7 @@ Tests for the django comment client integration models ...@@ -3,7 +3,7 @@ Tests for the django comment client integration models
""" """
from django.test.testcases import TestCase from django.test.testcases import TestCase
from nose.plugins.attrib import attr from nose.plugins.attrib import attr
from opaque_keys.edx.locations import SlashSeparatedCourseKey from opaque_keys.edx.keys import CourseKey
from xmodule.modulestore.tests.django_utils import TEST_DATA_MIXED_TOY_MODULESTORE from xmodule.modulestore.tests.django_utils import TEST_DATA_MIXED_TOY_MODULESTORE
import django_comment_common.models as models import django_comment_common.models as models
...@@ -23,7 +23,7 @@ class RoleClassTestCase(ModuleStoreTestCase): ...@@ -23,7 +23,7 @@ class RoleClassTestCase(ModuleStoreTestCase):
# For course ID, syntax edx/classname/classdate is important # For course ID, syntax edx/classname/classdate is important
# because xmodel.course_module.id_to_location looks for a string to split # because xmodel.course_module.id_to_location looks for a string to split
self.course_id = SlashSeparatedCourseKey("edX", "toy", "2012_Fall") self.course_id = CourseKey.from_string("edX/toy/2012_Fall")
self.student_role = models.Role.objects.get_or_create(name="Student", self.student_role = models.Role.objects.get_or_create(name="Student",
course_id=self.course_id)[0] course_id=self.course_id)[0]
self.student_role.add_permission("delete_thread") self.student_role.add_permission("delete_thread")
...@@ -31,7 +31,7 @@ class RoleClassTestCase(ModuleStoreTestCase): ...@@ -31,7 +31,7 @@ class RoleClassTestCase(ModuleStoreTestCase):
course_id=self.course_id)[0] course_id=self.course_id)[0]
self.TA_role = models.Role.objects.get_or_create(name="Community TA", self.TA_role = models.Role.objects.get_or_create(name="Community TA",
course_id=self.course_id)[0] course_id=self.course_id)[0]
self.course_id_2 = SlashSeparatedCourseKey("edx", "6.002x", "2012_Fall") self.course_id_2 = CourseKey.from_string("edX/6.002x/2012_Fall")
self.TA_role_2 = models.Role.objects.get_or_create(name="Community TA", self.TA_role_2 = models.Role.objects.get_or_create(name="Community TA",
course_id=self.course_id_2)[0] course_id=self.course_id_2)[0]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment