<commit_before>from django.conf import settings
from django.db import models
from django.db.models.fields import FieldDoesNotExist
from django.db.models.base import ModelBase
from django.utils import six
from django.utils.translation import ugettext_lazy as _

from .managers import OrgManager, ActiveOrgManager

USER_MODEL = getattr(settings, 'AUTH_USER_MODEL', 'auth.User')


class UnicodeMixin(object):
    """
    """
    if six.PY3:
        __str__ = lambda x: x.__unicode__()
    else:
        __str__ = lambda x: unicode(x).encode('utf-8')


class OrgMeta(ModelBase):
    """
    Base metaclass for dynamically linking related organization models.

    This is particularly useful for custom organizations that can avoid
    multitable inheritence and also add additional attributes to the
    organization users, in particular.

    The `module_registry` dictionary is used to track the architecture across
    different Django apps. If more than one application makes use of these
    base models, the extended models will share class relationships, which is
    clearly undesirable. This ensures that the relationships between models
    within a module using these base classes are from other organization models.

    """
    module_registry = {}

    def __new__(cls, name, bases, attrs):
        base_classes = ['OrgModel', 'OrgUserModel', 'OrgOwnerModel']
        model = super(OrgMeta, cls).__new__(cls, name, bases, attrs)
        module = model.__module__
        if not cls.module_registry.get(module):
            cls.module_registry[module] = {
                'OrgModel': None,
                'OrgUserModel': None,
                'OrgOwnerModel': None,
            }
        for b in bases:
            if b.__name__ == "OrganizationBase":
                cls.module_registry[module]['OrgModel'] = model
            elif b.__name__ == "OrganizationUserBase":
                cls.module_registry[module]['OrgUserModel'] = model
            elif b.__name__ == "OrganizationOwnerBase":
                cls.module_registry[module]['OrgOwnerModel'] = model

        if all([cls.module_registry[module][klass] for klass in base_classes]):
            model.update_org(module)
            model.update_org_users(module)
            model.update_org_owner(module)

        return model

    def update_org(cls, module):
        """
        Adds the `users` field to the organization model
        """
        try:
            cls.module_registry[module]['OrgModel']._meta.get_field("users")<commit_msg>Add in workaround for Django 1.4 and six<commit_after>import django

from django.conf import settings
from django.db import models
from django.db.models.fields import FieldDoesNotExist
from django.db.models.base import ModelBase
from django.utils import six
from django.utils.translation import ugettext_lazy as _

from .managers import OrgManager, ActiveOrgManager

USER_MODEL = getattr(settings, 'AUTH_USER_MODEL', 'auth.User')


class UnicodeMixin(object):
    """
    """
    if six.PY3:
        __str__ = lambda x: x.__unicode__()
    else:
        __str__ = lambda x: unicode(x).encode('utf-8')


class OrgMeta(ModelBase):
    """
    Base metaclass for dynamically linking related organization models.

    This is particularly useful for custom organizations that can avoid
    multitable inheritence and also add additional attributes to the
    organization users, in particular.

    The `module_registry` dictionary is used to track the architecture across
    different Django apps. If more than one application makes use of these
    base models, the extended models will share class relationships, which is
    clearly undesirable. This ensures that the relationships between models
    within a module using these base classes are from other organization models.

    """
    module_registry = {}

    def __new__(cls, name, bases, attrs):
        # Borrowed from Django-polymorphic
        # Workaround compatibility issue with six.with_metaclass() and custom
        # Django model metaclasses:
        if not attrs and name == 'NewBase':
            if django.VERSION < (1, 5):
                # Let Django fully ignore the class which is inserted in between.
                # Django 1.5 fixed this, see https://code.djangoproject.com/ticket/19688
                attrs['__module__'] = 'django.utils.six'
                attrs['Meta'] = type('Meta', (), {'abstract': True})
            return super(OrgMeta, cls).__new__(cls, name, bases, attrs)

        base_classes = ['OrgModel', 'OrgUserModel', 'OrgOwnerModel']
        model = super(OrgMeta, cls).__new__(cls, name, bases, attrs)
        module = model.__module__
        if not cls.module_registry.get(module):
            cls.module_registry[module] = {
                'OrgModel': None,
                'OrgUserModel': None,
                'OrgOwnerModel': None,
            }
        for b in bases:
            if b.__name__ == "OrganizationBase":
                cls.module_registry[module]['OrgModel'] = model
            elif b.__name__ == "OrganizationUserBase":
                cls.module_registry[module]['OrgUserModel'] = model
            elif b.__name__ == "OrganizationOwnerBase":
                cls.module_registry[module]['OrgOwnerModel'] = model

        if all([cls.module_registry[module][klass] for klass in base_classes]):
            model.update_org(module)
            model.update_org_users(module)
            model.update_org_owner(module)

        return model

    def update_org(cls, module):
        """
        Adds the `users` field to the organization model
        """
        try:
            cls.module_registry[module]['OrgModel']._meta.get_field("users")
{'fraction_non_alphanumeric': 0.0875373527860784, 'fraction_numerical': 0.002636667252592723, 'mean_word_length': 2.7165251469627694, 'pattern_counts': {'":': 6, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 1, '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': '5591982', 'n_tokens_mistral': 1552, 'n_tokens_neox': 1470, 'n_words': 470}