Exemple #1
0
    def test_rsr_send_mail_to_users(self):
        """
        Checks sending a test mail to RSR users.
        """
        rsr_send_mail_to_users([self.user])

        # Test that the mail is in the outbox.
        self.assertIn("Test subject", [sent_mail.subject for sent_mail in mail.outbox])
Exemple #2
0
    def test_rsr_send_mail_to_users(self):
        """
        Checks sending a test mail to RSR users.
        """
        rsr_send_mail_to_users([self.user])

        # Test that the mail is in the outbox.
        self.assertIn("Test subject", [sent_mail.subject for sent_mail in mail.outbox])
Exemple #3
0
def employment_post_save(sender, **kwargs):
    """
    If a new employment is created:
    - Set 'Users' Group for this employment if no group has been set
    - Inform RSR support users, organisation admins and organisation user managers

    If an existing employment is saved:
    - Set User to is_staff (for admin access) when the employment is approved and the Group is set to 'Project Editors',
    'User managers' or 'Admins', or when the user is a superuser or general admin.
    """
    users_group = Group.objects.get(name='Users')
    project_editors_group = Group.objects.get(name='Project Editors')
    user_managers_group = Group.objects.get(name='User Managers')
    admins_group = Group.objects.get(name='Admins')
    employment = kwargs.get("instance", None)
    if employment:
        user = employment.user
        if kwargs['created']:
            if not employment.group:
                employment.group = users_group
                employment.save()
            organisation = employment.organisation
            users = get_user_model().objects.all()
            notify = (
                users.filter(is_admin=True, is_support=True) |
                users.filter(
                    employers__organisation=organisation,
                    employers__group__in=[user_managers_group, admins_group],
                    is_support=True
                )
            ).distinct()
            rsr_send_mail_to_users(
                notify,
                subject='registration/user_organisation_request_subject.txt',
                message='registration/user_organisation_request_message.txt',
                subject_context={
                    'user': user,
                    'organisation': organisation
                },
                msg_context={
                    'user': user,
                    'organisation': organisation
                },
            )
        else:
            if (employment.group in [project_editors_group, user_managers_group, admins_group] and
                    employment.is_approved) or user.is_superuser or user.is_admin:
                user.is_staff = True
                user.save()
Exemple #4
0
def employment_post_save(sender, **kwargs):
    """
    If a new employment is created:
    - Set 'Users' Group for this employment if no group has been set
    - Inform RSR support users, organisation admins and organisation user managers

    If an existing employment is saved:
    - Set User to is_staff (for admin access) when the employment is approved and the Group is set to 'Project Editors',
    'User managers' or 'Admins', or when the user is a superuser or general admin.
    """
    users_group = Group.objects.get(name='Users')
    project_editors_group = Group.objects.get(name='Project Editors')
    user_managers_group = Group.objects.get(name='User Managers')
    admins_group = Group.objects.get(name='Admins')
    employment = kwargs.get("instance", None)
    if employment:
        user = employment.user
        if kwargs['created']:
            if not employment.group:
                employment.group = users_group
                employment.save()
            organisation = employment.organisation
            users = get_user_model().objects.all()
            notify = (users.filter(
                is_admin=True, is_support=True) | users.filter(
                    employers__organisation=organisation,
                    employers__group__in=[user_managers_group, admins_group],
                    is_support=True)).distinct()
            rsr_send_mail_to_users(
                notify,
                subject='registration/user_organisation_request_subject.txt',
                message='registration/user_organisation_request_message.txt',
                subject_context={
                    'user': user,
                    'organisation': organisation
                },
                msg_context={
                    'user': user,
                    'organisation': organisation
                },
            )
        else:
            if (employment.group in [
                    project_editors_group, user_managers_group, admins_group
            ] and employment.is_approved
                ) or user.is_superuser or user.is_admin:
                user.is_staff = True
                user.save()
Exemple #5
0
def user_activated_callback(sender, **kwargs):
    if not getattr(settings, 'REGISTRATION_NOTIFICATION_EMAILS', True):
        return

    user = kwargs.get("user", False)
    if user:
        org = user.userprofile.organisation
        users = User.objects.all()
        #find all users that are 1) superusers 2) RSR editors
        #3) org admins for the same org as the just activated user
        notify = (users.filter(is_superuser=True) | users.filter(groups__name__in=[GROUP_RSR_EDITORS]) | \
            users.filter(userprofile__organisation=org, groups__name__in=[GROUP_RSR_PARTNER_ADMINS])).distinct()
        rsr_send_mail_to_users(notify,
                               subject='email/new_user_registered_subject.txt',
                               message='email/new_user_registered_message.txt',
                               subject_context={'organisation': org},
                               msg_context={'user': user, 'organisation': org}
                              )
Exemple #6
0
def user_activated_callback(sender, **kwargs):
    if not getattr(settings, 'REGISTRATION_NOTIFICATION_EMAILS', True):
        return

    user = kwargs.get("user", False)
    if user:
        org = user.get_profile().organisation
        users = User.objects.all()
        #find all users that are 1) superusers 2) RSR editors
        #3) org admins for the same org as the just activated user
        notify = (users.filter(is_superuser=True) | users.filter(groups__name__in=[GROUP_RSR_EDITORS]) | \
            users.filter(userprofile__organisation=org, groups__name__in=[GROUP_RSR_PARTNER_ADMINS])).distinct()
        rsr_send_mail_to_users(notify,
                               subject='email/new_user_registered_subject.txt',
                               message='email/new_user_registered_message.txt',
                               subject_context={'organisation': org},
                               msg_context={
                                   'user': user,
                                   'organisation': org
                               })
Exemple #7
0
def employment_post_save(sender, **kwargs):
    """
    For all employments:
    - Set User to is_staff (for admin access) when the employment is approved and the Group is set
      to 'Project Editors', 'User managers' or 'Admins', or when the user is a superuser or general
      admin.

    If a new employment is created for an active user of which the employment is not approved yet:
    - Inform RSR support users, organisation admins and organisation user managers of the request
    """
    # Disable signal handler when loading fixtures
    if kwargs.get('raw', False):
        return

    # Retrieve all user groups and the employment
    project_editors_group = Group.objects.get(name='Project Editors')
    user_managers_group = Group.objects.get(name='User Managers')
    admins_group = Group.objects.get(name='Admins')
    employment = kwargs.get("instance", None)

    if not employment:
        return

    # If this is an employment with a shadow org, don't send email
    if employment.organisation.original is not None:
        return

    user = employment.user

    # Set user to staff when in a certain group
    if (employment.group in [project_editors_group, user_managers_group, admins_group]
            and employment.is_approved) or user.is_superuser or user.is_admin:
        user.is_staff = True
        user.save()

    # Send an 'Organisation request' mail when an employment has been newly created, the
    # user is active and the employment has not been approved yet.
    if kwargs['created'] and user.is_active and not employment.is_approved:
        organisation = employment.organisation

        # Retrieve all active support users
        active_support_users = get_user_model().objects.filter(is_active=True, is_support=True)

        # General (support) admins will always be informed
        admin_users = active_support_users.filter(is_admin=True)

        # As well as organisation (+ content owners) User managers and Admins
        employer_users = active_support_users.filter(
            employers__organisation__in=organisation.content_owned_by(),
            employers__group__in=[user_managers_group, admins_group]
        )

        notify = active_support_users.filter(
            Q(pk__in=admin_users.values_list('pk', flat=True))
            | Q(pk__in=employer_users.values_list('pk', flat=True))
        ).exclude(pk=user.pk).distinct()

        rsr_send_mail_to_users(
            notify,
            subject='registration/user_organisation_request_subject.txt',
            message='registration/user_organisation_request_message.txt',
            subject_context={
                'user': user,
                'organisation': organisation
            },
            msg_context={
                'user': user,
                'organisation': organisation
            },
        )
Exemple #8
0
def employment_post_save(sender, **kwargs):
    """
    For all employments:
    - Set User to is_staff (for admin access) when the employment is approved and the Group is set
      to 'Project Editors', 'User managers' or 'Admins', or when the user is a superuser or general
      admin.

    If a new employment is created for an active user of which the employment is not approved yet:
    - Inform RSR support users, organisation admins and organisation user managers of the request
    """
    # Retrieve all user groups and the employment
    project_editors_group = Group.objects.get(name='Project Editors')
    user_managers_group = Group.objects.get(name='User Managers')
    admins_group = Group.objects.get(name='Admins')
    employment = kwargs.get("instance", None)

    if not employment:
        return

    user = employment.user

    # Set user to staff when in a certain group
    if (employment.group in [project_editors_group, user_managers_group, admins_group] and
            employment.is_approved) or user.is_superuser or user.is_admin:
        user.is_staff = True
        user.save()

    # Send an 'Organisation request' mail when an employment has been newly created, the
    # user is active and the employment has not been approved yet.
    if kwargs['created'] and user.is_active and not employment.is_approved:
        organisation = employment.organisation

        # Retrieve all active support users
        active_support_users = get_user_model().objects.filter(is_active=True, is_support=True)

        # General (support) admins will always be informed
        admin_users = active_support_users.filter(is_admin=True)

        # As well as organisation (+ content owners) User managers and Admins
        employer_users = active_support_users.filter(
            employers__organisation__in=organisation.content_owned_by(),
            employers__group__in=[user_managers_group, admins_group]
        )

        notify = active_support_users.filter(
            Q(pk__in=admin_users.values_list('pk', flat=True)) |
            Q(pk__in=employer_users.values_list('pk', flat=True))
        ).exclude(pk=user.pk).distinct()

        rsr_send_mail_to_users(
            notify,
            subject='registration/user_organisation_request_subject.txt',
            message='registration/user_organisation_request_message.txt',
            subject_context={
                'user': user,
                'organisation': organisation
            },
            msg_context={
                'user': user,
                'organisation': organisation
            },
        )