Changeset - fed3c9eee488
[Not reviewed]
default
1 1 0
Branko Majic (branko) - 6 years ago 2017-12-22 11:35:31
branko@majic.rs
CONNT-25: Removed Django admin customisation:

- Removed custom HTML template for Django Admin when adding a new
communication.
- Reimplemented limiting of source/destination to use filtering
information passed-in by default from Django Admin.
2 files changed with 14 insertions and 26 deletions:
0 comments (0 inline, 0 general)
conntrackt/admin.py
Show inline comments
 
@@ -19,48 +19,52 @@
 
#
 

	
 

	
 
# -*- coding: utf-8 -*-
 
#
 
# Copyright (C) 2013 Branko Majic
 
#
 
# This file is part of Django Conntrackt.
 
#
 
# Django Conntrackt is free software: you can redistribute it and/or modify it
 
# under the terms of the GNU General Public License as published by the Free
 
# Software Foundation, either version 3 of the License, or (at your option) any
 
# later version.
 
#
 
# Django Conntrackt is distributed in the hope that it will be useful, but
 
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 
# details.
 
#
 
# You should have received a copy of the GNU General Public License along with
 
# Django Conntrackt.  If not, see <http://www.gnu.org/licenses/>.
 
#
 

	
 

	
 
# Standard library imports.
 
from urlparse import parse_qsl
 

	
 

	
 
# Django imports.
 
from django.contrib import admin
 
from django.urls import resolve
 

	
 
# Application imports.
 
from .models import Project, Location, Entity, Interface, Communication
 

	
 

	
 
class InterfaceInline(admin.StackedInline):
 
    """
 
    This class implements the inline admin view of the Interface instances. This
 
    is used when adding the entities (since it's easier to specify interface for
 
    an entity right away).
 

	
 
    Properties:
 

	
 
      - model - Model that this admin class refers to.
 
      - extra - Number of interfaces that should be show inline for
 
        adding/editing.
 
    """
 

	
 
    model = Interface
 
    extra = 1
 

	
 
@@ -209,64 +213,64 @@ class CommunicationAdmin(admin.ModelAdmi
 
    list_display = ('source', 'destination', 'protocol', 'port', 'edit_link')
 
    # Make the extra edit link the main link for bringing-up admin page for
 
    # editing the communication.
 
    list_display_links = ('edit_link',)
 
    # All of the fields should be editable inline for ease-of-use purposes.
 
    list_editable = ('source', 'destination', 'protocol', 'port')
 
    # Add filters for project/location.
 
    list_filter = (CommunicationProjectListFilter, CommunicationLocationListFilter)
 

	
 
    def formfield_for_foreignkey(self, db_field, request, **kwargs):
 
        """
 
        Overrides the default queryset for the foreign key fields. This lets us
 
        limit the specification of communication to specific project and/or
 
        location. These are in turn passed through the GET parameters.
 

	
 
        Arguments:
 

	
 
          db_field - Name of the model field for which this method is called.
 

	
 
          request - Request associated with the calling view.
 

	
 
          kwargs - Additional keyword arguments
 
        """
 

	
 
        # Resolve the view name based on the request's path.
 
        view_name = resolve(request.path).view_name
 

	
 
        # Only process the source and destination fields that point to
 
        # interfaces.
 
        if db_field.name == "source" or db_field.name == "destination":
 
            # Perform no filtering by default.
 
            interface_filter = {}
 

	
 
            # If project was specified in GET requests, add it as a filter.
 
            if 'project' in request.GET:
 
                interface_filter['entity__project'] = request.GET['project']
 
            # If location was specified in GET request, add it as a filter.
 
            if 'location' in request.GET:
 
                interface_filter['entity__location'] = request.GET['location']
 
            # If there are any filtering options for the show interfaces, apply them.
 
            # Parse the filter from URL GET parameter passed-in by the Django Admin.
 
            changelist_filters = dict(parse_qsl(request.GET.get('_changelist_filters', '')))
 

	
 
            # Apply project filter if requested.
 
            if 'project' in changelist_filters:
 
                interface_filter['entity__project'] = changelist_filters['project']
 
            # Apply location filter if requested.
 
            if 'location' in changelist_filters:
 
                interface_filter['entity__location'] = changelist_filters['location']
 
            # Apply our filters to queryset if there are any.
 
            if interface_filter:
 
                kwargs["queryset"] = Interface.objects.filter(**interface_filter)
 

	
 
        # Call the parent's method so it would do any of its magic.
 
        return super(CommunicationAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
 

	
 

	
 
class EntityAdmin(admin.ModelAdmin):
 
    """
 
    This class implements the admin view of the entity instances. It adds some
 
    inline capability that can be edited for the entity, and also adds inline
 
    editing of interfaces related to the entity.
 
    """
 

	
 
    # Show the interfaces inline when editing an entity.
 
    inlines = [InterfaceInline]
 
    # Specify what should be viewed in a list display.
 
    list_display = ('name', 'project', 'location')
 
    # Allow the user to change project and location directly in the list.
 
    list_editable = ('project', 'location')
 
    # Enable filtering based on project and location.
 
    list_filter = ['project', 'location']
 

	
 

	
conntrackt/templates/admin/conntrackt/communication/change_list.html
Show inline comments
 
deleted file
0 comments (0 inline, 0 general)