Пример #1
0
class ApproveRegistration(ModelForm):
    role = forms.ChoiceField(
        label='Desired role',
        choices=map(
            lambda x: (x, x),
            RolesManager.get_roles_names()
        )
    )

    class Meta:
        model = models.UserProfile
        fields = []

    def save(self, notify):
        user = self.instance.user

        # remove any existing roles
        for role in get_user_roles(user):
            role.remove_role_from_user(user)

        to_assign = RolesManager.retrieve_role(self.cleaned_data['role'])
        to_assign.assign_role_to_user(user)

        user.is_active = True
        user.save()

        notify(user, to_assign.get_name())

    def delete(self, notify):
        email = self.instance.user.email
        self.instance.user.delete()
        notify(email)
def get_user_role(user):
    if user:
        roles = user.groups.filter(name__in=RolesManager.get_roles_names())
        if roles:
            return RolesManager.retrieve_role(roles[0].name)

    return None
def get_user_role(user):
    if user:
        roles = user.groups.filter(name__in=RolesManager.get_roles_names())
        if roles:
            return RolesManager.retrieve_role(roles[0].name)

    return None
Пример #4
0
def get_all_roles():
    roles_list = enumerate(list(RolesManager.get_roles_names()))
    roles = []
    for key, role in roles_list:
        roles.append((role, role.title()))
    return tuple(roles)
Пример #5
0
def sync_user_delay(mocker):
    return mocker.patch(
        'pythonpro.domain.user_facade.sync_user_on_discourse.delay')


@pytest.fixture
def email_market_mocks(mocker):
    maybe_function_names = dir(user_facade._email_marketing_facade)
    task_function_mocks = []
    for name in maybe_function_names:
        maybe_task_function = getattr(user_facade._email_marketing_facade,
                                      name)
        if hasattr(maybe_task_function, 'delay'):
            task_function_mocks.append(
                mocker.patch(
                    f'pythonpro.domain.user_facade._email_marketing_facade.{name}.delay',
                    side_effeict=maybe_task_function))
    return task_function_mocks


@pytest.mark.parametrize('role', RolesManager.get_roles_names())
def test_make_actions(logged_user, role, django_user_model, sync_user_delay,
                      email_market_mocks, cohort):
    if role in {'lead', 'client'}:
        return  # Client product is not active anymore
    admin = UserAdmin(django_user_model, AdminSite())
    make_method = getattr(admin, f'make_{role}')
    assert not has_role(logged_user, role)
    make_method(None, [logged_user])
    assert has_role(logged_user, role)
Пример #6
0
class RegisterForm(ModelForm):

    first_name = forms.CharField(max_length=30)
    last_name = forms.CharField(max_length=150)
    email = forms.EmailField(label='Email address')
    role = forms.ChoiceField(
        label='Desired role',
        choices=map(lambda x: (x, x), filter(
            lambda n: n != roles.SiteAdministrator.get_name(),
            RolesManager.get_roles_names()
        ))
    )
    affiliation = forms.CharField(max_length=255, required=True)

    class Meta:
        model = models.UserProfile
        exclude = ['user', 'countries']
        labels = {
            'home_country': 'Country',
        }

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        widgets = (field.widget for field in self.fields.values())
        for widget in widgets:
            widget.attrs['class'] = 'form-control'

    def clean_email(self):
        email = self.cleaned_data['email']
        users_with_email = models.User.objects.filter(
            Q(email=email) | Q(username=email)
        )

        if users_with_email:
            raise ValidationError('That email address is already registered!')

        return email

    def save(self, commit=False):
        profile = super().save(commit=False)

        first_name = self.cleaned_data['first_name']
        last_name = self.cleaned_data['last_name']
        email = self.cleaned_data['email']
        role = RolesManager.retrieve_role(self.cleaned_data['role'])

        # create user
        user = models.User.objects.create_user(
            email,
            email=email,
            first_name=first_name,
            last_name=last_name
        )
        role.assign_role_to_user(user)
        user.is_active = False
        user.save()

        # assign to profile
        profile.user = user
        profile.save()

        return profile
Пример #7
0
def get_nonprivileged_roles():
    return [
        name for name in RolesManager.get_roles_names()
        if name != SiteAdministrator.get_name()
        and name != ContentManager.get_name()
    ]
Пример #8
0
 def get(self, request):
     result = [{'name': entry} for entry in RolesManager.get_roles_names()]
     return JsonResponse(result, safe=False)