Matteo Scandolo | aca8665 | 2017-08-08 13:05:27 -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 | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 17 | from django.contrib import admin |
| 18 | |
| 19 | from services.vsg.models import * |
| 20 | from django import forms |
| 21 | from django.utils.safestring import mark_safe |
| 22 | from django.contrib.auth.admin import UserAdmin |
| 23 | from django.contrib.admin.widgets import FilteredSelectMultiple |
| 24 | from django.contrib.auth.forms import ReadOnlyPasswordHashField |
| 25 | from django.contrib.auth.signals import user_logged_in |
| 26 | from django.utils import timezone |
| 27 | from django.contrib.contenttypes import generic |
| 28 | from suit.widgets import LinkedSelect |
Scott Baker | 80238f8 | 2017-07-18 16:01:10 -0700 | [diff] [blame] | 29 | from core.admin import ServiceAppAdmin,SliceInline,ServiceAttrAsTabInline, ReadOnlyAwareAdmin, XOSTabularInline, ServicePrivilegeInline, SubscriberLinkInline, ProviderLinkInline, ProviderDependencyInline,SubscriberDependencyInline |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 30 | from core.middleware import get_request |
| 31 | |
| 32 | from functools import update_wrapper |
| 33 | from django.contrib.admin.views.main import ChangeList |
| 34 | from django.core.urlresolvers import reverse |
| 35 | from django.contrib.admin.utils import quote |
| 36 | |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 37 | class VSGServiceAdmin(ReadOnlyAwareAdmin): |
| 38 | model = VSGService |
| 39 | verbose_name = "vSG Service" |
| 40 | verbose_name_plural = "vSG Service" |
| 41 | list_display = ("backend_status_icon", "name", "enabled") |
| 42 | list_display_links = ('backend_status_icon', 'name', ) |
| 43 | fieldsets = [(None, {'fields': ['backend_status_text', 'name','enabled','versionNumber', 'description', "view_url", "icon_url", "service_specific_attribute", "node_label"], |
| 44 | 'classes':['suit-tab suit-tab-general']}), |
Scott Baker | cc9b98d | 2017-03-20 09:42:37 -0700 | [diff] [blame] | 45 | ("backend config", {'fields': [ "url_filter_kind" ], |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 46 | 'classes':['suit-tab suit-tab-backend']}), |
Scott Baker | 8e66d66 | 2016-10-13 13:22:49 -0700 | [diff] [blame] | 47 | ("vSG config", {'fields': ["dns_servers", "docker_image_name", "docker_insecure_registry"], |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 48 | 'classes':['suit-tab suit-tab-vsg']}) ] |
| 49 | readonly_fields = ('backend_status_text', "service_specific_attribute") |
Scott Baker | 80238f8 | 2017-07-18 16:01:10 -0700 | [diff] [blame] | 50 | inlines = [SliceInline,ServiceAttrAsTabInline,ServicePrivilegeInline, ProviderDependencyInline,SubscriberDependencyInline] |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 51 | |
| 52 | extracontext_registered_admins = True |
| 53 | |
| 54 | user_readonly_fields = ["name", "enabled", "versionNumber", "description"] |
| 55 | |
| 56 | suit_form_tabs =(('general', 'Service Details'), |
| 57 | ('backend', 'Backend Config'), |
| 58 | ('vsg', 'vSG Config'), |
| 59 | ('administration', 'Administration'), |
| 60 | #('tools', 'Tools'), |
| 61 | ('slices','Slices'), |
| 62 | ('serviceattrs','Additional Attributes'), |
Scott Baker | 80238f8 | 2017-07-18 16:01:10 -0700 | [diff] [blame] | 63 | ('servicetenants', 'Dependencies'), |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 64 | ('serviceprivileges','Privileges') , |
| 65 | ) |
| 66 | |
| 67 | suit_form_includes = (('vcpeadmin.html', 'top', 'administration'), |
| 68 | ) #('hpctools.html', 'top', 'tools') ) |
| 69 | |
Zack Williams | 0e6714d | 2016-06-27 13:11:08 -0700 | [diff] [blame] | 70 | def get_queryset(self, request): |
Scott Baker | 248ed75 | 2017-05-24 16:40:28 -0700 | [diff] [blame] | 71 | return VSGService.select_by_user(request.user) |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 72 | |
| 73 | class VSGTenantForm(forms.ModelForm): |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 74 | last_ansible_hash = forms.CharField(required=False) |
| 75 | wan_container_ip = forms.CharField(required=False) |
| 76 | wan_container_mac = forms.CharField(required=False) |
| 77 | |
| 78 | def __init__(self,*args,**kwargs): |
| 79 | super (VSGTenantForm,self ).__init__(*args,**kwargs) |
Scott Baker | 80238f8 | 2017-07-18 16:01:10 -0700 | [diff] [blame] | 80 | self.fields['owner'].queryset = VSGService.objects.all() |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 81 | if self.instance: |
| 82 | # fields for the attributes |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 83 | self.fields['last_ansible_hash'].initial = self.instance.last_ansible_hash |
| 84 | self.fields['wan_container_ip'].initial = self.instance.wan_container_ip |
| 85 | self.fields['wan_container_mac'].initial = self.instance.wan_container_mac |
| 86 | if (not self.instance) or (not self.instance.pk): |
| 87 | # default fields for an 'add' form |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 88 | self.fields['creator'].initial = get_request().user |
Scott Baker | cc9b98d | 2017-03-20 09:42:37 -0700 | [diff] [blame] | 89 | if VSGService.objects.exists(): |
Scott Baker | 80238f8 | 2017-07-18 16:01:10 -0700 | [diff] [blame] | 90 | self.fields["owner"].initial = VSGService.objects.all()[0] |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 91 | |
| 92 | def save(self, commit=True): |
| 93 | self.instance.creator = self.cleaned_data.get("creator") |
| 94 | self.instance.instance = self.cleaned_data.get("instance") |
| 95 | self.instance.last_ansible_hash = self.cleaned_data.get("last_ansible_hash") |
| 96 | return super(VSGTenantForm, self).save(commit=commit) |
| 97 | |
| 98 | class Meta: |
| 99 | model = VSGTenant |
Zack Williams | d7ca08f | 2016-06-27 08:32:50 -0700 | [diff] [blame] | 100 | fields = '__all__' |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 101 | |
| 102 | class VSGTenantAdmin(ReadOnlyAwareAdmin): |
Scott Baker | 80238f8 | 2017-07-18 16:01:10 -0700 | [diff] [blame] | 103 | list_display = ('backend_status_icon', 'id', ) |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 104 | list_display_links = ('backend_status_icon', 'id') |
Scott Baker | 80238f8 | 2017-07-18 16:01:10 -0700 | [diff] [blame] | 105 | fieldsets = [ (None, {'fields': ['backend_status_text', 'owner', 'service_specific_id', |
Scott Baker | cc9b98d | 2017-03-20 09:42:37 -0700 | [diff] [blame] | 106 | 'wan_container_ip', 'wan_container_mac', 'creator', 'instance', 'last_ansible_hash'], |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 107 | 'classes':['suit-tab suit-tab-general']})] |
Scott Baker | cc9b98d | 2017-03-20 09:42:37 -0700 | [diff] [blame] | 108 | readonly_fields = ('backend_status_text', 'service_specific_attribute', 'wan_container_ip', 'wan_container_mac') |
Scott Baker | 80238f8 | 2017-07-18 16:01:10 -0700 | [diff] [blame] | 109 | inlines = (ProviderLinkInline, SubscriberLinkInline) |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 110 | form = VSGTenantForm |
| 111 | |
Scott Baker | 80238f8 | 2017-07-18 16:01:10 -0700 | [diff] [blame] | 112 | suit_form_tabs = (('general','Details'), |
| 113 | ('servicelinks','Links'),) |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 114 | |
Zack Williams | 0e6714d | 2016-06-27 13:11:08 -0700 | [diff] [blame] | 115 | def get_queryset(self, request): |
Scott Baker | 248ed75 | 2017-05-24 16:40:28 -0700 | [diff] [blame] | 116 | return VSGTenant.select_by_user(request.user) |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 117 | |
| 118 | |
| 119 | admin.site.register(VSGService, VSGServiceAdmin) |
| 120 | admin.site.register(VSGTenant, VSGTenantAdmin) |
| 121 | |