Files
@ 421171af6c75
Branch filter:
Location: conntrackt/conntrackt/admin.py
421171af6c75
10.2 KiB
text/x-python
CONNT-25: Updating application and project to use Django 1.11.x:
- Bumped Django version in both the development requirements and setup
script to 1.11.x.
- Bumped Django Crispy Forms to version 1.7.0 in both development
requirements and setup script.
- Updated import of URL-related modules to use the new path (previous
one is deprecated).
- Added explicit on_delete = models.CASCADE option to all foreign key
fields (old implicit behaviour will be deprecated in Django 2.0).
- Fixed the custom change_list.html template used in Django Admin to
render without errors.
- Bumped Django version in both the development requirements and setup
script to 1.11.x.
- Bumped Django Crispy Forms to version 1.7.0 in both development
requirements and setup script.
- Updated import of URL-related modules to use the new path (previous
one is deprecated).
- Added explicit on_delete = models.CASCADE option to all foreign key
fields (old implicit behaviour will be deprecated in Django 2.0).
- Fixed the custom change_list.html template used in Django Admin to
render without errors.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 | # -*- 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/>.
#
# -*- 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/>.
#
# 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
class CommunicationProjectListFilter(admin.SimpleListFilter):
"""
This class implements a project-based filter for the communications list
view. The filter is applied on both the source and destination field of a
communication.
The filter assumes that the communication belongs to a project by following
the relationships through source and destination field towards interface,
then entity, and then finally entity's project.
Both source and destination must fullfil the requirement of belonging to the
same project in order for the communication to be part of the resulting
queryset.
"""
# Set-up the filter title and parameter name that will be used for GET
# request.
title = "project"
parameter_name = "project"
def lookups(self, request, model_admin):
"""
Returns a list of tuples that provide possible filter values that can be
applied.
Arguments:
request - Request associated with the calling view.
model_admin - Modem admin that can be used for accessing the model
data.
Returns:
List of (project_id, project_object) tuples.
"""
return [(p.id, p) for p in Project.objects.all()]
def queryset(self, request, queryset):
"""
Applies filtering by project ID on the provided communication queryset.
Arguments:
request - Request associated with the calling view.
queryset - Current queryset used for displaying the information in the
view.
Returns:
Queryset with applied filtering by object (if any). If no filtering
needs to be done, returns original queryset.
"""
# Apply the project filter on source and destination entity's project
# ID, if it was specified.
if self.value():
return queryset.filter(source__entity__project=self.value(),
destination__entity__project=self.value())
return queryset
class CommunicationLocationListFilter(admin.SimpleListFilter):
"""
This class implements a location-based filter for the communications list
view. The filter is applied on both the source and destination field of a
communication.
The filter assumes that the communication belongs to a location by following
the relationships through source and destination field towards interface,
then entity, and then finally entity's location.
Both source and destination must fullfil the requirement of belonging to the
same location in order for the communication to be part of the resulting
queryset.
"""
# Set-up the filter title and parameter name that will be used for GET
# request.
title = "location"
parameter_name = "location"
def lookups(self, request, model_admin):
"""
Returns a list of tuples that provide possible filter values that can be
applied.
Arguments:
request - Request associated with the calling view.
model_admin - Modem admin that can be used for accessing the model
data.
Returns:
List of (project_id, project_object) tuples.
"""
return [(p.id, p) for p in Location.objects.all()]
def queryset(self, request, queryset):
"""
Applies filtering by project ID on the provided communication queryset.
Arguments:
request - Request associated with the calling view.
queryset - Current queryset used for displaying the information in the
view.
Returns:
Queryset with applied filtering by object (if any). If no filtering
needs to be done, returns original queryset.
"""
# Apply the location filter on source and destination entity's project
# ID, if it was specified.
if self.value():
return queryset.filter(source__entity__location=self.value(),
destination__entity__location=self.value())
return queryset
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.
"""
# 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')
# 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.
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)
admin.site.register(Interface, InterfaceAdmin)
admin.site.register(Communication, CommunicationAdmin)
|