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