Пример #1
0
def handle_post_migrate(sender, **kwargs):
    """
    Set up groups after migration and group permissions when DEBUG is
    True.
    :param sender:
    :param kwargs:
    :return:
    """
    logger.debug("ela_handler_post_migrations is called")
    from django.contrib.contenttypes.models import ContentType
    from django.contrib.auth.models import (Permission, Group)
    from usr.models import User
    from ela.roles import (StudentRole, TeacherRole, SupervisorRole)
    from rolepermissions.verifications import has_permission
    from ela.forms import (
        MainAdminAuthenticationForm,
        StudentAdminAuthenticationForm,
        TeacherAdminAuthenticationForm,
        SupervisorAdminAuthenticationForm
    )

    logger.debug(
        'Handling post_migrate signal. '
        'DEBUG={}, interactive={}'.format(
            settings.DEBUG,
            kwargs['interactive'])
    )

    # Getting groups
    student_group = Group.objects.create(
        name=settings.STUDENT_ROLE_GROUP_NAME
    )
    teacher_group = Group.objects.create(
        name=settings.TEACHER_ROLE_GROUP_NAME
    )
    supervisor_group = Group.objects.create(
        name=settings.SUPERVISOR_ROLE_GROUP_NAME
    )

    logger.debug(
        "student_group={}, "
        "teacher_group={}, "
        "supervisor_group={}".format(
            student_group,
            teacher_group,
            supervisor_group
        )
    )

    if settings.DEBUG:
        # Setting up initial users
        superuser = User.objects.create_superuser(
            "admin",
            "Arsalan",
            "Karami",
            "*****@*****.**",
            "test",
        )

        staff = User.objects.create_user(
            "staff",
            "Ms",
            "Kashani",
            "*****@*****.**",
            "test",
        )

        student1 = User.objects.create_user(
            "student1",
            "Mohammad",
            "Haghighat",
            "*****@*****.**",
            "test",
        )

        student2 = User.objects.create_user(
            "student2",
            "Alireza",
            "Molaee",
            "*****@*****.**",
            "test",
        )

        teacher = User.objects.create_user(
            "teacher",
            "Sina",
            "Amini",
            "*****@*****.**",
            "test",
        )

        supervisor = User.objects.create_user(
            "supervisor",
            "Masoud",
            "Pirbodaqi",
            "*****@*****.**",
            "test"
        )

        # Modifying user is_active flag.
        superuser.is_active = True
        staff.is_active = True
        student1.is_active = True
        student2.is_active = False
        teacher.is_active = True
        supervisor.is_active = True

        staff.is_staff = True

        # Saving users
        superuser.save()
        staff.save()
        student1.save()
        student2.save()
        teacher.save()
        supervisor.save()

        # Assigning role (group) to each user.
        StudentRole.assign_role_to_user(student1)
        StudentRole.assign_role_to_user(student2)

        TeacherRole.assign_role_to_user(teacher)
        SupervisorRole.assign_role_to_user(supervisor)

        logger.debug("superuser={}".format(superuser))
        logger.debug("staff={}".format(staff))
        logger.debug("student1={}".format(student1))
        logger.debug("student2={}".format(student2))
        logger.debug("teacher={}".format(teacher))
        logger.debug("supervisor={}".format(supervisor))

        # Check if users can login into their account
        main_admin_authentication_form = MainAdminAuthenticationForm()
        student_admin_authentication_form = StudentAdminAuthenticationForm()
        teacher_admin_authentication_form = TeacherAdminAuthenticationForm()
        supervisor_admin_authentication_form = \
            SupervisorAdminAuthenticationForm()

        logger.debug("Checking superuser")
        main_admin_authentication_form.confirm_login_allowed(superuser)

        logger.debug("Checking staff")
        main_admin_authentication_form.confirm_login_allowed(staff)

        logger.debug("Checking student1")
        student_admin_authentication_form.confirm_login_allowed(student1)

        logger.debug("Checking student2")
        student_admin_authentication_form.confirm_login_allowed(student2)

        logger.debug("Checking teacher")
        teacher_admin_authentication_form.confirm_login_allowed(teacher)

        logger.debug("Checking supervisor")
        supervisor_admin_authentication_form.confirm_login_allowed(supervisor)
Пример #2
0
 def get_queryset(self):
     """
     Filter user with have student role.
     """
     return super(StudentManager, self).get_queryset().filter(
         groups__name=StudentRole.get_name())