blob: 285e7513f414935d44ccab349979dcd1e0069811 [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
Scott Bakercafcd552015-10-15 16:09:18 -070011from suit.widgets import LinkedSelect
12from core.admin import ServiceAppAdmin,SliceInline,ServiceAttrAsTabInline, ReadOnlyAwareAdmin, XOSTabularInline, ServicePrivilegeInline, TenantRootTenantInline, TenantRootPrivilegeInline, TenantAttrAsTabInline
13from core.middleware import get_request
14
15from functools import update_wrapper
16from django.contrib.admin.views.main import ChangeList
17from django.core.urlresolvers import reverse
18from django.contrib.admin.utils import quote
19
Scott Baker6f578922015-10-19 17:37:01 -070020class ONOSServiceForm(forms.ModelForm):
Scott Bakerb2d8d212016-02-10 12:25:41 -080021 rest_hostname = forms.CharField(required=False)
22 rest_port = forms.CharField(required=False)
23 no_container = forms.BooleanField(required=False)
24# external_hostname = forms.CharField(required=False)
25# external_container = forms.CharField(required=False)
Scott Baker6f578922015-10-19 17:37:01 -070026
Sapan Bhatia16be1432016-01-14 11:41:38 -050027 def __init__(self,*args,**kwargs):
28 super (ONOSServiceForm,self ).__init__(*args,**kwargs)
29 if self.instance:
30 # fields for the attributes
Scott Bakerb2d8d212016-02-10 12:25:41 -080031 self.fields['rest_hostname'].initial = self.instance.rest_hostname
32 self.fields['rest_port'].initial = self.instance.rest_port
33 self.fields['no_container'].initial = self.instance.no_container
34# self.fields['external_hostname'].initial = self.instance.external_hostname
35# self.fields['external_container'].initial = self.instance.external_hostname
Sapan Bhatia16be1432016-01-14 11:41:38 -050036
37 def save(self, commit=True):
Scott Bakerb2d8d212016-02-10 12:25:41 -080038 self.instance.rest_hostname = self.cleaned_data.get("rest_hostname")
39 self.instance.rest_port = self.cleaned_data.get("rest_port")
40 self.instance.no_container = self.cleaned_data.get("no_container")
41# self.instance.external_hostname = self.cleaned_data.get("external_hostname")
42# self.instance.external_container = self.cleaned_data.get("external_container")
Sapan Bhatia16be1432016-01-14 11:41:38 -050043 return super(ONOSServiceForm, self).save(commit=commit)
44
45 class Meta:
46 model = ONOSService
Zack Williamscca78892016-04-21 15:41:28 -070047 fields = '__all__'
Sapan Bhatia16be1432016-01-14 11:41:38 -050048
Scott Bakercafcd552015-10-15 16:09:18 -070049class ONOSServiceAdmin(ReadOnlyAwareAdmin):
50 model = ONOSService
51 verbose_name = "ONOS Service"
52 verbose_name_plural = "ONOS Services"
53 list_display = ("backend_status_icon", "name", "enabled")
54 list_display_links = ('backend_status_icon', 'name', )
Scott Bakerb2d8d212016-02-10 12:25:41 -080055 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 -070056 readonly_fields = ('backend_status_text', )
57 inlines = [SliceInline,ServiceAttrAsTabInline,ServicePrivilegeInline]
Scott Baker6f578922015-10-19 17:37:01 -070058 form = ONOSServiceForm
Scott Bakercafcd552015-10-15 16:09:18 -070059
60 extracontext_registered_admins = True
61
62 user_readonly_fields = ["name", "enabled", "versionNumber", "description"]
63
64 suit_form_tabs =(('general', 'ONOS Service Details'),
65 ('administration', 'Administration'),
66 ('slices','Slices'),
67 ('serviceattrs','Additional Attributes'),
68 ('serviceprivileges','Privileges'),
69 )
70
71 suit_form_includes = (('onosadmin.html', 'top', 'administration'),
72 )
73
Zack Williams4a435692016-05-16 15:57:00 -070074 def get_queryset(self, request):
Scott Bakercafcd552015-10-15 16:09:18 -070075 return ONOSService.get_service_objects_by_user(request.user)
76
77class ONOSAppForm(forms.ModelForm):
78 creator = forms.ModelChoiceField(queryset=User.objects.all())
79 name = forms.CharField()
Scott Baker6f578922015-10-19 17:37:01 -070080 dependencies = forms.CharField(required=False)
Scott Bakercafcd552015-10-15 16:09:18 -070081
Sapan Bhatia16be1432016-01-14 11:41:38 -050082 def __init__(self,*args,**kwargs):
83 super (ONOSAppForm,self ).__init__(*args,**kwargs)
84 self.fields['kind'].widget.attrs['readonly'] = True
85 self.fields['provider_service'].queryset = ONOSService.get_service_objects().all()
86 if self.instance:
87 # fields for the attributes
88 self.fields['creator'].initial = self.instance.creator
89 self.fields['name'].initial = self.instance.name
90 self.fields['dependencies'].initial = self.instance.dependencies
91 if (not self.instance) or (not self.instance.pk):
92 # default fields for an 'add' form
93 self.fields['kind'].initial = ONOS_KIND
94 self.fields['creator'].initial = get_request().user
95 if ONOSService.get_service_objects().exists():
96 self.fields["provider_service"].initial = ONOSService.get_service_objects().all()[0]
97
98 def save(self, commit=True):
99 self.instance.creator = self.cleaned_data.get("creator")
100 self.instance.name = self.cleaned_data.get("name")
101 self.instance.dependencies = self.cleaned_data.get("dependencies")
102 return super(ONOSAppForm, self).save(commit=commit)
103
104 class Meta:
Scott Bakercafcd552015-10-15 16:09:18 -0700105 model = ONOSApp
Zack Williamscca78892016-04-21 15:41:28 -0700106 fields = '__all__'
Scott Bakercafcd552015-10-15 16:09:18 -0700107
108class ONOSAppAdmin(ReadOnlyAwareAdmin):
109 list_display = ('backend_status_icon', 'name', )
110 list_display_links = ('backend_status_icon', 'name')
Scott Bakerafa48eb2015-10-20 21:22:23 -0700111 fieldsets = [ (None, {'fields': ['backend_status_text', 'kind', 'name', 'provider_service', 'subscriber_service', 'service_specific_attribute', "dependencies",
Scott Bakercafcd552015-10-15 16:09:18 -0700112 'creator'],
113 'classes':['suit-tab suit-tab-general']})]
114 readonly_fields = ('backend_status_text', 'instance', 'service_specific_attribute')
115 inlines = [TenantAttrAsTabInline]
116 form = ONOSAppForm
117
118 suit_form_tabs = (('general','Details'), ('tenantattrs', 'Attributes'))
119
Zack Williams4a435692016-05-16 15:57:00 -0700120 def get_queryset(self, request):
Scott Bakercafcd552015-10-15 16:09:18 -0700121 return ONOSApp.get_tenant_objects_by_user(request.user)
122
123admin.site.register(ONOSService, ONOSServiceAdmin)
124admin.site.register(ONOSApp, ONOSAppAdmin)
125