Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Improve Django admin pages #2296

Merged
merged 2 commits into from
May 8, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 75 additions & 12 deletions portal/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,46 @@
from django.http import HttpResponse
from import_export.admin import ExportActionMixin

from portal.forms.admin import AdminChangeUserPasswordForm, AdminUserCreationForm
from portal.forms.admin import (
AdminChangeUserPasswordForm,
AdminUserCreationForm,
)
from portal.views.api import anonymise


class ClassAdmin(admin.ModelAdmin, ExportActionMixin):
search_fields = ["name", "teacher__new_user__first_name", "teacher__new_user__last_name", "teacher__school__name"]
list_display = ["__str__", "teacher", "teacher_school"]
search_fields = [
"name",
"teacher__new_user__first_name",
"teacher__new_user__last_name",
"teacher__school__name",
]
list_display = [
"__str__",
"teacher",
"teacher_school",
"number_of_students",
"id",
]
autocomplete_fields = ["teacher"]

def teacher_school(self, obj):
return obj.teacher.school

def number_of_students(self, obj):
return len(obj.students.all())


class SchoolAdmin(admin.ModelAdmin, ExportActionMixin):
search_fields = ["name", "country", "county"]
list_filter = ["county", "country"]
list_display = ["__str__", "country", "county", "number_of_teachers", "number_of_classes"]
list_display = [
"__str__",
"country",
"county",
"number_of_teachers",
"number_of_classes",
]

def number_of_teachers(self, obj):
return len(obj.teacher_school.all())
Expand All @@ -52,7 +75,13 @@ class StudentAdmin(admin.ModelAdmin, ExportActionMixin):
"class_field__teacher__new_user__last_name",
"class_field__teacher__school__name",
]
list_display = ["__str__", "class_field", "class_field_teacher", "class_field_school", "new_user"]
list_display = [
"__str__",
"class_field",
"class_field_teacher",
"class_field_school",
"new_user",
]
readonly_fields = ["user", "new_user", "login_id"]
autocomplete_fields = ["class_field", "pending_class_request"]

Expand All @@ -70,18 +99,45 @@ def class_field_school(self, obj):


class TeacherAdmin(admin.ModelAdmin, ExportActionMixin):
search_fields = ["new_user__first_name", "new_user__last_name", "school__name", "new_user__username"]
list_display = ["__str__", "school", "new_user", "is_admin"]
search_fields = [
"new_user__first_name",
"new_user__last_name",
"school__name",
"new_user__username",
]
list_display = [
"__str__",
"school",
"new_user",
"number_of_classes",
"is_admin",
]
readonly_fields = ["user", "new_user"]
autocomplete_fields = ["school", "invited_by"]

def number_of_classes(self, obj):
return len(obj.class_teacher.all())


class UserProfileAdmin(admin.ModelAdmin, ExportActionMixin):
search_fields = ["user__first_name", "user__last_name", "user__username", "user__date_joined"]
search_fields = [
"id",
"user__first_name",
"user__last_name",
"user__username",
"user__date_joined",
]
list_filter = ["user__date_joined"]
list_display = ["user", "__str__", "is_verified"]
list_display = ["user", "__str__", "is_verified", "type", "id"]
readonly_fields = ["user"]

def type(self, obj):
if hasattr(obj, "student"):
if obj.student.class_field is None:
return "INDEPENDENT"
return "STUDENT"
return "TEACHER"


class SchoolTeacherInvitationAdmin(admin.ModelAdmin, ExportActionMixin):
search_fields = [
Expand All @@ -103,6 +159,11 @@ def has_delete_permission(self, request, obj=None):
return False


class DailyActivityAdmin(admin.ModelAdmin, ExportActionMixin):
search_fields = ["date"]
list_display = ["__str__", "date"]


class TotalActivityAdmin(admin.ModelAdmin, ExportActionMixin):
def has_add_permission(self, request):
return False
Expand All @@ -118,7 +179,9 @@ def anonymise_user(user_admin, request, queryset):

def export_as_csv(self, request, queryset):
meta = self.model._meta
field_names = [field.name for field in meta.fields if field.name != "password"]
field_names = [
field.name for field in meta.fields if field.name != "password"
]

response = HttpResponse(content_type="text/csv")
response["Content-Disposition"] = "attachment; filename={}.csv".format(meta)
Expand All @@ -135,7 +198,7 @@ def export_as_csv(self, request, queryset):
export_as_csv.short_description = "Export selected users data as CSV"


UserAdmin.list_display += ("date_joined",)
UserAdmin.list_display += ("date_joined", "id")
UserAdmin.list_filter += ("date_joined",)
UserAdmin.add_form = AdminUserCreationForm
UserAdmin.change_password_form = AdminChangeUserPasswordForm
Expand All @@ -152,5 +215,5 @@ def export_as_csv(self, request, queryset):
admin.site.register(UserProfile, UserProfileAdmin)
admin.site.register(SchoolTeacherInvitation, SchoolTeacherInvitationAdmin)
admin.site.register(DynamicElement, DynamicElementAdmin)
admin.site.register(DailyActivity)
admin.site.register(DailyActivity, DailyActivityAdmin)
admin.site.register(TotalActivity, TotalActivityAdmin)
Loading