<commit_before>from inspect import isclass

import django
from django.db.models import get_model
from django.db.models.base import ModelBase
from django.core.exceptions import ImproperlyConfigured
from django.utils.six import string_types


from actstream.compat import generic


def setup_generic_relations(model_class):
    """
    Set up GenericRelations for actionable models.
    """
    related_attr_name = 'related_name'
    related_attr_value = 'actions_with_%s' % label(model_class)
    if django.VERSION >= (1, 7):
        related_attr_name = 'related_query_name'
    relations = {}
    for field in ('actor', 'target', 'action_object'):
        attr = '%s_actions' % field
        attr_value = '%s_as_%s' % (related_attr_value, field)
        kwargs = {
            'content_type_field': '%s_content_type' % field,
            'object_id_field': '%s_object_id' % field,
            related_attr_name: attr_value
        }
        rel = generic.GenericRelation('actstream.Action', **kwargs
                        ).contribute_to_class(model_class, attr)
        relations[field] = rel
    return relations


def label(model_class):
    if hasattr(model_class._meta, 'model_name'):
        model_name = model_class._meta.model_name<commit_msg>Fix deletion of target/actor objects<commit_after>from inspect import isclass

import django
from django.db.models import get_model
from django.db.models.base import ModelBase
from django.core.exceptions import ImproperlyConfigured
from django.utils.six import string_types


from actstream.compat import generic


def setup_generic_relations(model_class):
    """
    Set up GenericRelations for actionable models.
    """
    Action = get_model('actstream', 'Action')

    related_attr_name = 'related_name'
    related_attr_value = 'actions_with_%s' % label(model_class)
    if django.VERSION >= (1, 7):
        related_attr_name = 'related_query_name'
    relations = {}
    for field in ('actor', 'target', 'action_object'):
        attr = '%s_actions' % field
        attr_value = '%s_as_%s' % (related_attr_value, field)
        kwargs = {
            'content_type_field': '%s_content_type' % field,
            'object_id_field': '%s_object_id' % field,
            related_attr_name: attr_value
        }
        rel = generic.GenericRelation('actstream.Action', **kwargs
                        ).contribute_to_class(model_class, attr)
        relations[field] = rel

        setattr(Action, attr_value, None)
    return relations


def label(model_class):
    if hasattr(model_class._meta, 'model_name'):
        model_name = model_class._meta.model_name
{'fraction_non_alphanumeric': 0.09736540664375716, 'fraction_numerical': 0.0015273004963726614, 'mean_word_length': 3.2740619902120716, 'pattern_counts': {'":': 0, '<': 3, '<?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': '7018575', 'n_tokens_mistral': 783, 'n_tokens_neox': 763, 'n_words': 177}