Skip to content

Commit

Permalink
fix: non school teachers cannot be admins
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Jan 13, 2025
1 parent 3c442aa commit 7e2993a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from django.apps.registry import Apps
from django.db import migrations


def set_non_school_teachers_as_non_admins(apps: Apps, *args):
Teacher = apps.get_model("common", "Teacher")

Teacher.objects.filter(
new_user__is_active=True,
is_admin=True,
school__isnull=True,
).update(is_admin=False)


class Migration(migrations.Migration):

dependencies = [
("common", "0055_alter_schoolteacherinvitation_token"),
]

operations = [
migrations.RunPython(
code=set_non_school_teachers_as_non_admins,
reverse_code=migrations.RunPython.noop,
),
]
19 changes: 19 additions & 0 deletions cfl_common/common/migrations/0057_teacher_teacher__is_admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.2.17 on 2025-01-13 17:34

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("common", "0056_set_non_school_teachers_as_non_admins"),
]

operations = [
migrations.AddConstraint(
model_name="teacher",
constraint=models.CheckConstraint(
check=models.Q(("is_admin", True), ("school__isnull", True), _negated=True), name="teacher__is_admin"
),
),
]
11 changes: 11 additions & 0 deletions cfl_common/common/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,17 @@ class Teacher(models.Model):

objects = TeacherModelManager()

class Meta:
constraints = [
models.CheckConstraint(
check=~models.Q(
school__isnull=True,
is_admin=True,
),
name="teacher__is_admin",
)
]

def teaches(self, userprofile):
if hasattr(userprofile, "student"):
student = userprofile.student
Expand Down

0 comments on commit 7e2993a

Please sign in to comment.