<commit_before>"""
Custom field types for mongoengine
"""
import mongoengine
from xmodule.modulestore.locations import SlashSeparatedCourseKey, Location
from types import NoneType
from xmodule.modulestore.keys import CourseKey


class CourseKeyField(mongoengine.StringField):
    """
    Serializes and deserializes CourseKey's to mongo dbs which use mongoengine
    """
    def __init__(self, **kwargs):
        # it'd be useful to add init args such as support_deprecated, force_deprecated
        super(CourseKeyField, self).__init__(**kwargs)

    def to_mongo(self, course_key):
        """
        For now saves the course key in the deprecated form
        """
        assert isinstance(course_key, (NoneType, SlashSeparatedCourseKey))
        if course_key:
            # don't call super as base.BaseField.to_mongo calls to_python() for some odd reason
            return course_key.to_deprecated_string()
        else:
            return None

    def to_python(self, course_key):
        """
        Deserialize to a CourseKey instance
        """
        # 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, SlashSeparatedCourseKey))
        if course_key == '':
            return None
        if isinstance(course_key, basestring):
            return SlashSeparatedCourseKey.from_deprecated_string(course_key)
        else:
            return course_key

    def validate(self, value):
        assert isinstance(value, (NoneType, basestring, SlashSeparatedCourseKey))
        if isinstance(value, CourseKey):
            return super(CourseKeyField, self).validate(value.to_deprecated_string())
        else:
            return super(CourseKeyField, self).validate(value)

    def prepare_query_value(self, _opt, value):
        return self.to_mongo(value)


class UsageKeyField(mongoengine.StringField):
    """
    Represent a UsageKey as a single string in Mongo
    """
    def to_mongo(self, location):
        """
        For now saves the usage key in the deprecated location i4x/c4x form
        """
        assert isinstance(location, (NoneType, SlashSeparatedCourseKey))
        if location is None:
            return location
        return super(UsageKeyField, self).to_mongo(location.to_deprecated_string())

    def to_python(self, location):
        """
        Deserialize to a UsageKey instance: for now it's a location missing the run
        """
        assert isinstance(location, (NoneType, basestring, Location))
        if location == '':
            return None
        if isinstance(location, basestring):
            location = super(UsageKeyField, self).to_python(location)
            return Location.from_deprecated_string(location)
        else:
            return location

    def validate(self, value):
        assert isinstance(value, (NoneType, basestring, Location))
        if isinstance(value, Location):
            return super(UsageKeyField, self).validate(value.to_deprecated_string())
        else:
            return super(UsageKeyField, self).validate(value)
<commit_msg>Make mongoengine field for UsageKeys expect a Location<commit_after>"""
Custom field types for mongoengine
"""
import mongoengine
from xmodule.modulestore.locations import SlashSeparatedCourseKey, Location
from types import NoneType
from xmodule.modulestore.keys import CourseKey


class CourseKeyField(mongoengine.StringField):
    """
    Serializes and deserializes CourseKey's to mongo dbs which use mongoengine
    """
    def __init__(self, **kwargs):
        # it'd be useful to add init args such as support_deprecated, force_deprecated
        super(CourseKeyField, self).__init__(**kwargs)

    def to_mongo(self, course_key):
        """
        For now saves the course key in the deprecated form
        """
        assert isinstance(course_key, (NoneType, SlashSeparatedCourseKey))
        if course_key:
            # don't call super as base.BaseField.to_mongo calls to_python() for some odd reason
            return course_key.to_deprecated_string()
        else:
            return None

    def to_python(self, course_key):
        """
        Deserialize to a CourseKey instance
        """
        # 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, SlashSeparatedCourseKey))
        if course_key == '':
            return None
        if isinstance(course_key, basestring):
            return SlashSeparatedCourseKey.from_deprecated_string(course_key)
        else:
            return course_key

    def validate(self, value):
        assert isinstance(value, (NoneType, basestring, SlashSeparatedCourseKey))
        if isinstance(value, CourseKey):
            return super(CourseKeyField, self).validate(value.to_deprecated_string())
        else:
            return super(CourseKeyField, self).validate(value)

    def prepare_query_value(self, _opt, value):
        return self.to_mongo(value)


class UsageKeyField(mongoengine.StringField):
    """
    Represent a UsageKey as a single string in Mongo
    """
    def to_mongo(self, location):
        """
        For now saves the usage key in the deprecated location i4x/c4x form
        """
        assert isinstance(location, (NoneType, Location))
        if location is None:
            return None
        return super(UsageKeyField, self).to_mongo(location.to_deprecated_string())

    def to_python(self, location):
        """
        Deserialize to a UsageKey instance: for now it's a location missing the run
        """
        assert isinstance(location, (NoneType, basestring, Location))
        if location == '':
            return None
        if isinstance(location, basestring):
            location = super(UsageKeyField, self).to_python(location)
            return Location.from_deprecated_string(location)
        else:
            return location

    def validate(self, value):
        assert isinstance(value, (NoneType, basestring, Location))
        if isinstance(value, Location):
            return super(UsageKeyField, self).validate(value.to_deprecated_string())
        else:
            return super(UsageKeyField, self).validate(value)

{'fraction_non_alphanumeric': 0.08026233603997501, 'fraction_numerical': 0.0006246096189881324, 'mean_word_length': 2.8399280575539567, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4668125', 'n_tokens_mistral': 1745, 'n_tokens_neox': 1689, 'n_words': 489}