Commits

George Notaras committed daff553

Added view for bulk zone type changes. The admin action also redirects to this view.

Comments (0)

Files changed (5)

src/powerdns_manager/actions.py

 
 
 def set_domain_type_bulk(modeladmin, request, queryset):
-    """Actions that sets the domain type on the selected Domain instances.
-    
-    This action first displays a page which provides a dropdown box for the
-    user to select the domain type and then sets the new domain type on the
-    sele3cted objects.
-    
-    It checks if the user has change permission.
-    
-    Based on: https://github.com/django/django/blob/1.4.2/django/contrib/admin/actions.py
-    
-    Important
-    ---------
-    In order to work requires some special form fields (see the template).
-    
-    """
-    opts = modeladmin.model._meta
-    app_label = opts.app_label
-    
-    # Check that the user has change permission for the Domain model
+    """Actions that sets the domain type on the selected Domain instances."""
+    # Check that the user has change permission for the change modeladmin form.
     if not modeladmin.has_change_permission(request):
         raise PermissionDenied
-    
-    # The user has selected a new domain type through the
-    # forms.ZoneTypeSelectionForm form. Make the changes to the selected
-    # objects and return a None to display the change list view again.
-    #if request.method == 'POST':
-    if request.POST.get('post'):
-        domain_type = request.POST.get('domaintype')
-        n = queryset.count()
-        
-        if n and domain_type:
-            for obj in queryset:
-                obj.type = domain_type
-                obj.update_serial()
-                obj.save()
-                obj_display = force_unicode(obj)
-                modeladmin.log_change(request, obj, obj_display)
-            messages.info(request, 'Successfully updated %d domains.' % n)
-        # Return None to display the change list page again.
-        return None
-    
-    info_dict = {
-        'form': ZoneTypeSelectionForm(),
-        'queryset': queryset,
-        'opts': opts,
-        'app_label': app_label,
-        'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME,
-    }
-    return render_to_response(
-        'powerdns_manager/actions/set_domain_type.html', info_dict, context_instance=RequestContext(request))
+    selected = request.POST.getlist(helpers.ACTION_CHECKBOX_NAME)
+    return HttpResponseRedirect(reverse('zone_set_type', args=(','.join(selected),)))
 set_domain_type_bulk.short_description = "Set domain type"
 
 

src/powerdns_manager/templates/powerdns_manager/actions/set_domain_type.html

-{% extends "admin/base_site.html" %}
-{% load i18n l10n static %}
-{% load url from future %}
-{% load admin_urls %}
-
-{% block extrastyle %}{{ block.super }}<link rel="stylesheet" type="text/css" href="{% static "admin/css/forms.css" %}" />{% endblock %}
-
-{% block breadcrumbs %}
-	<div class="breadcrumbs">
-		<a href="{% url 'admin:index' %}">{% trans 'Home' %}</a>
-		&rsaquo; <a href="{% url 'admin:app_list' app_label=app_label %}">{{ app_label|capfirst|escape }}</a>
-		&rsaquo; <a href="{% url opts|admin_urlname:'changelist' %}">{{ opts.verbose_name_plural|capfirst }}</a>
-		&rsaquo; {% trans 'Set type for selected zones' %}
-	</div>
-{% endblock %}
-
-{% block title %}{% trans 'Set zone type' %}{% endblock %}
-
-{% block content %}
-    <div id="content-main">
-        
-        <form action="" method="post">{% csrf_token %}
-        <div>
-            {% if form.errors %}
-                <p class="errornote">
-                {% blocktrans count counter=form.errors.items|length %}Please correct the error below.{% plural %}Please correct the errors below.{% endblocktrans %}
-                </p>
-            {% endif %}
-
-            <h1>{% trans 'Set zone type' %}</h1>
-            <p>{% trans "Select the new zone type from the drop down menu." %}</p>
-            
-            <fieldset class="module aligned">
-    
-                <div class="form-row">
-                    {{ form.domaintype.errors }}
-                    <label for="id_domaintype" class="">{% trans 'Zone type' %}:</label>{{ form.domaintype }}
-                </div>
-
-            </fieldset>
-
-            {# Special Fields #}
-            {# These are needed for the action code to work. This an undocumented Django feature #}
-            {% for obj in queryset %}
-                <input type="hidden" name="{{ action_checkbox_name }}" value="{{ obj.pk|unlocalize }}" />
-            {% endfor %}
-            <input type="hidden" name="action" value="set_domain_type_bulk" />
-            <input type="hidden" name="post" value="yes" />
-            
-            <div class="submit-row">
-                <input type="submit" value="{% trans 'Save' %}" class="default" />
-            </div>
-
-            <script type="text/javascript">document.getElementById("id_domaintype").focus();</script>
-        </div>
-        </form>
-
-    </div> <!-- content-main -->
-{% endblock %}

src/powerdns_manager/templates/powerdns_manager/zone/set_type.html

+{% extends "admin/base_site.html" %}
+{% load i18n l10n static %}
+{% load url from future %}
+{% load admin_urls %}
+
+{% block extrastyle %}{{ block.super }}<link rel="stylesheet" type="text/css" href="{% static "admin/css/forms.css" %}" />{% endblock %}
+
+{% block breadcrumbs %}
+    <div class="breadcrumbs">
+        <a href="{% url 'admin:index' %}">{% trans 'Home' %}</a>
+        &rsaquo; <a href="{% url 'admin:app_list' app_label="powerdns_manager" %}">{% trans "PowerDNS Manager" %}</a>
+        &rsaquo; <a href="{% url 'admin:powerdns_manager_domain_changelist' %}">{% trans "Zone list" %}</a>
+        {% if id_list|length == 1 %}
+            &rsaquo; <a href="{% url 'admin:powerdns_manager_domain_change' id_list|first %}">{% trans "Zone change form" %}</a>
+        {% endif %}
+        &rsaquo; {% trans 'Set type for selected zones' %}
+    </div>
+{% endblock %}
+
+{% block title %}{% trans 'Set zone type' %}{% endblock %}
+
+{% block content %}
+    <div id="content-main">
+        
+        <form action="" method="post">{% csrf_token %}
+        <div>
+            {% if form.errors %}
+                <p class="errornote">
+                {% blocktrans count counter=form.errors.items|length %}Please correct the error below.{% plural %}Please correct the errors below.{% endblocktrans %}
+                </p>
+            {% endif %}
+
+            <h1>{% trans 'Set zone type' %}</h1>
+            <p>{% trans "Select the new zone type from the drop down menu." %}</p>
+            
+            <fieldset class="module aligned">
+    
+                <div class="form-row">
+                    {{ form.domaintype.errors }}
+                    <label for="id_domaintype" class="">{% trans 'Zone type' %}:</label>{{ form.domaintype }}
+                </div>
+
+            </fieldset>
+            
+            <div class="submit-row">
+                <input type="submit" value="{% trans 'Save' %}" class="default" />
+            </div>
+
+            <script type="text/javascript">document.getElementById("id_domaintype").focus();</script>
+        </div>
+        </form>
+
+    </div> <!-- content-main -->
+{% endblock %}

src/powerdns_manager/urls.py

     url(r'^import/axfr/$', 'import_axfr_view', name='import_axfr'),
     url(r'^export/(?P<origin>[/.\-_\w]+)/$', 'export_zone_view', name='export_zone'),
     url(r'^update/$', 'dynamic_ip_update_view', name='dynamic_ip_update'),
+    url(r'^zone/set-type/(?P<id_list>[0-9,]+)/$', 'zone_set_type_view', name='zone_set_type'),
     url(r'^zone/set-ttl/(?P<id_list>[0-9,]+)/$', 'zone_set_ttl_view', name='zone_set_ttl'),
     url(r'^zone/clone/(?P<zone_id>[0-9]+)/$', 'zone_clone_view', name='zone_clone'),
     url(r'^zone/transfer/(?P<id_list>[0-9,]+)/$', 'zone_transfer_view', name='zone_transfer'),

src/powerdns_manager/views.py

 from powerdns_manager.forms import ZoneTransferForm
 from powerdns_manager.forms import TemplateOriginForm
 from powerdns_manager.forms import TtlSelectionForm
+from powerdns_manager.forms import ZoneTypeSelectionForm
 from powerdns_manager.forms import ClonedZoneDomainForm
 from powerdns_manager.utils import process_zone_file
 from powerdns_manager.utils import process_axfr_response
         return render_to_response(
             'powerdns_manager/zone/transfer.html', info_dict, context_instance=RequestContext(request))
     
+
+
+@login_required
+@csrf_protect
+def zone_set_type_view(request, id_list):
+    """sets the domain type on the selected zones.
+    
+    Accepts a comma-delimited list of Domain object IDs.
+    
+    An intermediate page asking for the new zone type is used.
+    
+    """
+    # Create a list from the provided comma-delimited list of IDs.
+    id_list = id_list.split(',')
+    
+    # Permission check on models.
+    if not request.user.has_perms([
+            'powerdns_manager.change_domain',
+        ]):
+        messages.error(request, 'Insufficient permissions for this action.')
+        return HttpResponseRedirect(reverse('admin:powerdns_manager_domain_changelist'))
+    
+    if request.method == 'POST':
+        domain_type = request.POST.get('domaintype')
+        
+        Domain = cache.get_model('powerdns_manager', 'Domain')
+        
+        for n, zone_id in enumerate(id_list):
+            obj = Domain.objects.get(id=zone_id)
+            obj_display = force_unicode(obj)
+            
+            # Check change permission
+            if not request.user.has_perm('powerdns_manager.change_domain', obj):
+                messages.error(request, 'Permission denied for domain: %s' % obj_display)
+            else:
+                obj.type = domain_type
+                obj.update_serial()
+                obj.save()
+                
+        n += 1
+        if n == 1:
+            messages.info(request, "Successfully set the type of '%s' to '%s'" % (obj_display, domain_type))
+        elif n > 1:
+            messages.info(request, "Successfully set the type of %s zones to '%s'" % (n, domain_type))
+            
+        # Redirect to the Domain changelist.
+        return HttpResponseRedirect(reverse('admin:powerdns_manager_domain_changelist'))
+    
+    else:
+        info_dict = {
+            'form': ZoneTypeSelectionForm(),
+            'id_list': id_list,
+        }
+        return render_to_response(
+            'powerdns_manager/zone/set_type.html', info_dict, context_instance=RequestContext(request))
+        
+        
+        
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.