<commit_before>from django import newforms as forms
from django.contrib import admin
from django.conf import settings
from django.utils.translation import ugettext

from tagging.models import TaggingInlineOptions

from ella.photos.models import FormatedPhoto, Format, Photo
from ella.photos.views import format_photo_json


class FormatedPhotoForm(forms.BaseForm):
    def clean(self):
        """
        Validation function that checks the dimensions of the crop whether it fits into the original and the format.
        """
        data = self.cleaned_data
        photo = data['photo']
        if (
            (data['crop_left'] >  photo.width) or
            (data['crop_top'] > photo.height) or
            ((data['crop_left'] + data['crop_width']) > photo.width) or
            ((data['crop_top'] + data['crop_height']) > photo.height)
):
            raise forms.ValidationError, ugettext("The specified crop coordinates do not fit into the source photo.")
        return data


class FormatOptions(admin.ModelAdmin):
    list_display = ('name', 'max_width', 'max_height', 'stretch', 'resample_quality',)
    list_filter = ('site', 'stretch', 'nocrop',)
    search_fields = ('name',)


class CropAreaWidget(forms.TextInput):
    class Media:
        JS_JQUERY = 'js/jquery.js'
        JS_INTERFACE = 'js/interface.js'
        JS_CROP = 'js/crop.js'
        CSS_CROP = 'css/crop.css'
        js = (
            settings.ADMIN_MEDIA_PREFIX + JS_JQUERY,
            settings.ADMIN_MEDIA_PREFIX + JS_INTERFACE,
            settings.ADMIN_MEDIA_PREFIX + JS_CROP,
)
        css = {
            'screen': (settings.ADMIN_MEDIA_PREFIX + CSS_CROP,),
}

    def __init__(self, attrs={}):
        super(CropAreaWidget, self).__init__(attrs={'class': 'crop'})


class FormatedPhotoInlineOptions(admin.TabularInline):
    model = FormatedPhoto
    def formfield_for_dbfield(self, db_field, **kwargs):
        if db_field.name in ['crop_width', 'crop_height', 'crop_left', 'crop_top']:
            kwargs['widget'] = CropAreaWidget
        return super(self.__class__, self).formfield_for_dbfield(db_field, **kwargs)


class PhotoOptions(admin.ModelAdmin):
    inlines = (FormatedPhotoInlineOptions, TaggingInlineOptions,)
    list_display = ('title', 'width', 'height', 'thumb') ## 'authors')
    list_filter = ('created',)
    prepopulated_fields = {'slug': ('title',)}
    search_fields = ('title', 'image', 'description',)

    def __call__(self, request, url):
        if url and url.endswith('json'):
            return format_photo_json(request, *url.split('/')[-3:-1])
        return super(PhotoOptions, self).__call__(request, url)


class FormatedPhotoOptions(admin.ModelAdmin):
    base_form = FormatedPhotoForm
    list_display = ('filename', 'format', 'width', 'height')
    list_filter = ('format',)
    search_fields = ('filename',)
    raw_id_fields = ('photo',)


admin.site.register(Format, FormatOptions)
admin.site.register(Photo, PhotoOptions)
admin.site.register(FormatedPhoto, FormatedPhotoOptions)
<commit_msg>Adding search of Photo by ID.<commit_after>from django import newforms as forms
from django.contrib import admin
from django.conf import settings
from django.utils.translation import ugettext

from tagging.models import TaggingInlineOptions

from ella.photos.models import FormatedPhoto, Format, Photo
from ella.photos.views import format_photo_json


class FormatedPhotoForm(forms.BaseForm):
    def clean(self):
        """
        Validation function that checks the dimensions of the crop whether it fits into the original and the format.
        """
        data = self.cleaned_data
        photo = data['photo']
        if (
            (data['crop_left'] >  photo.width) or
            (data['crop_top'] > photo.height) or
            ((data['crop_left'] + data['crop_width']) > photo.width) or
            ((data['crop_top'] + data['crop_height']) > photo.height)
):
            raise forms.ValidationError, ugettext("The specified crop coordinates do not fit into the source photo.")
        return data


class FormatOptions(admin.ModelAdmin):
    list_display = ('name', 'max_width', 'max_height', 'stretch', 'resample_quality',)
    list_filter = ('site', 'stretch', 'nocrop',)
    search_fields = ('name',)


class CropAreaWidget(forms.TextInput):
    class Media:
        JS_JQUERY = 'js/jquery.js'
        JS_INTERFACE = 'js/interface.js'
        JS_CROP = 'js/crop.js'
        CSS_CROP = 'css/crop.css'
        js = (
            settings.ADMIN_MEDIA_PREFIX + JS_JQUERY,
            settings.ADMIN_MEDIA_PREFIX + JS_INTERFACE,
            settings.ADMIN_MEDIA_PREFIX + JS_CROP,
)
        css = {
            'screen': (settings.ADMIN_MEDIA_PREFIX + CSS_CROP,),
}

    def __init__(self, attrs={}):
        super(CropAreaWidget, self).__init__(attrs={'class': 'crop'})


class FormatedPhotoInlineOptions(admin.TabularInline):
    model = FormatedPhoto
    def formfield_for_dbfield(self, db_field, **kwargs):
        if db_field.name in ['crop_width', 'crop_height', 'crop_left', 'crop_top']:
            kwargs['widget'] = CropAreaWidget
        return super(self.__class__, self).formfield_for_dbfield(db_field, **kwargs)


class PhotoOptions(admin.ModelAdmin):
    inlines = (FormatedPhotoInlineOptions, TaggingInlineOptions,)
    list_display = ('title', 'width', 'height', 'thumb') ## 'authors')
    list_filter = ('created',)
    prepopulated_fields = {'slug': ('title',)}
    search_fields = ('title', 'image', 'description', 'id',)

    def __call__(self, request, url):
        if url and url.endswith('json'):
            return format_photo_json(request, *url.split('/')[-3:-1])
        return super(PhotoOptions, self).__call__(request, url)


class FormatedPhotoOptions(admin.ModelAdmin):
    base_form = FormatedPhotoForm
    list_display = ('filename', 'format', 'width', 'height')
    list_filter = ('format',)
    search_fields = ('filename',)
    raw_id_fields = ('photo',)


admin.site.register(Format, FormatOptions)
admin.site.register(Photo, PhotoOptions)
admin.site.register(FormatedPhoto, FormatedPhotoOptions)

{'fraction_non_alphanumeric': 0.12734452122408688, 'fraction_numerical': 0.0006581112207963146, 'mean_word_length': 3.502962962962963, 'pattern_counts': {'":': 0, '<': 3, '<?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': '470251', 'n_tokens_mistral': 1866, 'n_tokens_neox': 1850, 'n_words': 420}