Matteo Scandolo | 5b7a5d4 | 2017-08-08 13:05:26 -0700 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
| 16 | |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 17 | from core.admin import ReadOnlyAwareAdmin, SliceInline |
| 18 | from core.middleware import get_request |
| 19 | from core.models import User |
| 20 | |
| 21 | from django import forms |
| 22 | from django.contrib import admin |
| 23 | |
| 24 | from services.vmme.models import * |
| 25 | |
| 26 | class VMMEServiceForm(forms.ModelForm): |
| 27 | |
| 28 | class Meta: |
| 29 | model = VMMEService |
| 30 | fields = '__all__' |
| 31 | |
| 32 | def __init__(self, *args, **kwargs): |
| 33 | super(VMMEServiceForm, self).__init__(*args, **kwargs) |
| 34 | |
| 35 | def save(self, commit=True): |
| 36 | return super(VMMEServiceForm, self).save(commit=commit) |
| 37 | |
| 38 | class VMMEServiceAdmin(ReadOnlyAwareAdmin): |
| 39 | |
| 40 | model = VMMEService |
| 41 | verbose_name = "VMME Service" |
| 42 | verbose_name_plural = "VMME Services" |
| 43 | form = VMMEServiceForm |
| 44 | inlines = [SliceInline] |
| 45 | |
| 46 | list_display = ('backend_status_icon', 'name', 'enabled') |
| 47 | list_display_links = ('backend_status_icon', 'name') |
| 48 | |
| 49 | fieldsets = [(None, { |
| 50 | 'fields': ['backend_status_text', 'name', 'enabled', 'versionNumber', 'description',], |
| 51 | 'classes':['suit-tab suit-tab-general',], |
| 52 | })] |
| 53 | |
| 54 | readonly_fields = ('backend_status_text', ) |
| 55 | user_readonly_fields = ['name', 'enabled', 'versionNumber', 'description',] |
| 56 | |
| 57 | extracontext_registered_admins = True |
| 58 | |
| 59 | suit_form_tabs = ( |
| 60 | ('general', 'VMME Service Details', ), |
| 61 | ('slices', 'Slices',), |
| 62 | ) |
| 63 | |
Saleil Bhat | fcbe1eb | 2017-01-10 16:16:17 -0800 | [diff] [blame] | 64 | suit_form_includes = (('mcordadmin.html', |
| 65 | 'top', |
| 66 | 'administration'),) |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 67 | |
| 68 | def get_queryset(self, request): |
| 69 | return VMMEService.get_service_objects_by_user(request.user) |
| 70 | |
| 71 | admin.site.register(VMMEService, VMMEServiceAdmin) |
| 72 | |
| 73 | class VMMETenantForm(forms.ModelForm): |
| 74 | |
| 75 | class Meta: |
| 76 | model = VMMETenant |
| 77 | fields = '__all__' |
| 78 | |
| 79 | creator = forms.ModelChoiceField(queryset=User.objects.all()) |
| 80 | |
| 81 | def __init__(self, *args, **kwargs): |
| 82 | super(VMMETenantForm, self).__init__(*args, **kwargs) |
| 83 | |
| 84 | self.fields['kind'].widget.attrs['readonly'] = True |
| 85 | self.fields['kind'].initial = "vmme" |
| 86 | |
| 87 | self.fields['provider_service'].queryset = VMMEService.get_service_objects().all() |
| 88 | |
| 89 | if self.instance: |
| 90 | self.fields['creator'].initial = self.instance.creator |
| 91 | self.fields['tenant_message'].initial = self.instance.tenant_message |
Saleil Bhat | 22afba8 | 2017-01-21 23:10:09 -0800 | [diff] [blame] | 92 | self.fields['image_name'].initial = self.instance.image_name |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 93 | |
| 94 | if (not self.instance) or (not self.instance.pk): |
| 95 | self.fields['creator'].initial = get_request().user |
| 96 | if VMMEService.get_service_objects().exists(): |
| 97 | self.fields['provider_service'].initial = VMMEService.get_service_objects().all()[0] |
| 98 | |
| 99 | def save(self, commit=True): |
| 100 | self.instance.creator = self.cleaned_data.get('creator') |
| 101 | self.instance.tenant_message = self.cleaned_data.get('tenant_message') |
Saleil Bhat | 22afba8 | 2017-01-21 23:10:09 -0800 | [diff] [blame] | 102 | self.instance.image_name = self.cleaned_data.get('image_name') |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 103 | return super(VMMETenantForm, self).save(commit=commit) |
| 104 | |
| 105 | |
| 106 | class VMMETenantAdmin(ReadOnlyAwareAdmin): |
| 107 | |
| 108 | verbose_name = "VMME Service Tenant" |
| 109 | verbose_name_plural = "VMME Service Tenants" |
| 110 | |
Saleil Bhat | 22afba8 | 2017-01-21 23:10:09 -0800 | [diff] [blame] | 111 | list_display = ('id', 'backend_status_icon', 'instance', 'tenant_message', 'image_name') |
| 112 | list_display_links = ('backend_status_icon', 'instance', 'tenant_message', 'id', 'image_name') |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 113 | |
| 114 | fieldsets = [(None, { |
Saleil Bhat | 22afba8 | 2017-01-21 23:10:09 -0800 | [diff] [blame] | 115 | 'fields': ['backend_status_text', 'kind', 'provider_service', 'instance', 'creator', 'tenant_message', 'image_name'], |
Saleil Bhat | 1c5593d | 2017-01-06 15:25:57 -0800 | [diff] [blame] | 116 | 'classes': ['suit-tab suit-tab-general'], |
| 117 | })] |
| 118 | |
| 119 | readonly_fields = ('backend_status_text', 'instance',) |
| 120 | |
| 121 | form = VMMETenantForm |
| 122 | |
| 123 | suit_form_tabs = (('general', 'Details'),) |
| 124 | |
| 125 | def get_queryset(self, request): |
| 126 | return VMMETenant.get_tenant_objects_by_user(request.user) |
| 127 | |
| 128 | admin.site.register(VMMETenant, VMMETenantAdmin) |