-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.py
More file actions
108 lines (87 loc) · 2.64 KB
/
admin.py
File metadata and controls
108 lines (87 loc) · 2.64 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
from django import forms
from django.contrib import admin
from django.contrib.contenttypes.models import ContentType
from django.db.models import Q
from django.utils.html import format_html
from courses.models import Popup, Skill, Task, TaskObject
from questions.models import QuestionSingleAnswer
@admin.register(Skill)
class SkillAdmin(admin.ModelAdmin):
list_display = (
"id",
"name",
"quantity_of_levels",
"status",
"free_access",
)
search_fields = ("name",)
@admin.register(Task)
class TaskAdmin(admin.ModelAdmin):
list_display = (
"id",
"name",
"skill",
"level",
"status",
"week",
"free_access",
)
list_filter = ["week"]
class TaskObjectForm(forms.ModelForm):
content_type = forms.ModelChoiceField(
queryset=ContentType.objects.filter(
Q(app_label="questions", model="infoslide")
| Q(app_label="questions", model="questionconnect")
| Q(app_label="questions", model="questionsingleanswer")
| Q(app_label="questions", model="questionwrite")
),
label="Content Type",
)
class Meta:
model = TaskObject
fields = "__all__"
@admin.register(TaskObject)
class TaskObjectAdmin(admin.ModelAdmin):
form = TaskObjectForm
ordering = ("-task__id",)
filter_horizontal = ("popup",)
list_display = (
"id",
"task_name",
"question_type",
"validate_answer",
"has_popups",
"ordinal_number",
)
list_filter = ["task__name"]
def task_name(self, obj):
return obj.task.name
def question_type(self, obj):
if isinstance(obj.content_object, QuestionSingleAnswer) and obj.content_object.is_exclude:
return f"{obj.content_type} (Исключающий)"
else:
return obj.content_type
def has_popups(self, obj):
return obj.popup.exists()
has_popups.boolean = True
@admin.register(Popup)
class PopupAdmin(admin.ModelAdmin):
list_display = (
"id",
"popup_title",
"popup_text",
"popup_file_link",
"task_object_ids",
)
def popup_title(self, obj):
return self.trim_text(obj.title)
def popup_text(self, obj):
return self.trim_text(obj.text)
def popup_file_link(self, obj):
if obj.file:
return format_html(f'<a href="{obj.file.link}">{obj.file.link}</a>')
return "-"
def task_object_ids(self, obj):
return list(obj.task_objects.values_list("id", flat=True)) or "-"
def trim_text(self, text):
return text[:30] if text else "-"