Ejemplo n.º 1
0
def user_and_group_permission(request, model):
    if not request.user.is_superuser:
        raise PermissionDenied

    model_mapper = {"profile": get_user_model(), "groupprofile": GroupProfile}

    model_class = model_mapper[model]

    ids = request.POST.get("ids")
    if "cancel" in request.POST or not ids:
        return HttpResponseRedirect(get_url_for_app_model(model, model_class))

    if request.method == 'POST':
        form = UserAndGroupPermissionsForm(request.POST)
        ids = ids.split(",")
        if form.is_valid():
            resources_names = [
                layer.name for layer in form.cleaned_data.get('layers')
            ]
            users_usernames = [
                user.username
                for user in model_class.objects.filter(id__in=ids)
            ] if model == 'profile' else None
            groups_names = [
                group_profile.group.name
                for group_profile in model_class.objects.filter(id__in=ids)
            ] if model in ('group', 'groupprofile') else None

            if users_usernames and 'AnonymousUser' in users_usernames and \
                    (not groups_names or 'anonymous' not in groups_names):
                if not groups_names:
                    groups_names = []
                groups_names.append('anonymous')
            if groups_names and 'anonymous' in groups_names and \
                    (not users_usernames or 'AnonymousUser' not in users_usernames):
                if not users_usernames:
                    users_usernames = []
                users_usernames.append('AnonymousUser')

            delete_flag = form.cleaned_data.get('mode') == 'unset'
            permissions_names = form.cleaned_data.get('permission_type')

            if permissions_names:
                set_permissions.apply_async(
                    (permissions_names, resources_names, users_usernames,
                     groups_names, delete_flag))

        return HttpResponseRedirect(get_url_for_app_model(model, model_class))

    form = UserAndGroupPermissionsForm({
        'permission_type': ('r', ),
        'mode': 'set',
    })
    return render(request,
                  "base/user_and_group_permissions.html",
                  context={
                      "form": form,
                      "model": model
                  })
Ejemplo n.º 2
0
def set_user_and_group_layer_permission(modeladmin, request, queryset):
    ids = ','.join([str(element.pk) for element in queryset])
    resource = queryset[0].__class__.__name__.lower()

    model_mapper = {"profile": "people", "groupprofile": "groups"}

    form = UserAndGroupPermissionsForm({
        'permission_type': ('r', ),
        'mode': 'set',
        'ids': ids,
    })

    return render(request,
                  "base/user_and_group_permissions.html",
                  context={
                      "form": form,
                      "model": model_mapper[resource]
                  })