<commit_before># ------------------------------------------------------------------------
# coding=utf-8
# ------------------------------------------------------------------------

from __future__ import absolute_import

import mimetypes
from os.path import splitext

from django.contrib.admin.widgets import AdminFileWidget
from django.contrib.admin.widgets import ForeignKeyRawIdWidget
from django.db import models
from django.utils.html import escape
from django.utils.safestring import mark_safe
from django.utils.text import truncate_words
from django.utils.translation import ugettext_lazy as _

from feincms.admin.item_editor import FeinCMSInline
from feincms.templatetags import feincms_thumbnail
from .models import MediaFile

__all__ = ('MediaFileForeignKey', 'ContentWithMediaFile')

# ------------------------------------------------------------------------
class MediaFileForeignKeyRawIdWidget(ForeignKeyRawIdWidget):
    def __init__(self, original):
        self.__dict__ = original.__dict__

    def label_for_value(self, value):
        key = self.rel.get_related_field().name
        try:
            obj = self.rel.to._default_manager.using(self.db).get(**{key: value})
            label = [u'&nbsp;<strong>%s</strong>' % escape(truncate_words(obj, 14))]

            if obj.type == 'image':
                image = feincms_thumbnail.thumbnail(obj.file.name, '240x120')
                label.append(u'<br /><img src="%s" alt="" style="margin:1em 0 0 10em" />' % image)

            return u''.join(label)
        except (ValueError, self.rel.to.DoesNotExist):
            return ''


class MediaFileForeignKey(models.ForeignKey):
    """
    Drop-in replacement for Django's ``models.ForeignKey`` which automatically adds a
    thumbnail of media files if the media file foreign key is shown using ``raw_id_fields``.
    """
    def formfield(self, **kwargs):
        if 'widget' in kwargs and isinstance(kwargs['widget'], ForeignKeyRawIdWidget):
            kwargs['widget'] = MediaFileForeignKeyRawIdWidget(kwargs['widget'])
        return super(MediaFileForeignKey, self).formfield(**kwargs)


class ContentWithMediaFile(models.Model):
    class feincms_item_editor_inline(FeinCMSInline):
        raw_id_fields = ('mediafile',)

    mediafile = MediaFileForeignKey(MediaFile, verbose_name=_('media file'),
        related_name='+')<commit_msg>Use the same thumbnail size for admin thumbnails everywhere<commit_after># ------------------------------------------------------------------------
# coding=utf-8
# ------------------------------------------------------------------------

from __future__ import absolute_import

import mimetypes
from os.path import splitext

from django.contrib.admin.widgets import AdminFileWidget
from django.contrib.admin.widgets import ForeignKeyRawIdWidget
from django.db import models
from django.utils.html import escape
from django.utils.safestring import mark_safe
from django.utils.text import truncate_words
from django.utils.translation import ugettext_lazy as _

from feincms.admin.item_editor import FeinCMSInline
from feincms.templatetags import feincms_thumbnail
from .models import MediaFile

__all__ = ('MediaFileForeignKey', 'ContentWithMediaFile')

# ------------------------------------------------------------------------
class MediaFileForeignKeyRawIdWidget(ForeignKeyRawIdWidget):
    def __init__(self, original):
        self.__dict__ = original.__dict__

    def label_for_value(self, value):
        key = self.rel.get_related_field().name
        try:
            obj = self.rel.to._default_manager.using(self.db).get(**{key: value})
            label = [u'&nbsp;<strong>%s</strong>' % escape(truncate_words(obj, 14))]

            if obj.type == 'image':
                image = feincms_thumbnail.thumbnail(obj.file.name, '100x100')
                label.append(u'<br /><img src="%s" alt="" style="margin:1em 0 0 10em" />' % image)

            return u''.join(label)
        except (ValueError, self.rel.to.DoesNotExist):
            return ''


class MediaFileForeignKey(models.ForeignKey):
    """
    Drop-in replacement for Django's ``models.ForeignKey`` which automatically adds a
    thumbnail of media files if the media file foreign key is shown using ``raw_id_fields``.
    """
    def formfield(self, **kwargs):
        if 'widget' in kwargs and isinstance(kwargs['widget'], ForeignKeyRawIdWidget):
            kwargs['widget'] = MediaFileForeignKeyRawIdWidget(kwargs['widget'])
        return super(MediaFileForeignKey, self).formfield(**kwargs)


class ContentWithMediaFile(models.Model):
    class feincms_item_editor_inline(FeinCMSInline):
        raw_id_fields = ('mediafile',)

    mediafile = MediaFileForeignKey(MediaFile, verbose_name=_('media file'),
        related_name='+')
{'fraction_non_alphanumeric': 0.18634840871021777, 'fraction_numerical': 0.005862646566164154, 'mean_word_length': 2.6271829916476843, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '958736', 'n_tokens_mistral': 1322, 'n_tokens_neox': 1271, 'n_words': 303}