2023-06-26 22:06:05 +08:00
|
|
|
from django.contrib import admin
|
2023-06-26 23:50:55 +08:00
|
|
|
from .models import StudentStatus
|
2023-07-10 17:06:43 +08:00
|
|
|
from leaflet.admin import LeafletGeoAdmin
|
2023-09-17 19:25:44 +08:00
|
|
|
from django import forms
|
2023-09-17 19:40:27 +08:00
|
|
|
from subjects.models import SubjectInstance, Subject
|
2023-09-17 19:25:44 +08:00
|
|
|
from accounts.models import CustomUser
|
|
|
|
|
|
|
|
|
|
|
|
class CustomStudentStatusForm(forms.ModelForm):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
super(CustomStudentStatusForm, self).__init__(*args, **kwargs)
|
|
|
|
if self.instance:
|
|
|
|
studentstatus = StudentStatus.objects.filter(
|
|
|
|
user=self.instance.user).first()
|
|
|
|
user = CustomUser.objects.filter(
|
|
|
|
id=studentstatus.user.id).first()
|
2023-09-17 19:40:27 +08:00
|
|
|
subject_instances = SubjectInstance.objects.filter(
|
2023-09-17 19:25:44 +08:00
|
|
|
course=user.course)
|
2023-09-17 19:40:27 +08:00
|
|
|
# Get the names of the SubjectInstance objects
|
|
|
|
subject_instance_names = subject_instances.values_list(
|
|
|
|
'subject', flat=True)
|
|
|
|
|
|
|
|
# Filter the Subject objects by these names
|
|
|
|
subjects = Subject.objects.filter(name__in=subject_instance_names)
|
2023-09-17 19:25:44 +08:00
|
|
|
self.fields['subject'].queryset = subjects
|
2023-09-28 21:03:34 +08:00
|
|
|
# To fix: study group is still empty despite student already joined one
|
2023-09-17 19:40:27 +08:00
|
|
|
subject = forms.ModelChoiceField(
|
|
|
|
queryset=Subject.objects.none(), required=False)
|
2023-09-17 19:25:44 +08:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = StudentStatus
|
|
|
|
fields = '__all__'
|
2023-06-26 22:06:05 +08:00
|
|
|
|
2023-07-14 23:55:54 +08:00
|
|
|
|
|
|
|
class StudentStatusAdmin(LeafletGeoAdmin):
|
2023-09-17 19:25:44 +08:00
|
|
|
model = StudentStatus
|
|
|
|
form = CustomStudentStatusForm
|
2023-07-14 23:55:54 +08:00
|
|
|
# define which fields are required
|
2023-09-17 19:25:44 +08:00
|
|
|
|
2023-07-14 23:55:54 +08:00
|
|
|
def get_form(self, request, obj=None, **kwargs):
|
|
|
|
form = super(StudentStatusAdmin, self).get_form(request, obj, **kwargs)
|
|
|
|
form.base_fields['landmark'].required = False
|
|
|
|
return form
|
|
|
|
|
|
|
|
|
|
|
|
# Register the new StudentStatus model
|
|
|
|
admin.site.register(StudentStatus, StudentStatusAdmin)
|