from django.contrib import admin
from django import forms
from django.utils.translation import ugettext_lazy as _

from . import models


class ScheduleExperienceAdminInline(admin.StackedInline):
    model = models.ScheduleExperience


@admin.register(models.Schedule)
class ScheduleAdmin(admin.ModelAdmin):
    list_display = ('username', 'course_id', 'active', 'start', 'upgrade_deadline')
    raw_id_fields = ('enrollment',)
    readonly_fields = ('modified',)
    search_fields = ('enrollment__user__username', 'enrollment__course__id',)
    inlines = (ScheduleExperienceAdminInline,)

    def username(self, obj):
        return obj.enrollment.user.username

    username.short_description = _('Username')

    def course_id(self, obj):
        return obj.enrollment.course_id

    course_id.short_description = _('Course ID')

    def get_queryset(self, request):
        qs = super(ScheduleAdmin, self).get_queryset(request)
        qs = qs.select_related('enrollment', 'enrollment__user')
        return qs


class ScheduleConfigAdminForm(forms.ModelForm):

    def clean_hold_back_ratio(self):
        hold_back_ratio = self.cleaned_data["hold_back_ratio"]
        if hold_back_ratio < 0 or hold_back_ratio > 1:
            raise forms.ValidationError("Invalid hold back ratio, the value must be between 0 and 1.")
        return hold_back_ratio


@admin.register(models.ScheduleConfig)
class ScheduleConfigAdmin(admin.ModelAdmin):
    search_fields = ('site',)
    list_display = (
        'site', 'create_schedules',
        'enqueue_recurring_nudge', 'deliver_recurring_nudge',
        'enqueue_upgrade_reminder', 'deliver_upgrade_reminder',
        'enqueue_course_update', 'deliver_course_update',
        'hold_back_ratio',
    )
    form = ScheduleConfigAdminForm

@@ -383,16 +383,98 @@
 eadline'
+, 'experience_display')%0A    list_filter = ('experience__experience_type', 'active'
 )%0A    ra
@@ -663,16 +663,1869 @@
 Inline,)
+%0A    actions = %5B'deactivate_schedules', 'activate_schedules', 'set_experience_to_default', 'set_experience_to_course_updates'%5D%0A%0A    def deactivate_schedules(self, request, queryset):%0A        rows_updated = queryset.update(active=False)%0A        self.message_user(request, %22%7B%7D schedule(s) were deactivated%22.format(rows_updated))%0A    deactivate_schedules.short_description = %22Deactivate selected schedules%22%0A%0A    def activate_schedules(self, request, queryset):%0A        rows_updated = queryset.update(active=True)%0A        self.message_user(request, %22%7B%7D schedule(s) were activated%22.format(rows_updated))%0A    activate_schedules.short_description = %22Activate selected schedules%22%0A%0A    def set_experience_to_default(self, request, queryset):%0A        rows_updated = models.ScheduleExperience.objects.filter(%0A            schedule__in=list(queryset)%0A        ).update(%0A            experience_type=models.ScheduleExperience.EXPERIENCES.default%0A        )%0A        self.message_user(request, %22%7B%7D schedule(s) were changed to use the default experience%22.format(rows_updated))%0A    set_experience_to_default.short_description = %22Convert the selected schedules to the default experience%22%0A%0A    def set_experience_to_course_updates(self, request, queryset):%0A        rows_updated = models.ScheduleExperience.objects.filter(%0A            schedule__in=list(queryset)%0A        ).update(%0A            experience_type=models.ScheduleExperience.EXPERIENCES.course_updates%0A        )%0A        self.message_user(request, %22%7B%7D schedule(s) were changed to use the course update experience%22.format(rows_updated))%0A    set_experience_to_course_updates.short_description = %22Convert the selected schedules to the course updates experience%22%0A%0A    def experience_display(self, obj):%0A        return obj.experience.get_experience_type_display()%0A    experience_display.short_descriptions = _('Experience')
 %0A%0A    de

Add actions and filtering to schedules admin page to allow it to function better as a support tool