<commit_before>from datetime import timedelta
from django.contrib import admin
from django.utils.translation import ugettext_lazy as _

from helpers import humanize_duration
from models import CronJobLog


class DurationFilter(admin.SimpleListFilter):
    title = _('duration')
    parameter_name = 'duration'

    def lookups(self, request, model_admin):
        return (
            ('lt_minute', _('< 1 minute')),
            ('lt_hour', _('< 1 hour')),
            ('lt_day', _('< 1 day')),
            ('gte_day', _('>= 1 day')),
        )

    def queryset(self, request, queryset):
        if self.value() == 'lt_minute':
            return queryset.extra(where=["end_time-start_time < %s"], params=[timedelta(minutes=1)])
        if self.value() == 'lt_hour':
            return queryset.extra(where=["end_time-start_time < %s"], params=[timedelta(hours=1)])
        if self.value() == 'lt_day':
            return queryset.extra(where=["end_time-start_time < %s"], params=[timedelta(days=1)])
        if self.value() == 'gte_day':
            return queryset.extra(where=["end_time-start_time >= %s"], params=[timedelta(days=1)])


class CronJobLogAdmin(admin.ModelAdmin):
    class Meta:
        model = CronJobLog

    search_fields = ('code', 'message')
    ordering = ('-start_time',)
    list_display = ('code', 'start_time', 'end_time', 'humanize_duration', 'is_success')
    list_filter = ('code', 'start_time', 'is_success', DurationFilter)

    def get_readonly_fields(self, request, obj=None):
        if not request.user.is_superuser and obj is not None:
            names = [f.name for f in CronJobLog._meta.fields if f.name != 'id']
            return self.readonly_fields + tuple(names)<commit_msg>more useful DurationFilter lookups values<commit_after>from datetime import timedelta
from django.contrib import admin
from django.utils.translation import ugettext_lazy as _

from helpers import humanize_duration
from models import CronJobLog


class DurationFilter(admin.SimpleListFilter):
    title = _('duration')
    parameter_name = 'duration'

    def lookups(self, request, model_admin):
        return (
            ('lte_minute', _('<= 1 minute')),
            ('gt_minute', _('> 1 minute')),
            ('gt_hour', _('> 1 hour')),
            ('gt_day', _('> 1 day')),
        )

    def queryset(self, request, queryset):
        if self.value() == 'lte_minute':
            return queryset.extra(where=["end_time-start_time <= %s"], params=[timedelta(minutes=1)])
        if self.value() == 'gt_minute':
            return queryset.extra(where=["end_time-start_time > %s"], params=[timedelta(minutes=1)])
        if self.value() == 'gt_hour':
            return queryset.extra(where=["end_time-start_time > %s"], params=[timedelta(hours=1)])
        if self.value() == 'gt_day':
            return queryset.extra(where=["end_time-start_time > %s"], params=[timedelta(days=1)])


class CronJobLogAdmin(admin.ModelAdmin):
    class Meta:
        model = CronJobLog

    search_fields = ('code', 'message')
    ordering = ('-start_time',)
    list_display = ('code', 'start_time', 'end_time', 'humanize_duration', 'is_success')
    list_filter = ('code', 'start_time', 'is_success', DurationFilter)

    def get_readonly_fields(self, request, obj=None):
        if not request.user.is_superuser and obj is not None:
            names = [f.name for f in CronJobLog._meta.fields if f.name != 'id']
            return self.readonly_fields + tuple(names)
{'fraction_non_alphanumeric': 0.14589316484778864, 'fraction_numerical': 0.004595060310166571, 'mean_word_length': 3.284132841328413, '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': '6677320', 'n_tokens_mistral': 1110, 'n_tokens_neox': 1066, 'n_words': 247}