diff --git a/conntrackt/admin.py b/conntrackt/admin.py --- a/conntrackt/admin.py +++ b/conntrackt/admin.py @@ -1,45 +1,118 @@ +# Import all the models from the application. from conntrackt.models import * + +# Import the administrator application. from django.contrib import admin + +# Import resolver used for figuring-out the view being called in custom query +# calls. from django.core.urlresolvers import resolve 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 -def add_under_project(modeladmin, request, queryset): - print "Hello" -add_under_project.short_description = "Do things." +class CommunicationAdmin(admin.ModelAdmin): + """ + Modifies the default admin class for the Communication class. The + communication class needs to be modified in a number of ways in order to + cater for easier adding of communication links, letting us limit the + interfaces being shown as source/destination to specific project and/or + site. + """ -class CommunicationAdmin(admin.ModelAdmin): + # Show standard fields of the model, and also include a separate edit link + # so that other fields can be editable. 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') - list_display_links = ('edit_link',) + # Add filters for project/location. list_filter = ['source__entity__project', 'source__entity__location'] - actions = [add_under_project] 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 'source__entity__project__id__exact' in request.GET: interface_filter['entity__project'] = request.GET['source__entity__project__id__exact'] + # If location was specified in GET request, add it as a filter. if 'source__entity__location__id__exact' in request.GET: interface_filter['entity__location'] = request.GET['source__entity__location__id__exact'] + # If there are any filtering options for the show interfaces, apply them. 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'] class InterfaceAdmin(admin.ModelAdmin): + """ + This class implements the admin view of the interface instances. It allows + editing the IP address and netmask of an interface directly in the listing. + + It also adds some filtering capability based on project and/or location. + """ + + # Specify fields that should be visible in the list view. list_display = ('entity', 'address', 'netmask') + # Allow changing of IP address and netmask directly in the list view. list_editable = ('address', 'netmask') + # Enable filtering based on project and location. + list_filter = ['entity__project', 'entity__location'] - +# Register our admin classes. admin.site.register(Project) admin.site.register(Location) admin.site.register(Entity, EntityAdmin)