Commit c676eeb1 by Matt Drayer Committed by Jonathan Piacenti

mattdrayer/rebase-20140926: Repaired faulty keygen logic

parent 896c0d5f
""" Utility functions related to HTTP requests """ """ Utility functions related to HTTP requests """
from django.core.handlers.base import BaseHandler
from django.test import RequestFactory
import re import re
import logging
from django.conf import settings from django.conf import settings
from microsite_configuration import microsite from microsite_configuration import microsite
from opaque_keys import InvalidKeyError from opaque_keys import InvalidKeyError
from opaque_keys.edx.keys import CourseKey
from opaque_keys.edx.locations import SlashSeparatedCourseKey from opaque_keys.edx.locations import SlashSeparatedCourseKey
log = logging.getLogger(__name__)
COURSE_REGEX = re.compile(r'^.*?/courses/{}'.format(settings.COURSE_ID_PATTERN)) COURSE_REGEX = re.compile(r'^.*?/courses/{}'.format(settings.COURSE_ID_PATTERN))
...@@ -33,6 +38,17 @@ def course_id_from_url(url): ...@@ -33,6 +38,17 @@ def course_id_from_url(url):
if not url: if not url:
return None return None
deprecated = False
if '/' in url:
deprecated = True
if deprecated:
COURSE_REGEX = re.compile(r'^.*/courses/(?P<course_id>[^/]+/[^/]+/[^/]+)')
key_generator = SlashSeparatedCourseKey.from_deprecated_string
else:
COURSE_REGEX = re.compile(r'^.*?/courses/(?P<course_id>[a-zA-Z0-9_+\/:]+)')
key_generator = CourseKey.from_string
match = COURSE_REGEX.match(url) match = COURSE_REGEX.match(url)
if match is None: if match is None:
...@@ -44,6 +60,45 @@ def course_id_from_url(url): ...@@ -44,6 +60,45 @@ def course_id_from_url(url):
return None return None
try: try:
return SlashSeparatedCourseKey.from_deprecated_string(course_id) course_key = key_generator(course_id)
except InvalidKeyError: except InvalidKeyError:
log.warning(
'unable to parse course_id "{}"'.format(course_id),
exc_info=True
)
return None return None
return course_key
class RequestMock(RequestFactory):
"""
RequestMock is used to create generic/dummy request objects in
scenarios where a regular request might not be available for use
"""
def request(self, **request):
"Construct a generic request object."
request = RequestFactory.request(self, **request)
handler = BaseHandler()
handler.load_middleware()
for middleware_method in handler._request_middleware:
if middleware_method(request):
raise Exception("Couldn't create request mock object - "
"request middleware returned a response")
return request
class RequestMockWithoutMiddleware(RequestMock):
"""
RequestMockWithoutMiddleware is used to create generic/dummy request
objects in scenarios where a regular request might not be available for use.
It's similiar to its parent except for the fact that it skips the loading
of middleware.
"""
def request(self, **request):
"Construct a generic request object."
request = RequestFactory.request(self, **request)
if not hasattr(request, 'session'):
request.session = {}
return request
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