Admin files refactoring
This commit is contained in:
75
techdb/flightslot/admins/student_adm.py
Normal file
75
techdb/flightslot/admins/student_adm.py
Normal file
@@ -0,0 +1,75 @@
|
||||
from django import forms
|
||||
from django.db.models.query import QuerySet
|
||||
from django.http import HttpRequest
|
||||
from django.contrib import admin, messages
|
||||
from django.utils.safestring import SafeText
|
||||
|
||||
from import_export import fields
|
||||
from import_export.admin import ImportMixin
|
||||
from import_export.resources import ModelResource
|
||||
from import_export.widgets import CharWidget
|
||||
|
||||
from django_admin_action_forms import AdminActionFormsMixin, AdminActionForm, action_with_form
|
||||
|
||||
from ..models.courses import Course
|
||||
from ..models.students import Student
|
||||
|
||||
from ..custom.colortag import course_color
|
||||
|
||||
# Resource Class for Student data import
|
||||
class StudentResource(ModelResource):
|
||||
surname = fields.Field(attribute="surname", column_name="surname", widget=CharWidget())
|
||||
name = fields.Field(attribute="name", column_name="name", widget=CharWidget())
|
||||
email = fields.Field(attribute="email", column_name="email", widget=CharWidget())
|
||||
phone = fields.Field(attribute="phone", column_name="phone", widget=CharWidget())
|
||||
|
||||
# Cleanup fields before entering
|
||||
def before_import_row(self, row: dict[str, str], **kwargs) -> None:
|
||||
row["name"] = SafeText(row["name"].capitalize().strip())
|
||||
row["surname"] = SafeText(row["surname"].capitalize().strip())
|
||||
row["phone"] = SafeText(row["phone"].replace(" ",""))
|
||||
row["email"] = SafeText(row["email"].lower().strip())
|
||||
return super().before_import_row(row, **kwargs)
|
||||
|
||||
class Meta:
|
||||
model = Student
|
||||
skip_unchanged = True
|
||||
report_skipped = True
|
||||
fields = ("surname", "name", "email", "phone")
|
||||
import_id_fields = ("email", "phone")
|
||||
|
||||
# Form Class for Student course change
|
||||
class ChangeCourseForm(AdminActionForm):
|
||||
course = forms.ModelChoiceField(queryset=Course.objects.all())
|
||||
|
||||
class StudentAdmin(ImportMixin, AdminActionFormsMixin, admin.ModelAdmin):
|
||||
list_display = ("surname", "name", "course", "course_color", "email", "phone", "password", "active")
|
||||
list_filter = ("course", "active")
|
||||
actions = ("change_course", "disable_students")
|
||||
resource_classes = [StudentResource]
|
||||
|
||||
@admin.display(description="Color")
|
||||
def course_color(self, obj: Student) -> SafeText:
|
||||
if not obj.course:
|
||||
return SafeText("")
|
||||
return course_color(obj.course.color)
|
||||
|
||||
@admin.display(description="Password")
|
||||
def password(self, obj: Student) -> SafeText:
|
||||
return SafeText(obj.default_password())
|
||||
|
||||
@admin.action(description="Disable Students")
|
||||
def disable_students(self, request: HttpRequest, queryset: QuerySet[Student]):
|
||||
for q in queryset.all():
|
||||
if q.user:
|
||||
q.user.is_staff = False
|
||||
q.user.save()
|
||||
count: int = queryset.update(active = False)
|
||||
messages.success(request, f"{count} students deactivated")
|
||||
pass
|
||||
|
||||
@action_with_form(ChangeCourseForm, description="Change Student Course")
|
||||
def change_course(self, request: HttpRequest, queryset: QuerySet[Student], data):
|
||||
course = data["course"]
|
||||
count: int = queryset.update(course=course)
|
||||
messages.success(request, f"{count} students updated to {course}")
|
||||
Reference in New Issue
Block a user