<commit_before>from django.db import models
from django import forms
from django.utils.translation import ugettext_lazy as _
from sorl.thumbnail.conf import settings
from sorl.thumbnail.helpers import get_module_class
from sorl.thumbnail.images import ImageFile


__all__ = ('ImageField', 'ImageFormField')


class ImageField(models.FileField):
    def delete_file(self, instance, sender, **kwargs):
        """
        Adds deletion of thumbnails and key kalue store references to the
        parent class implementation.
        """
        file_ = getattr(instance, self.attname)
        # If no other object of this type references the file,
        # and it's not the default value for future objects,
        # delete it from the backend.
        if file_ and file_.name != self.default and \
            not sender._default_manager.filter(**{self.name: file_.name}):
                file_.delete(save=False)
                # now delete the kvstore references and thumbnails
                image_file = ImageFile(file_)
                kvstore = get_module_class(settings.THUMBNAIL_KVSTORE)()
                kvstore.delete(image_file)
        elif file_:
            # Otherwise, just close the file, so it doesn't tie up resources.
            file_.close()

    def formfield(self, **kwargs):
        defaults = {'form_class': ImageFormField}
        defaults.update(kwargs)<commit_msg>closing #1<commit_after>from django.db import models
from django import forms
from django.utils.translation import ugettext_lazy as _
from sorl.thumbnail.conf import settings
from sorl.thumbnail.helpers import get_module_class
from sorl.thumbnail.images import ImageFile


__all__ = ('ImageField', 'ImageFormField')


class South(object):
    """
    Just a south introspection Mixin
    """
    def south_field_triple(self):
        from south.modelsinspector import introspector
        cls_name = '%s.%s' % (self.__class__.__module__ , self.__class__.__name__)
        args, kwargs = introspector(self)
        return (cls_name, args, kwargs)


class ImageField(South, models.FileField):
    def delete_file(self, instance, sender, **kwargs):
        """
        Adds deletion of thumbnails and key kalue store references to the
        parent class implementation.
        """
        file_ = getattr(instance, self.attname)
        # If no other object of this type references the file,
        # and it's not the default value for future objects,
        # delete it from the backend.
        if file_ and file_.name != self.default and \
            not sender._default_manager.filter(**{self.name: file_.name}):
                file_.delete(save=False)
                # now delete the kvstore references and thumbnails
                image_file = ImageFile(file_)
                kvstore = get_module_class(settings.THUMBNAIL_KVSTORE)()
                kvstore.delete(image_file)
        elif file_:
            # Otherwise, just close the file, so it doesn't tie up resources.
            file_.close()

    def formfield(self, **kwargs):
        defaults = {'form_class': ImageFormField}
        defaults.update(kwargs)
{'fraction_non_alphanumeric': 0.08152173913043478, 'fraction_numerical': 0.00031969309462915604, 'mean_word_length': 2.651108518086348, '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': '779938', 'n_tokens_mistral': 862, 'n_tokens_neox': 834, 'n_words': 263}