Matteo Scandolo | 6288d5a | 2017-08-08 13:05:26 -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 | |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 17 | from django.contrib import admin |
| 18 | |
| 19 | from services.veg.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 |
| 29 | from core.admin import ServiceAppAdmin,SliceInline,ServiceAttrAsTabInline, ReadOnlyAwareAdmin, XOSTabularInline, ServicePrivilegeInline, TenantRootTenantInline, TenantRootPrivilegeInline |
| 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 | |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 37 | class VEGServiceAdmin(ReadOnlyAwareAdmin): |
| 38 | model = VEGService |
| 39 | verbose_name = "vEG Service" |
| 40 | verbose_name_plural = "vEG 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']}), |
Andrea Campanella | 08c14ca | 2017-03-31 16:13:09 +0200 | [diff] [blame] | 45 | ("backend config", {'fields': ["url_filter_kind"], |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 46 | 'classes':['suit-tab suit-tab-backend']}), |
| 47 | ("vEG config", {'fields': ["dns_servers", "docker_image_name", "docker_insecure_registry"], |
| 48 | 'classes':['suit-tab suit-tab-veg']}) ] |
| 49 | readonly_fields = ('backend_status_text', "service_specific_attribute") |
| 50 | inlines = [SliceInline,ServiceAttrAsTabInline,ServicePrivilegeInline] |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [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 | ('veg', 'vEG Config'), |
| 59 | ('administration', 'Administration'), |
| 60 | #('tools', 'Tools'), |
| 61 | ('slices','Slices'), |
| 62 | ('serviceattrs','Additional Attributes'), |
| 63 | ('serviceprivileges','Privileges') , |
| 64 | ) |
| 65 | |
| 66 | suit_form_includes = (('vegadmin.html', 'top', 'administration'), |
| 67 | ) #('hpctools.html', 'top', 'tools') ) |
| 68 | |
| 69 | def get_queryset(self, request): |
| 70 | return VEGService.get_service_objects_by_user(request.user) |
| 71 | |
| 72 | class VEGTenantForm(forms.ModelForm): |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 73 | last_ansible_hash = forms.CharField(required=False) |
| 74 | wan_container_ip = forms.CharField(required=False) |
| 75 | wan_container_mac = forms.CharField(required=False) |
| 76 | |
| 77 | def __init__(self,*args,**kwargs): |
| 78 | super (VEGTenantForm,self ).__init__(*args,**kwargs) |
| 79 | self.fields['kind'].widget.attrs['readonly'] = True |
Andrea Campanella | 08c14ca | 2017-03-31 16:13:09 +0200 | [diff] [blame] | 80 | self.fields['provider_service'].queryset = VEGService.objects.all() |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 81 | if self.instance: |
| 82 | # fields for the attributes |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [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 |
| 88 | self.fields['kind'].initial = VEG_KIND |
| 89 | self.fields['creator'].initial = get_request().user |
Andrea Campanella | 08c14ca | 2017-03-31 16:13:09 +0200 | [diff] [blame] | 90 | if VEGService.objects.exists(): |
| 91 | self.fields["provider_service"].initial = VEGService.objects.all()[0] |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 92 | |
| 93 | def save(self, commit=True): |
| 94 | self.instance.creator = self.cleaned_data.get("creator") |
| 95 | self.instance.instance = self.cleaned_data.get("instance") |
| 96 | self.instance.last_ansible_hash = self.cleaned_data.get("last_ansible_hash") |
| 97 | return super(VEGTenantForm, self).save(commit=commit) |
| 98 | |
| 99 | class Meta: |
| 100 | model = VEGTenant |
| 101 | fields = '__all__' |
| 102 | |
| 103 | class VEGTenantAdmin(ReadOnlyAwareAdmin): |
| 104 | list_display = ('backend_status_icon', 'id', 'subscriber_tenant' ) |
| 105 | list_display_links = ('backend_status_icon', 'id') |
Andrea Campanella | 08c14ca | 2017-03-31 16:13:09 +0200 | [diff] [blame] | 106 | fieldsets = [ (None, {'fields': ['backend_status_text', 'kind', 'provider_service', 'subscriber_tenant', 'service_specific_id', |
| 107 | 'wan_container_ip', 'wan_container_mac', 'creator', 'instance', 'last_ansible_hash'], |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 108 | 'classes':['suit-tab suit-tab-general']})] |
Andrea Campanella | 08c14ca | 2017-03-31 16:13:09 +0200 | [diff] [blame] | 109 | readonly_fields = ('backend_status_text', 'service_specific_attribute', 'wan_container_ip', 'wan_container_mac') |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 110 | form = VEGTenantForm |
| 111 | |
| 112 | suit_form_tabs = (('general','Details'),) |
| 113 | |
| 114 | def get_queryset(self, request): |
| 115 | return VEGTenant.get_tenant_objects_by_user(request.user) |
| 116 | |
| 117 | |
| 118 | admin.site.register(VEGService, VEGServiceAdmin) |
| 119 | admin.site.register(VEGTenant, VEGTenantAdmin) |
| 120 | |