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