Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: tests #137

Merged
merged 2 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions codeforlife/user/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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 = [
Expand Down Expand Up @@ -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={
Expand Down
3 changes: 2 additions & 1 deletion codeforlife/user/models/otp_bypass_token.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
62 changes: 28 additions & 34 deletions codeforlife/user/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
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")

return user_class.objects.filter(pk=user.pk)

Expand Down
9 changes: 5 additions & 4 deletions codeforlife/user/views/user_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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}"},
)

Expand Down