-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathcourse.py
More file actions
128 lines (105 loc) · 4.06 KB
/
course.py
File metadata and controls
128 lines (105 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from django import forms
from django.contrib import admin
from django.utils import timezone
from unfold.admin import ModelAdmin, TabularInline
from unfold.widgets import (
UnfoldAdminTextInputWidget,
UnfoldAdminTextareaWidget,
)
from django.contrib import messages
from courses.models import Course, ReviewCriteria, CourseRegistration
from courses.scoring import update_leaderboard
class CriteriaForm(forms.ModelForm):
class Meta:
model = ReviewCriteria
fields = "__all__"
widgets = {
"description": UnfoldAdminTextInputWidget(
attrs={"size": "60"}
),
"options": UnfoldAdminTextareaWidget(
attrs={"cols": 60, "rows": 4}
),
}
class CriteriaInline(TabularInline):
model = ReviewCriteria
form = CriteriaForm
extra = 0
def update_leaderboard_admin(modeladmin, request, queryset):
for course in queryset:
update_leaderboard(course)
modeladmin.message_user(
request,
f"Leaderboard updated for course {course}",
level=messages.SUCCESS,
)
update_leaderboard_admin.short_description = "Update leaderboard"
def duplicate_course(modeladmin, request, queryset):
current_year = timezone.now().year
for course in queryset:
# Create a new course instance
old_title = course.title
new_title = old_title.replace(
str(current_year - 1), str(current_year)
)
if str(current_year - 1) not in old_title:
new_title = f"{old_title} {current_year}"
old_slug = course.slug
new_slug = old_slug.replace(
str(current_year - 1), str(current_year)
)
if str(current_year - 1) not in old_slug:
new_slug = f"{old_slug}-{current_year}"
# Create new course with updated fields
new_course = Course.objects.create(
title=new_title,
slug=new_slug,
description=course.description,
social_media_hashtag=course.social_media_hashtag,
first_homework_scored=False,
finished=False,
faq_document_url=course.faq_document_url,
project_passing_score=course.project_passing_score,
)
# Copy review criteria with all fields
for criteria in course.reviewcriteria_set.all():
ReviewCriteria.objects.create(
course=new_course,
description=criteria.description,
options=criteria.options,
review_criteria_type=criteria.review_criteria_type,
)
modeladmin.message_user(
request,
f"Course '{old_title}' was duplicated to '{new_title}'",
level=messages.SUCCESS,
)
duplicate_course.short_description = "Duplicate selected courses"
@admin.register(Course)
class CourseAdmin(ModelAdmin):
actions = [update_leaderboard_admin, duplicate_course]
inlines = [CriteriaInline]
list_display = ["title", "state"]
list_filter = ["state", "finished"]
fieldsets = (
("Basic Information", {
"fields": ("slug", "title", "description", "social_media_hashtag")
}),
("Course State", {
"fields": ("state", "finished", "first_homework_scored")
}),
("Landing Page Content", {
"fields": ("about_content", "video_url", "hero_image_url", "meta_description", "mailchimp_tag"),
"classes": ("collapse",)
}),
("Settings", {
"fields": ("faq_document_url", "min_projects_to_pass", "project_passing_score", "homework_problems_comments_field")
}),
)
@admin.register(CourseRegistration)
class CourseRegistrationAdmin(ModelAdmin):
list_display = ["name", "email", "course", "country", "role", "registered_at", "mailchimp_subscribed"]
list_filter = ["course", "role", "country", "mailchimp_subscribed", "registered_at"]
search_fields = ["name", "email", "comment"]
readonly_fields = ["registered_at", "mailchimp_subscribed"]
date_hierarchy = "registered_at"