django管理员行动表格不发布

时间:2013-12-26 11:55:30

标签: python django forms django-forms django-admin

我正在开发一个django管理员操作,其中管理员可以选择一个用户,然后从操作下拉菜单中选择“撤消权限”。这会将他重定向到一个中介页面,在那里他可以看到所选用户当前可以访问的“流”(在我的模型上)的多选小部件。选择所需的流后,“okay”按钮将删除该用户的那些流的权限。

我面临的问题是,当表单正确显示时,提交时生成的POST不会被捕获。我在创建授予权限的操作时使用了相同的广泛布局,并且工作正常。

actions.py

class SelectStreamForm(forms.Form):
    _selected_action = forms.CharField(widget=forms.MultipleHiddenInput)

    def __init__(self, *args, **kwargs):
        super(SelectStreamForm, self).__init__(*args, **kwargs)

        u_id = kwargs.pop('initial', None).pop('_selected_action')[0]
        user = ZenatixUser.objects.filter(pk=u_id).get()
        clientID = user.corp_id
        clientAppName = Client.objects.filter(pk=clientID).get().appName
        streamModel = get_model(clientAppName, 'tagStream')

        streamList = []
        permList = UserObjectPermission.objects.filter(user_id=u_id)
        for p in permList:
            streamList.append(p.object_pk)
        userAccessStreams = streamModel.objects.filter(pk__in=streamList)
        self.fields['stream'] = forms.ModelMultipleChoiceField(userAccessStreams,
                                                               label=user.username + ' has the read access to the following streams')


def revoke_read_permission(modeladmin, request, queryset):
    opts = modeladmin.model._meta
    app_label = opts.app_label

    form = None
    print 'Expecting a POST reply...'
    #if request.method == 'POST':
    if request.POST.get('post'):
        print 'Got a POST...'
        form = SelectStreamForm(request.POST)
        print 'Errors:', form.errors

        if form.is_valid():
            print 'Form validated...'
            streams = form.cleaned_data['stream']
            user_count = queryset.count()
            stream_count = len(streams)
            for user in queryset:
                for stream in streams:
                    print user, stream
                    remove_perm('read_stream', user, stream)

            plural = ['', '']
            if user_count != 1:
                plural[0] = 's'
            if stream_count != 1:
                plural[1] = 's'

            modeladmin.message_user(request, "Successfully granted read permission to %d user%s on %d stream%s." % (
                user_count, plural[0], stream_count, plural[1]))

            return None

    if not form:
        form = SelectStreamForm(initial={'_selected_action': request.POST.getlist(admin.ACTION_CHECKBOX_NAME)})

    if len(queryset) == 1:
        objects_name = force_unicode(opts.verbose_name)
    else:
        objects_name = force_unicode(opts.verbose_name_plural)

    stream_list = []
    #for stream in queryset:
    stream_list.append(queryset[:1])

    title = _("Are you sure?")

    context = {
        "title": title,
        "objects_name": objects_name,
        "opts": opts,
        "app_label": app_label,
        'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME,
        'tag_form': form,
    }

    return render_to_response("admin/revoke_read_permission.html", context,
                              context_instance=template.RequestContext(request)) 

revoke_read_permission.html

{% extends "admin/base_site.html" %}
{% load i18n l10n admin_urls %}
{% block breadcrumbs %}
    <ul class="grp-horizontal-list">
        <li><a href="{% url 'admin:index' %}">{% trans "Home" %}</a></li>
        <li><a href="{% url 'admin:app_list' app_label=app_label %}">{% trans app_label|capfirst|escape %}</a></li>
        <!--<li><a href="{% url opts|admin_urlname:'changelist' %}">{{ opts.verbose_name_plural|capfirst|escape }}</a></li>-->
        <li>{% trans 'Revoke permissions from user' %}</li>
    </ul>
{% endblock %}

{% block content %}
    <div class="g-d-c">
        <p>Select streams to revoke read permission for:</p>

        <form action="" method="post">
            {% csrf_token %}
            {{ tag_form }}
            <div style="margin-bottom: 30px"></div>

            <input type="hidden" name="action" value="revoke_read_permission"/>
            <input type="hidden" name="post" value="yes"/>
            <ul>
                <li class="grp-float-left"><a href="." class="grp-button grp-cancel-link">{% trans "Cancel" %}</a></li>
                <li><input type="submit" value="{% trans "Yes, I'm sure" %}" class="grp-button grp-default"/></li>
            </ul>
            <input type="hidden" name="post" value="yes"/>
            <!--<input type="submit" name="apply" value="Grant"/>-->
        </form>
    </div>
{% endblock %}

输出

Expecting a POST reply... // when the action is selected
[26/Dec/2013 17:17:52] "POST /admin/customauth/zenatixuser/ HTTP/1.1" 200 138609
[26/Dec/2013 17:17:58] "POST /admin/customauth/zenatixuser/ HTTP/1.1" 200 23766
[26/Dec/2013 17:17:58] "GET /admin/jsi18n/ HTTP/1.1" 200 2372
// now redirected to the user admin page, with no message

工作(授予)表格输出

Expecting a POST reply... // when the action is selected
[26/Dec/2013 17:20:14] "POST /admin/iiitd/tagstream/ HTTP/1.1" 200 16525
Expecting a POST reply...
Got a POST...
Errors: 
Form validated...
[26/Dec/2013 17:20:19] "POST /admin/iiitd/tagstream/ HTTP/1.1" 302 0
[26/Dec/2013 17:20:22] "GET /admin/iiitd/tagstream/ HTTP/1.1" 200 798484
[26/Dec/2013 17:20:22] "GET /admin/jsi18n/ HTTP/1.1" 200 2372
// now redirected to the streams admin page with appropriate message on top

0 个答案:

没有答案