blob: 50ac7fc60ae1aa09f3b2119b0371f2d0e91e86b8 [file] [log] [blame]
Matteo Scandolo35113f72017-08-08 13:05:25 -07001
2# Copyright 2017-present Open Networking Foundation
3#
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16
Scott Baker619de672016-06-20 12:49:38 -070017# admin.py - ExampleService Django Admin
18
19from core.admin import ReadOnlyAwareAdmin, SliceInline
20from core.middleware import get_request
21from core.models import User
22
23from django import forms
24from django.contrib import admin
25
26from services.exampleservice.models import *
27
28class ExampleServiceForm(forms.ModelForm):
29
30 class Meta:
31 model = ExampleService
Zack Williamsc6318902016-06-27 08:21:19 -070032 fields = '__all__'
Scott Baker619de672016-06-20 12:49:38 -070033
34 def __init__(self, *args, **kwargs):
35 super(ExampleServiceForm, self).__init__(*args, **kwargs)
36
37 if self.instance:
38 self.fields['service_message'].initial = self.instance.service_message
39
40 def save(self, commit=True):
41 self.instance.service_message = self.cleaned_data.get('service_message')
42 return super(ExampleServiceForm, self).save(commit=commit)
43
44class ExampleServiceAdmin(ReadOnlyAwareAdmin):
45
46 model = ExampleService
47 verbose_name = SERVICE_NAME_VERBOSE
48 verbose_name_plural = SERVICE_NAME_VERBOSE_PLURAL
49 form = ExampleServiceForm
50 inlines = [SliceInline]
51
52 list_display = ('backend_status_icon', 'name', 'service_message', 'enabled')
53 list_display_links = ('backend_status_icon', 'name', 'service_message' )
54
55 fieldsets = [(None, {
56 'fields': ['backend_status_text', 'name', 'enabled', 'versionNumber', 'service_message', 'description',],
57 'classes':['suit-tab suit-tab-general',],
58 })]
59
60 readonly_fields = ('backend_status_text', )
61 user_readonly_fields = ['name', 'enabled', 'versionNumber', 'description',]
62
63 extracontext_registered_admins = True
64
65 suit_form_tabs = (
66 ('general', 'Example Service Details', ),
67 ('slices', 'Slices',),
68 )
69
70 suit_form_includes = ((
71 'top',
72 'administration'),
73 )
74
Zack Williams86f0b7f2016-06-27 13:21:24 -070075 def get_queryset(self, request):
Scott Baker589e6c42017-05-25 16:55:18 -070076 return ExampleService.select_by_user(request.user)
Scott Baker619de672016-06-20 12:49:38 -070077
78admin.site.register(ExampleService, ExampleServiceAdmin)
79
80class ExampleTenantForm(forms.ModelForm):
81
82 class Meta:
83 model = ExampleTenant
Zack Williamsc6318902016-06-27 08:21:19 -070084 fields = '__all__'
Scott Baker619de672016-06-20 12:49:38 -070085
86 creator = forms.ModelChoiceField(queryset=User.objects.all())
87
88 def __init__(self, *args, **kwargs):
89 super(ExampleTenantForm, self).__init__(*args, **kwargs)
90
91 self.fields['kind'].widget.attrs['readonly'] = True
92 self.fields['kind'].initial = SERVICE_NAME
93
Scott Baker589e6c42017-05-25 16:55:18 -070094 self.fields['provider_service'].queryset = ExampleService.objects.all()
Scott Baker619de672016-06-20 12:49:38 -070095
96 if self.instance:
97 self.fields['creator'].initial = self.instance.creator
98 self.fields['tenant_message'].initial = self.instance.tenant_message
99
100 if (not self.instance) or (not self.instance.pk):
101 self.fields['creator'].initial = get_request().user
Scott Baker589e6c42017-05-25 16:55:18 -0700102 if ExampleService.objects.exists():
103 self.fields['provider_service'].initial = ExampleService.objects.all()[0]
Scott Baker619de672016-06-20 12:49:38 -0700104
105 def save(self, commit=True):
106 self.instance.creator = self.cleaned_data.get('creator')
107 self.instance.tenant_message = self.cleaned_data.get('tenant_message')
108 return super(ExampleTenantForm, self).save(commit=commit)
109
110
111class ExampleTenantAdmin(ReadOnlyAwareAdmin):
112
113 verbose_name = TENANT_NAME_VERBOSE
114 verbose_name_plural = TENANT_NAME_VERBOSE_PLURAL
115
116 list_display = ('id', 'backend_status_icon', 'instance', 'tenant_message')
117 list_display_links = ('backend_status_icon', 'instance', 'tenant_message', 'id')
118
119 fieldsets = [(None, {
120 'fields': ['backend_status_text', 'kind', 'provider_service', 'instance', 'creator', 'tenant_message'],
121 'classes': ['suit-tab suit-tab-general'],
122 })]
123
124 readonly_fields = ('backend_status_text', 'instance',)
125
126 form = ExampleTenantForm
127
128 suit_form_tabs = (('general', 'Details'),)
129
Zack Williams86f0b7f2016-06-27 13:21:24 -0700130 def get_queryset(self, request):
Scott Baker589e6c42017-05-25 16:55:18 -0700131 return ExampleTenant.select_by_user(request.user)
Scott Baker619de672016-06-20 12:49:38 -0700132
133admin.site.register(ExampleTenant, ExampleTenantAdmin)
134