Thursday, December 26, 2013

django admin action form not posting

I'm working on a django admin action wherein the admin can select a user and then choose 'revoke permissions' fro the action dropdown. This redirects him t an intermediatory page where he can see a multiselect widget of the 'streams' (on of my models) that the selected user currently has access to. After selecting the required stream, the 'okay' button removes the permissions for those streams for that user.

The problem I'm facing is that while the form is displayed correctly, the POST generated on its submission is not being caught. I used the same broad layout while creating an action to grant permissions and it worked fine.

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 %}

Output
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

Working (grant) forms Output

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

I had a look at the POST contents of the form and it seemed to be missing the '_selected_action' fields when the user confirms the revoke permissions. The other (grant) form had this field in it, automatically. 








--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/b9af9d78-562e-4d11-90c1-94078bf70b63%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

No comments:

Post a Comment