Skip to content

Commit

Permalink
merge from new_data_models
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Dec 18, 2023
2 parents 321d66f + c5c11c2 commit 790986f
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 65 deletions.
36 changes: 18 additions & 18 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion codeforlife/user/fixtures/schools.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"fields": {
"last_saved_at": "2023-01-01 00:00:00.0+00:00",
"name": "Example School",
"country": "UK",
"country": "GB",
"uk_county": "Surrey"
}
}
Expand Down
14 changes: 11 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.20 on 2023-12-15 10:31
# Generated by Django 3.2.20 on 2023-12-18 11:09

from django.conf import settings
import django.core.validators
Expand Down Expand Up @@ -150,7 +150,7 @@ class Migration(migrations.Migration):
),
migrations.AddConstraint(
model_name='school',
constraint=models.CheckConstraint(check=models.Q(('uk_county__isnull', True), ('country', 'UK'), _connector='OR'), name='school__no_uk_county_if_country_not_uk'),
constraint=models.CheckConstraint(check=models.Q(('uk_county__isnull', True), ('country', 'GB'), _connector='OR'), name='school__no_uk_county_if_country_not_uk'),
),
migrations.AddField(
model_name='otpbypasstoken',
Expand Down Expand Up @@ -210,7 +210,7 @@ class Migration(migrations.Migration):
),
migrations.AddConstraint(
model_name='user',
constraint=models.CheckConstraint(check=models.Q(models.Q(('student__isnull', False), ('teacher__isnull', True)), models.Q(('student__isnull', True), ('teacher__isnull', False)), models.Q(('student__isnull', True), ('teacher__isnull', True)), _connector='OR'), name='user__profile'),
constraint=models.CheckConstraint(check=models.Q(('student__isnull', False), ('teacher__isnull', False), _negated=True), name='user__profile'),
),
migrations.AddConstraint(
model_name='user',
Expand All @@ -220,4 +220,12 @@ class Migration(migrations.Migration):
model_name='user',
constraint=models.CheckConstraint(check=models.Q(models.Q(('last_name__isnull', False), ('teacher__isnull', False)), models.Q(('last_name__isnull', True), ('student__isnull', False)), models.Q(('last_name__isnull', False), ('student__isnull', True), ('teacher__isnull', True)), _connector='OR'), name='user__last_name'),
),
migrations.AddConstraint(
model_name='user',
constraint=models.CheckConstraint(check=models.Q(('is_staff', True), ('student__isnull', False), _negated=True), name='user__is_staff'),
),
migrations.AddConstraint(
model_name='user',
constraint=models.CheckConstraint(check=models.Q(('is_superuser', True), ('student__isnull', False), _negated=True), name='user__is_superuser'),
),
]
2 changes: 1 addition & 1 deletion codeforlife/user/models/school.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class Meta(TypedModelMeta):
verbose_name_plural = _("schools")
constraints = [
models.CheckConstraint(
check=Q(uk_county__isnull=True) | Q(country="UK"),
check=Q(uk_county__isnull=True) | Q(country=Country.GB),
name="school__no_uk_county_if_country_not_uk",
),
]
30 changes: 17 additions & 13 deletions codeforlife/user/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,19 +199,9 @@ class Meta(TypedModelMeta):
constraints = [
# pylint: disable=unsupported-binary-operation
models.CheckConstraint(
check=(
Q(
teacher__isnull=True,
student__isnull=False,
)
| Q(
teacher__isnull=False,
student__isnull=True,
)
| Q(
teacher__isnull=True,
student__isnull=True,
)
check=~Q(
teacher__isnull=False,
student__isnull=False,
),
name="user__profile",
),
Expand Down Expand Up @@ -251,6 +241,20 @@ class Meta(TypedModelMeta):
),
name="user__last_name",
),
models.CheckConstraint(
check=~Q(
student__isnull=False,
is_staff=True,
),
name="user__is_staff",
),
models.CheckConstraint(
check=~Q(
student__isnull=False,
is_superuser=True,
),
name="user__is_superuser",
),
# pylint: enable=unsupported-binary-operation
]

Expand Down
61 changes: 35 additions & 26 deletions codeforlife/user/tests/models/test_user.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,44 @@ def test_constraints__last_name__indy(self):
with self.assert_raises_integrity_error():
User.objects.create_user(
password="password",
first_name="teacher",
first_name="Indiana",
email="[email protected]",
)

def test_constraints__is_staff(self):
"""
Students cannot be a staff user.
"""

with self.assert_raises_integrity_error():
User.objects.create_user(
first_name="Indiana",
password="password",
student=Student.objects.create(
auto_gen_password="password",
klass=self.klass__AB123,
school=self.school__1,
),
is_staff=True,
)

def test_constraints__is_superuser(self):
"""
Students cannot be a super user.
"""

with self.assert_raises_integrity_error():
User.objects.create_user(
first_name="Indiana",
password="password",
student=Student.objects.create(
auto_gen_password="password",
klass=self.klass__AB123,
school=self.school__1,
),
is_superuser=True,
)

def test_objects__create(self):
"""
Cannot call objects.create.
Expand Down Expand Up @@ -263,31 +297,6 @@ def test_objects__create_superuser__teacher(self):
assert user.is_staff
assert user.is_superuser

def test_objects__create_superuser__student(self):
"""
Create a student super user.
"""

user_fields = {
"first_name": "first_name",
"password": "password",
"student": Student.objects.create(
auto_gen_password="password",
klass=self.klass__AB123,
school=self.school__1,
),
}

user = User.objects.create_superuser(
**user_fields # type: ignore[arg-type]
)
assert user.first_name == user_fields["first_name"]
assert user.password != user_fields["password"]
assert user.check_password(user_fields["password"])
assert user.student == user_fields["student"]
assert user.is_staff
assert user.is_superuser

def test_objects__create_superuser__indy(self):
"""
Create an independent super user.
Expand Down
6 changes: 3 additions & 3 deletions codeforlife/user/views/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ def get_queryset(self):
if not isinstance(user, User):
return User.objects.none()

if user.student:
if user.student is not None:
return User.objects.filter(student__klass_id=user.student.klass_id)

if user.teacher:
if user.teacher is not None:
teachers = User.objects.none()
students = User.objects.none()

if user.teacher.school_id:
if user.teacher.school_id is not None:
teachers = User.objects.filter(
teacher__school_id=user.teacher.school_id
)
Expand Down

0 comments on commit 790986f

Please sign in to comment.