<commit_before>from django.db import models
from django.db.models.fields.related \
        import ReverseSingleRelatedObjectDescriptor
from .models import AccessObject
from .widgets import AccessWidget
from .forms import AccessFormField


class AccessField(models.OneToOneField):
    def __init__(self):
        super(AccessField, self).__init__(AccessObject, to_field=None,
                null=True, blank=True)

    def contribute_to_class(self, cls, name):
        super(AccessField, self).contribute_to_class(cls, name)
        setattr(cls, self.name, AccessDescriptor(self))

    def formfield(self, **kwargs):
        return AccessFormField(**kwargs)


try:
    from south.modelsinspector import add_introspection_rules
    add_introspection_rules([], ["^armstrong\.core\.arm_access\.fields\.AccessField"])
except ImportError:
    pass


class AccessDescriptor(ReverseSingleRelatedObjectDescriptor):
    def __set__(self, instance, value):
        # we accept None, a list of Assignments or a single assignment
        obj = self.__get__(instance)
        if obj is None and value is not None:
            obj = AccessObject.objects.create()
            instance.access_id = obj.id

        if value is None:
            if obj is not None:
                instance.access_id = None
                instance.save()
                obj.delete()
            # use our super class for cache invalidation
            super(AccessDescriptor, self).__set__(instance, None)
        else:
            obj.clear()
            if hasattr(value, '__iter__'):
                obj.add(*value)
            else:
                obj.add(value)
            super(AccessDescriptor, self).__set__(instance, obj)
<commit_msg>switch to using south_field_triple, cuz we do not take arguments<commit_after>from django.db import models
from django.db.models.fields.related \
        import ReverseSingleRelatedObjectDescriptor
from .models import AccessObject
from .widgets import AccessWidget
from .forms import AccessFormField


class AccessField(models.OneToOneField):
    def __init__(self):
        super(AccessField, self).__init__(AccessObject, to_field=None,
                null=True, blank=True)

    def contribute_to_class(self, cls, name):
        super(AccessField, self).contribute_to_class(cls, name)
        setattr(cls, self.name, AccessDescriptor(self))

    def formfield(self, **kwargs):
        return AccessFormField(**kwargs)

    def south_field_triple(self):
        return ('armstrong.core.arm_access.fields.AccessField', [], {})


class AccessDescriptor(ReverseSingleRelatedObjectDescriptor):
    def __set__(self, instance, value):
        # we accept None, a list of Assignments or a single assignment
        obj = self.__get__(instance)
        if obj is None and value is not None:
            obj = AccessObject.objects.create()
            instance.access_id = obj.id

        if value is None:
            if obj is not None:
                instance.access_id = None
                instance.save()
                obj.delete()
            # use our super class for cache invalidation
            super(AccessDescriptor, self).__set__(instance, None)
        else:
            obj.clear()
            if hasattr(value, '__iter__'):
                obj.add(*value)
            else:
                obj.add(value)
            super(AccessDescriptor, self).__set__(instance, obj)

{'fraction_non_alphanumeric': 0.08561341571050309, 'fraction_numerical': 0.0, 'mean_word_length': 2.5196687370600412, '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': '6594615', 'n_tokens_mistral': 921, 'n_tokens_neox': 882, 'n_words': 243}