Matteo Scandolo | 35113f7 | 2017-08-08 13:05:25 -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 | |
Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 17 | # admin.py - ExampleService Django Admin |
| 18 | |
| 19 | from core.admin import ReadOnlyAwareAdmin, SliceInline |
| 20 | from core.middleware import get_request |
| 21 | from core.models import User |
| 22 | |
| 23 | from django import forms |
| 24 | from django.contrib import admin |
| 25 | |
| 26 | from services.exampleservice.models import * |
| 27 | |
| 28 | class ExampleServiceForm(forms.ModelForm): |
| 29 | |
| 30 | class Meta: |
| 31 | model = ExampleService |
Zack Williams | c631890 | 2016-06-27 08:21:19 -0700 | [diff] [blame] | 32 | fields = '__all__' |
Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 33 | |
| 34 | def __init__(self, *args, **kwargs): |
| 35 | super(ExampleServiceForm, self).__init__(*args, **kwargs) |
| 36 | |
| 37 | if self.instance: |
| 38 | self.fields['service_message'].initial = self.instance.service_message |
| 39 | |
| 40 | def save(self, commit=True): |
| 41 | self.instance.service_message = self.cleaned_data.get('service_message') |
| 42 | return super(ExampleServiceForm, self).save(commit=commit) |
| 43 | |
| 44 | class ExampleServiceAdmin(ReadOnlyAwareAdmin): |
| 45 | |
| 46 | model = ExampleService |
| 47 | verbose_name = SERVICE_NAME_VERBOSE |
| 48 | verbose_name_plural = SERVICE_NAME_VERBOSE_PLURAL |
| 49 | form = ExampleServiceForm |
| 50 | inlines = [SliceInline] |
| 51 | |
| 52 | list_display = ('backend_status_icon', 'name', 'service_message', 'enabled') |
| 53 | list_display_links = ('backend_status_icon', 'name', 'service_message' ) |
| 54 | |
| 55 | fieldsets = [(None, { |
| 56 | 'fields': ['backend_status_text', 'name', 'enabled', 'versionNumber', 'service_message', 'description',], |
| 57 | 'classes':['suit-tab suit-tab-general',], |
| 58 | })] |
| 59 | |
| 60 | readonly_fields = ('backend_status_text', ) |
| 61 | user_readonly_fields = ['name', 'enabled', 'versionNumber', 'description',] |
| 62 | |
| 63 | extracontext_registered_admins = True |
| 64 | |
| 65 | suit_form_tabs = ( |
| 66 | ('general', 'Example Service Details', ), |
| 67 | ('slices', 'Slices',), |
| 68 | ) |
| 69 | |
| 70 | suit_form_includes = (( |
| 71 | 'top', |
| 72 | 'administration'), |
| 73 | ) |
| 74 | |
Zack Williams | 86f0b7f | 2016-06-27 13:21:24 -0700 | [diff] [blame] | 75 | def get_queryset(self, request): |
Scott Baker | 589e6c4 | 2017-05-25 16:55:18 -0700 | [diff] [blame] | 76 | return ExampleService.select_by_user(request.user) |
Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 77 | |
| 78 | admin.site.register(ExampleService, ExampleServiceAdmin) |
| 79 | |
| 80 | class ExampleTenantForm(forms.ModelForm): |
| 81 | |
| 82 | class Meta: |
| 83 | model = ExampleTenant |
Zack Williams | c631890 | 2016-06-27 08:21:19 -0700 | [diff] [blame] | 84 | fields = '__all__' |
Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 85 | |
| 86 | creator = forms.ModelChoiceField(queryset=User.objects.all()) |
| 87 | |
| 88 | def __init__(self, *args, **kwargs): |
| 89 | super(ExampleTenantForm, self).__init__(*args, **kwargs) |
| 90 | |
| 91 | self.fields['kind'].widget.attrs['readonly'] = True |
| 92 | self.fields['kind'].initial = SERVICE_NAME |
| 93 | |
Scott Baker | 589e6c4 | 2017-05-25 16:55:18 -0700 | [diff] [blame] | 94 | self.fields['provider_service'].queryset = ExampleService.objects.all() |
Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 95 | |
| 96 | if self.instance: |
| 97 | self.fields['creator'].initial = self.instance.creator |
| 98 | self.fields['tenant_message'].initial = self.instance.tenant_message |
| 99 | |
| 100 | if (not self.instance) or (not self.instance.pk): |
| 101 | self.fields['creator'].initial = get_request().user |
Scott Baker | 589e6c4 | 2017-05-25 16:55:18 -0700 | [diff] [blame] | 102 | if ExampleService.objects.exists(): |
| 103 | self.fields['provider_service'].initial = ExampleService.objects.all()[0] |
Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 104 | |
| 105 | def save(self, commit=True): |
| 106 | self.instance.creator = self.cleaned_data.get('creator') |
| 107 | self.instance.tenant_message = self.cleaned_data.get('tenant_message') |
| 108 | return super(ExampleTenantForm, self).save(commit=commit) |
| 109 | |
| 110 | |
| 111 | class ExampleTenantAdmin(ReadOnlyAwareAdmin): |
| 112 | |
| 113 | verbose_name = TENANT_NAME_VERBOSE |
| 114 | verbose_name_plural = TENANT_NAME_VERBOSE_PLURAL |
| 115 | |
| 116 | list_display = ('id', 'backend_status_icon', 'instance', 'tenant_message') |
| 117 | list_display_links = ('backend_status_icon', 'instance', 'tenant_message', 'id') |
| 118 | |
| 119 | fieldsets = [(None, { |
| 120 | 'fields': ['backend_status_text', 'kind', 'provider_service', 'instance', 'creator', 'tenant_message'], |
| 121 | 'classes': ['suit-tab suit-tab-general'], |
| 122 | })] |
| 123 | |
| 124 | readonly_fields = ('backend_status_text', 'instance',) |
| 125 | |
| 126 | form = ExampleTenantForm |
| 127 | |
| 128 | suit_form_tabs = (('general', 'Details'),) |
| 129 | |
Zack Williams | 86f0b7f | 2016-06-27 13:21:24 -0700 | [diff] [blame] | 130 | def get_queryset(self, request): |
Scott Baker | 589e6c4 | 2017-05-25 16:55:18 -0700 | [diff] [blame] | 131 | return ExampleTenant.select_by_user(request.user) |
Scott Baker | 619de67 | 2016-06-20 12:49:38 -0700 | [diff] [blame] | 132 | |
| 133 | admin.site.register(ExampleTenant, ExampleTenantAdmin) |
| 134 | |