From c5f04328e57b60d8ae56ee6580b7c15534dd42aa Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Thu, 28 Sep 2023 21:03:34 +0800 Subject: [PATCH] Made group name unique and improved group creation functionality --- stude/schema.yml | 8 +++----- stude/student_status/admin.py | 2 +- stude/student_status/serializers.py | 7 ++++++- .../migrations/0003_alter_studygroup_name.py | 18 ++++++++++++++++++ stude/study_groups/models.py | 2 +- stude/study_groups/serializers.py | 9 +++++---- stude/study_groups/views.py | 16 ++++------------ 7 files changed, 38 insertions(+), 24 deletions(-) create mode 100644 stude/study_groups/migrations/0003_alter_studygroup_name.py diff --git a/stude/schema.yml b/stude/schema.yml index c697ef8..fa8f089 100644 --- a/stude/schema.yml +++ b/stude/schema.yml @@ -1161,7 +1161,7 @@ components: active: type: boolean study_group: - type: integer + type: string nullable: true landmark: type: string @@ -1250,7 +1250,7 @@ components: active: type: boolean study_group: - type: integer + type: string nullable: true landmark: type: string @@ -1278,8 +1278,7 @@ components: type: boolean readOnly: true study_group: - type: integer - readOnly: true + type: string nullable: true landmark: type: string @@ -1288,7 +1287,6 @@ components: - active - distance - location - - study_group - subject - user StudyGroup: diff --git a/stude/student_status/admin.py b/stude/student_status/admin.py index 2b762d6..78241be 100644 --- a/stude/student_status/admin.py +++ b/stude/student_status/admin.py @@ -23,7 +23,7 @@ class CustomStudentStatusForm(forms.ModelForm): # Filter the Subject objects by these names subjects = Subject.objects.filter(name__in=subject_instance_names) self.fields['subject'].queryset = subjects - + # To fix: study group is still empty despite student already joined one subject = forms.ModelChoiceField( queryset=Subject.objects.none(), required=False) diff --git a/stude/student_status/serializers.py b/stude/student_status/serializers.py index c7c68b5..2178deb 100644 --- a/stude/student_status/serializers.py +++ b/stude/student_status/serializers.py @@ -4,6 +4,7 @@ from subjects.models import Subject from django.contrib.gis.geos import Point from drf_extra_fields.geo_fields import PointField from landmarks.models import Landmark +from study_groups.models import StudyGroup class StudentStatusSerializer(serializers.ModelSerializer): @@ -16,6 +17,8 @@ class StudentStatusSerializer(serializers.ModelSerializer): timestamp = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S") active = serializers.BooleanField(required=True) + study_group = serializers.SlugRelatedField(queryset=StudyGroup.objects.all( + ), many=False, slug_field='name', required=False, allow_null=True) class Meta: model = StudentStatus @@ -40,7 +43,7 @@ class StudentStatusSerializer(serializers.ModelSerializer): validated_data['subject'] = None validated_data['landmark'] = None validated_data['study_group'] = None - else: + elif ('active' in validated_data): if 'subject' not in validated_data: raise serializers.ValidationError( {'subject': 'This field may not be empty if active is true'}) @@ -62,6 +65,8 @@ class StudentStatusLocationSerializer(serializers.ModelSerializer): distance = serializers.SerializerMethodField() landmark = serializers.SlugRelatedField( queryset=Landmark.objects.all(), many=False, slug_field='name', required=False, allow_null=True) + study_group = serializers.SlugRelatedField(queryset=StudyGroup.objects.all( + ), many=False, slug_field='name', required=False, allow_null=True) class Meta: model = StudentStatus diff --git a/stude/study_groups/migrations/0003_alter_studygroup_name.py b/stude/study_groups/migrations/0003_alter_studygroup_name.py new file mode 100644 index 0000000..0f87ae2 --- /dev/null +++ b/stude/study_groups/migrations/0003_alter_studygroup_name.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.5 on 2023-09-28 12:25 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('study_groups', '0002_remove_studygroup_active'), + ] + + operations = [ + migrations.AlterField( + model_name='studygroup', + name='name', + field=models.CharField(max_length=48, unique=True), + ), + ] diff --git a/stude/study_groups/models.py b/stude/study_groups/models.py index 239f086..9cfcb20 100644 --- a/stude/study_groups/models.py +++ b/stude/study_groups/models.py @@ -6,7 +6,7 @@ from django.contrib.gis.geos import Point class StudyGroup(models.Model): - name = models.CharField(max_length=48) + name = models.CharField(max_length=48, unique=True) location = gis_models.PointField(blank=True, null=True, srid=4326) subject = models.ForeignKey(Subject, on_delete=models.CASCADE) timestamp = models.DateField(auto_now_add=True) diff --git a/stude/study_groups/serializers.py b/stude/study_groups/serializers.py index caaa3ea..759b49e 100644 --- a/stude/study_groups/serializers.py +++ b/stude/study_groups/serializers.py @@ -55,10 +55,11 @@ class StudyGroupCreateSerializer(serializers.ModelSerializer): def update(self, instance, validated_data): # Check each landmark to see if our location is within it - for landmark in Landmark.objects.all(): - if landmark.location.contains(validated_data['location']): - validated_data['landmark'] = landmark - break + if ('location' in validated_data): + for landmark in Landmark.objects.all(): + if landmark.location.contains(validated_data['location']): + validated_data['landmark'] = landmark + break return super().update(instance, validated_data) class Meta: diff --git a/stude/study_groups/views.py b/stude/study_groups/views.py index 551f1c6..1fa16e2 100644 --- a/stude/study_groups/views.py +++ b/stude/study_groups/views.py @@ -31,11 +31,6 @@ class StudyGroupListView(generics.ListAPIView): def get_queryset(self): user = self.request.user - if not user.is_student: - raise PermissionDenied( - "You must be a student to view study groups" - ) - # Get the user's course user_course = user.course print(user_course) @@ -72,19 +67,16 @@ class StudyGroupListNearView(generics.ListAPIView): queryset = StudyGroup.objects.all() def get_queryset(self): + user = self.request.user user_status = StudentStatus.objects.filter(user=user).first() - user_location = fromstr( - user_status.location, srid=4326) - - if not user.is_student: - raise PermissionDenied( - "You must be a student to view study groups" - ) if user_status.active is False: raise exceptions.ValidationError("Student Status is not active") + user_location = fromstr( + user_status.location, srid=4326) + # Get the user's course user_course = user.course print(user_course)