From c10ef2d78479a8b3fa97a42135343ac7f0a7fbe4 Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Tue, 18 Jul 2023 17:56:23 +0800 Subject: [PATCH] Auto subject adding/dropping when changing year level, semester, or course --- stude/accounts/admin.py | 2 +- stude/accounts/models.py | 3 ++- stude/accounts/serializers.py | 21 +++++++++++++++++++++ stude/subjects/models.py | 12 +++++++++++- 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/stude/accounts/admin.py b/stude/accounts/admin.py index 1eaaa68..37df597 100644 --- a/stude/accounts/admin.py +++ b/stude/accounts/admin.py @@ -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: diff --git a/stude/accounts/models.py b/stude/accounts/models.py index 191ea70..296ac68 100644 --- a/stude/accounts/models.py +++ b/stude/accounts/models.py @@ -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 diff --git a/stude/accounts/serializers.py b/stude/accounts/serializers.py index 7f70f5f..4a9ae36 100644 --- a/stude/accounts/serializers.py +++ b/stude/accounts/serializers.py @@ -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 diff --git a/stude/subjects/models.py b/stude/subjects/models.py index 6b1b801..964c823 100644 --- a/stude/subjects/models.py +++ b/stude/subjects/models.py @@ -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']]