From 36e6f729fe8619e108f426eb1cb014751dbd744d Mon Sep 17 00:00:00 2001 From: SKairinos Date: Fri, 27 Sep 2024 10:10:50 +0000 Subject: [PATCH 1/2] fix tests --- codeforlife/user/migrations/0001_initial.py | 6 +++--- codeforlife/user/models/otp_bypass_token.py | 3 ++- codeforlife/user/views/user.py | 4 ++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/codeforlife/user/migrations/0001_initial.py b/codeforlife/user/migrations/0001_initial.py index 2b700d29..8ab186a3 100644 --- a/codeforlife/user/migrations/0001_initial.py +++ b/codeforlife/user/migrations/0001_initial.py @@ -1,4 +1,4 @@ -# Generated by Django 3.2.24 on 2024-04-15 10:04 +# Generated by Django 3.2.25 on 2024-09-27 10:06 import codeforlife.user.models.user import django.contrib.auth.models @@ -12,7 +12,7 @@ class Migration(migrations.Migration): dependencies = [ ('auth', '0012_alter_user_first_name_max_length'), - ('common', '0048_unique_school_names'), + ('common', '0054_delete_aimmo_models'), ] operations = [ @@ -81,7 +81,7 @@ class Migration(migrations.Migration): name='OtpBypassToken', fields=[ ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('token', models.CharField(max_length=8, verbose_name='token')), + ('token', models.CharField(help_text='The hashed equivalent of the token.', max_length=88, verbose_name='token')), ('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='otp_bypass_tokens', to='user.user')), ], options={ diff --git a/codeforlife/user/models/otp_bypass_token.py b/codeforlife/user/models/otp_bypass_token.py index ec118bf2..d06cdd0b 100644 --- a/codeforlife/user/models/otp_bypass_token.py +++ b/codeforlife/user/models/otp_bypass_token.py @@ -73,7 +73,8 @@ def bulk_create(self, user: User): # type: ignore[override] token = models.CharField( _("token"), - max_length=length, + max_length=88, + help_text=_("The hashed equivalent of the token."), ) class Meta(TypedModelMeta): diff --git a/codeforlife/user/views/user.py b/codeforlife/user/views/user.py index 647b944a..e1f930ad 100644 --- a/codeforlife/user/views/user.py +++ b/codeforlife/user/views/user.py @@ -35,7 +35,7 @@ def get_queryset( new_student__class_field=user.student.class_field ) - return teachers | students + return (teachers | students).order_by("pk") user = self.request.teacher_user if user.teacher.school: @@ -66,7 +66,7 @@ def get_queryset( ) ) - return teachers | students | independents + return (teachers | students | independents).order_by("pk") return user_class.objects.filter(pk=user.pk) From d3eaeda6aba689ecdd5263124166461e3420610e Mon Sep 17 00:00:00 2001 From: SKairinos Date: Fri, 27 Sep 2024 10:26:08 +0000 Subject: [PATCH 2/2] fix: tests --- codeforlife/user/views/user.py | 62 +++++++++++++---------------- codeforlife/user/views/user_test.py | 9 +++-- 2 files changed, 33 insertions(+), 38 deletions(-) diff --git a/codeforlife/user/views/user.py b/codeforlife/user/views/user.py index e1f930ad..55c869f5 100644 --- a/codeforlife/user/views/user.py +++ b/codeforlife/user/views/user.py @@ -5,6 +5,8 @@ import typing as t +from django.db.models import Q + from ...views import ModelViewSet from ..filters import UserFilterSet from ..models import AnyUser, User @@ -26,47 +28,39 @@ def get_queryset( user = self.request.auth_user if user.student: if user.student.class_field is None: - return user_class.objects.filter(id=user.id) - - teachers = user_class.objects.filter( - new_teacher=user.student.class_field.teacher - ) - students = user_class.objects.filter( - new_student__class_field=user.student.class_field - ) + return user_class.objects.filter(pk=user.pk) - return (teachers | students).order_by("pk") + return user_class.objects.filter( + Q(new_teacher=user.student.class_field.teacher) + | Q(new_student__class_field=user.student.class_field) + ).order_by("pk") user = self.request.teacher_user if user.teacher.school: - teachers = user_class.objects.filter( - new_teacher__school=user.teacher.school_id - ) - students = ( - user_class.objects.filter( - # TODO: add school foreign key to student model. - new_student__class_field__teacher__school=( - user.teacher.school_id - ), - ) - if user.teacher.is_admin - else user_class.objects.filter( - new_student__class_field__teacher=user.teacher - ) - ) - independents = ( - user_class.objects.filter( - new_student__pending_class_request__teacher__school=( - user.teacher.school_id + return user_class.objects.filter( + Q(new_teacher__school=user.teacher.school_id) + | ( + Q( + # TODO: add school foreign key to student model. + new_student__class_field__teacher__school=( + user.teacher.school_id + ), ) + if user.teacher.is_admin + else Q(new_student__class_field__teacher=user.teacher) ) - if user.teacher.is_admin - else user_class.objects.filter( - new_student__pending_class_request__teacher=user.teacher + | ( + Q( + new_student__pending_class_request__teacher__school=( + user.teacher.school_id + ) + ) + if user.teacher.is_admin + else Q( + new_student__pending_class_request__teacher=user.teacher + ) ) - ) - - return (teachers | students | independents).order_by("pk") + ).order_by("pk") return user_class.objects.filter(pk=user.pk) diff --git a/codeforlife/user/views/user_test.py b/codeforlife/user/views/user_test.py index 5beb6f40..02cabb34 100644 --- a/codeforlife/user/views/user_test.py +++ b/codeforlife/user/views/user_test.py @@ -5,6 +5,7 @@ import typing as t +from django.db.models import Q from django.db.models.query import QuerySet from ...tests import ModelViewSetTestCase @@ -231,10 +232,10 @@ def test_list__name(self): self.client.login_as(user, password="abc123") self.client.list( - models=( - school_users.filter(first_name__icontains=first_name) - | school_users.filter(last_name__icontains=last_name) - ), + models=school_users.filter( + Q(first_name__icontains=first_name) + | Q(last_name__icontains=last_name) + ).order_by("pk"), filters={"name": f"{first_name} {last_name}"}, )