Deal with NoneType access.

The `NoneType` object is not available from the `types` module in
python 3. This is a workaronud to deal with that.
This commit is contained in:
Feanil Patel
2019-07-18 14:57:48 -04:00
parent e51fae3784
commit d9c9d7bab9
2 changed files with 11 additions and 11 deletions

View File

@@ -1,11 +1,12 @@
"""
Custom field types for mongoengine
"""
from __future__ import absolute_import
import mongoengine
from opaque_keys.edx.locations import Location
from types import NoneType
from opaque_keys.edx.keys import CourseKey, UsageKey
from six import text_type
import six
class CourseKeyField(mongoengine.StringField):
@@ -20,7 +21,7 @@ class CourseKeyField(mongoengine.StringField):
"""
For now saves the course key in the deprecated form
"""
assert isinstance(course_key, (NoneType, CourseKey))
assert isinstance(course_key, (type(None), CourseKey))
if course_key:
# don't call super as base.BaseField.to_mongo calls to_python() for some odd reason
return text_type(course_key)
@@ -33,16 +34,16 @@ class CourseKeyField(mongoengine.StringField):
"""
# calling super b/c it decodes utf (and doesn't have circularity of from_python)
course_key = super(CourseKeyField, self).to_python(course_key)
assert isinstance(course_key, (NoneType, basestring, CourseKey))
assert isinstance(course_key, (type(None), six.string_types, CourseKey))
if course_key == '':
return None
if isinstance(course_key, basestring):
if isinstance(course_key, six.string_types):
return CourseKey.from_string(course_key)
else:
return course_key
def validate(self, value):
assert isinstance(value, (NoneType, basestring, CourseKey))
assert isinstance(value, (type(None), six.string_types, CourseKey))
if isinstance(value, CourseKey):
return super(CourseKeyField, self).validate(text_type(value))
else:
@@ -60,7 +61,7 @@ class UsageKeyField(mongoengine.StringField):
"""
For now saves the usage key in the deprecated location i4x/c4x form
"""
assert isinstance(location, (NoneType, UsageKey))
assert isinstance(location, (type(None), UsageKey))
if location is None:
return None
return super(UsageKeyField, self).to_mongo(text_type(location))
@@ -69,17 +70,17 @@ class UsageKeyField(mongoengine.StringField):
"""
Deserialize to a UsageKey instance: for now it's a location missing the run
"""
assert isinstance(location, (NoneType, basestring, UsageKey))
assert isinstance(location, (type(None), six.string_types, UsageKey))
if location == '':
return None
if isinstance(location, basestring):
if isinstance(location, six.string_types):
location = super(UsageKeyField, self).to_python(location)
return Location.from_string(location)
else:
return location
def validate(self, value):
assert isinstance(value, (NoneType, basestring, UsageKey))
assert isinstance(value, (type(None), six.string_types, UsageKey))
if isinstance(value, UsageKey):
return super(UsageKeyField, self).validate(text_type(value))
else:

View File

@@ -6,7 +6,6 @@ Module for checking permissions with the comment_client backend
from __future__ import absolute_import
import logging
from types import NoneType
import six
from edx_django_utils.cache import DEFAULT_REQUEST_CACHE
@@ -23,7 +22,7 @@ from openedx.core.lib.cache_utils import request_cached
def has_permission(user, permission, course_id=None):
assert isinstance(course_id, (NoneType, CourseKey))
assert isinstance(course_id, (type(None), CourseKey))
request_cache_dict = DEFAULT_REQUEST_CACHE.data
cache_key = "django_comment_client.permissions.has_permission.all_permissions.{}.{}".format(
user.id, course_id