blob: 55d6a569e855afd1bc985cb8ce8f992acc9c5a7b [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
7from services.vpgwc.models import VPGWCService, 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 VPGWCServiceAdmin(ReadOnlyAwareAdmin):
13 # We must set the model so that the admin knows what fields to use
14 model = VPGWCService
15 verbose_name = "vPGWC Service"
16 verbose_name_plural = "vPGWC 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', 'vPGWC 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 VPGWCService.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 VPGWCComponentForm(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(VPGWCComponentForm, 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 = VPGWCService.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 VPGWCService.get_service_objects().exists():
103 self.fields["provider_service"].initial = VPGWCService.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(VPGWCComponentForm, self).save(commit=commit)
113
114 class Meta:
115 model = VPGWCComponent
116 fields = '__all__'
117
118
119# Define the admin form for the tenant. This uses a similar structure as the
120# service but uses HelloWorldTenantCompleteForm to change the python behavior.
121class VPGWCComponentAdmin(ReadOnlyAwareAdmin):
122 verbose_name = "vPGWC Component"
123 verbose_name_plural = "vPGWC Components"
124 list_display = ('id', 'backend_status_icon', 'instance', 'display_message')
125 list_display_links = ('backend_status_icon', 'instance', 'display_message',
126 'id')
127 fieldsets = [(None, {'fields': ['backend_status_text', 'kind',
128 'provider_service', 'instance', 'creator',
129 'display_message'],
130 'classes': ['suit-tab suit-tab-general']})]
131 readonly_fields = ('backend_status_text', 'instance',)
132 form = VPGWCComponentForm
133
134 suit_form_tabs = (('general', 'Details'),)
135
136 def queryset(self, request):
137 return VPGWCComponent.get_tenant_objects_by_user(request.user)
138
139# Associate the admin forms with the models.
140admin.site.register(VPGWCService, VPGWCServiceAdmin)
141admin.site.register(VPGWCComponent, VPGWCComponentAdmin)