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

from models import (BroadcastFormat, MusicFocus, ShowInformation, ShowTopic,
                    Host, Note, ProgramSlot, Show, TimeSlot)
from forms import MusicFocusForm

from datetime import date


class BroadcastFormatAdmin(admin.ModelAdmin):
    list_display = ('format', 'enabled', 'admin_color')
    prepopulated_fields = {'slug': ('format',)}


class MusicFocusAdmin(admin.ModelAdmin):
    form = MusicFocusForm
    list_display = ('focus', 'abbrev', 'admin_buttons')
    prepopulated_fields = {'slug': ('focus',)}


class ShowInformationAdmin(admin.ModelAdmin):
    list_display = ('information', 'abbrev', 'admin_buttons')
    prepopulated_fields = {'slug': ('information',)}


class ShowTopicAdmin(admin.ModelAdmin):
    list_display = ('topic', 'abbrev', 'admin_buttons')
    prepopulated_fields = {'slug': ('topic',)}


class NoteAdmin(admin.ModelAdmin):
    date_hierarchy = 'start'
    list_display = ('title', 'show', 'start', 'status')
    list_filter = ('status',)
    ordering = ('timeslot',)

    def queryset(self, request):
        shows = request.user.shows.all()
        return super(NoteAdmin, self).queryset(request).filter(show__in=shows)

    def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
        if db_field.name == 'timeslot':
            shows = request.user.shows.all()
            kwargs['queryset'] = TimeSlot.objects.filter(show__in=shows)

        return super(NoteAdmin, self).formfield_for_foreignkey(db_field,
                                                               request,
                                                               **kwargs)

    def save_model(self, request, obj, form, change):
        obj.save()


class TimeSlotInline(admin.TabularInline):
    model = TimeSlot


def renew(modeladmin, request, queryset):
    next_year = date.today().year + 1
    queryset.update(until=date(next_year, 12, 31))


renew.short_description = _("Renew selected time slots")


class ProgramSlotAdmin(admin.ModelAdmin):
    actions = (renew,)
    inlines = (TimeSlotInline,)
    list_display = ('show', 'byweekday', 'rrule', 'tstart', 'tend', 'until',
                    'timeslot_count')
    list_filter = ('byweekday', 'rrule', 'is_repetition')
    ordering = ('byweekday', 'dstart')
    save_on_top = True
    search_fields = ('show__name',)


class ProgramSlotInline(admin.TabularInline):
    model = ProgramSlot


class ShowAdmin(admin.ModelAdmin):
    filter_horizontal = ('hosts', 'owners', 'musicfocus', 'showinformation',
                         'showtopic')
    inlines = (ProgramSlotInline,)
    list_display = ('name', 'short_description', 'broadcastformat',
                    'has_active_programslots')<commit_msg>added save_as to NoteAdmin and limited time slot to the last four weeks. made renew action inside a method<commit_after>from django.contrib import admin
from django.utils.translation import ugettext_lazy as _

from models import (BroadcastFormat, MusicFocus, ShowInformation, ShowTopic,
                    Host, Note, ProgramSlot, Show, TimeSlot)
from forms import MusicFocusForm

from datetime import date, datetime, timedelta


class BroadcastFormatAdmin(admin.ModelAdmin):
    list_display = ('format', 'enabled', 'admin_color')
    prepopulated_fields = {'slug': ('format',)}


class MusicFocusAdmin(admin.ModelAdmin):
    form = MusicFocusForm
    list_display = ('focus', 'abbrev', 'admin_buttons')
    prepopulated_fields = {'slug': ('focus',)}


class ShowInformationAdmin(admin.ModelAdmin):
    list_display = ('information', 'abbrev', 'admin_buttons')
    prepopulated_fields = {'slug': ('information',)}


class ShowTopicAdmin(admin.ModelAdmin):
    list_display = ('topic', 'abbrev', 'admin_buttons')
    prepopulated_fields = {'slug': ('topic',)}


class NoteAdmin(admin.ModelAdmin):
    date_hierarchy = 'start'
    list_display = ('title', 'show', 'start', 'status')
    list_filter = ('status',)
    ordering = ('timeslot',)
    save_as = True

    def queryset(self, request):
        shows = request.user.shows.all()
        return super(NoteAdmin, self).queryset(request).filter(show__in=shows)

    def formfield_for_foreignkey(self, db_field, request=None, **kwargs):
        four_weeks = datetime.now() - timedelta(weeks=4)
        if db_field.name == 'timeslot':
            shows = request.user.shows.all()
            kwargs['queryset'] = TimeSlot.objects.filter(show__in=shows,
                                                         start__gt=four_weeks)

        return super(NoteAdmin, self).formfield_for_foreignkey(db_field,
                                                               request,
                                                               **kwargs)

    def save_model(self, request, obj, form, change):
        obj.save()


class TimeSlotInline(admin.TabularInline):
    model = TimeSlot


class ProgramSlotAdmin(admin.ModelAdmin):
    actions = ('renew',)
    inlines = (TimeSlotInline,)
    list_display = ('show', 'byweekday', 'rrule', 'tstart', 'tend', 'until',
                    'timeslot_count')
    list_filter = ('byweekday', 'rrule', 'is_repetition')
    ordering = ('byweekday', 'dstart')
    save_on_top = True
    search_fields = ('show__name',)

    def renew(self, request, queryset):
        next_year = date.today().year + 1
        until = date(next_year, 12, 31)
        renewed = queryset.update(until=until)
        if renewed == 1:
            message = _("1 program slot was renewed until %s") % until
        else:
            message = _("%s program slots were renewed until %s") % until
        self.message_user(request, message)
    renew.short_description = _("Renew selected program slots")


class ProgramSlotInline(admin.TabularInline):
    model = ProgramSlot


class ShowAdmin(admin.ModelAdmin):
    filter_horizontal = ('hosts', 'owners', 'musicfocus', 'showinformation',
                         'showtopic')
    inlines = (ProgramSlotInline,)
    list_display = ('name', 'short_description', 'broadcastformat',
                    'has_active_programslots')
{'fraction_non_alphanumeric': 0.1235590193213184, 'fraction_numerical': 0.0021107322617308004, 'mean_word_length': 3.041994750656168, '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': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6766863', 'n_tokens_mistral': 1792, 'n_tokens_neox': 1733, 'n_words': 402}