Matteo Scandolo | 6288d5a | 2017-08-08 13:05:26 -0700 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 15 | from django.contrib import admin |
| 16 | |
| 17 | from services.veg.models import * |
| 18 | from django import forms |
| 19 | from django.utils.safestring import mark_safe |
| 20 | from django.contrib.auth.admin import UserAdmin |
| 21 | from django.contrib.admin.widgets import FilteredSelectMultiple |
| 22 | from django.contrib.auth.forms import ReadOnlyPasswordHashField |
| 23 | from django.contrib.auth.signals import user_logged_in |
| 24 | from django.utils import timezone |
| 25 | from django.contrib.contenttypes import generic |
| 26 | from suit.widgets import LinkedSelect |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame^] | 27 | from core.admin import ServiceAppAdmin,SliceInline,ServiceAttrAsTabInline, ReadOnlyAwareAdmin, XOSTabularInline, ServicePrivilegeInline, SubscriberLinkInline, ProviderLinkInline, ProviderDependencyInline,SubscriberDependencyInline |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 28 | from core.middleware import get_request |
| 29 | |
| 30 | from functools import update_wrapper |
| 31 | from django.contrib.admin.views.main import ChangeList |
| 32 | from django.core.urlresolvers import reverse |
| 33 | from django.contrib.admin.utils import quote |
| 34 | |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 35 | class VEGServiceAdmin(ReadOnlyAwareAdmin): |
| 36 | model = VEGService |
| 37 | verbose_name = "vEG Service" |
| 38 | verbose_name_plural = "vEG Service" |
| 39 | list_display = ("backend_status_icon", "name", "enabled") |
| 40 | list_display_links = ('backend_status_icon', 'name', ) |
| 41 | fieldsets = [(None, {'fields': ['backend_status_text', 'name','enabled','versionNumber', 'description', "view_url", "icon_url", "service_specific_attribute", "node_label"], |
| 42 | 'classes':['suit-tab suit-tab-general']}), |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame^] | 43 | ("backend config", {'fields': [ "url_filter_kind" ], |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 44 | 'classes':['suit-tab suit-tab-backend']}), |
| 45 | ("vEG config", {'fields': ["dns_servers", "docker_image_name", "docker_insecure_registry"], |
| 46 | 'classes':['suit-tab suit-tab-veg']}) ] |
| 47 | readonly_fields = ('backend_status_text', "service_specific_attribute") |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame^] | 48 | inlines = [SliceInline,ServiceAttrAsTabInline,ServicePrivilegeInline, ProviderDependencyInline,SubscriberDependencyInline] |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 49 | |
| 50 | extracontext_registered_admins = True |
| 51 | |
| 52 | user_readonly_fields = ["name", "enabled", "versionNumber", "description"] |
| 53 | |
| 54 | suit_form_tabs =(('general', 'Service Details'), |
| 55 | ('backend', 'Backend Config'), |
| 56 | ('veg', 'vEG Config'), |
| 57 | ('administration', 'Administration'), |
| 58 | #('tools', 'Tools'), |
| 59 | ('slices','Slices'), |
| 60 | ('serviceattrs','Additional Attributes'), |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame^] | 61 | ('servicetenants', 'Dependencies'), |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 62 | ('serviceprivileges','Privileges') , |
| 63 | ) |
| 64 | |
| 65 | suit_form_includes = (('vegadmin.html', 'top', 'administration'), |
| 66 | ) #('hpctools.html', 'top', 'tools') ) |
| 67 | |
| 68 | def get_queryset(self, request): |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame^] | 69 | return VEGService.select_by_user(request.user) |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 70 | |
| 71 | class VEGTenantForm(forms.ModelForm): |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 72 | last_ansible_hash = forms.CharField(required=False) |
| 73 | wan_container_ip = forms.CharField(required=False) |
| 74 | wan_container_mac = forms.CharField(required=False) |
| 75 | |
| 76 | def __init__(self,*args,**kwargs): |
| 77 | super (VEGTenantForm,self ).__init__(*args,**kwargs) |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame^] | 78 | self.fields['owner'].queryset = VEGService.objects.all() |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 79 | if self.instance: |
| 80 | # fields for the attributes |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 81 | self.fields['last_ansible_hash'].initial = self.instance.last_ansible_hash |
| 82 | self.fields['wan_container_ip'].initial = self.instance.wan_container_ip |
| 83 | self.fields['wan_container_mac'].initial = self.instance.wan_container_mac |
| 84 | if (not self.instance) or (not self.instance.pk): |
| 85 | # default fields for an 'add' form |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 86 | self.fields['creator'].initial = get_request().user |
Andrea Campanella | 08c14ca | 2017-03-31 16:13:09 +0200 | [diff] [blame] | 87 | if VEGService.objects.exists(): |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame^] | 88 | self.fields["owner"].initial = VEGService.objects.all()[0] |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 89 | |
| 90 | def save(self, commit=True): |
| 91 | self.instance.creator = self.cleaned_data.get("creator") |
| 92 | self.instance.instance = self.cleaned_data.get("instance") |
| 93 | self.instance.last_ansible_hash = self.cleaned_data.get("last_ansible_hash") |
| 94 | return super(VEGTenantForm, self).save(commit=commit) |
| 95 | |
| 96 | class Meta: |
| 97 | model = VEGTenant |
| 98 | fields = '__all__' |
| 99 | |
| 100 | class VEGTenantAdmin(ReadOnlyAwareAdmin): |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame^] | 101 | list_display = ('backend_status_icon', 'id', ) |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 102 | list_display_links = ('backend_status_icon', 'id') |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame^] | 103 | fieldsets = [ (None, {'fields': ['backend_status_text', 'owner', 'service_specific_id', |
Andrea Campanella | 08c14ca | 2017-03-31 16:13:09 +0200 | [diff] [blame] | 104 | 'wan_container_ip', 'wan_container_mac', 'creator', 'instance', 'last_ansible_hash'], |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 105 | 'classes':['suit-tab suit-tab-general']})] |
Andrea Campanella | 08c14ca | 2017-03-31 16:13:09 +0200 | [diff] [blame] | 106 | readonly_fields = ('backend_status_text', 'service_specific_attribute', 'wan_container_ip', 'wan_container_mac') |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame^] | 107 | inlines = (ProviderLinkInline, SubscriberLinkInline) |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 108 | form = VEGTenantForm |
| 109 | |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame^] | 110 | suit_form_tabs = (('general','Details'), |
| 111 | ('servicelinks','Links'),) |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 112 | |
| 113 | def get_queryset(self, request): |
Andrea Campanella | 2a2df42 | 2017-08-30 16:59:17 +0200 | [diff] [blame^] | 114 | return VEGTenant.select_by_user(request.user) |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 115 | |
| 116 | |
| 117 | admin.site.register(VEGService, VEGServiceAdmin) |
| 118 | admin.site.register(VEGTenant, VEGTenantAdmin) |
| 119 | |