From 49afdc981f2c860be7251c9f9bbcbf35c4beb400 Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Sat, 2 Sep 2023 13:24:55 +0800 Subject: [PATCH] Improved serializers for filtering nearby students studying --- ...0003_alter_customuser_student_id_number.py | 18 ++++++++ stude/accounts/models.py | 41 ++++++++++++++++--- stude/student_status/serializers.py | 19 ++++++++- stude/student_status/views.py | 8 ++-- 4 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 stude/accounts/migrations/0003_alter_customuser_student_id_number.py diff --git a/stude/accounts/migrations/0003_alter_customuser_student_id_number.py b/stude/accounts/migrations/0003_alter_customuser_student_id_number.py new file mode 100644 index 0000000..fc7805a --- /dev/null +++ b/stude/accounts/migrations/0003_alter_customuser_student_id_number.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.3 on 2023-09-02 05:03 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('accounts', '0002_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='customuser', + name='student_id_number', + field=models.IntegerField(unique=True), + ), + ] diff --git a/stude/accounts/models.py b/stude/accounts/models.py index 7182f42..97dcf8f 100644 --- a/stude/accounts/models.py +++ b/stude/accounts/models.py @@ -42,7 +42,7 @@ class CustomUser(AbstractUser): is_student = models.BooleanField(default=True) is_studying = models.BooleanField(default=False) irregular = models.BooleanField(default=False) - student_id_number = models.IntegerField(null=False) + student_id_number = models.IntegerField(null=False, unique=True) avatar = models.ImageField(upload_to=_get_upload_to, null=True) course = models.ForeignKey( Course, @@ -93,9 +93,6 @@ def create_superuser(sender, **kwargs): student_id_number = 2020300490 first_name = 'Keannu' last_name = 'Bernasol' - # course = 'Bachelor of Science in Information Technology' - # year_level = '1st Year' - # semester = '1st Semester' if not User.objects.filter(username=username).exists(): # Create the superuser with is_active set to False @@ -104,5 +101,39 @@ def create_superuser(sender, **kwargs): # Activate the user user.is_active = True - print('Created keannu account') + print('Created keannu125 account') + user.save() + + username = 'keannu126' + email = os.getenv('DJANGO_ADMIN_EMAIL') + password = os.getenv('DJANGO_ADMIN_PASSWORD') + student_id_number = 2020300491 + first_name = 'Keannu2' + last_name = 'Bernasol2' + + 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, student_id_number=student_id_number) + + # Activate the user + user.is_active = True + print('Created keannu126 account') + user.save() + + username = 'keannu127' + email = os.getenv('DJANGO_ADMIN_EMAIL') + password = os.getenv('DJANGO_ADMIN_PASSWORD') + student_id_number = 2020300492 + first_name = 'Keannu3' + last_name = 'Bernasol3' + + 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, student_id_number=student_id_number) + + # Activate the user + user.is_active = True + print('Created keannu127 account') user.save() diff --git a/stude/student_status/serializers.py b/stude/student_status/serializers.py index 59dcbc7..5765c55 100644 --- a/stude/student_status/serializers.py +++ b/stude/student_status/serializers.py @@ -55,8 +55,25 @@ class StudentStatusSerializer(serializers.ModelSerializer): class StudentStatusLocationSerializer(serializers.ModelSerializer): + user = serializers.CharField(source='user.full_name', read_only=True) + subject = serializers.SlugRelatedField( + queryset=Subject.objects.all(), slug_field='name', required=True) location = PointField(required=True) + distance = serializers.SerializerMethodField() + landmark = serializers.SlugRelatedField( + queryset=Landmark.objects.all(), many=False, slug_field='name', required=False, allow_null=True) class Meta: model = StudentStatus - fields = ['location'] + fields = ['user', 'location', 'distance', + 'subject', 'active', 'study_group', 'landmark'] + read_only_fields = ['user', 'distance', 'subject', + 'active', 'study_group', 'landmark'] + + def get_distance(self, obj): + return obj.distance.km + + def to_representation(self, instance): + representation = super().to_representation(instance) + representation['distance'] = self.get_distance(instance) + return representation diff --git a/stude/student_status/views.py b/stude/student_status/views.py index cc6ff18..b7aa8fe 100644 --- a/stude/student_status/views.py +++ b/stude/student_status/views.py @@ -29,8 +29,9 @@ class ActiveStudentStatusListAPIView(generics.ListAPIView): class StudentStatusListByStudentStatusLocation(generics.ListAPIView): - serializer_class = StudentStatusSerializer + serializer_class = StudentStatusLocationSerializer permission_classes = [IsAuthenticated] + http_method_names = ['get'] def get_queryset(self): user = self.request.user @@ -46,7 +47,8 @@ class StudentStatusListByStudentStatusLocation(generics.ListAPIView): class StudentStatusListByCurrentLocation(viewsets.ViewSet): serializer_class = StudentStatusLocationSerializer - # permission_classes = [IsAuthenticated] + permission_classes = [IsAuthenticated] + http_method_names = ['post'] def create(self, request): user = self.request.user @@ -57,5 +59,5 @@ class StudentStatusListByCurrentLocation(viewsets.ViewSet): user_location = fromstr(location_str, srid=4326) queryset = StudentStatus.objects.filter(subject__in=user.subjects.all()).annotate( distance=Distance('location', user_location)).filter(distance__lte=50) - serializer = StudentStatusSerializer(queryset, many=True) + serializer = StudentStatusLocationSerializer(queryset, many=True) return Response(serializer.data)