<commit_before>from django.db import models
from django.db.models.fields.related import RelatedField

from milkman import generators

class MilkmanRegistry(object):    
    def __init__(self):
        self.default_generators = {}
    
    def add_generator(self, cls, func):
        self.default_generators[cls] = func
    
    def get(self, cls):
        return self.default_generators.get(cls, lambda f: generators.loop(lambda: ''))

    @staticmethod
    def register():
        registry = MilkmanRegistry()
        registry.add_generator(models.AutoField, generators.random_auto_field_maker)
        try:
            registry.add_generator(models.BigIntegerField, generators.random_big_integer_maker)
        except AttributeError:
            pass  # Only supported in django 1.2+
        registry.add_generator(models.BooleanField, generators.random_boolean_maker)
        registry.add_generator(models.CharField, generators.random_string_maker)
        registry.add_generator(models.CommaSeparatedIntegerField, generators.random_comma_seperated_integer_maker)
        registry.add_generator(models.DateField, generators.random_date_string_maker)
        registry.add_generator(models.DateTimeField, generators.random_datetime_string_maker)
        registry.add_generator(models.DecimalField, generators.random_decimal_maker)
        registry.add_generator(models.EmailField, generators.email_generator('user', 'example.com'))
        registry.add_generator(models.FloatField, generators.random_float_maker)
        registry.add_generator(models.IntegerField, generators.random_integer_maker)
        registry.add_generator(models.IPAddressField, generators.random_ipaddress_maker)
        registry.add_generator(models.NullBooleanField, generators.random_null_boolean_maker)
        registry.add_generator(models.PositiveIntegerField, generators.random_positive_integer_maker)
        registry.add_generator(models.PositiveSmallIntegerField, generators.random_small_positive_integer_maker)
        registry.add_generator(models.SlugField, generators.random_string_maker)
        registry.add_generator(models.SmallIntegerField, generators.random_small_integer_maker)
        registry.add_generator(models.TextField, generators.random_string_maker)
        registry.add_generator(models.TimeField, generators.random_time_string_maker)
        # registry.add_generator(models.URLField, generators.random_url_maker)
        # registry.add_generator(models.FileField, default_generator)
        # registry.add_generator(models.FilePathField, default_generator)
        # registry.add_generator(models.ImageField, default_generator)
        # registry.add_generator(models.XMLField, default_generator)
        return registry

class MilkTruck(object):
    def __init__(self, model_class):
        self.model_class = model_class
        self.generators = {}
    
    def deliver(self, the_milkman, **explicit_values):
        exclude = []
        if explicit_values:
            exclude = explicit_values.keys()
        
        target = self.model_class()
        
        self.set_explicit_values(target, explicit_values)
        self.set_local_fields(target, the_milkman, exclude)
        target.save()
        
        self.set_m2m_explicit_values(target, explicit_values)
        self.set_m2m_fields(target, the_milkman, exclude)
        
        return target

    def is_m2m(self, field):
        return field in [f.name for f in self.model_class._meta.local_many_to_many]
    
    def has_explicit_through_table(self, field):
        return not field.rel.through._meta.auto_created
    
    def set_explicit_values(self, target, explicit_values):
        for k,v in explicit_values.iteritems():
            if not self.is_m2m(k):<commit_msg>handle differences in previous version of django in how through models were represented<commit_after>from django.db import models
from django.db.models.fields.related import RelatedField

from milkman import generators

class MilkmanRegistry(object):    
    def __init__(self):
        self.default_generators = {}
    
    def add_generator(self, cls, func):
        self.default_generators[cls] = func
    
    def get(self, cls):
        return self.default_generators.get(cls, lambda f: generators.loop(lambda: ''))

    @staticmethod
    def register():
        registry = MilkmanRegistry()
        registry.add_generator(models.AutoField, generators.random_auto_field_maker)
        try:
            registry.add_generator(models.BigIntegerField, generators.random_big_integer_maker)
        except AttributeError:
            pass  # Only supported in django 1.2+
        registry.add_generator(models.BooleanField, generators.random_boolean_maker)
        registry.add_generator(models.CharField, generators.random_string_maker)
        registry.add_generator(models.CommaSeparatedIntegerField, generators.random_comma_seperated_integer_maker)
        registry.add_generator(models.DateField, generators.random_date_string_maker)
        registry.add_generator(models.DateTimeField, generators.random_datetime_string_maker)
        registry.add_generator(models.DecimalField, generators.random_decimal_maker)
        registry.add_generator(models.EmailField, generators.email_generator('user', 'example.com'))
        registry.add_generator(models.FloatField, generators.random_float_maker)
        registry.add_generator(models.IntegerField, generators.random_integer_maker)
        registry.add_generator(models.IPAddressField, generators.random_ipaddress_maker)
        registry.add_generator(models.NullBooleanField, generators.random_null_boolean_maker)
        registry.add_generator(models.PositiveIntegerField, generators.random_positive_integer_maker)
        registry.add_generator(models.PositiveSmallIntegerField, generators.random_small_positive_integer_maker)
        registry.add_generator(models.SlugField, generators.random_string_maker)
        registry.add_generator(models.SmallIntegerField, generators.random_small_integer_maker)
        registry.add_generator(models.TextField, generators.random_string_maker)
        registry.add_generator(models.TimeField, generators.random_time_string_maker)
        # registry.add_generator(models.URLField, generators.random_url_maker)
        # registry.add_generator(models.FileField, default_generator)
        # registry.add_generator(models.FilePathField, default_generator)
        # registry.add_generator(models.ImageField, default_generator)
        # registry.add_generator(models.XMLField, default_generator)
        return registry

class MilkTruck(object):
    def __init__(self, model_class):
        self.model_class = model_class
        self.generators = {}
    
    def deliver(self, the_milkman, **explicit_values):
        exclude = []
        if explicit_values:
            exclude = explicit_values.keys()
        
        target = self.model_class()
        
        self.set_explicit_values(target, explicit_values)
        self.set_local_fields(target, the_milkman, exclude)
        target.save()
        
        self.set_m2m_explicit_values(target, explicit_values)
        self.set_m2m_fields(target, the_milkman, exclude)
        
        return target

    def is_m2m(self, field):
        return field in [f.name for f in self.model_class._meta.local_many_to_many]
    
    def has_explicit_through_table(self, field):
        if isinstance(field.rel.through, models.base.ModelBase):  # Django 1.2
            return not field.rel.through._meta.auto_created
        if isinstance(field.rel.through, (str, unicode)):  # Django 1.1
            return True
        return False
    
    def set_explicit_values(self, target, explicit_values):
        for k,v in explicit_values.iteritems():
            if not self.is_m2m(k):
{'fraction_non_alphanumeric': 0.07808078209416001, 'fraction_numerical': 0.0020581425263699513, 'mean_word_length': 3.723572296476306, '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': '3550935', 'n_tokens_mistral': 2186, 'n_tokens_neox': 2116, 'n_words': 338}