Пример #1
0
    def create_pootle_permission_sets(self):
        """Create the default permission set for the 'nobody' and 'default' users.

        'nobody' is the anonymous (non-logged in) user, and 'default' is the
        logged in user.
        """
        User = get_user_model()

        nobody = User.objects.get(username="******")
        default = User.objects.get(username="******")

        view = get_pootle_permission("view")
        suggest = get_pootle_permission("suggest")
        translate = get_pootle_permission("translate")

        # Default permissions for tree root.
        criteria = {"user": nobody, "directory": Directory.objects.root}
        self._create_pootle_permission_set([view, suggest], **criteria)

        criteria["user"] = default
        self._create_pootle_permission_set([view, suggest, translate], **criteria)

        # Default permissions for templates language.
        # Override with no permissions for templates language.
        criteria = {"user": nobody, "directory": Directory.objects.get(pootle_path="/templates/")}
        self._create_pootle_permission_set([], **criteria)

        criteria["user"] = default
        self._create_pootle_permission_set([], **criteria)
Пример #2
0
    def create_pootle_permission_sets(self):
        """Create the default permission set for the 'nobody' and 'default' users.

        'nobody' is the anonymous (non-logged in) user, and 'default' is the
        logged in user.
        """
        User = get_user_model()

        nobody = User.objects.get(username='******')
        default = User.objects.get(username='******')

        view = get_pootle_permission('view')
        suggest = get_pootle_permission('suggest')
        translate = get_pootle_permission('translate')

        # Default permissions for tree root.
        criteria = {
            'user': nobody,
            'directory': Directory.objects.root,
        }
        self._create_pootle_permission_set([view, suggest], **criteria)

        criteria['user'] = default
        self._create_pootle_permission_set(
            [view, suggest, translate], **criteria)

        criteria['user'] = default
        self._create_pootle_permission_set([], **criteria)
Пример #3
0
def create_pootle_permission_sets():
    """Create the default permission set for the 'nobody' and 'default' users.

    'nobody' is the anonymous (non-logged in) user, and 'default' is the logged
    in user.
    """
    User = get_user_model()

    nobody = User.objects.get(username='******')
    default = User.objects.get(username='******')

    view = get_pootle_permission('view')
    suggest = get_pootle_permission('suggest')
    translate = get_pootle_permission('translate')

    # Default permissions for tree root.
    criteria = {
        'user': nobody,
        'directory': Directory.objects.root,
    }
    _create_pootle_permission_set([view, suggest], **criteria)

    criteria['user'] = default
    _create_pootle_permission_set([view, suggest, translate], **criteria)

    # Default permissions for templates language.
    # Override with no permissions for templates language.
    criteria = {
        'user': nobody,
        'directory': Directory.objects.get(pootle_path="/templates/"),
    }
    _create_pootle_permission_set([], **criteria)

    criteria['user'] = default
    _create_pootle_permission_set([], **criteria)
Пример #4
0
def create_pootle_permission_sets():
    """Create the default permission set for the 'nobody' and 'default' users.

    'nobody' is the anonymous (non-logged in) user, and 'default' is the logged
    in user.
    """
    nobody = User.objects.get(username="******")
    default = User.objects.get(username="******")

    view = get_pootle_permission('view')
    suggest = get_pootle_permission('suggest')
    translate = get_pootle_permission('translate')
    archive = get_pootle_permission('archive')

    # Default permissions for tree root.
    criteria = {
        'user': nobody,
        'directory': Directory.objects.root,
    }
    permission_set, created = PermissionSet.objects.get_or_create(**criteria)
    if created:
        permission_set.positive_permissions = [view, suggest]
        permission_set.save()

    criteria["user"] = default
    permission_set, created = PermissionSet.objects.get_or_create(**criteria)
    if created:
        permission_set.positive_permissions = [
            view, suggest, translate, archive
        ]
        permission_set.save()

    # Default permissions for templates language.
    # Override with no permissions for templates language.
    criteria = {
        'user': nobody,
        'directory': Directory.objects.get(pootle_path="/templates/"),
    }
    permission_set, created = PermissionSet.objects.get_or_create(**criteria)
    if created:
        permission_set.positive_permissions = []
        permission_set.save()

    criteria["user"] = default
    permission_set, created = PermissionSet.objects.get_or_create(**criteria)
    if created:
        permission_set.positive_permissions = []
        permission_set.save()
Пример #5
0
def test_form_permissions_users(project0, member, member2):

    # must supply a directory
    with pytest.raises(KeyError):
        PermissionsUsersSearchForm()

    form = PermissionsUsersSearchForm(
        directory=project0.directory, data={})

    assert not form.is_valid()
    assert "q" in form.errors

    form = PermissionsUsersSearchForm(
        directory=project0.directory,
        data=dict(q="mem"))

    assert form.is_valid()
    assert form.cleaned_data == dict(q="mem")
    results = form.search()["results"]
    assert results[0]['text'] == member.username
    assert results[0]['id'] == member.pk
    assert results[1]['text'] == member2.username
    assert results[1]['id'] == member2.pk

    # providing a user with permissions in this directory
    # means they are excluded from search results
    view = get_pootle_permission('view')
    perm_set = PermissionSet.objects.create(
        user=member,
        directory=project0.directory)
    perm_set.positive_permissions.add(view)
    assert form.search() == {
        'results': [
            {'text': member2.username, 'id': member2.pk}]}
Пример #6
0
def create_pootle_permission_sets():
    """Create the default permission set for the 'nobody' and 'default' users.

    'nobody' is the anonymous (non-logged in) user, and 'default' is the logged
    in user.
    """
    nobody = PootleProfile.objects.get(user__username='******')
    default = PootleProfile.objects.get(user__username='******')

    view = get_pootle_permission('view')
    suggest = get_pootle_permission('suggest')
    translate = get_pootle_permission('translate')
    archive = get_pootle_permission('archive')

    # Default permissions for tree root.
    criteria = {
        'profile': nobody,
        'directory': Directory.objects.root,
    }
    permission_set, created = PermissionSet.objects.get_or_create(**criteria)
    if created:
        permission_set.positive_permissions = [view, suggest]
        permission_set.save()

    criteria['profile'] = default
    permission_set, created = PermissionSet.objects.get_or_create(**criteria)
    if created:
        permission_set.positive_permissions = [view, suggest, translate,
                                               archive]
        permission_set.save()

    # Default permissions for templates language.
    # Override with no permissions for templates language.
    criteria = {
        'profile': nobody,
        'directory': Directory.objects.get(pootle_path="/templates/"),
    }
    permission_set, created = PermissionSet.objects.get_or_create(**criteria)
    if created:
        permission_set.positive_permissions = []
        permission_set.save()

    criteria['profile'] = default
    permission_set, created = PermissionSet.objects.get_or_create(**criteria)
    if created:
        permission_set.positive_permissions = []
        permission_set.save()
Пример #7
0
def create_pootle_permission_sets():
    """
    Create the default permission set for the anonymous (non-logged in) user
    ('nobody') and for the logged in user ('default').
    """
    nobody = PootleProfile.objects.get(user__username='******')
    default = PootleProfile.objects.get(user__username='******')

    view = get_pootle_permission('view')
    suggest = get_pootle_permission('suggest')
    translate = get_pootle_permission('translate')
    archive = get_pootle_permission('archive')

    # Default permissions for tree root
    root = Directory.objects.root
    permission_set, created = PermissionSet.objects.get_or_create(
        profile=nobody, directory=root)
    if created:
        permission_set.positive_permissions = [view, suggest]
        permission_set.save()

    permission_set, created = PermissionSet.objects.get_or_create(
        profile=default, directory=root)
    if created:
        permission_set.positive_permissions = [view, suggest,
                                               translate, archive]
        permission_set.save()

    # Default permissions for templates language
    templates = Directory.objects.get(pootle_path="/templates/")

    #override with no permissions for templates language
    permission_set, created = PermissionSet.objects.get_or_create(
        profile=nobody, directory=templates)
    if created:
        permission_set.positive_permissions = [view]
        permission_set.save()

    permission_set, created = PermissionSet.objects.get_or_create(
        profile=default, directory=templates)
    if created:
        permission_set.positive_permissions = [view]
        permission_set.save()
Пример #8
0
def create_pootle_permission_sets():
    """Create the default permission set for the anonymous (non-logged in) user
    ('nobody') and for the logged in user ('default')."""
    nobody = PootleProfile.objects.get(user__username='******')
    default = PootleProfile.objects.get(user__username='******')

    view = get_pootle_permission('view')
    suggest = get_pootle_permission('suggest')
    translate = get_pootle_permission('translate')
    archive = get_pootle_permission('archive')

    # Default permissions for tree root
    root = Directory.objects.root
    permission_set, created = PermissionSet.objects.get_or_create(
        profile=nobody, directory=root)
    if created:
        permission_set.positive_permissions = [view, suggest]
        permission_set.save()

    permission_set, created = PermissionSet.objects.get_or_create(
        profile=default, directory=root)
    if created:
        permission_set.positive_permissions = [
            view, suggest, translate, archive
        ]
        permission_set.save()

    # Default permissions for templates language
    templates = Directory.objects.get(pootle_path="/templates/")

    #override with no permissions for templates language
    permission_set, created = PermissionSet.objects.get_or_create(
        profile=nobody, directory=templates)
    if created:
        permission_set.positive_permissions = [view]
        permission_set.save()

    permission_set, created = PermissionSet.objects.get_or_create(
        profile=default, directory=templates)
    if created:
        permission_set.positive_permissions = [view]
        permission_set.save()
Пример #9
0
    def create_pootle_permission_sets(self):
        """Create the default permission set for the 'nobody' and 'default' users.

        'nobody' is the anonymous (non-logged in) user, and 'default' is the
        logged in user.
        """
        User = get_user_model()

        nobody = User.objects.get(username="******")
        default = User.objects.get(username="******")

        view = get_pootle_permission("view")
        suggest = get_pootle_permission("suggest")

        # Default permissions for tree root.
        criteria = {
            "user": nobody,
            "directory": Directory.objects.root,
        }
        self._create_pootle_permission_set([view], **criteria)

        criteria["user"] = default
        self._create_pootle_permission_set([view, suggest], **criteria)
Пример #10
0
    def handle(self, *args, **options):
        """Assign permissions to a user in a project, language or translation
        project.
        """
        project_code = options.get("project_code", None)
        language_code = options.get("language_code", None)
        username = options.get("username", None)
        permissions = options.get("permissions", None)

        # Make sure all the required parameters are provided.
        if username is None:
            raise CommandError("A username must be provided.")

        if permissions is None:
            raise CommandError("A permissions list must be provided.")

        if project_code is None and language_code is None:
            raise CommandError("Either a project code or a language code must " "be provided.")

        # Get the object we are assigning permissions for. This object can be a
        # translation project, a language or a project. This checks if it
        # exists.
        if project_code is not None and language_code is not None:
            try:
                criteria = {"project__code": project_code, "language__code": language_code}
                perms_for = TranslationProject.objects.get(**criteria)
            except TranslationProject.DoesNotExist:
                raise CommandError(
                    "Translation project for project '%s' and "
                    "language '%s' doesn't exist." % (project_code, language_code)
                )
        elif project_code is not None:
            try:
                perms_for = Project.objects.get(code=project_code)
            except Project.DoesNotExist:
                raise CommandError("Project '%s' does not exist." % project_code)
        elif language_code is not None:
            try:
                perms_for = Language.objects.get(code=language_code)
            except Language.DoesNotExist:
                raise CommandError("Language '%s' does not exist." % language_code)

        # Get the profile for the specified username. This checks if it exists.
        try:
            profile = PootleProfile.objects.get(user__username=username)
        except PootleProfile.DoesNotExist:
            raise CommandError("Profile for user '%s' does not exist." % username)

        # Get all the specified permissions. This checks if they exist.
        permission_list = permissions.split(",")
        perms_dict = {}

        for permission in permission_list:
            try:
                perms_dict[permission] = get_pootle_permission(permission)
            except Permission.DoesNotExist:
                raise CommandError("Permission with code '%s' doesn't exist." % permission)

        # Assign the permissions to the user.
        params = {"profile": profile, "directory": perms_for.directory}
        permission_set, created = PermissionSet.objects.get_or_create(**params)

        if created:
            # If the user doesn't yet have any permission for this object, just
            # apply all the permissions.
            permission_set.positive_permissions = perms_dict.values()
            permission_set.save()
        else:
            # If the user already has some permissions for this object.
            has_perms = permission_set.positive_permissions.all()
            has_perms_list = "\n\t".join([perm.codename for perm in has_perms])
            logging.info("The user already has the permissions:\n\t%s", has_perms_list)

            # Get the permissions not yet applied.
            missing_perms = [permission for permission in perms_dict.values() if permission not in has_perms]
            missing_perms_list = "\n\t".join([perm.codename for perm in missing_perms])
            logging.info("About to apply the missing permissions:\n\t%s", missing_perms_list)

            # Apply the missing permissions.
            for missing in missing_perms:
                permission_set.positive_permissions.add(missing)

        # Nofify success in permissions assignment.
        if project_code is not None and language_code is not None:
            logging.info(
                "Sucessfully applied the permissions to user '%s' in "
                "translation project for project '%s' and language "
                "'%s'.",
                username,
                project_code,
                language_code,
            )
        elif project_code is not None:
            logging.info("Sucessfully applied the permissions to user '%s' in " "project '%s'.", username, project_code)
        elif language_code is not None:
            logging.info(
                "Sucessfully applied the permissions to user '%s' in " "language '%s'.", username, language_code
            )
Пример #11
0
    def handle(self, *args, **options):
        """Assign permissions to a user in a project, language or translation
        project.
        """
        project_code = options.get('project_code', None)
        language_code = options.get('language_code', None)
        username = options.get('username', None)
        permissions = options.get('permissions', None)

        # Make sure all the required parameters are provided.
        if username is None:
            raise CommandError("A username must be provided.")

        if permissions is None:
            raise CommandError("A permissions list must be provided.")

        if project_code is None and language_code is None:
            raise CommandError("Either a project code or a language code must "
                               "be provided.")

        # Get the object we are assigning permissions for. This object can be a
        # translation project, a language or a project. This checks if it
        # exists.
        if project_code is not None and language_code is not None:
            try:
                criteria = {
                    'project__code': project_code,
                    'language__code': language_code,
                }
                perms_for = TranslationProject.objects.get(**criteria)
            except TranslationProject.DoesNotExist:
                raise CommandError("Translation project for project '%s' and "
                                   "language '%s' doesn't exist." %
                                   (project_code, language_code))
        elif project_code is not None:
            try:
                perms_for = Project.objects.get(code=project_code)
            except Project.DoesNotExist:
                raise CommandError("Project '%s' does not exist." %
                                   project_code)
        elif language_code is not None:
            try:
                perms_for = Language.objects.get(code=language_code)
            except Language.DoesNotExist:
                raise CommandError("Language '%s' does not exist." %
                                   language_code)

        # Get the profile for the specified username. This checks if it exists.
        try:
            profile = PootleProfile.objects.get(user__username=username)
        except PootleProfile.DoesNotExist:
            raise CommandError("Profile for user '%s' does not exist." %
                               username)

        # Get all the specified permissions. This checks if they exist.
        permission_list = permissions.split(",")
        perms_dict = {}

        for permission in permission_list:
            try:
                perms_dict[permission] = get_pootle_permission(permission)
            except Permission.DoesNotExist:
                raise CommandError("Permission with code '%s' doesn't exist." %
                                   permission)

        # Assign the permissions to the user.
        params = {
            'profile': profile,
            'directory': perms_for.directory,
        }
        permission_set, created = PermissionSet.objects.get_or_create(**params)

        if created:
            # If the user doesn't yet have any permission for this object, just
            # apply all the permissions.
            permission_set.positive_permissions = perms_dict.values()
            permission_set.save()
        else:
            # If the user already has some permissions for this object.
            has_perms = permission_set.positive_permissions.all()
            has_perms_list = "\n\t".join([perm.codename for perm in has_perms])
            logging.info("The user already has the permissions:\n\t%s",
                         has_perms_list)

            # Get the permissions not yet applied.
            missing_perms = [
                permission for permission in perms_dict.values()
                if permission not in has_perms
            ]
            missing_perms_list = "\n\t".join(
                [perm.codename for perm in missing_perms])
            logging.info("About to apply the missing permissions:\n\t%s",
                         missing_perms_list)

            # Apply the missing permissions.
            for missing in missing_perms:
                permission_set.positive_permissions.add(missing)

        # Nofify success in permissions assignment.
        if project_code is not None and language_code is not None:
            logging.info(
                "Sucessfully applied the permissions to user '%s' in "
                "translation project for project '%s' and language "
                "'%s'.", username, project_code, language_code)
        elif project_code is not None:
            logging.info(
                "Sucessfully applied the permissions to user '%s' in "
                "project '%s'.", username, project_code)
        elif language_code is not None:
            logging.info(
                "Sucessfully applied the permissions to user '%s' in "
                "language '%s'.", username, language_code)