-
Notifications
You must be signed in to change notification settings - Fork 73
/
managers.py
122 lines (100 loc) · 3.72 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
from typing import Any
from django.http import Http404
from django.shortcuts import get_object_or_404
from django.utils.translation import gettext_lazy as _
from exercise.models import (
LearningObject,
CourseChapter,
BaseExercise,
LTIExercise,
StaticExercise,
ExerciseWithAttachment,
LTI1p3Exercise,
)
from course.models import LearningObjectCategory, CourseModule
from .exercise_forms import (
CourseChapterForm,
BaseExerciseForm,
LTIExerciseForm,
ExerciseWithAttachmentForm,
StaticExerciseForm,
ExerciseCollectionExerciseForm,
LTI1p3ExerciseForm,
)
from .course_forms import LearningObjectCategoryForm, CourseModuleForm
from exercise.exercisecollection_models import ExerciseCollection
class ModelManager:
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): # pylint: disable=unused-argument redefined-builtin
return self.object_class(course_instance=instance) # pylint: disable=not-callable
def get_form_class(self, obj): # pylint: disable=unused-argument
return self.form_class
def can_delete(self, obj): # pylint: disable=unused-argument
return True
class ExerciseContainerMixin:
def can_delete(self, obj: Any) -> bool:
return not obj.learning_objects.exists()
class CategoryManager(ExerciseContainerMixin, ModelManager):
object_class = LearningObjectCategory
form_class = LearningObjectCategoryForm
name = _('CATEGORY_lowercase')
class ModuleManager(ExerciseContainerMixin, ModelManager):
object_class = CourseModule
form_class = CourseModuleForm
name = _('MODULE')
def new_object(self, instance, parent_id, type): # pylint: disable=redefined-builtin
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 new_object(self, instance, parent_id, type): # pylint: disable=redefined-builtin
CLASSES = {
None: BaseExercise,
"lti": LTIExercise,
"chapter": CourseChapter,
"static": StaticExercise,
"attachment": ExerciseWithAttachment,
}
if type not 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,
LTI1p3Exercise: LTI1p3ExerciseForm,
StaticExercise: StaticExerciseForm,
ExerciseWithAttachment: ExerciseWithAttachmentForm,
ExerciseCollection: ExerciseCollectionExerciseForm,
}
if obj.__class__ not in FORMS:
raise TypeError("No form known for the object type: %s", # pylint: disable=raising-format-tuple
obj.__class__)
return FORMS[obj.__class__]