<commit_before># -*- coding: UTF-8 -*-
# Copyright 2009-2016 by <NAME>.
# License: BSD, see file LICENSE for more details.

"""
Documented in :ref:`dpy`.

"""

from __future__ import unicode_literals
from __future__ import print_function
# from future import standard_library
# standard_library.install_aliases()
from builtins import str
from builtins import object
import six

import logging
logger = logging.getLogger(__name__)


from io import StringIO
import os
from os.path import dirname
import imp
from decimal import Decimal
from unipath import Path
from lino import AFTER17

from django.conf import settings
from django.db import models
from django.db.models import ForeignKey

from django.utils.module_loading import import_string

from django.db import IntegrityError
from django.db.models.fields import NOT_PROVIDED
from django.core.serializers import base
from django.core.exceptions import ValidationError, ObjectDoesNotExist
from django.utils.encoding import smart_text, is_protected_type, force_text
from django.utils import translation

from lino.utils.mldbc.fields import BabelCharField, BabelTextField
from lino.core.choicelists import ChoiceListField
from lino.core.utils import obj2str, sorted_models_list, full_model_name

SUFFIX = '.py'


def create_mti_child(parent_model, pk, child_model, **kw):
    """Similar to :func:`lino.utils.mti.insert_child`, but for usage in
    Python dumps (generated by :manage:`dump2py`).

    The difference is very tricky.  The return value here is an
    "almost normal" model instance, whose `save` and `full_clean`
    methods have been hacked.  These are the only methods that will be
    called by :class:`Deserializer`.  You should not use this instance
    for anything else and throw it away when the save() has been
    called.

    """
    parent_link_field = child_model._meta.parents.get(parent_model, None)
    if parent_link_field is None:
        raise ValidationError("A %s cannot be parent for a %s" % (
            parent_model.__name__, child_model.__name__))
    ignored = {}
    for f in parent_model._meta.fields:
        if f.name in kw:
            ignored[f.name] = kw.pop(f.name)
    kw[parent_link_field.name + "_id"] = pk
    if ignored:
        raise Exception(
            "create_mti_child() %s %s from %s : "
            "ignored non-local fields %s" % (
                child_model.__name__,
                pk,
                parent_model.__name__,
                ignored))
    child_obj = child_model(**kw)

    def full_clean(*args, **kw):
        pass

    def save(*args, **kw):
        kw.update(raw=True, force_insert=True)
        child_obj.save_base(**kw)

    child_obj.save = save
    child_obj.full_clean = full_clean
    return child_obj<commit_msg>Fixed #1926 BabelFields on MTI parent fail to dump/restore<commit_after># -*- coding: UTF-8 -*-
# Copyright 2009-2016 by <NAME>.
# License: BSD, see file LICENSE for more details.

"""
Documented in :ref:`dpy`.

"""

from __future__ import unicode_literals
from __future__ import print_function
# from future import standard_library
# standard_library.install_aliases()
from builtins import str
from builtins import object
import six

import logging
logger = logging.getLogger(__name__)


from io import StringIO
import os
from os.path import dirname
import imp
from decimal import Decimal
from unipath import Path
from lino import AFTER17

from django.conf import settings
from django.db import models
from django.db.models import ForeignKey

from django.utils.module_loading import import_string

from django.db import IntegrityError
from django.db.models.fields import NOT_PROVIDED
from django.core.serializers import base
from django.core.exceptions import ValidationError, ObjectDoesNotExist
from django.utils.encoding import smart_text, is_protected_type, force_text
from django.utils import translation

from lino.utils.mldbc.fields import BabelCharField, BabelTextField
from lino.core.choicelists import ChoiceListField
from lino.core.utils import obj2str, sorted_models_list, full_model_name

SUFFIX = '.py'


def create_mti_child(parent_model, pk, child_model, **kw):
    """Similar to :func:`lino.utils.mti.insert_child`, but for usage in
    Python dumps (generated by :manage:`dump2py`).

    The difference is very tricky.  The return value here is an
    "almost normal" model instance, whose `save` and `full_clean`
    methods have been hacked.  These are the only methods that will be
    called by :class:`Deserializer`.  You should not use this instance
    for anything else and throw it away when the save() has been
    called.

    """
    parent_link_field = child_model._meta.parents.get(parent_model, None)
    if parent_link_field is None:
        raise ValidationError("A %s cannot be parent for a %s" % (
            parent_model.__name__, child_model.__name__))
    pfields = {}
    for f in parent_model._meta.fields:
        if f.name in kw:
            pfields[f.name] = kw.pop(f.name)
    kw[parent_link_field.name + "_id"] = pk
    # if ignored:
    #     raise Exception(
    #         "create_mti_child() %s %s from %s : "
    #         "ignored non-local fields %s" % (
    #             child_model.__name__,
    #             pk,
    #             parent_model.__name__,
    #             ignored))
    
    child_obj = child_model(**kw)
    
    for k, v in pfields.items():
        setattr(child_obj, k, v)

    def full_clean(*args, **kw):
        pass

    def save(*args, **kw):
        kw.update(raw=True, force_insert=True)
        child_obj.save_base(**kw)

    child_obj.save = save
    child_obj.full_clean = full_clean
    return child_obj
{'fraction_non_alphanumeric': 0.08201948627103632, 'fraction_numerical': 0.005314437555358724, 'mean_word_length': 3.380139643134213, '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': '2710249', 'n_tokens_mistral': 1720, 'n_tokens_neox': 1661, 'n_words': 549}