blob: 303f75819f35e0a53f07c566772eff358576af1e [file] [log] [blame]
Pingping Linf3e24a92016-09-19 21:35:16 +00001
Matteo Scandolo86d63692017-08-08 13:05:26 -07002# 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
17
Pingping Linf3e24a92016-09-19 21:35:16 +000018from core.admin import ReadOnlyAwareAdmin, SliceInline
19from core.middleware import get_request
20from core.models import User
21from django import forms
22from django.contrib import admin
Pingping Linc537fd92017-01-17 20:52:09 -080023from services.vpgwc.models import VPGWCService, VPGWCTenant, MCORD_KIND
24
25class VPGWCServiceForm(forms.ModelForm):
26
27 class Meta:
28 model = VPGWCService
29 fields = '__all__'
30
31 def __init__(self, *args, **kwargs):
32 super(VPGWCServiceForm, self).__init__(*args, **kwargs)
33
34 def save(self, commit=True):
35 return super(VPGWCServiceForm, self).save(commit=commit)
Pingping Linf3e24a92016-09-19 21:35:16 +000036
37# The class to provide an admin interface on the web for the service.
38# We do only configuration here and don't change any logic because the logic
39# is taken care of for us by ReadOnlyAwareAdmin
40class VPGWCServiceAdmin(ReadOnlyAwareAdmin):
41 # We must set the model so that the admin knows what fields to use
42 model = VPGWCService
43 verbose_name = "vPGWC Service"
44 verbose_name_plural = "vPGWC Services"
Pingping Linc537fd92017-01-17 20:52:09 -080045 # Inlines are used to denote other models that can be edited on the same
46 # form as this one. In this case the service form also allows changes
47 # to slices.
48 inlines = [SliceInline]
Pingping Linf3e24a92016-09-19 21:35:16 +000049
50 # Setting list_display creates columns on the admin page, each value here
51 # is a column, the column is populated for every instance of the model.
52 list_display = ("backend_status_icon", "name", "enabled")
53
54 # Used to indicate which values in the columns of the admin form are links.
55 list_display_links = ('backend_status_icon', 'name', )
56
57 # Denotes the sections of the form, the fields in the section, and the
58 # CSS classes used to style them. We represent this as a set of tuples, each
59 # tuple as a name (or None) and a set of fields and classes.
60 # Here the first section does not have a name so we use none. That first
61 # section has several fields indicated in the 'fields' attribute, and styled
62 # by the classes indicated in the 'classes' attribute. The classes given
63 # here are important for rendering the tabs on the form. To give the tabs
64 # we must assign the classes suit-tab and suit-tab-<name> where
65 # where <name> will be used later.
66 fieldsets = [(None, {'fields': ['backend_status_text', 'name', 'enabled',
67 'versionNumber', 'description', "view_url"],
68 'classes':['suit-tab suit-tab-general']})]
69
70 # Denotes the fields that are readonly and cannot be changed.
71 readonly_fields = ('backend_status_text', )
72
Pingping Linf3e24a92016-09-19 21:35:16 +000073 extracontext_registered_admins = True
74
75 # Denotes the fields that can be changed by an admin but not be all users
76 user_readonly_fields = ["name", "enabled", "versionNumber", "description"]
77
78 # Associates fieldsets from this form and from the inlines.
79 # The format here are tuples, of (<name>, tab title). <name> comes from the
80 # <name> in the fieldsets.
81 suit_form_tabs = (('general', 'vPGWC Service Details'),
82 ('administration', 'Components'),
83 ('slices', 'Slices'),)
84
85 # Used to include a template for a tab. Here we include the
86 # helloworldserviceadmin template in the top position for the administration
87 # tab.
88 suit_form_includes = (('mcordadmin.html',
89 'top',
90 'administration'),)
91
92 # Used to get the objects for this model that are associated with the
93 # requesting user.
94 def queryset(self, request):
95 return VPGWCService.get_service_objects_by_user(request.user)
96
97# Class to represent the form to add and edit tenants.
98# We need to define this instead of just using an admin like we did for the
99# service because tenants vary more than services and there isn't a common form.
100# This allows us to change the python behavior for the admin form to save extra
101# fields and control defaults.
Pingping Linc537fd92017-01-17 20:52:09 -0800102class VPGWCTenantForm(forms.ModelForm):
103
104 class Meta:
105 model = VPGWCTenant
106 fields = '__all__'
Pingping Linf3e24a92016-09-19 21:35:16 +0000107 # Defines a field for the creator of this service. It is a dropdown which
108 # is populated with all of the users.
109 creator = forms.ModelChoiceField(queryset=User.objects.all())
110 # Defines a text field for the display message, it is not required.
111 display_message = forms.CharField(required=False)
112
113 def __init__(self, *args, **kwargs):
Pingping Linc537fd92017-01-17 20:52:09 -0800114 super(VPGWCTenantForm, self).__init__(*args, **kwargs)
Pingping Linf3e24a92016-09-19 21:35:16 +0000115 # Set the kind field to readonly
116 self.fields['kind'].widget.attrs['readonly'] = True
Pingping Linc537fd92017-01-17 20:52:09 -0800117 self.fields['kind'].initial = "vpgwc"
Pingping Linf3e24a92016-09-19 21:35:16 +0000118 # Define the logic for obtaining the objects for the provider_service
119 # dropdown of the tenant form.
120 self.fields[
121 'provider_service'].queryset = VPGWCService.get_service_objects().all()
122 # Set the initial kind to HELLO_WORLD_KIND for this tenant.
123 self.fields['kind'].initial = MCORD_KIND
124 # If there is an instance of this model then we can set the initial
125 # form values to the existing values.
126 if self.instance:
127 self.fields['creator'].initial = self.instance.creator
128 self.fields[
129 'display_message'].initial = self.instance.display_message
Saleil Bhat4f45a4f2017-02-01 17:04:22 -0800130 self.fields[
131 'image_name'].initial = self.instance.image_name
Pingping Linf3e24a92016-09-19 21:35:16 +0000132
133 # If there is not an instance then we need to set initial values.
134 if (not self.instance) or (not self.instance.pk):
135 self.fields['creator'].initial = get_request().user
136 if VPGWCService.get_service_objects().exists():
137 self.fields["provider_service"].initial = VPGWCService.get_service_objects().all()[0]
Pingping Linf3e24a92016-09-19 21:35:16 +0000138 # This function describes what happens when the save button is pressed on
139 # the tenant form. In this case we set the values for the instance that were
140 # entered.
141 def save(self, commit=True):
142 self.instance.creator = self.cleaned_data.get("creator")
143 self.instance.display_message = self.cleaned_data.get(
144 "display_message")
Pingping Linc537fd92017-01-17 20:52:09 -0800145 return super(VPGWCTenantForm, self).save(commit=commit)
Pingping Linf3e24a92016-09-19 21:35:16 +0000146
147
148# Define the admin form for the tenant. This uses a similar structure as the
149# service but uses HelloWorldTenantCompleteForm to change the python behavior.
Pingping Linc537fd92017-01-17 20:52:09 -0800150class VPGWCTenantAdmin(ReadOnlyAwareAdmin):
151 verbose_name = "vPGWC Service Tenant"
152 verbose_name_plural = "vPGWC Service Tenants"
153 form = VPGWCTenantForm
154
Saleil Bhat4f45a4f2017-02-01 17:04:22 -0800155 list_display = ('id', 'backend_status_icon', 'instance', 'display_message', 'image_name')
156 list_display_links = ('backend_status_icon', 'instance', 'display_message', 'id', 'image_name')
Pingping Linf3e24a92016-09-19 21:35:16 +0000157 fieldsets = [(None, {'fields': ['backend_status_text', 'kind',
158 'provider_service', 'instance', 'creator',
159 'display_message'],
160 'classes': ['suit-tab suit-tab-general']})]
161 readonly_fields = ('backend_status_text', 'instance',)
Pingping Linc537fd92017-01-17 20:52:09 -0800162
Pingping Linf3e24a92016-09-19 21:35:16 +0000163
164 suit_form_tabs = (('general', 'Details'),)
165
166 def queryset(self, request):
Pingping Linc537fd92017-01-17 20:52:09 -0800167 return VPGWCTenant.get_tenant_objects_by_user(request.user)
Pingping Linf3e24a92016-09-19 21:35:16 +0000168
169# Associate the admin forms with the models.
170admin.site.register(VPGWCService, VPGWCServiceAdmin)
Pingping Linc537fd92017-01-17 20:52:09 -0800171admin.site.register(VPGWCTenant, VPGWCTenantAdmin)