-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathadmin.py
More file actions
115 lines (75 loc) · 2.83 KB
/
Copy pathadmin.py
File metadata and controls
115 lines (75 loc) · 2.83 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
from django.contrib import admin
from django.contrib.admin.sites import NotRegistered
from polymorphic.admin import PolymorphicChildModelAdmin, PolymorphicParentModelAdmin
from dojo.models import (
Answer,
Answered_Survey,
Choice,
ChoiceAnswer,
ChoiceQuestion,
Engagement_Survey,
Question,
TextAnswer,
TextQuestion,
)
# Django's default admin gate is `is_active and is_staff`. Under the legacy
# OS auth model is_staff is already a near-superuser bypass for queryset
# filters and most permission checks, so the standard UserAdmin change form
# would let any is_staff user with auth.change_user tick is_superuser on
# themselves. Rather than narrowing each ModelAdmin individually, restrict
# the entire admin site to superusers — there is no DefectDojo OS role that
# legitimately needs /admin/ access without being a superuser.
def _admin_site_has_permission(request):
return request.user.is_active and request.user.is_superuser
admin.site.has_permission = _admin_site_has_permission
# Conditionally unregister LogEntry from auditlog if it's registered
try:
from auditlog.models import LogEntry
admin.site.unregister(LogEntry)
except (ImportError, NotRegistered):
# auditlog not available or LogEntry not registered
pass
# ==============================
# Defect Dojo Engaegment Surveys
# ==============================
class QuestionChildAdmin(PolymorphicChildModelAdmin):
"""Base admin class for all child models of Question"""
base_model = Question
class TextQuestionAdmin(QuestionChildAdmin):
"""ModelAdmin for a TextQuestion"""
class ChoiceQuestionAdmin(QuestionChildAdmin):
"""ModelAdmin for a ChoiceQuestion"""
class QuestionParentAdmin(PolymorphicParentModelAdmin):
"""Question parent model admin"""
base_model = Question
child_models = (
TextQuestion,
ChoiceQuestion,
)
admin.site.register(TextQuestion, TextQuestionAdmin)
admin.site.register(ChoiceQuestion, ChoiceQuestionAdmin)
admin.site.register(Question, QuestionParentAdmin)
admin.site.register(Choice)
class AnswerChildAdmin(PolymorphicChildModelAdmin):
"""Base admin class for all child Answer models"""
base_model = Answer
class TextAnswerAdmin(AnswerChildAdmin):
"""ModelAdmin for TextAnswer"""
class ChoiceAnswerAdmin(AnswerChildAdmin):
"""ModelAdmin for ChoiceAnswer"""
class AnswerParentAdmin(PolymorphicParentModelAdmin):
"""The parent model admin for answer"""
list_display = (
"answered_survey",
"question",
)
base_model = Answer
child_models = (
TextAnswer,
ChoiceAnswer,
)
admin.site.register(TextAnswer, TextAnswerAdmin)
admin.site.register(ChoiceAnswer, ChoiceAnswerAdmin)
admin.site.register(Answer, AnswerParentAdmin)
admin.site.register(Engagement_Survey)
admin.site.register(Answered_Survey)