blob: 49019396f25ad687221104d800b17778b91ff0c9 [file] [log] [blame]
Scott Baker87eb7402016-06-20 17:21:50 -07001from django.contrib import admin
2
3from services.vrouter.models import *
4from django import forms
5from django.utils.safestring import mark_safe
6from django.contrib.auth.admin import UserAdmin
7from django.contrib.admin.widgets import FilteredSelectMultiple
8from django.contrib.auth.forms import ReadOnlyPasswordHashField
9from django.contrib.auth.signals import user_logged_in
10from django.utils import timezone
11from django.contrib.contenttypes import generic
12from suit.widgets import LinkedSelect
13from core.models import AddressPool
14from core.admin import ServiceAppAdmin,SliceInline,ServiceAttrAsTabInline, ReadOnlyAwareAdmin, XOSTabularInline, ServicePrivilegeInline, AddressPoolInline
15from core.middleware import get_request
16
17from functools import update_wrapper
18from django.contrib.admin.views.main import ChangeList
19from django.core.urlresolvers import reverse
20from django.contrib.admin.utils import quote
21
22class VRouterServiceForm(forms.ModelForm):
23 def __init__(self,*args,**kwargs):
24 super (VRouterServiceForm,self ).__init__(*args,**kwargs)
25
26 def save(self, commit=True):
27 return super(VRouterServiceForm, self).save(commit=commit)
28
29 class Meta:
30 model = VRouterService
Zack Williamsbde22ab2016-06-27 12:27:01 -070031 fields = '__all__'
Scott Baker87eb7402016-06-20 17:21:50 -070032
33class VRouterServiceAdmin(ReadOnlyAwareAdmin):
34 model = VRouterService
35 verbose_name = "vRouter Service"
36 verbose_name_plural = "vRouter Service"
37 list_display = ("backend_status_icon", "name", "enabled")
38 list_display_links = ('backend_status_icon', 'name', )
39 fieldsets = [(None, {'fields': ['backend_status_text', 'name','enabled','versionNumber', 'description', "view_url", "icon_url", ],
40 'classes':['suit-tab suit-tab-general']})]
41 readonly_fields = ('backend_status_text', )
42 inlines = [SliceInline,ServiceAttrAsTabInline,ServicePrivilegeInline,AddressPoolInline]
43 form = VRouterServiceForm
44
45 extracontext_registered_admins = True
46
47 user_readonly_fields = ["name", "enabled", "versionNumber", "description"]
48
49 suit_form_tabs =(('general', 'vRouter Service Details'),
50 ('administration', 'Administration'),
51 ('addresspools', 'Addresses'),
52 #('tools', 'Tools'),
53 ('slices','Slices'),
54 ('serviceattrs','Additional Attributes'),
55 ('serviceprivileges','Privileges'),
56 )
57
58 suit_form_includes = (('vrouteradmin.html', 'top', 'administration'),
59 ) #('hpctools.html', 'top', 'tools') )
60
Zack Williamsbde22ab2016-06-27 12:27:01 -070061 def get_queryset(self, request):
Scott Baker87eb7402016-06-20 17:21:50 -070062 return VRouterService.get_service_objects_by_user(request.user)
63
64class VRouterTenantForm(forms.ModelForm):
65 public_ip = forms.CharField(required=True)
66 public_mac = forms.CharField(required=True)
67 gateway_ip = forms.CharField(required=False)
68 gateway_mac = forms.CharField(required=False)
69 cidr = forms.CharField(required=False)
70 address_pool = forms.ModelChoiceField(queryset=AddressPool.objects.all(),required=False)
71
72 def __init__(self,*args,**kwargs):
73 super (VRouterTenantForm,self ).__init__(*args,**kwargs)
74 self.fields['kind'].widget.attrs['readonly'] = True
75 self.fields['provider_service'].queryset = VRouterService.get_service_objects().all()
76 if self.instance:
77 # fields for the attributes
78 self.fields['address_pool'].initial = self.instance.address_pool
79 self.fields['public_ip'].initial = self.instance.public_ip
80 self.fields['public_mac'].initial = self.instance.public_mac
81 self.fields['gateway_ip'].initial = self.instance.gateway_ip
82 self.fields['gateway_mac'].initial = self.instance.gateway_mac
83 self.fields['cidr'].initial = self.instance.cidr
84 if (not self.instance) or (not self.instance.pk):
85 # default fields for an 'add' form
86 self.fields['kind'].initial = VROUTER_KIND
87 if VRouterService.get_service_objects().exists():
88 self.fields["provider_service"].initial = VRouterService.get_service_objects().all()[0]
89
90 def save(self, commit=True):
91 self.instance.public_ip = self.cleaned_data.get("public_ip")
92 self.instance.public_mac = self.cleaned_data.get("public_mac")
93 self.instance.address_pool = self.cleaned_data.get("address_pool")
94 return super(VRouterTenantForm, self).save(commit=commit)
95
96 class Meta:
97 model = VRouterTenant
Zack Williamsedbf95f2016-06-27 08:31:44 -070098 fields = '__all__'
Scott Baker87eb7402016-06-20 17:21:50 -070099
100class VRouterTenantAdmin(ReadOnlyAwareAdmin):
101 list_display = ('backend_status_icon', 'id', 'subscriber_tenant', 'public_ip' )
102 list_display_links = ('backend_status_icon', 'id')
103 fieldsets = [ (None, {'fields': ['backend_status_text', 'kind', 'provider_service', 'subscriber_tenant', 'subscriber_service',
104 'address_pool', 'public_ip', 'public_mac', 'gateway_ip', 'gateway_mac', 'cidr'],
105 'classes':['suit-tab suit-tab-general']})]
106 readonly_fields = ('backend_status_text', 'service_specific_attribute', 'gateway_ip', 'gateway_mac', 'cidr')
107 form = VRouterTenantForm
108
109 suit_form_tabs = (('general','Details'),)
110
Zack Williamsbde22ab2016-06-27 12:27:01 -0700111 def get_queryset(self, request):
Scott Baker87eb7402016-06-20 17:21:50 -0700112 return VRouterTenant.get_tenant_objects_by_user(request.user)
113
114admin.site.register(VRouterService, VRouterServiceAdmin)
115admin.site.register(VRouterTenant, VRouterTenantAdmin)
116