mirror of
https://github.com/lemeow125/StudE-Backend.git
synced 2024-11-17 06:19:24 +08:00
Subject selection improvement in admin panel and for irregular students
This commit is contained in:
parent
0fd790505e
commit
accf87e332
2 changed files with 16 additions and 5 deletions
|
@ -9,6 +9,12 @@ from subjects.models import Subject
|
||||||
|
|
||||||
|
|
||||||
class CustomUserForm(forms.ModelForm):
|
class CustomUserForm(forms.ModelForm):
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super(CustomUserForm, self).__init__(*args, **kwargs)
|
||||||
|
if self.instance:
|
||||||
|
self.fields['subjects'].queryset = Subject.objects.filter(
|
||||||
|
course=self.instance.course)
|
||||||
|
|
||||||
year_level = forms.ModelChoiceField(
|
year_level = forms.ModelChoiceField(
|
||||||
queryset=Year_Level.objects.all(), required=False)
|
queryset=Year_Level.objects.all(), required=False)
|
||||||
semester = forms.ModelChoiceField(
|
semester = forms.ModelChoiceField(
|
||||||
|
@ -16,7 +22,7 @@ class CustomUserForm(forms.ModelForm):
|
||||||
course = forms.ModelChoiceField(
|
course = forms.ModelChoiceField(
|
||||||
queryset=Course.objects.all(), required=False)
|
queryset=Course.objects.all(), required=False)
|
||||||
subjects = forms.ModelMultipleChoiceField(
|
subjects = forms.ModelMultipleChoiceField(
|
||||||
queryset=Subject.objects.all(), required=False, widget=forms.CheckboxSelectMultiple)
|
queryset=Subject.objects.none(), required=False, widget=forms.CheckboxSelectMultiple)
|
||||||
avatar = forms.ImageField(required=False)
|
avatar = forms.ImageField(required=False)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
|
|
|
@ -66,9 +66,11 @@ class CustomUserSerializer(BaseUserSerializer):
|
||||||
print(validated_data)
|
print(validated_data)
|
||||||
# If course, year_level, or semester is changed
|
# If course, year_level, or semester is changed
|
||||||
if any(field in validated_data for field in ['course', 'year_level', 'semester']):
|
if any(field in validated_data for field in ['course', 'year_level', 'semester']):
|
||||||
if (instance.course not in validated_data['course'] or
|
if (
|
||||||
instance.year_level not in validated_data['year_level'] or
|
instance.course is not validated_data['course'] or
|
||||||
instance.semester not in validated_data['semester']):
|
instance.year_level is not validated_data['year_level'] or
|
||||||
|
instance.semester is not validated_data['semester'] or
|
||||||
|
instance.irregular is not validated_data['irregular']):
|
||||||
|
|
||||||
# Clear all subjects
|
# Clear all subjects
|
||||||
instance.subjects.clear()
|
instance.subjects.clear()
|
||||||
|
@ -76,10 +78,13 @@ class CustomUserSerializer(BaseUserSerializer):
|
||||||
instance = super().update(instance, validated_data)
|
instance = super().update(instance, validated_data)
|
||||||
# Then add new subjects matching the new criteria
|
# Then add new subjects matching the new criteria
|
||||||
self.add_subjects(instance)
|
self.add_subjects(instance)
|
||||||
|
|
||||||
|
# This is what I want you to do. This comment below! Ignore any other comments
|
||||||
|
# Add another condition here to check if the user recently changed his/her irregular field from true to false. And if it has changed to false from being true, run the same if statement above
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# Else update as usual
|
# Else update as usual
|
||||||
super().update(instance, validated_data)
|
super().update(instance, validated_data)
|
||||||
|
|
||||||
return instance
|
return instance
|
||||||
|
|
||||||
def add_subjects(self, instance):
|
def add_subjects(self, instance):
|
||||||
|
|
Loading…
Reference in a new issue