Commit 3ff118ea by Calen Pennington

Dynamically generate the actions to update to specific experiences

parent 6756f97d
import functools
from django.contrib import admin
from django import forms
from django.utils.translation import ugettext_lazy as _
......@@ -9,6 +11,36 @@ class ScheduleExperienceAdminInline(admin.StackedInline):
model = models.ScheduleExperience
def _set_experience(db_name, human_name, modeladmin, request, queryset):
"""
A django action which will set all selected schedules to the supplied experience.
The intended usage is with functools.partial to generate the action for each experience type
dynamically.
Arguments:
db_name: the database name of the experience being selected
human_name: the human name of the experience being selected
modeladmin: The ModelAdmin subclass, passed by django as part of the standard Action interface
request: The current request, passed by django as part of the standard Action interface
queryset: The queryset selecting schedules, passed by django as part of the standard Action interface
"""
rows_updated = models.ScheduleExperience.objects.filter(
schedule__in=list(queryset)
).update(
experience_type=db_name
)
modeladmin.message_user(request, "{} schedule(s) were changed to use the {} experience".format(rows_updated, human_name))
# Generate a list of all "set_experience_to_X" actions
experience_actions = []
for (db_name, human_name) in models.ScheduleExperience.EXPERIENCES:
partial = functools.partial(_set_experience, db_name, human_name)
partial.short_description = "Convert the selected schedules to the {} experience".format(human_name)
partial.__name__ = "set_experience_to_{}".format(db_name)
experience_actions.append(partial)
@admin.register(models.Schedule)
class ScheduleAdmin(admin.ModelAdmin):
list_display = ('username', 'course_id', 'active', 'start', 'upgrade_deadline', 'experience_display')
......@@ -17,7 +49,7 @@ class ScheduleAdmin(admin.ModelAdmin):
readonly_fields = ('modified',)
search_fields = ('enrollment__user__username', 'enrollment__course__id',)
inlines = (ScheduleExperienceAdminInline,)
actions = ['deactivate_schedules', 'activate_schedules', 'set_experience_to_default', 'set_experience_to_course_updates']
actions = ['deactivate_schedules', 'activate_schedules'] + experience_actions
def deactivate_schedules(self, request, queryset):
rows_updated = queryset.update(active=False)
......@@ -29,24 +61,6 @@ class ScheduleAdmin(admin.ModelAdmin):
self.message_user(request, "{} schedule(s) were activated".format(rows_updated))
activate_schedules.short_description = "Activate selected schedules"
def set_experience_to_default(self, request, queryset):
rows_updated = models.ScheduleExperience.objects.filter(
schedule__in=list(queryset)
).update(
experience_type=models.ScheduleExperience.EXPERIENCES.default
)
self.message_user(request, "{} schedule(s) were changed to use the default experience".format(rows_updated))
set_experience_to_default.short_description = "Convert the selected schedules to the default experience"
def set_experience_to_course_updates(self, request, queryset):
rows_updated = models.ScheduleExperience.objects.filter(
schedule__in=list(queryset)
).update(
experience_type=models.ScheduleExperience.EXPERIENCES.course_updates
)
self.message_user(request, "{} schedule(s) were changed to use the course update experience".format(rows_updated))
set_experience_to_course_updates.short_description = "Convert the selected schedules to the course updates experience"
def experience_display(self, obj):
return obj.experience.get_experience_type_display()
experience_display.short_descriptions = _('Experience')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment