Pingping Lin | d28ab98 | 2016-08-29 18:42:52 +0000 | [diff] [blame] | 1 | |
| 2 | from core.admin import ReadOnlyAwareAdmin, SliceInline |
| 3 | from core.middleware import get_request |
Yunpeng Zhang | d01f3ce | 2017-07-06 20:06:35 -0400 | [diff] [blame] | 4 | # from core.models import User |
Pingping Lin | d28ab98 | 2016-08-29 18:42:52 +0000 | [diff] [blame] | 5 | from django import forms |
| 6 | from django.contrib import admin |
Yunpeng Zhang | d01f3ce | 2017-07-06 20:06:35 -0400 | [diff] [blame] | 7 | # from services.vbbu.models import * |
| 8 | from synchronizers.new_base.modelaccessor import * |
Pingping Lin | d28ab98 | 2016-08-29 18:42:52 +0000 | [diff] [blame] | 9 | |
| 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 |
| 13 | class MCORDServiceAdmin(ReadOnlyAwareAdmin): |
| 14 | # We must set the model so that the admin knows what fields to use |
| 15 | model = MCORDService |
| 16 | verbose_name = "MCORD Service" |
| 17 | verbose_name_plural = "MCORD Services" |
| 18 | |
| 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. |
| 62 | suit_form_includes = (('mcordadmin.html', |
| 63 | '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): |
| 69 | return MCORDService.get_service_objects_by_user(request.user) |
| 70 | |
| 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 Zhang | d01f3ce | 2017-07-06 20:06:35 -0400 | [diff] [blame] | 76 | class VBBUTenantForm(forms.ModelForm): |
Pingping Lin | d28ab98 | 2016-08-29 18:42:52 +0000 | [diff] [blame] | 77 | # 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 Zhang | d01f3ce | 2017-07-06 20:06:35 -0400 | [diff] [blame] | 84 | super(VBBUTenantForm, self).__init__(*args, **kwargs) |
Pingping Lin | d28ab98 | 2016-08-29 18:42:52 +0000 | [diff] [blame] | 85 | # 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[ |
| 90 | 'provider_service'].queryset = MCORDService.get_service_objects().all() |
| 91 | # 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 |
| 103 | if MCORDService.get_service_objects().exists(): |
| 104 | self.fields["provider_service"].initial = MCORDService.get_service_objects().all()[0] |
| 105 | |
| 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 Zhang | d01f3ce | 2017-07-06 20:06:35 -0400 | [diff] [blame] | 113 | return super(VBBUTenantForm, self).save(commit=commit) |
Pingping Lin | d28ab98 | 2016-08-29 18:42:52 +0000 | [diff] [blame] | 114 | |
| 115 | class Meta: |
Yunpeng Zhang | d01f3ce | 2017-07-06 20:06:35 -0400 | [diff] [blame] | 116 | model = VBBUTenant |
Pingping Lin | d28ab98 | 2016-08-29 18:42:52 +0000 | [diff] [blame] | 117 | fields = '__all__' |
| 118 | |
Pingping Lin | d28ab98 | 2016-08-29 18:42:52 +0000 | [diff] [blame] | 119 | |
| 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 Zhang | d01f3ce | 2017-07-06 20:06:35 -0400 | [diff] [blame] | 123 | class VBBUTenantAdmin(ReadOnlyAwareAdmin): |
Pingping Lin | d28ab98 | 2016-08-29 18:42:52 +0000 | [diff] [blame] | 124 | 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 Zhang | d01f3ce | 2017-07-06 20:06:35 -0400 | [diff] [blame] | 134 | form = VBBUTenantForm |
Pingping Lin | d28ab98 | 2016-08-29 18:42:52 +0000 | [diff] [blame] | 135 | |
| 136 | suit_form_tabs = (('general', 'Details'),) |
| 137 | |
| 138 | def queryset(self, request): |
Yunpeng Zhang | d01f3ce | 2017-07-06 20:06:35 -0400 | [diff] [blame] | 139 | return VBBUTenant.get_tenant_objects_by_user(request.user) |
Pingping Lin | d28ab98 | 2016-08-29 18:42:52 +0000 | [diff] [blame] | 140 | |
| 141 | |
Pingping Lin | d28ab98 | 2016-08-29 18:42:52 +0000 | [diff] [blame] | 142 | # Associate the admin forms with the models. |
| 143 | admin.site.register(MCORDService, MCORDServiceAdmin) |
Yunpeng Zhang | d01f3ce | 2017-07-06 20:06:35 -0400 | [diff] [blame] | 144 | admin.site.register(VBBUTenant, VBBUTenantAdmin) |