mirror of
https://github.com/lemeow125/StudE-Backend.git
synced 2024-11-16 22:09:25 +08:00
Made group name unique and improved group creation functionality
This commit is contained in:
parent
032ed36af6
commit
c5f04328e5
7 changed files with 38 additions and 24 deletions
|
@ -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:
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
18
stude/study_groups/migrations/0003_alter_studygroup_name.py
Normal file
18
stude/study_groups/migrations/0003_alter_studygroup_name.py
Normal 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),
|
||||
),
|
||||
]
|
|
@ -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)
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue