blob: 9055b3bb1bbf9fc709f77918ee693d4f21397a0a [file] [log] [blame]
Pingping Linf3e24a92016-09-19 21:35:16 +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
Pingping Lina62b7552017-01-17 20:52:09 -08007from services.vpgwc.models import VPGWCService, VPGWCTenant, MCORD_KIND
8
9class VPGWCServiceForm(forms.ModelForm):
10
11 class Meta:
12 model = VPGWCService
13 fields = '__all__'
14
15 def __init__(self, *args, **kwargs):
16 super(VPGWCServiceForm, self).__init__(*args, **kwargs)
17
18 def save(self, commit=True):
19 return super(VPGWCServiceForm, self).save(commit=commit)
Pingping Linf3e24a92016-09-19 21:35:16 +000020
21# The class to provide an admin interface on the web for the service.
22# We do only configuration here and don't change any logic because the logic
23# is taken care of for us by ReadOnlyAwareAdmin
24class VPGWCServiceAdmin(ReadOnlyAwareAdmin):
25 # We must set the model so that the admin knows what fields to use
26 model = VPGWCService
27 verbose_name = "vPGWC Service"
28 verbose_name_plural = "vPGWC Services"
Pingping Lina62b7552017-01-17 20:52:09 -080029 # Inlines are used to denote other models that can be edited on the same
30 # form as this one. In this case the service form also allows changes
31 # to slices.
32 inlines = [SliceInline]
Pingping Linf3e24a92016-09-19 21:35:16 +000033
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
Pingping Linf3e24a92016-09-19 21:35:16 +000057 extracontext_registered_admins = True
58
59 # Denotes the fields that can be changed by an admin but not be all users
60 user_readonly_fields = ["name", "enabled", "versionNumber", "description"]
61
62 # Associates fieldsets from this form and from the inlines.
63 # The format here are tuples, of (<name>, tab title). <name> comes from the
64 # <name> in the fieldsets.
65 suit_form_tabs = (('general', 'vPGWC Service Details'),
66 ('administration', 'Components'),
67 ('slices', 'Slices'),)
68
69 # Used to include a template for a tab. Here we include the
70 # helloworldserviceadmin template in the top position for the administration
71 # tab.
72 suit_form_includes = (('mcordadmin.html',
73 'top',
74 'administration'),)
75
76 # Used to get the objects for this model that are associated with the
77 # requesting user.
78 def queryset(self, request):
79 return VPGWCService.get_service_objects_by_user(request.user)
80
81# Class to represent the form to add and edit tenants.
82# We need to define this instead of just using an admin like we did for the
83# service because tenants vary more than services and there isn't a common form.
84# This allows us to change the python behavior for the admin form to save extra
85# fields and control defaults.
Pingping Lina62b7552017-01-17 20:52:09 -080086class VPGWCTenantForm(forms.ModelForm):
87
88 class Meta:
89 model = VPGWCTenant
90 fields = '__all__'
Pingping Linf3e24a92016-09-19 21:35:16 +000091 # Defines a field for the creator of this service. It is a dropdown which
92 # is populated with all of the users.
93 creator = forms.ModelChoiceField(queryset=User.objects.all())
94 # Defines a text field for the display message, it is not required.
95 display_message = forms.CharField(required=False)
96
97 def __init__(self, *args, **kwargs):
Pingping Lina62b7552017-01-17 20:52:09 -080098 super(VPGWCTenantForm, self).__init__(*args, **kwargs)
Pingping Linf3e24a92016-09-19 21:35:16 +000099 # Set the kind field to readonly
100 self.fields['kind'].widget.attrs['readonly'] = True
Pingping Lina62b7552017-01-17 20:52:09 -0800101 self.fields['kind'].initial = "vpgwc"
Pingping Linf3e24a92016-09-19 21:35:16 +0000102 # Define the logic for obtaining the objects for the provider_service
103 # dropdown of the tenant form.
104 self.fields[
105 'provider_service'].queryset = VPGWCService.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 VPGWCService.get_service_objects().exists():
119 self.fields["provider_service"].initial = VPGWCService.get_service_objects().all()[0]
Pingping Linf3e24a92016-09-19 21:35:16 +0000120 # This function describes what happens when the save button is pressed on
121 # the tenant form. In this case we set the values for the instance that were
122 # entered.
123 def save(self, commit=True):
124 self.instance.creator = self.cleaned_data.get("creator")
125 self.instance.display_message = self.cleaned_data.get(
126 "display_message")
Pingping Lina62b7552017-01-17 20:52:09 -0800127 return super(VPGWCTenantForm, self).save(commit=commit)
Pingping Linf3e24a92016-09-19 21:35:16 +0000128
129
130# Define the admin form for the tenant. This uses a similar structure as the
131# service but uses HelloWorldTenantCompleteForm to change the python behavior.
Pingping Lina62b7552017-01-17 20:52:09 -0800132class VPGWCTenantAdmin(ReadOnlyAwareAdmin):
133 verbose_name = "vPGWC Service Tenant"
134 verbose_name_plural = "vPGWC Service Tenants"
135 form = VPGWCTenantForm
136
Pingping Linf3e24a92016-09-19 21:35:16 +0000137 list_display = ('id', 'backend_status_icon', 'instance', 'display_message')
Pingping Lina62b7552017-01-17 20:52:09 -0800138 list_display_links = ('backend_status_icon', 'instance', 'display_message', 'id')
Pingping Linf3e24a92016-09-19 21:35:16 +0000139 fieldsets = [(None, {'fields': ['backend_status_text', 'kind',
140 'provider_service', 'instance', 'creator',
141 'display_message'],
142 'classes': ['suit-tab suit-tab-general']})]
143 readonly_fields = ('backend_status_text', 'instance',)
Pingping Lina62b7552017-01-17 20:52:09 -0800144
Pingping Linf3e24a92016-09-19 21:35:16 +0000145
146 suit_form_tabs = (('general', 'Details'),)
147
148 def queryset(self, request):
Pingping Lina62b7552017-01-17 20:52:09 -0800149 return VPGWCTenant.get_tenant_objects_by_user(request.user)
Pingping Linf3e24a92016-09-19 21:35:16 +0000150
151# Associate the admin forms with the models.
152admin.site.register(VPGWCService, VPGWCServiceAdmin)
Pingping Lina62b7552017-01-17 20:52:09 -0800153admin.site.register(VPGWCTenant, VPGWCTenantAdmin)