mirror of
https://github.com/lemeow125/StudE-Backend.git
synced 2024-11-17 06:19:24 +08:00
Improved serializers for filtering nearby students studying
This commit is contained in:
parent
0436f8082f
commit
49afdc981f
4 changed files with 77 additions and 9 deletions
|
@ -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),
|
||||
),
|
||||
]
|
|
@ -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()
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in a new issue