Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 1 | from django.contrib import admin |
| 2 | |
| 3 | from services.veg.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 | |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 21 | class VEGServiceAdmin(ReadOnlyAwareAdmin): |
| 22 | model = VEGService |
| 23 | verbose_name = "vEG Service" |
| 24 | verbose_name_plural = "vEG 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']}), |
Andrea Campanella | 08c14ca | 2017-03-31 16:13:09 +0200 | [diff] [blame] | 29 | ("backend config", {'fields': ["url_filter_kind"], |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 30 | 'classes':['suit-tab suit-tab-backend']}), |
| 31 | ("vEG config", {'fields': ["dns_servers", "docker_image_name", "docker_insecure_registry"], |
| 32 | 'classes':['suit-tab suit-tab-veg']}) ] |
| 33 | readonly_fields = ('backend_status_text', "service_specific_attribute") |
| 34 | inlines = [SliceInline,ServiceAttrAsTabInline,ServicePrivilegeInline] |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [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 | ('veg', 'vEG Config'), |
| 43 | ('administration', 'Administration'), |
| 44 | #('tools', 'Tools'), |
| 45 | ('slices','Slices'), |
| 46 | ('serviceattrs','Additional Attributes'), |
| 47 | ('serviceprivileges','Privileges') , |
| 48 | ) |
| 49 | |
| 50 | suit_form_includes = (('vegadmin.html', 'top', 'administration'), |
| 51 | ) #('hpctools.html', 'top', 'tools') ) |
| 52 | |
| 53 | def get_queryset(self, request): |
| 54 | return VEGService.get_service_objects_by_user(request.user) |
| 55 | |
| 56 | class VEGTenantForm(forms.ModelForm): |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 57 | last_ansible_hash = forms.CharField(required=False) |
| 58 | wan_container_ip = forms.CharField(required=False) |
| 59 | wan_container_mac = forms.CharField(required=False) |
| 60 | |
| 61 | def __init__(self,*args,**kwargs): |
| 62 | super (VEGTenantForm,self ).__init__(*args,**kwargs) |
| 63 | self.fields['kind'].widget.attrs['readonly'] = True |
Andrea Campanella | 08c14ca | 2017-03-31 16:13:09 +0200 | [diff] [blame] | 64 | self.fields['provider_service'].queryset = VEGService.objects.all() |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 65 | if self.instance: |
| 66 | # fields for the attributes |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [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 |
| 72 | self.fields['kind'].initial = VEG_KIND |
| 73 | self.fields['creator'].initial = get_request().user |
Andrea Campanella | 08c14ca | 2017-03-31 16:13:09 +0200 | [diff] [blame] | 74 | if VEGService.objects.exists(): |
| 75 | self.fields["provider_service"].initial = VEGService.objects.all()[0] |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 76 | |
| 77 | def save(self, commit=True): |
| 78 | self.instance.creator = self.cleaned_data.get("creator") |
| 79 | self.instance.instance = self.cleaned_data.get("instance") |
| 80 | self.instance.last_ansible_hash = self.cleaned_data.get("last_ansible_hash") |
| 81 | return super(VEGTenantForm, self).save(commit=commit) |
| 82 | |
| 83 | class Meta: |
| 84 | model = VEGTenant |
| 85 | fields = '__all__' |
| 86 | |
| 87 | class VEGTenantAdmin(ReadOnlyAwareAdmin): |
| 88 | list_display = ('backend_status_icon', 'id', 'subscriber_tenant' ) |
| 89 | list_display_links = ('backend_status_icon', 'id') |
Andrea Campanella | 08c14ca | 2017-03-31 16:13:09 +0200 | [diff] [blame] | 90 | fieldsets = [ (None, {'fields': ['backend_status_text', 'kind', 'provider_service', 'subscriber_tenant', 'service_specific_id', |
| 91 | 'wan_container_ip', 'wan_container_mac', 'creator', 'instance', 'last_ansible_hash'], |
Andrea Campanella | edfdbca | 2017-02-01 17:33:47 -0800 | [diff] [blame] | 92 | 'classes':['suit-tab suit-tab-general']})] |
Andrea Campanella | 08c14ca | 2017-03-31 16:13:09 +0200 | [diff] [blame] | 93 | 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] | 94 | form = VEGTenantForm |
| 95 | |
| 96 | suit_form_tabs = (('general','Details'),) |
| 97 | |
| 98 | def get_queryset(self, request): |
| 99 | return VEGTenant.get_tenant_objects_by_user(request.user) |
| 100 | |
| 101 | |
| 102 | admin.site.register(VEGService, VEGServiceAdmin) |
| 103 | admin.site.register(VEGTenant, VEGTenantAdmin) |
| 104 | |