forked from apluslms/a-plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
managers.py
108 lines (87 loc) · 3.41 KB
/
managers.py
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
from django.shortcuts import get_object_or_404
from django.utils.translation import ugettext_lazy as _
from exercise.models import LearningObject, CourseChapter, \
BaseExercise, LTIExercise, StaticExercise, ExerciseWithAttachment
from course.models import LearningObjectCategory, CourseModule
from .exercise_forms import CourseChapterForm, BaseExerciseForm, \
LTIExerciseForm, ExerciseWithAttachmentForm, StaticExerciseForm, \
ExerciseCollectionExerciseForm
from .course_forms import LearningObjectCategoryForm, CourseModuleForm
from exercise.exercisecollection_models import ExerciseCollection
class ModelManager(object):
object_class = None
instance_field = "course_instance"
form_class = None
name = None
def get_object(self, instance, object_id):
fields = {
"id": object_id,
self.instance_field: instance,
}
return get_object_or_404(self.object_class, **fields)
def new_object(self, instance, parent_id, type):
return self.object_class(course_instance=instance)
def get_form_class(self, obj):
return self.form_class
def can_delete(self, obj):
return True
class ExerciseContainerMixin(object):
def can_delete(self, obj):
return obj.learning_objects.count() == 0
class CategoryManager(ExerciseContainerMixin, ModelManager):
object_class = LearningObjectCategory
form_class = LearningObjectCategoryForm
name = _("category")
class ModuleManager(ExerciseContainerMixin, ModelManager):
object_class = CourseModule
form_class = CourseModuleForm
name = _("module")
def new_object(self, instance, parent_id, type):
return self.object_class(
course_instance=instance,
order=(instance.course_modules.count() + 1)
)
class ExerciseManager(ModelManager):
object_class = LearningObject
instance_field = "course_module__course_instance"
name = _("learning object")
def get_object(self, instance, object_id):
obj = super().get_object(instance, object_id)
return obj.as_leaf_class()
def new_object(self, instance, parent_id, type):
CLASSES = {
None: BaseExercise,
"lti": LTIExercise,
"chapter": CourseChapter,
"static": StaticExercise,
"attachment": ExerciseWithAttachment,
}
if not type in CLASSES:
raise Http404()
object_class = CLASSES[type]
module = get_object_or_404(
CourseModule,
id=parent_id,
course_instance=instance
)
kwargs = {
"course_module": module,
"order": module.learning_objects.filter(parent__isnull=True).count() + 1,
}
first_category = instance.categories.first()
if first_category:
kwargs["category"] = first_category
return object_class(**kwargs)
def get_form_class(self, obj):
FORMS = {
CourseChapter: CourseChapterForm,
BaseExercise: BaseExerciseForm,
LTIExercise: LTIExerciseForm,
StaticExercise: StaticExerciseForm,
ExerciseWithAttachment: ExerciseWithAttachmentForm,
ExerciseCollection: ExerciseCollectionExerciseForm,
}
if obj.__class__ not in FORMS:
raise TypeError("No form known for the object type: %s",
obj.__class__)
return FORMS[obj.__class__]