blob: f7347aeedc6e25dd0b5b4924c387604c3db3096d [file] [log] [blame]
Scott Bakercafcd552015-10-15 16:09:18 -07001from django.contrib import admin
2
3from services.onos.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.admin import ServiceAppAdmin,SliceInline,ServiceAttrAsTabInline, ReadOnlyAwareAdmin, XOSTabularInline, ServicePrivilegeInline, TenantRootTenantInline, TenantRootPrivilegeInline, TenantAttrAsTabInline
14from core.middleware import get_request
15
16from functools import update_wrapper
17from django.contrib.admin.views.main import ChangeList
18from django.core.urlresolvers import reverse
19from django.contrib.admin.utils import quote
20
Scott Baker6f578922015-10-19 17:37:01 -070021class ONOSServiceForm(forms.ModelForm):
Scott Bakerb2d8d212016-02-10 12:25:41 -080022 rest_hostname = forms.CharField(required=False)
23 rest_port = forms.CharField(required=False)
24 no_container = forms.BooleanField(required=False)
25# external_hostname = forms.CharField(required=False)
26# external_container = forms.CharField(required=False)
Scott Baker6f578922015-10-19 17:37:01 -070027
Sapan Bhatia16be1432016-01-14 11:41:38 -050028 def __init__(self,*args,**kwargs):
29 super (ONOSServiceForm,self ).__init__(*args,**kwargs)
30 if self.instance:
31 # fields for the attributes
Scott Bakerb2d8d212016-02-10 12:25:41 -080032 self.fields['rest_hostname'].initial = self.instance.rest_hostname
33 self.fields['rest_port'].initial = self.instance.rest_port
34 self.fields['no_container'].initial = self.instance.no_container
35# self.fields['external_hostname'].initial = self.instance.external_hostname
36# self.fields['external_container'].initial = self.instance.external_hostname
Sapan Bhatia16be1432016-01-14 11:41:38 -050037
38 def save(self, commit=True):
Scott Bakerb2d8d212016-02-10 12:25:41 -080039 self.instance.rest_hostname = self.cleaned_data.get("rest_hostname")
40 self.instance.rest_port = self.cleaned_data.get("rest_port")
41 self.instance.no_container = self.cleaned_data.get("no_container")
42# self.instance.external_hostname = self.cleaned_data.get("external_hostname")
43# self.instance.external_container = self.cleaned_data.get("external_container")
Sapan Bhatia16be1432016-01-14 11:41:38 -050044 return super(ONOSServiceForm, self).save(commit=commit)
45
46 class Meta:
47 model = ONOSService
Zack Williamscca78892016-04-21 15:41:28 -070048 fields = '__all__'
Sapan Bhatia16be1432016-01-14 11:41:38 -050049
Scott Bakercafcd552015-10-15 16:09:18 -070050class ONOSServiceAdmin(ReadOnlyAwareAdmin):
51 model = ONOSService
52 verbose_name = "ONOS Service"
53 verbose_name_plural = "ONOS Services"
54 list_display = ("backend_status_icon", "name", "enabled")
55 list_display_links = ('backend_status_icon', 'name', )
Scott Bakerb2d8d212016-02-10 12:25:41 -080056 fieldsets = [(None, {'fields': ['backend_status_text', 'name','enabled','versionNumber', 'description',"view_url","icon_url", "rest_hostname", "rest_port", "no_container" ], 'classes':['suit-tab suit-tab-general']})]
Scott Bakercafcd552015-10-15 16:09:18 -070057 readonly_fields = ('backend_status_text', )
58 inlines = [SliceInline,ServiceAttrAsTabInline,ServicePrivilegeInline]
Scott Baker6f578922015-10-19 17:37:01 -070059 form = ONOSServiceForm
Scott Bakercafcd552015-10-15 16:09:18 -070060
61 extracontext_registered_admins = True
62
63 user_readonly_fields = ["name", "enabled", "versionNumber", "description"]
64
65 suit_form_tabs =(('general', 'ONOS Service Details'),
66 ('administration', 'Administration'),
67 ('slices','Slices'),
68 ('serviceattrs','Additional Attributes'),
69 ('serviceprivileges','Privileges'),
70 )
71
72 suit_form_includes = (('onosadmin.html', 'top', 'administration'),
73 )
74
75 def queryset(self, request):
76 return ONOSService.get_service_objects_by_user(request.user)
77
78class ONOSAppForm(forms.ModelForm):
79 creator = forms.ModelChoiceField(queryset=User.objects.all())
80 name = forms.CharField()
Scott Baker6f578922015-10-19 17:37:01 -070081 dependencies = forms.CharField(required=False)
Scott Bakercafcd552015-10-15 16:09:18 -070082
Sapan Bhatia16be1432016-01-14 11:41:38 -050083 def __init__(self,*args,**kwargs):
84 super (ONOSAppForm,self ).__init__(*args,**kwargs)
85 self.fields['kind'].widget.attrs['readonly'] = True
86 self.fields['provider_service'].queryset = ONOSService.get_service_objects().all()
87 if self.instance:
88 # fields for the attributes
89 self.fields['creator'].initial = self.instance.creator
90 self.fields['name'].initial = self.instance.name
91 self.fields['dependencies'].initial = self.instance.dependencies
92 if (not self.instance) or (not self.instance.pk):
93 # default fields for an 'add' form
94 self.fields['kind'].initial = ONOS_KIND
95 self.fields['creator'].initial = get_request().user
96 if ONOSService.get_service_objects().exists():
97 self.fields["provider_service"].initial = ONOSService.get_service_objects().all()[0]
98
99 def save(self, commit=True):
100 self.instance.creator = self.cleaned_data.get("creator")
101 self.instance.name = self.cleaned_data.get("name")
102 self.instance.dependencies = self.cleaned_data.get("dependencies")
103 return super(ONOSAppForm, self).save(commit=commit)
104
105 class Meta:
Scott Bakercafcd552015-10-15 16:09:18 -0700106 model = ONOSApp
Zack Williamscca78892016-04-21 15:41:28 -0700107 fields = '__all__'
Scott Bakercafcd552015-10-15 16:09:18 -0700108
109class ONOSAppAdmin(ReadOnlyAwareAdmin):
110 list_display = ('backend_status_icon', 'name', )
111 list_display_links = ('backend_status_icon', 'name')
Scott Bakerafa48eb2015-10-20 21:22:23 -0700112 fieldsets = [ (None, {'fields': ['backend_status_text', 'kind', 'name', 'provider_service', 'subscriber_service', 'service_specific_attribute', "dependencies",
Scott Bakercafcd552015-10-15 16:09:18 -0700113 'creator'],
114 'classes':['suit-tab suit-tab-general']})]
115 readonly_fields = ('backend_status_text', 'instance', 'service_specific_attribute')
116 inlines = [TenantAttrAsTabInline]
117 form = ONOSAppForm
118
119 suit_form_tabs = (('general','Details'), ('tenantattrs', 'Attributes'))
120
121 def queryset(self, request):
122 return ONOSApp.get_tenant_objects_by_user(request.user)
123
124admin.site.register(ONOSService, ONOSServiceAdmin)
125admin.site.register(ONOSApp, ONOSAppAdmin)
126