Made group name unique and improved group creation functionality

This commit is contained in:
Keannu Bernasol 2023-09-28 21:03:34 +08:00
parent 032ed36af6
commit c5f04328e5
7 changed files with 38 additions and 24 deletions

View file

@ -1161,7 +1161,7 @@ components:
active: active:
type: boolean type: boolean
study_group: study_group:
type: integer type: string
nullable: true nullable: true
landmark: landmark:
type: string type: string
@ -1250,7 +1250,7 @@ components:
active: active:
type: boolean type: boolean
study_group: study_group:
type: integer type: string
nullable: true nullable: true
landmark: landmark:
type: string type: string
@ -1278,8 +1278,7 @@ components:
type: boolean type: boolean
readOnly: true readOnly: true
study_group: study_group:
type: integer type: string
readOnly: true
nullable: true nullable: true
landmark: landmark:
type: string type: string
@ -1288,7 +1287,6 @@ components:
- active - active
- distance - distance
- location - location
- study_group
- subject - subject
- user - user
StudyGroup: StudyGroup:

View file

@ -23,7 +23,7 @@ class CustomStudentStatusForm(forms.ModelForm):
# Filter the Subject objects by these names # Filter the Subject objects by these names
subjects = Subject.objects.filter(name__in=subject_instance_names) subjects = Subject.objects.filter(name__in=subject_instance_names)
self.fields['subject'].queryset = subjects self.fields['subject'].queryset = subjects
# To fix: study group is still empty despite student already joined one
subject = forms.ModelChoiceField( subject = forms.ModelChoiceField(
queryset=Subject.objects.none(), required=False) queryset=Subject.objects.none(), required=False)

View file

@ -4,6 +4,7 @@ from subjects.models import Subject
from django.contrib.gis.geos import Point from django.contrib.gis.geos import Point
from drf_extra_fields.geo_fields import PointField from drf_extra_fields.geo_fields import PointField
from landmarks.models import Landmark from landmarks.models import Landmark
from study_groups.models import StudyGroup
class StudentStatusSerializer(serializers.ModelSerializer): class StudentStatusSerializer(serializers.ModelSerializer):
@ -16,6 +17,8 @@ class StudentStatusSerializer(serializers.ModelSerializer):
timestamp = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S") timestamp = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S")
active = serializers.BooleanField(required=True) 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: class Meta:
model = StudentStatus model = StudentStatus
@ -40,7 +43,7 @@ class StudentStatusSerializer(serializers.ModelSerializer):
validated_data['subject'] = None validated_data['subject'] = None
validated_data['landmark'] = None validated_data['landmark'] = None
validated_data['study_group'] = None validated_data['study_group'] = None
else: elif ('active' in validated_data):
if 'subject' not in validated_data: if 'subject' not in validated_data:
raise serializers.ValidationError( raise serializers.ValidationError(
{'subject': 'This field may not be empty if active is true'}) {'subject': 'This field may not be empty if active is true'})
@ -62,6 +65,8 @@ class StudentStatusLocationSerializer(serializers.ModelSerializer):
distance = serializers.SerializerMethodField() distance = serializers.SerializerMethodField()
landmark = serializers.SlugRelatedField( landmark = serializers.SlugRelatedField(
queryset=Landmark.objects.all(), many=False, slug_field='name', required=False, allow_null=True) 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: class Meta:
model = StudentStatus model = StudentStatus

View file

@ -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),
),
]

View file

@ -6,7 +6,7 @@ from django.contrib.gis.geos import Point
class StudyGroup(models.Model): 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) location = gis_models.PointField(blank=True, null=True, srid=4326)
subject = models.ForeignKey(Subject, on_delete=models.CASCADE) subject = models.ForeignKey(Subject, on_delete=models.CASCADE)
timestamp = models.DateField(auto_now_add=True) timestamp = models.DateField(auto_now_add=True)

View file

@ -55,10 +55,11 @@ class StudyGroupCreateSerializer(serializers.ModelSerializer):
def update(self, instance, validated_data): def update(self, instance, validated_data):
# Check each landmark to see if our location is within it # Check each landmark to see if our location is within it
for landmark in Landmark.objects.all(): if ('location' in validated_data):
if landmark.location.contains(validated_data['location']): for landmark in Landmark.objects.all():
validated_data['landmark'] = landmark if landmark.location.contains(validated_data['location']):
break validated_data['landmark'] = landmark
break
return super().update(instance, validated_data) return super().update(instance, validated_data)
class Meta: class Meta:

View file

@ -31,11 +31,6 @@ class StudyGroupListView(generics.ListAPIView):
def get_queryset(self): def get_queryset(self):
user = self.request.user 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 # Get the user's course
user_course = user.course user_course = user.course
print(user_course) print(user_course)
@ -72,19 +67,16 @@ class StudyGroupListNearView(generics.ListAPIView):
queryset = StudyGroup.objects.all() queryset = StudyGroup.objects.all()
def get_queryset(self): def get_queryset(self):
user = self.request.user user = self.request.user
user_status = StudentStatus.objects.filter(user=user).first() 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: if user_status.active is False:
raise exceptions.ValidationError("Student Status is not active") raise exceptions.ValidationError("Student Status is not active")
user_location = fromstr(
user_status.location, srid=4326)
# Get the user's course # Get the user's course
user_course = user.course user_course = user.course
print(user_course) print(user_course)