<commit_before># -*- coding: utf-8 -*-
import logging
from functools import update_wrapper

from django.forms.formsets import all_valid
from django.contrib import admin
from django.contrib.contenttypes.models import ContentType
from django.contrib.admin.util import unquote
from django.views.decorators.csrf import csrf_protect
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse
from django.http import Http404
from django.template.response import  TemplateResponse
from django.utils.decorators import method_decorator
from django.utils.html import escape
from django.utils.text import capfirst
from django.utils.translation import ugettext_lazy as _
from django.utils.encoding import force_unicode
from django.utils.crypto import get_random_string
from django.contrib.admin import widgets, helpers

from xanax.settings import GET_SETTING


LOGGER = logging.getLogger(__name__)

csrf_protect_m = method_decorator(csrf_protect)


def fine_setattr(preview_object, attr, value):
    if object.__dict__.get(attr):
        setattr(preview_object, attr, value)
    else:
        setattr(preview_object.__class__, attr, value)


def prepare_M2M_field(preview_object, field, m2m_related_list=None):
    if m2m_related_list == None:
        m2m_related_list = []
    fine_setattr(preview_object, field, m2m_related_list)


def prepeare_object(preview_object, preview_token):
    proxy_model = type(
        str(preview_object.__class__.__name__)
        + 'Preview%s' % preview_token,
        (preview_object.__class__, ),
        {'__module__': __name__}
    )
    preview_object.__class__ = proxy_model
    return preview_object


# TODO: NAGOVNOKOZENO?
def get_inline_objects(formsets):
    result = {}
    for formset in formsets:
        deleted_objects_id = []
        for form in formset.initial_forms:
            if formset.can_delete and formset._should_delete_form(form):
                pk_name = formset._pk_field.name
                raw_pk_value = form._raw_value(pk_name)
                pk_value = form.fields[pk_name].clean(raw_pk_value)
                deleted_objects_id.append(getattr(pk_value, 'pk', pk_value))
        # TODO: ZATYCHKA?
        formset._should_delete_form = lambda x: []
        changed_objects = formset.save(commit=False)
        changed_objects_id = [i.id for i in changed_objects]
        inline_objects = [i for i in list(formset.get_queryset())
                          if i.id not in changed_objects_id] +\
                         [i for i in changed_objects
                          if i.id not in deleted_objects_id]
        result.update({formset.model.__name__: inline_objects})
    return result<commit_msg>Fixed inline formset bug on add action<commit_after># -*- coding: utf-8 -*-
import logging
from functools import update_wrapper

from django.forms.formsets import all_valid
from django.contrib import admin
from django.contrib.contenttypes.models import ContentType
from django.contrib.admin.util import unquote
from django.views.decorators.csrf import csrf_protect
from django.core.exceptions import PermissionDenied
from django.core.urlresolvers import reverse
from django.http import Http404
from django.template.response import  TemplateResponse
from django.utils.decorators import method_decorator
from django.utils.html import escape
from django.utils.text import capfirst
from django.utils.translation import ugettext_lazy as _
from django.utils.encoding import force_unicode
from django.utils.crypto import get_random_string
from django.contrib.admin import widgets, helpers

from xanax.settings import GET_SETTING


LOGGER = logging.getLogger(__name__)

csrf_protect_m = method_decorator(csrf_protect)


def fine_setattr(preview_object, attr, value):
    if object.__dict__.get(attr):
        setattr(preview_object, attr, value)
    else:
        setattr(preview_object.__class__, attr, value)


def prepare_M2M_field(preview_object, field, m2m_related_list=None):
    if m2m_related_list == None:
        m2m_related_list = []
    fine_setattr(preview_object, field, m2m_related_list)


def prepeare_object(preview_object, preview_token):
    proxy_model = type(
        str(preview_object.__class__.__name__)
        + 'Preview%s' % preview_token,
        (preview_object.__class__, ),
        {'__module__': __name__}
    )
    preview_object.__class__ = proxy_model
    preview_object.pk = 0
    return preview_object


# TODO: NAGOVNOKOZENO?
def get_inline_objects(formsets):
    result = {}
    for formset in formsets:
        deleted_objects_id = []
        for form in formset.initial_forms:
            if formset.can_delete and formset._should_delete_form(form):
                pk_name = formset._pk_field.name
                raw_pk_value = form._raw_value(pk_name)
                pk_value = form.fields[pk_name].clean(raw_pk_value)
                deleted_objects_id.append(getattr(pk_value, 'pk', pk_value))
        # TODO: ZATYCHKA?
        formset._should_delete_form = lambda x: []
        changed_objects = formset.save(commit=False)
        changed_objects_id = [i.id for i in changed_objects]
        inline_objects = [i for i in list(formset.get_queryset())
                          if i.id not in changed_objects_id] +\
                         [i for i in changed_objects
                          if i.id not in deleted_objects_id]
        result.update({formset.model.__name__: inline_objects})
    return result
{'fraction_non_alphanumeric': 0.07416926748668992, 'fraction_numerical': 0.003488158619423536, 'mean_word_length': 3.3970944309927362, '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': '3572282', 'n_tokens_mistral': 1636, 'n_tokens_neox': 1600, 'n_words': 381}