Skip to content

Commit

Permalink
quick save
Browse files Browse the repository at this point in the history
  • Loading branch information
SKairinos committed Dec 8, 2023
1 parent a310da8 commit bb7750b
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 78 deletions.
4 changes: 2 additions & 2 deletions codeforlife/user/fixtures/users.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"last_saved_at": "2023-01-01 00:00:00.0+00:00",
"first_name": "John",
"last_name": "Doe",
"username": "john.doe",
"email": "[email protected]",
"password": "password",
"teacher": 1
}
},
Expand All @@ -18,8 +18,8 @@
"last_saved_at": "2023-01-01 00:00:00.0+00:00",
"first_name": "Jane",
"last_name": "Doe",
"username": "jane.doe",
"email": "[email protected]",
"password": "password",
"teacher": 2
}
}
Expand Down
27 changes: 5 additions & 22 deletions codeforlife/user/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# Generated by Django 3.2.20 on 2023-12-08 12:27
# Generated by Django 3.2.20 on 2023-12-08 15:41

from django.conf import settings
import django.core.validators
from django.db import migrations, models
import django.db.models.deletion
import django.db.models.expressions
import django.utils.timezone


Expand Down Expand Up @@ -129,9 +128,9 @@ class Migration(migrations.Migration):
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('last_saved_at', models.DateTimeField(auto_now=True, help_text='Record the last time the model was saved. This is used by our data warehouse to know what data was modified since the last scheduled data transfer from the database to the data warehouse.', verbose_name='last saved at')),
('delete_after', models.DateTimeField(blank=True, help_text="When this data is scheduled for deletion. Set to null if not scheduled for deletion. This is used by our data warehouse to transfer data that's been scheduled for deletion before it's actually deleted. Data will actually be deleted in a CRON job after this point in time.", null=True, verbose_name='delete after')),
('auto_gen_password', models.CharField(editable=False, help_text='An auto-generated password that allows student to log directly into their account.', max_length=64, null=True, validators=[django.core.validators.MinLengthValidator(64)], verbose_name='automatically generated password')),
('klass', models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='students', to='user.class')),
('school', models.ForeignKey(editable=False, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='students', to='user.school')),
('auto_gen_password', models.CharField(editable=False, help_text='An auto-generated password that allows student to log directly into their account.', max_length=64, validators=[django.core.validators.MinLengthValidator(64)], verbose_name='automatically generated password')),
('klass', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='students', to='user.class')),
('school', models.ForeignKey(editable=False, on_delete=django.db.models.deletion.CASCADE, related_name='students', to='user.school')),
],
options={
'verbose_name': 'student',
Expand Down Expand Up @@ -194,14 +193,6 @@ class Migration(migrations.Migration):
name='user_permissions',
field=models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions'),
),
migrations.AddConstraint(
model_name='student',
constraint=models.CheckConstraint(check=models.Q(models.Q(('klass__isnull', True), ('school__isnull', True)), models.Q(('klass__isnull', False), ('school__isnull', False)), _connector='OR'), name='student__school_and_klass'),
),
migrations.AddConstraint(
model_name='student',
constraint=models.CheckConstraint(check=models.Q(models.Q(('auto_gen_password__isnull', False), ('school__isnull', False)), models.Q(('auto_gen_password__isnull', True), ('school__isnull', True)), _connector='OR'), name='student__auto_gen_password'),
),
migrations.AlterUniqueTogether(
name='sessionauthfactor',
unique_together={('session', 'auth_factor')},
Expand All @@ -210,20 +201,12 @@ class Migration(migrations.Migration):
name='otpbypasstoken',
unique_together={('user', 'token')},
),
migrations.AddConstraint(
model_name='class',
constraint=models.CheckConstraint(check=models.Q(('teacher__school', django.db.models.expressions.F('school'))), name='class__teacher_in_school'),
),
migrations.AlterUniqueTogether(
name='authfactor',
unique_together={('user', 'type')},
),
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)), _connector='OR'), name='user__teacher_or_student'),
),
migrations.AddConstraint(
model_name='user',
constraint=models.CheckConstraint(check=models.Q(models.Q(('email__isnull', False), ('teacher__isnull', False)), models.Q(('email__isnull', True), ('student__isnull', False), ('student__school__isnull', False)), models.Q(('email__isnull', False), ('student__isnull', False), ('student__school__isnull', True)), _connector='OR'), name='user__email'),
constraint=models.CheckConstraint(check=models.Q(models.Q(('email__isnull', False), ('teacher__isnull', False)), models.Q(('email__isnull', True), ('student__isnull', False)), models.Q(('email__isnull', False), ('student__isnull', True), ('teacher__isnull', True)), _connector='OR'), name='user__email'),
),
]
7 changes: 0 additions & 7 deletions codeforlife/user/models/klass.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

from django.core.validators import MinLengthValidator, RegexValidator
from django.db import models
from django.db.models import F, Q
from django.db.models.query import QuerySet
from django.utils.translation import gettext_lazy as _
from django_stubs_ext.db.models import TypedModelMeta
Expand Down Expand Up @@ -77,9 +76,3 @@ class Class(AbstractModel):
class Meta(TypedModelMeta):
verbose_name = _("class")
verbose_name_plural = _("classes")
constraints = [
models.CheckConstraint(
check=Q(teacher__school=F("school")),
name="class__teacher_in_school",
),
]
46 changes: 10 additions & 36 deletions codeforlife/user/models/student.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from django.contrib.auth.hashers import make_password
from django.core.validators import MinLengthValidator
from django.db import models
from django.db.models import Q
from django.utils.translation import gettext_lazy as _
from django_stubs_ext.db.models import TypedModelMeta

Expand All @@ -27,7 +26,7 @@ class Student(AbstractModel):
class Manager(models.Manager["Student"]):
def create( # type: ignore[override]
self,
auto_gen_password: t.Optional[str] = None,
auto_gen_password: str,
**fields,
):
"""Create a student.
Expand All @@ -39,10 +38,10 @@ def create( # type: ignore[override]
A student instance.
"""

if auto_gen_password:
auto_gen_password = make_password(auto_gen_password)

return super().create(**fields, auto_gen_password=auto_gen_password)
return super().create(
**fields,
auto_gen_password=make_password(auto_gen_password),
)

def bulk_create( # type: ignore[override]
self,
Expand All @@ -60,10 +59,9 @@ def bulk_create( # type: ignore[override]
"""

for student in students:
if student.auto_gen_password:
student.auto_gen_password = make_password(
student.auto_gen_password
)
student.auto_gen_password = make_password(
student.auto_gen_password
)

return super().bulk_create(students, *args, **kwargs)

Expand Down Expand Up @@ -115,31 +113,23 @@ def bulk_create_users(

user: "_user.User"

school: t.Optional[
"_school.School"
] = models.ForeignKey( # type: ignore[assignment]
school: "_school.School" = models.ForeignKey( # type: ignore[assignment]
"user.School",
related_name="students",
null=True,
editable=False,
on_delete=models.CASCADE,
)

klass: t.Optional[
"_class.Class"
] = models.ForeignKey( # type: ignore[assignment]
klass: "_class.Class" = models.ForeignKey( # type: ignore[assignment]
"user.Class",
related_name="students",
null=True,
editable=False,
on_delete=models.CASCADE,
)

auto_gen_password = models.CharField(
_("automatically generated password"),
max_length=64,
editable=False,
null=True,
help_text=_(
"An auto-generated password that allows student to log directly"
" into their account."
Expand All @@ -150,22 +140,6 @@ def bulk_create_users(
class Meta(TypedModelMeta):
verbose_name = _("student")
verbose_name_plural = _("students")
constraints = [
models.CheckConstraint(
check=(
Q(school__isnull=True, klass__isnull=True)
| Q(school__isnull=False, klass__isnull=False)
),
name="student__school_and_klass",
),
models.CheckConstraint(
check=(
Q(school__isnull=False, auto_gen_password__isnull=False)
| Q(school__isnull=True, auto_gen_password__isnull=True)
),
name="student__auto_gen_password",
),
]

@property
def teacher(self):
Expand Down
2 changes: 1 addition & 1 deletion codeforlife/user/models/teacher.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Meta(TypedModelMeta):
verbose_name_plural = _("teachers")

@property
def students(self):
def students(self) -> QuerySet["_student.Student"]:
"""All students in this teacher's classes.
Returns:
Expand Down
12 changes: 2 additions & 10 deletions codeforlife/user/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,6 @@ class Meta(TypedModelMeta):
verbose_name = _("user")
verbose_name_plural = _("users")
constraints = [
models.CheckConstraint(
check=(
Q(teacher__isnull=True, student__isnull=False)
| Q(teacher__isnull=False, student__isnull=True)
),
name="user__teacher_or_student",
),
models.CheckConstraint(
check=(
# pylint: disable-next=unsupported-binary-operation
Expand All @@ -218,12 +211,11 @@ class Meta(TypedModelMeta):
)
| Q(
student__isnull=False,
student__school__isnull=False,
email__isnull=True,
)
| Q(
student__isnull=False,
student__school__isnull=True,
teacher__isnull=True,
student__isnull=True,
email__isnull=False,
)
),
Expand Down

0 comments on commit bb7750b

Please sign in to comment.