Exemplo n.º 1
0
def zone_set_ttl_view(request, id_list):
    """Resets TTL information on all resource records of the zone.
    
    Accepts a comma-delimited list of Domain object IDs.
    
    An intermediate page asking for the new TTL 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',
            'powerdns_manager.change_record',
    ]):
        messages.error(request, 'Insufficient permissions for this action.')
        return HttpResponseRedirect(
            reverse('admin:powerdns_manager_domain_changelist'))

    if request.method == 'POST':
        form = TtlSelectionForm(request.POST)
        if form.is_valid():
            new_ttl = form.cleaned_data['new_ttl']
            reset_zone_minimum = form.cleaned_data['reset_zone_minimum']

            Domain = get_model('powerdns_manager', 'Domain')
            Record = get_model('powerdns_manager', 'Record')

            record_count = 0

            for n, zone_id in enumerate(id_list):
                obj = Domain.objects.get(id=zone_id)
                obj_display = force_unicode(obj)

                # Check zone ownership.
                if request.user != obj.created_by:
                    messages.error(
                        request,
                        'Permission denied for domain: %s' % obj_display)
                else:
                    # Find all resource records of this domain (excludes empty non-terminals)
                    qs = Record.objects.filter(domain=obj).exclude(
                        type__isnull=True)
                    # Now set the new TTL
                    for rr in qs:
                        rr.ttl = int(new_ttl)
                        # If this is the SOA record and ``reset_zone_minimum`` has
                        # been checked, set the minimum TTL of the SOA record equal
                        # to the ``new_ttl`` value
                        #
                        # Important: We do not call ``models.Domain.set_minimum_ttl()``
                        # because we edit the SOA record here.
                        #
                        if reset_zone_minimum and rr.type == 'SOA':
                            bits = rr.content.split()
                            # SOA content:  primary hostmaster serial refresh retry expire default_ttl
                            bits[6] = str(new_ttl)
                            rr.content = ' '.join(bits)
                        # Save the resource record
                        rr.save()
                        rr_display = force_unicode(rr)

                    # Update the domain serial
                    obj.update_serial()

                    record_count += len(qs)

            n += 1
            if n == 1:
                messages.info(
                    request,
                    "Successfully updated %s resource records of '%s'" %
                    (record_count, obj_display))
            elif n > 1:
                messages.info(
                    request,
                    'Successfully updated %s zones (%s total resource records).'
                    % (n, record_count))

            # Redirect to the Domain changelist.
            return HttpResponseRedirect(
                reverse('admin:powerdns_manager_domain_changelist'))

    else:
        form = TtlSelectionForm()

    info_dict = {
        'form': form,
        'id_list': id_list,
    }
    return render_to_response('powerdns_manager/zone/set_ttl.html',
                              info_dict,
                              context_instance=RequestContext(request))
Exemplo n.º 2
0
def zone_set_ttl_view(request, id_list):
    """Resets TTL information on all resource records of the zone.
    
    Accepts a comma-delimited list of Domain object IDs.
    
    An intermediate page asking for the new TTL 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',
            'powerdns_manager.change_record',
        ]):
        messages.error(request, 'Insufficient permissions for this action.')
        return HttpResponseRedirect(reverse('admin:powerdns_manager_domain_changelist'))
    
    if request.method == 'POST':
        form = TtlSelectionForm(request.POST)
        if form.is_valid():
            new_ttl = form.cleaned_data['new_ttl']
            reset_zone_minimum = form.cleaned_data['reset_zone_minimum']
            
            Domain = cache.get_model('powerdns_manager', 'Domain')
            Record = cache.get_model('powerdns_manager', 'Record')
            
            record_count = 0
            
            for n, zone_id in enumerate(id_list):
                obj = Domain.objects.get(id=zone_id)
                obj_display = force_unicode(obj)
                
                # Check zone ownership.
                if request.user != obj.created_by:
                    messages.error(request, 'Permission denied for domain: %s' % obj_display)
                else:
                    # Find all resource records of this domain (excludes empty non-terminals)
                    qs = Record.objects.filter(domain=obj).exclude(type__isnull=True)
                    # Now set the new TTL
                    for rr in qs:
                        rr.ttl = int(new_ttl)
                        # If this is the SOA record and ``reset_zone_minimum`` has
                        # been checked, set the minimum TTL of the SOA record equal
                        # to the ``new_ttl`` value
                        #
                        # Important: We do not call ``models.Domain.set_minimum_ttl()``
                        # because we edit the SOA record here.
                        #
                        if reset_zone_minimum and rr.type == 'SOA':
                            bits = rr.content.split()
                            # SOA content:  primary hostmaster serial refresh retry expire default_ttl
                            bits[6] = str(new_ttl)
                            rr.content = ' '.join(bits)
                        # Save the resource record
                        rr.save()
                        rr_display = force_unicode(rr)
                    
                    # Update the domain serial
                    obj.update_serial()
                    
                    record_count += len(qs)
            
            n += 1
            if n == 1:
                messages.info(request, "Successfully updated %s resource records of '%s'" % (record_count, obj_display))
            elif n > 1:
                messages.info(request, 'Successfully updated %s zones (%s total resource records).' % (n, record_count))
                
            # Redirect to the Domain changelist.
            return HttpResponseRedirect(reverse('admin:powerdns_manager_domain_changelist'))
        
    else:
        form = TtlSelectionForm()
        
    info_dict = {
        'form': form,
        'id_list': id_list,
    }
    return render_to_response(
        'powerdns_manager/zone/set_ttl.html', info_dict, context_instance=RequestContext(request))
Exemplo n.º 3
0
def set_ttl_bulk(modeladmin, request, queryset):
    """Actions that resets TTL information on all resource records of the zone
    to the specified value.
    
    This action first displays a page which provides an input box to enter
    the new TTL.
    
    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
    
    Domain = cache.get_model('powerdns_manager', 'Domain')
    Record = cache.get_model('powerdns_manager', 'Record')
    
    perm_domain_change = '%s.%s' % (opts.app_label, opts.get_change_permission())
    perm_record_change = '%s.change_record' % opts.app_label
    
    if not request.user.has_perms([perm_domain_change, perm_record_change]):
        raise PermissionDenied
    
    # Check that the user has change permission for the Re model
    if not modeladmin.has_change_permission(request):
        raise PermissionDenied
    
    # The user has set a new TTL value through the forms.TtlSelectionForm 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'):
        form = TtlSelectionForm(request.POST)
        if form.is_valid():
            new_ttl = form.cleaned_data['new_ttl']
            reset_zone_minimum = form.cleaned_data['reset_zone_minimum']
            
            n = queryset.count()
            record_count = 0
            
            if n and new_ttl:
                for domain_obj in queryset:
                    # Find all resource records of this domain
                    qs = Record.objects.filter(domain=domain_obj)
                    # Now set the new TTL
                    for rr in qs:
                        rr.ttl = int(new_ttl)
                        # If this is the SOA record and ``reset_zone_minimum`` has
                        # been checked, set the minimum TTL of the SOA record equal
                        # to the ``new_ttl`` value
                        #
                        # Important: We do not call ``models.Domain.set_minimum_ttl()``
                        # because we edit the SOA record here.
                        #
                        if reset_zone_minimum and rr.type == 'SOA':
                            bits = rr.content.split()
                            # SOA content:  primary hostmaster serial refresh retry expire default_ttl
                            bits[6] = str(new_ttl)
                            rr.content = ' '.join(bits)
                        # Save the resource record
                        rr.save()
                        rr_display = force_unicode(rr)
                        modeladmin.log_change(request, rr, rr_display)
                    
                    # Update the domain serial
                    domain_obj.update_serial()
                    
                    record_count += len(qs)
                messages.info(request, 'Successfully updated %d zones (%d total records).' % (n, record_count))
            # Return None to display the change list page again.
            return None
    else:
        form = TtlSelectionForm()
    
    info_dict = {
        'form': form,
        'queryset': queryset,
        'opts': opts,
        'app_label': app_label,
        'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME,
    }
    return render_to_response(
        'powerdns_manager/actions/set_ttl.html', info_dict, context_instance=RequestContext(request), mimetype='text/html')
Exemplo n.º 4
0
def set_ttl_bulk(modeladmin, request, queryset):
    """Actions that resets TTL information on all resource records of the zone
    to the specified value.
    
    This action first displays a page which provides an input box to enter
    the new TTL.
    
    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

    Domain = cache.get_model('powerdns_manager', 'Domain')
    Record = cache.get_model('powerdns_manager', 'Record')

    perm_domain_change = '%s.%s' % (opts.app_label,
                                    opts.get_change_permission())
    perm_record_change = '%s.change_record' % opts.app_label

    if not request.user.has_perms([perm_domain_change, perm_record_change]):
        raise PermissionDenied

    # Check that the user has change permission for the Re model
    if not modeladmin.has_change_permission(request):
        raise PermissionDenied

    # The user has set a new TTL value through the forms.TtlSelectionForm 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'):
        form = TtlSelectionForm(request.POST)
        if form.is_valid():
            new_ttl = form.cleaned_data['new_ttl']
            reset_zone_minimum = form.cleaned_data['reset_zone_minimum']

            n = queryset.count()
            record_count = 0

            if n and new_ttl:
                for domain_obj in queryset:
                    # Find all resource records of this domain
                    qs = Record.objects.filter(domain=domain_obj)
                    # Now set the new TTL
                    for rr in qs:
                        rr.ttl = int(new_ttl)
                        # If this is the SOA record and ``reset_zone_minimum`` has
                        # been checked, set the minimum TTL of the SOA record equal
                        # to the ``new_ttl`` value
                        #
                        # Important: We do not call ``models.Domain.set_minimum_ttl()``
                        # because we edit the SOA record here.
                        #
                        if reset_zone_minimum and rr.type == 'SOA':
                            bits = rr.content.split()
                            # SOA content:  primary hostmaster serial refresh retry expire default_ttl
                            bits[6] = str(new_ttl)
                            rr.content = ' '.join(bits)
                        # Save the resource record
                        rr.save()
                        rr_display = force_unicode(rr)
                        modeladmin.log_change(request, rr, rr_display)

                    # Update the domain serial
                    domain_obj.update_serial()

                    record_count += len(qs)
                messages.info(
                    request,
                    'Successfully updated %d zones (%d total records).' %
                    (n, record_count))
            # Return None to display the change list page again.
            return None
    else:
        form = TtlSelectionForm()

    info_dict = {
        'form': form,
        'queryset': queryset,
        'opts': opts,
        'app_label': app_label,
        'action_checkbox_name': helpers.ACTION_CHECKBOX_NAME,
    }
    return render_to_response('powerdns_manager/actions/set_ttl.html',
                              info_dict,
                              context_instance=RequestContext(request))