skillbox/server/users/services.py

49 lines
1.9 KiB
Python

from core.factories import UserFactory
from users.factories import SchoolClassFactory
from users.models import School, SchoolRole, UserSchoolRole, DEFAULT_SCHOOL_ID
def create_school_with_users(school_name, data=None):
school = School.create_school(school_name, id=DEFAULT_SCHOOL_ID)
teacher_role = SchoolRole.objects.get_default_teacher_role_for_school(school)
student_role = SchoolRole.objects.get_default_student_role_for_school(school)
if data is None:
teacher = UserFactory(username='teacher')
UserSchoolRole.objects.create(user=teacher, school_role=teacher_role)
for i in range(1, 7):
student = UserFactory(username='student{}'.format(i))
UserSchoolRole.objects.create(user=student, school_role=student_role)
SchoolClassFactory(users=[teacher, student], school=school)
else:
for school_class in data:
first, last = school_class.get('teacher')
teacher = UserFactory(
username='{}.{}'.format(first, last).lower(),
first_name=first,
last_name=last,
email='{}.{}@skillbox.example'.format(first, last).lower()
)
UserSchoolRole.objects.create(user=teacher, school_role=teacher_role)
students = []
for first, last in school_class.get('students'):
student = UserFactory(
username='{}.{}'.format(first, last).lower(),
first_name=first,
last_name=last,
email='{}.{}@skillbox.example'.format(first, last).lower()
)
UserSchoolRole.objects.create(user=student, school_role=student_role)
students.append(student)
SchoolClassFactory(
users=students + [teacher],
school=school,
name=school_class.get('class'),
year='2018'
)