Create class after regsitration
This commit is contained in:
parent
0b48607398
commit
80fce17efb
|
|
@ -0,0 +1,18 @@
|
||||||
|
# Generated by Django 2.0.6 on 2019-10-10 09:05
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('registration', '0001_initial'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='licensetype',
|
||||||
|
name='key',
|
||||||
|
field=models.CharField(max_length=128, unique=True),
|
||||||
|
),
|
||||||
|
]
|
||||||
|
|
@ -18,7 +18,7 @@ class LicenseType(models.Model):
|
||||||
|
|
||||||
name = models.CharField(_('License name'), max_length=255, blank=False, null=False)
|
name = models.CharField(_('License name'), max_length=255, blank=False, null=False)
|
||||||
for_role = models.ForeignKey(Role, blank=False, null=False, on_delete=models.CASCADE)
|
for_role = models.ForeignKey(Role, blank=False, null=False, on_delete=models.CASCADE)
|
||||||
key = models.CharField(max_length=128, blank=False, null=False)
|
key = models.CharField(max_length=128, blank=False, null=False, unique=True)
|
||||||
active = models.BooleanField(_('License active'), default=False)
|
active = models.BooleanField(_('License active'), default=False)
|
||||||
description = models.TextField(_('Description'), default="")
|
description = models.TextField(_('Description'), default="")
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -8,12 +8,11 @@
|
||||||
# Created on 2019-10-08
|
# Created on 2019-10-08
|
||||||
# @author: chrigu <christian.cueni@iterativ.ch>
|
# @author: chrigu <christian.cueni@iterativ.ch>
|
||||||
import graphene
|
import graphene
|
||||||
from django.conf import settings
|
|
||||||
from graphene import relay
|
from graphene import relay
|
||||||
|
|
||||||
from registration.models import License
|
from registration.models import License
|
||||||
from registration.serializers import RegistrationSerializer
|
from registration.serializers import RegistrationSerializer
|
||||||
from users.models import User, Role, UserRole
|
from users.models import User, Role, UserRole, SchoolClass
|
||||||
from users.mutations import UpdateError, FieldError
|
from users.mutations import UpdateError, FieldError
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -51,7 +50,9 @@ class Registration(relay.ClientIDMutation):
|
||||||
if sb_license.license_type.is_teacher_license():
|
if sb_license.license_type.is_teacher_license():
|
||||||
teacher_role = Role.objects.get(key=Role.objects.TEACHER_KEY)
|
teacher_role = Role.objects.get(key=Role.objects.TEACHER_KEY)
|
||||||
UserRole.objects.get_or_create(user=user, role=teacher_role)
|
UserRole.objects.get_or_create(user=user, role=teacher_role)
|
||||||
# create class
|
default_class_name = SchoolClass.generate_default_group_name()
|
||||||
|
default_class = SchoolClass.objects.create(name=default_class_name)
|
||||||
|
user.school_classes.add(default_class)
|
||||||
else:
|
else:
|
||||||
student_role = Role.objects.get(key=Role.objects.STUDENT_KEY)
|
student_role = Role.objects.get(key=Role.objects.STUDENT_KEY)
|
||||||
UserRole.objects.get_or_create(user=user, role=student_role)
|
UserRole.objects.get_or_create(user=user, role=student_role)
|
||||||
|
|
|
||||||
|
|
@ -15,10 +15,10 @@ from api.schema import schema
|
||||||
from registration.factories import LicenseTypeFactory, LicenseFactory
|
from registration.factories import LicenseTypeFactory, LicenseFactory
|
||||||
from registration.models import License
|
from registration.models import License
|
||||||
from users.managers import RoleManager
|
from users.managers import RoleManager
|
||||||
from users.models import Role, User, UserRole
|
from users.models import Role, User, UserRole, SchoolClass
|
||||||
|
|
||||||
|
|
||||||
class PasswordResetTests(TestCase):
|
class RegistrationTests(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
|
||||||
self.teacher_role = Role.objects.create(key=Role.objects.TEACHER_KEY, name="Teacher Role")
|
self.teacher_role = Role.objects.create(key=Role.objects.TEACHER_KEY, name="Teacher Role")
|
||||||
|
|
@ -73,9 +73,15 @@ class PasswordResetTests(TestCase):
|
||||||
|
|
||||||
def test_user_can_register_as_teacher(self):
|
def test_user_can_register_as_teacher(self):
|
||||||
self._assert_user_registration(0, self.email, RoleManager.TEACHER_KEY)
|
self._assert_user_registration(0, self.email, RoleManager.TEACHER_KEY)
|
||||||
|
school_classes = SchoolClass.objects.filter(name__startswith='Meine Gruppe')
|
||||||
|
self.assertEqual(len(school_classes), 0)
|
||||||
result = self.make_register_mutation(self.first_name, self.last_name, self.email, self.teacher_license_type.key)
|
result = self.make_register_mutation(self.first_name, self.last_name, self.email, self.teacher_license_type.key)
|
||||||
self.assertTrue(result.get('data').get('registration').get('success'))
|
self.assertTrue(result.get('data').get('registration').get('success'))
|
||||||
self._assert_user_registration(1, self.email, RoleManager.TEACHER_KEY)
|
self._assert_user_registration(1, self.email, RoleManager.TEACHER_KEY)
|
||||||
|
school_classes = SchoolClass.objects.filter(name__startswith='Meine Gruppe')
|
||||||
|
self.assertEqual(len(school_classes), 1)
|
||||||
|
user = User.objects.get(email=self.email)
|
||||||
|
self.assertTrue(school_classes[0].is_user_in_schoolclass(user))
|
||||||
|
|
||||||
def test_user_can_register_as_student(self):
|
def test_user_can_register_as_student(self):
|
||||||
self._assert_user_registration(0, self.email, RoleManager.STUDENT_KEY)
|
self._assert_user_registration(0, self.email, RoleManager.STUDENT_KEY)
|
||||||
|
|
|
||||||
|
|
@ -89,4 +89,3 @@ class UserManager(DjangoUserManager):
|
||||||
user.set_password(self.model.objects.make_random_password())
|
user.set_password(self.model.objects.make_random_password())
|
||||||
user.save()
|
user.save()
|
||||||
return user
|
return user
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
import re
|
||||||
|
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
from django.contrib.auth.models import AbstractUser, Permission
|
from django.contrib.auth.models import AbstractUser, Permission
|
||||||
from django.core.exceptions import ObjectDoesNotExist
|
from django.core.exceptions import ObjectDoesNotExist
|
||||||
|
|
@ -72,6 +74,25 @@ class SchoolClass(models.Model):
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return 'SchoolClass {}-{}'.format(self.id, self.name)
|
return 'SchoolClass {}-{}'.format(self.id, self.name)
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def generate_default_group_name(cls):
|
||||||
|
prefix = 'Meine Gruppe'
|
||||||
|
prefix_regex = r'Meine Gruppe (\d+)'
|
||||||
|
initial_default_group = '{} 1'.format(prefix)
|
||||||
|
my_group_filter = cls.objects.filter(name__startswith=prefix).order_by('-name')
|
||||||
|
|
||||||
|
if len(my_group_filter) == 0:
|
||||||
|
return initial_default_group
|
||||||
|
|
||||||
|
match = re.search(prefix_regex, my_group_filter[0].name)
|
||||||
|
|
||||||
|
if not match:
|
||||||
|
return initial_default_group
|
||||||
|
|
||||||
|
index = int(match.group(1))
|
||||||
|
|
||||||
|
return '{} {}'.format(prefix, index + 1)
|
||||||
|
|
||||||
def is_user_in_schoolclass(self, user):
|
def is_user_in_schoolclass(self, user):
|
||||||
return user.is_superuser or user.school_classes.filter(pk=self.id).count() > 0
|
return user.is_superuser or user.school_classes.filter(pk=self.id).count() > 0
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ from django.contrib.auth import authenticate
|
||||||
from users.factories import SchoolClassFactory
|
from users.factories import SchoolClassFactory
|
||||||
|
|
||||||
|
|
||||||
class PasswordUpdate(TestCase):
|
class MySchoolClasses(TestCase):
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.user = UserFactory(username='aschi')
|
self.user = UserFactory(username='aschi')
|
||||||
self.another_user = UserFactory(username='pesche')
|
self.another_user = UserFactory(username='pesche')
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,43 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
#
|
||||||
|
# ITerativ GmbH
|
||||||
|
# http://www.iterativ.ch/
|
||||||
|
#
|
||||||
|
# Copyright (c) 2019 ITerativ GmbH. All rights reserved.
|
||||||
|
#
|
||||||
|
# Created on 2019-10-10
|
||||||
|
# @author: chrigu <christian.cueni@iterativ.ch>
|
||||||
|
from django.conf import settings
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
#
|
||||||
|
# ITerativ GmbH
|
||||||
|
# http://www.iterativ.ch/
|
||||||
|
#
|
||||||
|
# Copyright (c) 2019 ITerativ GmbH. All rights reserved.
|
||||||
|
#
|
||||||
|
# Created on 2019-04-09
|
||||||
|
# @author: chrigu <christian.cueni@iterativ.ch>
|
||||||
|
from django.test import TestCase
|
||||||
|
|
||||||
|
from users.models import SchoolClass
|
||||||
|
|
||||||
|
|
||||||
|
class SchoolClasses(TestCase):
|
||||||
|
|
||||||
|
def setUp(self):
|
||||||
|
self.prefix = 'Meine Gruppe'
|
||||||
|
|
||||||
|
def test_default_class_name_initial(self):
|
||||||
|
class_name = SchoolClass.generate_default_group_name()
|
||||||
|
self.assertEqual('{} 1'.format(self.prefix), class_name)
|
||||||
|
|
||||||
|
def test_default_class_name_initial_with_similar_existing(self):
|
||||||
|
SchoolClass.objects.create(name='{} abc212'.format('Meine Gruppe'))
|
||||||
|
class_name = SchoolClass.generate_default_group_name()
|
||||||
|
self.assertEqual('{} 1'.format(self.prefix), class_name)
|
||||||
|
|
||||||
|
def test_default_class_name_if_existing(self):
|
||||||
|
SchoolClass.objects.create(name='{} 1'.format('Meine Gruppe'))
|
||||||
|
SchoolClass.objects.create(name='{} 10'.format('Meine Gruppe'))
|
||||||
|
class_name = SchoolClass.generate_default_group_name()
|
||||||
|
self.assertEqual('{} 11'.format(self.prefix), class_name)
|
||||||
Loading…
Reference in New Issue