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