<commit_before># -*- coding: utf-8 -*-
# Copyright (c) 2011-2013 <NAME>
# This code is distributed under the two-clause BSD license.

from __future__ import unicode_literals

"""Specific versions of XWorkflows to use with Django."""

from django.apps import apps
from django.db import models
from django.db import transaction
from django.conf import settings
from django.contrib.contenttypes import fields as ct_fields
from django.contrib.contenttypes import models as ct_models
from django.core import exceptions
from django.forms import fields
from django.forms import widgets
from django.utils.deconstruct import deconstructible
from django.utils.encoding import force_text, python_2_unicode_compatible
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _

from xworkflows import base


State = base.State
AbortTransition = base.AbortTransition
ForbiddenTransition = base.ForbiddenTransition
InvalidTransitionError = base.InvalidTransitionError
WorkflowError = base.WorkflowError

transition = base.transition


class StateSelect(widgets.Select):
    """Custom 'select' widget to handle state retrieval."""

    def render(self, name, value, attrs=None, *args, **kwargs):
        """Handle a few expected values for rendering the current choice.

        Extracts the state name from StateWrapper and State object.
        """
        if isinstance(value, base.StateWrapper):
            state_name = value.state.name
        elif isinstance(value, base.State):
            state_name = value.name
        else:
            state_name = str(value)
        return super(StateSelect, self).render(name, state_name, attrs, *args, **kwargs)


class StateFieldProperty(object):
    """Property-like attribute for WorkflowEnabled classes.

    Similar to django.db.models.fields.subclassing.Creator, but doesn't raise
    AttributeError.
    """
    def __init__(self, field, parent_property):
        self.field = field
        self.parent_property = parent_property

    def __get__(self, instance, owner):
        """Retrieve the related attributed from a class / an instance.

        If retrieving from an instance, return the actual value; if retrieving
        from a class, return the workflow.

        When working on instances with a .only() queryset, the instance value
        could be hidden behind a DeferredAttribute.
        """
        if instance:
            if self.parent_property and hasattr(self.parent_property, '__get__'):
                # We override a property.
                return self.parent_property.__get__(instance, owner)
            return instance.__dict__.get(self.field.name, self.field.workflow.initial_state)
        else:
            return self.field.workflow

    def __set__(self, instance, value):<commit_msg>instance.state should return a StateWrapper object<commit_after># -*- coding: utf-8 -*-
# Copyright (c) 2011-2013 <NAME>
# This code is distributed under the two-clause BSD license.

from __future__ import unicode_literals

"""Specific versions of XWorkflows to use with Django."""

from django.apps import apps
from django.db import models
from django.db import transaction
from django.conf import settings
from django.contrib.contenttypes import fields as ct_fields
from django.contrib.contenttypes import models as ct_models
from django.core import exceptions
from django.forms import fields
from django.forms import widgets
from django.utils.deconstruct import deconstructible
from django.utils.encoding import force_text, python_2_unicode_compatible
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _

from xworkflows import base


State = base.State
AbortTransition = base.AbortTransition
ForbiddenTransition = base.ForbiddenTransition
InvalidTransitionError = base.InvalidTransitionError
WorkflowError = base.WorkflowError

transition = base.transition


class StateSelect(widgets.Select):
    """Custom 'select' widget to handle state retrieval."""

    def render(self, name, value, attrs=None, *args, **kwargs):
        """Handle a few expected values for rendering the current choice.

        Extracts the state name from StateWrapper and State object.
        """
        if isinstance(value, base.StateWrapper):
            state_name = value.state.name
        elif isinstance(value, base.State):
            state_name = value.name
        else:
            state_name = str(value)
        return super(StateSelect, self).render(name, state_name, attrs, *args, **kwargs)


class StateFieldProperty(object):
    """Property-like attribute for WorkflowEnabled classes.

    Similar to django.db.models.fields.subclassing.Creator, but doesn't raise
    AttributeError.
    """
    def __init__(self, field, parent_property):
        self.field = field
        self.parent_property = parent_property

    def __get__(self, instance, owner):
        """Retrieve the related attributed from a class / an instance.

        If retrieving from an instance, return the actual value; if retrieving
        from a class, return the workflow.

        When working on instances with a .only() queryset, the instance value
        could be hidden behind a DeferredAttribute.
        """
        if instance:
            if self.parent_property and hasattr(self.parent_property, '__get__'):
                # We override a property.
                return self.parent_property.__get__(instance, owner)
            default = self.field.to_python(self.field.workflow.initial_state)
            return instance.__dict__.get(self.field.name, default)
        else:
            return self.field.workflow

    def __set__(self, instance, value):
{'fraction_non_alphanumeric': 0.07438744932134674, 'fraction_numerical': 0.0035254715318173806, 'mean_word_length': 3.378086419753086, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '3072477', 'n_tokens_mistral': 1475, 'n_tokens_neox': 1420, 'n_words': 506}