blob: 3a02eeb727e09608a4f57a99e6a6138c5b190719 [file] [log] [blame]
Pingping Lind28ab982016-08-29 18:42:52 +00001
2from core.admin import ReadOnlyAwareAdmin, SliceInline
3from core.middleware import get_request
Yunpeng Zhangd01f3ce2017-07-06 20:06:35 -04004# from core.models import User
Pingping Lind28ab982016-08-29 18:42:52 +00005from django import forms
6from django.contrib import admin
Yunpeng Zhangd01f3ce2017-07-06 20:06:35 -04007# from services.vbbu.models import *
8from synchronizers.new_base.modelaccessor import *
Pingping Lind28ab982016-08-29 18:42:52 +00009
10# The class to provide an admin interface on the web for the service.
11# We do only configuration here and don't change any logic because the logic
12# is taken care of for us by ReadOnlyAwareAdmin
Yunpeng Zhange5f2b9c2017-07-30 00:23:38 -040013class VBBUServiceAdmin(ReadOnlyAwareAdmin):
Pingping Lind28ab982016-08-29 18:42:52 +000014 # We must set the model so that the admin knows what fields to use
Yunpeng Zhange5f2b9c2017-07-30 00:23:38 -040015 model = VBBUService
16 verbose_name = "VBBU Service"
17 verbose_name_plural = "VBBU Services"
Pingping Lind28ab982016-08-29 18:42:52 +000018
19 # Setting list_display creates columns on the admin page, each value here
20 # is a column, the column is populated for every instance of the model.
21 list_display = ("backend_status_icon", "name", "enabled")
22
23 # Used to indicate which values in the columns of the admin form are links.
24 list_display_links = ('backend_status_icon', 'name', )
25
26 # Denotes the sections of the form, the fields in the section, and the
27 # CSS classes used to style them. We represent this as a set of tuples, each
28 # tuple as a name (or None) and a set of fields and classes.
29 # Here the first section does not have a name so we use none. That first
30 # section has several fields indicated in the 'fields' attribute, and styled
31 # by the classes indicated in the 'classes' attribute. The classes given
32 # here are important for rendering the tabs on the form. To give the tabs
33 # we must assign the classes suit-tab and suit-tab-<name> where
34 # where <name> will be used later.
35 fieldsets = [(None, {'fields': ['backend_status_text', 'name', 'enabled',
36 'versionNumber', 'description', "view_url"],
37 'classes':['suit-tab suit-tab-general']})]
38
39 # Denotes the fields that are readonly and cannot be changed.
40 readonly_fields = ('backend_status_text', )
41
42 # Inlines are used to denote other models that can be edited on the same
43 # form as this one. In this case the service form also allows changes
44 # to slices.
45 inlines = [SliceInline]
46
47 extracontext_registered_admins = True
48
49 # Denotes the fields that can be changed by an admin but not be all users
50 user_readonly_fields = ["name", "enabled", "versionNumber", "description"]
51
52 # Associates fieldsets from this form and from the inlines.
53 # The format here are tuples, of (<name>, tab title). <name> comes from the
54 # <name> in the fieldsets.
55 suit_form_tabs = (('general', 'MCORD Service Details'),
56 ('administration', 'Components'),
57 ('slices', 'Slices'),)
58
59 # Used to include a template for a tab. Here we include the
60 # helloworldserviceadmin template in the top position for the administration
61 # tab.
Yunpeng Zhange5f2b9c2017-07-30 00:23:38 -040062 suit_form_includes = (('admin.html',
Pingping Lind28ab982016-08-29 18:42:52 +000063 'top',
64 'administration'),)
65
66 # Used to get the objects for this model that are associated with the
67 # requesting user.
68 def queryset(self, request):
Yunpeng Zhange5f2b9c2017-07-30 00:23:38 -040069 return VBBUService.get_service_objects_by_user(request.user)
Pingping Lind28ab982016-08-29 18:42:52 +000070
71# Class to represent the form to add and edit tenants.
72# We need to define this instead of just using an admin like we did for the
73# service because tenants vary more than services and there isn't a common form.
74# This allows us to change the python behavior for the admin form to save extra
75# fields and control defaults.
Yunpeng Zhangd01f3ce2017-07-06 20:06:35 -040076class VBBUTenantForm(forms.ModelForm):
Pingping Lind28ab982016-08-29 18:42:52 +000077 # Defines a field for the creator of this service. It is a dropdown which
78 # is populated with all of the users.
79 creator = forms.ModelChoiceField(queryset=User.objects.all())
80 # Defines a text field for the display message, it is not required.
81 display_message = forms.CharField(required=False)
82
83 def __init__(self, *args, **kwargs):
Yunpeng Zhangd01f3ce2017-07-06 20:06:35 -040084 super(VBBUTenantForm, self).__init__(*args, **kwargs)
Pingping Lind28ab982016-08-29 18:42:52 +000085 # Set the kind field to readonly
86 self.fields['kind'].widget.attrs['readonly'] = True
87 # Define the logic for obtaining the objects for the provider_service
88 # dropdown of the tenant form.
89 self.fields[
Yunpeng Zhange5f2b9c2017-07-30 00:23:38 -040090 'provider_service'].queryset = VBBUService.get_service_objects().all()
Pingping Lind28ab982016-08-29 18:42:52 +000091 # Set the initial kind to HELLO_WORLD_KIND for this tenant.
92 self.fields['kind'].initial = MCORD_KIND
93 # If there is an instance of this model then we can set the initial
94 # form values to the existing values.
95 if self.instance:
96 self.fields['creator'].initial = self.instance.creator
97 self.fields[
98 'display_message'].initial = self.instance.display_message
99
100 # If there is not an instance then we need to set initial values.
101 if (not self.instance) or (not self.instance.pk):
102 self.fields['creator'].initial = get_request().user
Yunpeng Zhange5f2b9c2017-07-30 00:23:38 -0400103 if VBBUService.get_service_objects().exists():
104 self.fields["provider_service"].initial = VBBUService.get_service_objects().all()[0]
Pingping Lind28ab982016-08-29 18:42:52 +0000105
106 # This function describes what happens when the save button is pressed on
107 # the tenant form. In this case we set the values for the instance that were
108 # entered.
109 def save(self, commit=True):
110 self.instance.creator = self.cleaned_data.get("creator")
111 self.instance.display_message = self.cleaned_data.get(
112 "display_message")
Yunpeng Zhangd01f3ce2017-07-06 20:06:35 -0400113 return super(VBBUTenantForm, self).save(commit=commit)
Pingping Lind28ab982016-08-29 18:42:52 +0000114
115 class Meta:
Yunpeng Zhangd01f3ce2017-07-06 20:06:35 -0400116 model = VBBUTenant
Pingping Lind28ab982016-08-29 18:42:52 +0000117 fields = '__all__'
118
Pingping Lind28ab982016-08-29 18:42:52 +0000119
120# Define the admin form for the tenant. This uses a similar structure as the
121# service but uses HelloWorldTenantCompleteForm to change the python behavior.
122
Yunpeng Zhangd01f3ce2017-07-06 20:06:35 -0400123class VBBUTenantAdmin(ReadOnlyAwareAdmin):
Pingping Lind28ab982016-08-29 18:42:52 +0000124 verbose_name = "vBBU Component"
125 verbose_name_plural = "vBBU Components"
126 list_display = ('id', 'backend_status_icon', 'instance', 'display_message')
127 list_display_links = ('backend_status_icon', 'instance', 'display_message',
128 'id')
129 fieldsets = [(None, {'fields': ['backend_status_text', 'kind',
130 'provider_service', 'instance', 'creator',
131 'display_message'],
132 'classes': ['suit-tab suit-tab-general']})]
133 readonly_fields = ('backend_status_text', 'instance',)
Yunpeng Zhangd01f3ce2017-07-06 20:06:35 -0400134 form = VBBUTenantForm
Pingping Lind28ab982016-08-29 18:42:52 +0000135
136 suit_form_tabs = (('general', 'Details'),)
137
138 def queryset(self, request):
Yunpeng Zhangd01f3ce2017-07-06 20:06:35 -0400139 return VBBUTenant.get_tenant_objects_by_user(request.user)
Pingping Lind28ab982016-08-29 18:42:52 +0000140
141
Pingping Lind28ab982016-08-29 18:42:52 +0000142# Associate the admin forms with the models.
Yunpeng Zhange5f2b9c2017-07-30 00:23:38 -0400143admin.site.register(VBBUService, VBBUServiceAdmin)
Yunpeng Zhangd01f3ce2017-07-06 20:06:35 -0400144admin.site.register(VBBUTenant, VBBUTenantAdmin)