Changeset - ce8399670e39
[Not reviewed]
default
0 1 1
Branko Majic (branko) - 11 years ago 2013-02-28 22:58:08
branko@majic.rs
Implemented queryset for limitting selections for source/destination to a project/site in the admin site for Communication. Added customised admin class for more complex models.
2 files changed with 56 insertions and 3 deletions:
0 comments (0 inline, 0 general)
conntrackt/admin.py
Show inline comments
 
from conntrackt.models import *
 
from django.contrib import admin
 
from django.core.urlresolvers import resolve
 

	
 
class InterfaceInline(admin.StackedInline):
 
    model = Interface
 
    extra = 1
 

	
 
def add_under_project(modeladmin, request, queryset):
 
    print "Hello"
 
add_under_project.short_description = "Do things."
 

	
 
class CommunicationAdmin(admin.ModelAdmin):
 
    list_display = ('source', 'destination', 'protocol', 'port', 'edit_link')
 
    list_editable = ('source', 'destination', 'protocol', 'port')
 
    list_display_links = ('edit_link',)
 
    list_filter = ['source__entity__project', 'source__entity__location']
 
    actions = [add_under_project]
 

	
 
    def formfield_for_foreignkey(self, db_field, request, **kwargs):
 
        view_name = resolve(request.path).view_name
 
        if db_field.name == "source" or db_field.name == "destination":
 
            interface_filter = {}
 
            if 'source__entity__project__id__exact' in request.GET:
 
                interface_filter['entity__project'] = request.GET['source__entity__project__id__exact']
 
            if 'source__entity__location__id__exact' in request.GET:
 
                interface_filter['entity__location'] = request.GET['source__entity__location__id__exact']
 
            if interface_filter:
 
                kwargs["queryset"] = Interface.objects.filter(**interface_filter)
 
        return super(CommunicationAdmin, self).formfield_for_foreignkey(db_field, request, **kwargs)
 

	
 
class EntityAdmin(admin.ModelAdmin):
 
    inlines = [InterfaceInline]
 
    list_display = ('name', 'project', 'location')
 
    list_editable = ('project', 'location')
 
    list_filter = ['project', 'location']
 

	
 
class InterfaceAdmin(admin.ModelAdmin):
 
    list_display = ('entity', 'address', 'netmask')
 
    list_editable = ('address', 'netmask')
 

	
 

	
 
admin.site.register(Project)
 
admin.site.register(Location)
 
admin.site.register(Entity)
 
admin.site.register(Interface)
 
admin.site.register(Communication)
 
admin.site.register(Entity, EntityAdmin)
 
admin.site.register(Interface, InterfaceAdmin)
 
admin.site.register(Communication, CommunicationAdmin)
 

	
conntrackt/templates/admin/conntrackt/communication/change_list.html
Show inline comments
 
new file 100644
 
{% extends "admin/change_list.html" %}
 
{% load i18n admin_static admin_list %}
 
{% load url from future %}
 
{% load admin_urls %}
 

	
 
{% block object-tools-items %}
 
            <li>
 
              <a href="{% url cl.opts|admin_urlname:'add' %}?{% if is_popup %}_popup=1{% endif %}{% if 'source__entity__location__id__exact' in cl.params %}&source__entity__location__id__exact={{ cl.params.source__entity__location__id__exact }}{% endif %}{% if 'source__entity__project__id__exact' in cl.params %}&source__entity__project__id__exact={{ cl.params.source__entity__project__id__exact }}{% endif %}" class="addlink">
 
                {% blocktrans with cl.opts.verbose_name as name %}Add {{ name }}{% endblocktrans %}
 
              </a>
 
            </li>
 
{% endblock %}
 

	
0 comments (0 inline, 0 general)