Bulk import students, bulk change course
This commit is contained in:
@@ -1,15 +1,21 @@
|
||||
from django import forms
|
||||
from django.db.models.query import QuerySet
|
||||
from django.http import HttpRequest, HttpResponse
|
||||
from django.contrib import admin
|
||||
from django.contrib import messages
|
||||
from django.contrib import admin, messages
|
||||
from django.utils.translation import ngettext
|
||||
from django.utils.safestring import SafeText
|
||||
from durationwidget.widgets import TimeDurationWidget
|
||||
from datetime import date
|
||||
|
||||
import nested_admin
|
||||
|
||||
from durationwidget.widgets import TimeDurationWidget
|
||||
|
||||
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.hourbuildings import HourBuilding, HourBuildingLeg
|
||||
from .models.missions import Training, MissionProfile
|
||||
@@ -17,10 +23,10 @@ from .models.students import Student
|
||||
from .models.weekpref import WeekPreference
|
||||
|
||||
from .custom.colortag import course_color
|
||||
from .custom.defpassword import default_password
|
||||
|
||||
from .actions.exportweek import export_selected
|
||||
|
||||
from datetime import date
|
||||
|
||||
class TrainingForm(forms.ModelForm):
|
||||
model=Training
|
||||
|
||||
@@ -198,10 +204,38 @@ class WeekPreferenceAdmin(nested_admin.NestedModelAdmin):
|
||||
obj.student = request.user.student
|
||||
super().save_model(request, obj, form, change)
|
||||
|
||||
class StudentAdmin(admin.ModelAdmin):
|
||||
|
||||
# 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 = ("disable_students",)
|
||||
actions = ("change_course", "disable_students")
|
||||
resource_classes = [StudentResource]
|
||||
|
||||
@admin.display(description="Color")
|
||||
def course_color(self, obj: Student) -> SafeText:
|
||||
@@ -211,12 +245,23 @@ class StudentAdmin(admin.ModelAdmin):
|
||||
|
||||
@admin.display(description="Password")
|
||||
def password(self, obj: Student) -> SafeText:
|
||||
return SafeText(default_password(student=obj))
|
||||
return SafeText(obj.default_password())
|
||||
|
||||
@admin.action(description="Disable Students")
|
||||
def disable_students(modeladmin, request: HttpRequest, queryset: QuerySet[Student]):
|
||||
queryset.update(active = False)
|
||||
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}")
|
||||
|
||||
class CourseAdmin(admin.ModelAdmin):
|
||||
list_display = ("ctype", "cnumber","color_display", "year")
|
||||
|
||||
Reference in New Issue
Block a user