Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 1 | from django.contrib import admin |
| 2 | |
| 3 | from services.vsg.models import * |
| 4 | from django import forms |
| 5 | from django.utils.safestring import mark_safe |
| 6 | from django.contrib.auth.admin import UserAdmin |
| 7 | from django.contrib.admin.widgets import FilteredSelectMultiple |
| 8 | from django.contrib.auth.forms import ReadOnlyPasswordHashField |
| 9 | from django.contrib.auth.signals import user_logged_in |
| 10 | from django.utils import timezone |
| 11 | from django.contrib.contenttypes import generic |
| 12 | from suit.widgets import LinkedSelect |
| 13 | from core.admin import ServiceAppAdmin,SliceInline,ServiceAttrAsTabInline, ReadOnlyAwareAdmin, XOSTabularInline, ServicePrivilegeInline, TenantRootTenantInline, TenantRootPrivilegeInline |
| 14 | from core.middleware import get_request |
| 15 | |
| 16 | from functools import update_wrapper |
| 17 | from django.contrib.admin.views.main import ChangeList |
| 18 | from django.core.urlresolvers import reverse |
| 19 | from django.contrib.admin.utils import quote |
| 20 | |
| 21 | #----------------------------------------------------------------------------- |
| 22 | # vSG |
| 23 | #----------------------------------------------------------------------------- |
| 24 | |
| 25 | class VSGServiceForm(forms.ModelForm): |
| 26 | bbs_api_hostname = forms.CharField(required=False) |
| 27 | bbs_api_port = forms.IntegerField(required=False) |
| 28 | bbs_server = forms.CharField(required=False) |
| 29 | backend_network_label = forms.CharField(required=False) |
| 30 | bbs_slice = forms.ModelChoiceField(queryset=Slice.objects.all(), required=False) |
| 31 | dns_servers = forms.CharField(required=False) |
| 32 | url_filter_kind = forms.ChoiceField(choices=VSGService.URL_FILTER_KIND_CHOICES, required=False) |
| 33 | node_label = forms.CharField(required=False) |
| 34 | |
| 35 | def __init__(self,*args,**kwargs): |
| 36 | super (VSGServiceForm,self ).__init__(*args,**kwargs) |
| 37 | if self.instance: |
| 38 | self.fields['bbs_api_hostname'].initial = self.instance.bbs_api_hostname |
| 39 | self.fields['bbs_api_port'].initial = self.instance.bbs_api_port |
| 40 | self.fields['bbs_server'].initial = self.instance.bbs_server |
| 41 | self.fields['backend_network_label'].initial = self.instance.backend_network_label |
| 42 | self.fields['bbs_slice'].initial = self.instance.bbs_slice |
| 43 | self.fields['dns_servers'].initial = self.instance.dns_servers |
| 44 | self.fields['url_filter_kind']. initial = self.instance.url_filter_kind |
| 45 | self.fields['node_label'].initial = self.instance.node_label |
| 46 | |
| 47 | def save(self, commit=True): |
| 48 | self.instance.bbs_api_hostname = self.cleaned_data.get("bbs_api_hostname") |
| 49 | self.instance.bbs_api_port = self.cleaned_data.get("bbs_api_port") |
| 50 | self.instance.bbs_server = self.cleaned_data.get("bbs_server") |
| 51 | self.instance.backend_network_label = self.cleaned_data.get("backend_network_label") |
| 52 | self.instance.bbs_slice = self.cleaned_data.get("bbs_slice") |
| 53 | self.instance.dns_servers = self.cleaned_data.get("dns_servers") |
| 54 | self.instance.url_filter_kind = self.cleaned_data.get("url_filter_kind") |
| 55 | self.instance.node_label = self.cleaned_data.get("node_label") |
| 56 | return super(VSGServiceForm, self).save(commit=commit) |
| 57 | |
| 58 | class Meta: |
| 59 | model = VSGService |
Zack Williams | d7ca08f | 2016-06-27 08:32:50 -0700 | [diff] [blame] | 60 | fields = '__all__' |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 61 | |
| 62 | class VSGServiceAdmin(ReadOnlyAwareAdmin): |
| 63 | model = VSGService |
| 64 | verbose_name = "vSG Service" |
| 65 | verbose_name_plural = "vSG Service" |
| 66 | list_display = ("backend_status_icon", "name", "enabled") |
| 67 | list_display_links = ('backend_status_icon', 'name', ) |
| 68 | fieldsets = [(None, {'fields': ['backend_status_text', 'name','enabled','versionNumber', 'description', "view_url", "icon_url", "service_specific_attribute", "node_label"], |
| 69 | 'classes':['suit-tab suit-tab-general']}), |
| 70 | ("backend config", {'fields': [ "backend_network_label", "url_filter_kind", "bbs_api_hostname", "bbs_api_port", "bbs_server", "bbs_slice"], |
| 71 | 'classes':['suit-tab suit-tab-backend']}), |
| 72 | ("vSG config", {'fields': ["dns_servers"], |
| 73 | 'classes':['suit-tab suit-tab-vsg']}) ] |
| 74 | readonly_fields = ('backend_status_text', "service_specific_attribute") |
| 75 | inlines = [SliceInline,ServiceAttrAsTabInline,ServicePrivilegeInline] |
| 76 | form = VSGServiceForm |
| 77 | |
| 78 | extracontext_registered_admins = True |
| 79 | |
| 80 | user_readonly_fields = ["name", "enabled", "versionNumber", "description"] |
| 81 | |
| 82 | suit_form_tabs =(('general', 'Service Details'), |
| 83 | ('backend', 'Backend Config'), |
| 84 | ('vsg', 'vSG Config'), |
| 85 | ('administration', 'Administration'), |
| 86 | #('tools', 'Tools'), |
| 87 | ('slices','Slices'), |
| 88 | ('serviceattrs','Additional Attributes'), |
| 89 | ('serviceprivileges','Privileges') , |
| 90 | ) |
| 91 | |
| 92 | suit_form_includes = (('vcpeadmin.html', 'top', 'administration'), |
| 93 | ) #('hpctools.html', 'top', 'tools') ) |
| 94 | |
Zack Williams | 0e6714d | 2016-06-27 13:11:08 -0700 | [diff] [blame^] | 95 | def get_queryset(self, request): |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 96 | return VSGService.get_service_objects_by_user(request.user) |
| 97 | |
| 98 | class VSGTenantForm(forms.ModelForm): |
| 99 | bbs_account = forms.CharField(required=False) |
| 100 | creator = forms.ModelChoiceField(queryset=User.objects.all()) |
| 101 | instance = forms.ModelChoiceField(queryset=Instance.objects.all(),required=False) |
| 102 | last_ansible_hash = forms.CharField(required=False) |
| 103 | wan_container_ip = forms.CharField(required=False) |
| 104 | wan_container_mac = forms.CharField(required=False) |
| 105 | |
| 106 | def __init__(self,*args,**kwargs): |
| 107 | super (VSGTenantForm,self ).__init__(*args,**kwargs) |
| 108 | self.fields['kind'].widget.attrs['readonly'] = True |
| 109 | self.fields['provider_service'].queryset = VSGService.get_service_objects().all() |
| 110 | if self.instance: |
| 111 | # fields for the attributes |
| 112 | self.fields['bbs_account'].initial = self.instance.bbs_account |
| 113 | self.fields['creator'].initial = self.instance.creator |
| 114 | self.fields['instance'].initial = self.instance.instance |
| 115 | self.fields['last_ansible_hash'].initial = self.instance.last_ansible_hash |
| 116 | self.fields['wan_container_ip'].initial = self.instance.wan_container_ip |
| 117 | self.fields['wan_container_mac'].initial = self.instance.wan_container_mac |
| 118 | if (not self.instance) or (not self.instance.pk): |
| 119 | # default fields for an 'add' form |
| 120 | self.fields['kind'].initial = VCPE_KIND |
| 121 | self.fields['creator'].initial = get_request().user |
| 122 | if VSGService.get_service_objects().exists(): |
| 123 | self.fields["provider_service"].initial = VSGService.get_service_objects().all()[0] |
| 124 | |
| 125 | def save(self, commit=True): |
| 126 | self.instance.creator = self.cleaned_data.get("creator") |
| 127 | self.instance.instance = self.cleaned_data.get("instance") |
| 128 | self.instance.last_ansible_hash = self.cleaned_data.get("last_ansible_hash") |
| 129 | return super(VSGTenantForm, self).save(commit=commit) |
| 130 | |
| 131 | class Meta: |
| 132 | model = VSGTenant |
Zack Williams | d7ca08f | 2016-06-27 08:32:50 -0700 | [diff] [blame] | 133 | fields = '__all__' |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 134 | |
| 135 | class VSGTenantAdmin(ReadOnlyAwareAdmin): |
| 136 | list_display = ('backend_status_icon', 'id', 'subscriber_tenant' ) |
| 137 | list_display_links = ('backend_status_icon', 'id') |
| 138 | fieldsets = [ (None, {'fields': ['backend_status_text', 'kind', 'provider_service', 'subscriber_tenant', 'service_specific_id', # 'service_specific_attribute', |
| 139 | 'wan_container_ip', 'wan_container_mac', 'bbs_account', 'creator', 'instance', 'last_ansible_hash'], |
| 140 | 'classes':['suit-tab suit-tab-general']})] |
| 141 | readonly_fields = ('backend_status_text', 'service_specific_attribute', 'bbs_account', 'wan_container_ip', 'wan_container_mac') |
| 142 | form = VSGTenantForm |
| 143 | |
| 144 | suit_form_tabs = (('general','Details'),) |
| 145 | |
Zack Williams | 0e6714d | 2016-06-27 13:11:08 -0700 | [diff] [blame^] | 146 | def get_queryset(self, request): |
Scott Baker | 761e106 | 2016-06-20 17:18:17 -0700 | [diff] [blame] | 147 | return VSGTenant.get_tenant_objects_by_user(request.user) |
| 148 | |
| 149 | |
| 150 | admin.site.register(VSGService, VSGServiceAdmin) |
| 151 | admin.site.register(VSGTenant, VSGTenantAdmin) |
| 152 | |