<commit_before>from django.contrib import admin
from advertisements.models import Advertisement, Provider, Click
from django.db.models import Count


def make_enabled(modeladmin, request, queryset):
    # This is the only safe way to update due to the annotate below
    for advert in queryset:
        advert.enabled = True
        advert.save()
make_enabled.short_description = 'Enable the selected advertisements'


def make_disabled(modeladmin, request, queryset):
    # This is the only safe way to update due to the annotate below
    for advert in queryset:
        advert.enabled = False
        advert.save()
make_disabled.short_description = 'Disable the selected advertisements'


class AdvertisementAdmin(admin.ModelAdmin):
    list_display = ('provider', 'ad_type', 'image', 'created', 'enabled', 'total_views')
    date_hierarchy = 'created'
    list_filter = ('ad_type', 'enabled', 'created')
    actions = [make_enabled, make_disabled]

    def total_views(self, obj):
        return obj.click_set.count()
    total_views.admin_order_field = 'clicks'

    def queryset(self, request):
        q = super(AdvertisementAdmin, self).queryset(request)
        q = q.annotate(clicks=Count('click'))
        return q

admin.site.register(Provider)
admin.site.register(Advertisement, AdvertisementAdmin)<commit_msg>Moved bulk editors into ModeAdmin class<commit_after>from django.contrib import admin
from advertisements.models import Advertisement, Provider, Click
from django.db.models import Count


class AdvertisementAdmin(admin.ModelAdmin):
    list_display = ('provider', 'ad_type', 'image', 'created', 'enabled', 'total_views')
    date_hierarchy = 'created'
    list_filter = ('ad_type', 'enabled', 'created')

    def total_views(self, obj):
        return obj.click_set.count()
    total_views.admin_order_field = 'clicks'

    def queryset(self, request):
        q = super(AdvertisementAdmin, self).queryset(request)
        q = q.annotate(clicks=Count('click'))
        return q

    def make_enabled(self, request, queryset):
        # This is the only safe way to update due to the annotate below
        for advert in queryset:
            advert.enabled = True
            advert.save()
    make_enabled.short_description = 'Enable the selected advertisements'

    def make_disabled(self, request, queryset):
        # This is the only safe way to update due to the annotate below
        for advert in queryset:
            advert.enabled = False
            advert.save()
    make_disabled.short_description = 'Disable the selected advertisements'

    actions = [make_enabled, make_disabled]

admin.site.register(Provider)
admin.site.register(Advertisement, AdvertisementAdmin)
{'fraction_non_alphanumeric': 0.09161433321019578, 'fraction_numerical': 0.0, 'mean_word_length': 3.7425569176882663, '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': '1423557', 'n_tokens_mistral': 774, 'n_tokens_neox': 732, 'n_words': 224}