mirror of
https://github.com/lemeow125/StudE-Backend.git
synced 2024-11-17 06:19:24 +08:00
Auto subject adding/dropping when changing year level, semester, or course
This commit is contained in:
parent
db8fd8642f
commit
c10ef2d784
4 changed files with 35 additions and 3 deletions
|
@ -16,7 +16,7 @@ class CustomUserForm(forms.ModelForm):
|
|||
course = forms.ModelChoiceField(
|
||||
queryset=Course.objects.all(), required=False)
|
||||
subjects = forms.ModelMultipleChoiceField(
|
||||
queryset=Subject.objects.all(), required=False)
|
||||
queryset=Subject.objects.all(), required=False, widget=forms.CheckboxSelectMultiple)
|
||||
avatar = forms.ImageField(required=False)
|
||||
|
||||
class Meta:
|
||||
|
|
|
@ -82,6 +82,7 @@ def create_superuser(sender, **kwargs):
|
|||
username = 'keannu125'
|
||||
email = os.getenv('DJANGO_ADMIN_EMAIL')
|
||||
password = os.getenv('DJANGO_ADMIN_PASSWORD')
|
||||
student_id_number = '2020300490'
|
||||
first_name = 'Keannu'
|
||||
last_name = 'Bernasol'
|
||||
# course = 'Bachelor of Science in Information Technology'
|
||||
|
@ -91,7 +92,7 @@ def create_superuser(sender, **kwargs):
|
|||
if not User.objects.filter(username=username).exists():
|
||||
# Create the superuser with is_active set to False
|
||||
user = User.objects.create_user(
|
||||
username=username, email=email, password=password, first_name=first_name, last_name=last_name)
|
||||
username=username, email=email, password=password, first_name=first_name, last_name=last_name, student_id_number=student_id_number)
|
||||
|
||||
# Activate the superuser
|
||||
user.is_active = True
|
||||
|
|
|
@ -46,6 +46,27 @@ class CustomUserSerializer(BaseUserSerializer):
|
|||
def get_semester_shortname(self, instance):
|
||||
return instance.semester.shortname if instance.semester else None
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
# First, we'll remove all the existing subjects from the user
|
||||
instance.subjects.clear()
|
||||
print('Clearing user subjects')
|
||||
|
||||
# Update the user instance with the validated data
|
||||
instance = super().update(instance, validated_data)
|
||||
print('Subjects:', instance.subjects)
|
||||
# Next, we'll add new subjects based on the matching criteria
|
||||
self.add_subjects(instance)
|
||||
|
||||
return instance
|
||||
|
||||
def add_subjects(self, instance):
|
||||
# Get the matching subjects based on the user's course, year level, and semester
|
||||
matching_subjects = Subject.objects.filter(
|
||||
courses=instance.course, year_levels=instance.year_level, semesters=instance.semester)
|
||||
# Add the matching subjects to the user's subjects list
|
||||
print('Mathing subjects', matching_subjects)
|
||||
instance.subjects.add(*matching_subjects)
|
||||
|
||||
# The model from your custom user
|
||||
|
||||
|
||||
|
|
|
@ -9,7 +9,8 @@ class Subject(models.Model):
|
|||
code = models.CharField(max_length=16)
|
||||
courses = models.ManyToManyField(
|
||||
'courses.Course', through='subjects.SubjectCourse', related_name='SubjectCourse_subject')
|
||||
students = models.ManyToManyField('accounts.CustomUser', blank=True)
|
||||
students = models.ManyToManyField(
|
||||
'accounts.CustomUser', blank=True)
|
||||
|
||||
year_levels = models.ManyToManyField(
|
||||
'year_levels.Year_Level', through='subjects.SubjectYearLevel', related_name='SubjectYearLevel_subject')
|
||||
|
@ -29,6 +30,9 @@ class SubjectCourse(models.Model):
|
|||
def __str__(self):
|
||||
return f'Subject={self.subject.name}, Course={self.course.name}'
|
||||
|
||||
class Meta:
|
||||
unique_together = [['subject', 'course']]
|
||||
|
||||
|
||||
class SubjectYearLevel(models.Model):
|
||||
subject = models.ForeignKey(
|
||||
|
@ -39,6 +43,9 @@ class SubjectYearLevel(models.Model):
|
|||
def __str__(self):
|
||||
return f'Subject={self.subject.name}, Year Level={self.year_level.name}'
|
||||
|
||||
class Meta:
|
||||
unique_together = [['subject', 'year_level']]
|
||||
|
||||
|
||||
class SubjectSemester(models.Model):
|
||||
subject = models.ForeignKey(
|
||||
|
@ -48,3 +55,6 @@ class SubjectSemester(models.Model):
|
|||
|
||||
def __str__(self):
|
||||
return f'Subject={self.subject.name}, Semester={self.semester.name}'
|
||||
|
||||
class Meta:
|
||||
unique_together = [['subject', 'semester']]
|
||||
|
|
Loading…
Reference in a new issue