Add fragment type in admin
[flowspy] / flowspec / admin.py
1 #
2 # -*- coding: utf-8 -*- vim:fileencoding=utf-8:
3 #Copyright © 2011-2013 Greek Research and Technology Network (GRNET S.A.)
4
5 #Developed by Leonidas Poulopoulos (leopoul-at-noc-dot-grnet-dot-gr),
6 #GRNET NOC
7 #
8 #Permission to use, copy, modify, and/or distribute this software for any
9 #purpose with or without fee is hereby granted, provided that the above
10 #copyright notice and this permission notice appear in all copies.
11 #
12 #THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
13 #TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
14 #FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
15 #CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
16 #DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
17 #ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
18 #SOFTWARE.
19 #
20 from django.contrib import admin
21 from flowspy.flowspec.models import *
22 from flowspy.accounts.models import *
23 from utils import proxy as PR
24 from flowspec.tasks import *
25 from django.contrib.auth.models import User
26 from django.contrib.auth.admin import UserAdmin
27 from flowspy.peers.models import *
28 from flowspy.flowspec.forms import *
29 import datetime
30 from django.conf import settings
31
32 from flowspy.monkey_patch.forms import UserCreationForm, UserChangeForm
33
34 class RouteAdmin(admin.ModelAdmin):
35     form = RouteForm
36     actions = ['deactivate']
37     
38     def deactivate(self, request, queryset):
39         queryset = queryset.filter(status='ACTIVE')
40         response = batch_delete.delay(queryset, reason="ADMININACTIVE")
41         self.message_user(request, "Added request %s to job que. Check in a while for result" % response)
42     deactivate.short_description = "Remove selected routes from network"
43
44     def save_model(self, request, obj, form, change):
45         obj.status = "PENDING"
46         obj.save()
47         if change:
48             obj.commit_edit()
49         else:
50             obj.commit_add()
51
52     def has_delete_permission(self, request, obj=None):
53         return False
54
55     list_display = ('name', 'status', 'applier' , 'applier_peer', 'get_match', 'get_then', 'response', "expires", "comments")
56
57     fieldsets = [
58         (None,               {'fields': ['name','applier']}),
59         ("Match",               {'fields': ['source', 'sourceport', 'destination', 'destinationport', 'port']}),
60         ('Advanced Match Statements', {'fields': ['dscp', 'fragmenttype', 'icmpcode', 'icmptype', 'packetlength', 'protocol', 'tcpflag'], 'classes': ['collapse']}),
61         ("Then",               {'fields': ['then' ]}),
62         ("Expires",               {'fields': ['expires' ]}),
63         (None,               {'fields': ['comments',]}),
64         
65     ]
66     
67
68
69 class UserProfileInline(admin.StackedInline):
70     model = UserProfile
71     
72 class UserProfileAdmin(UserAdmin):
73     add_form = UserCreationForm
74     form = UserChangeForm
75     actions = ['deactivate', 'activate']
76     list_display = ('username', 'email', 'first_name' , 'last_name', 'is_staff', 'is_active', 'is_superuser', 'get_userprofile_peer')
77     inlines = [UserProfileInline]
78
79     def deactivate(self, request, queryset):
80         queryset = queryset.update(is_active=False)
81     deactivate.short_description = "Deactivate Selected Users"
82
83     def activate(self, request, queryset):
84         queryset = queryset.update(is_active=True)
85     activate.short_description = "Activate Selected Users"
86
87     def get_userprofile_peer(self, instance):
88         # instance is User instance
89         return instance.get_profile().peer
90     get_userprofile_peer.short_description = "User Peer"
91 #    fields = ('name', 'applier', 'expires')
92
93     #def formfield_for_dbfield(self, db_field, **kwargs):
94     #    if db_field.name == 'password':
95     #        kwargs['widget'] = PasswordInput
96     #    return db_field.formfield(**kwargs)
97
98 admin.site.unregister(User)
99 admin.site.register(MatchPort)
100 admin.site.register(MatchProtocol)
101 admin.site.register(MatchDscp)
102 admin.site.register(ThenAction)
103 admin.site.register(FragmentType)
104 admin.site.register(Route, RouteAdmin)
105 admin.site.register(User, UserProfileAdmin)
106 admin.site.disable_action('delete_selected')
107
108