Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 1 | from django.contrib import admin |
| 2 | |
| 3 | from services.vrouter.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.models import AddressPool |
| 14 | from core.admin import ServiceAppAdmin,SliceInline,ServiceAttrAsTabInline, ReadOnlyAwareAdmin, XOSTabularInline, ServicePrivilegeInline, AddressPoolInline |
| 15 | from core.middleware import get_request |
| 16 | |
| 17 | from functools import update_wrapper |
| 18 | from django.contrib.admin.views.main import ChangeList |
| 19 | from django.core.urlresolvers import reverse |
| 20 | from django.contrib.admin.utils import quote |
| 21 | |
| 22 | class VRouterServiceForm(forms.ModelForm): |
| 23 | def __init__(self,*args,**kwargs): |
| 24 | super (VRouterServiceForm,self ).__init__(*args,**kwargs) |
| 25 | |
| 26 | def save(self, commit=True): |
| 27 | return super(VRouterServiceForm, self).save(commit=commit) |
| 28 | |
| 29 | class Meta: |
| 30 | model = VRouterService |
Zack Williams | bde22ab | 2016-06-27 12:27:01 -0700 | [diff] [blame] | 31 | fields = '__all__' |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 32 | |
| 33 | class VRouterServiceAdmin(ReadOnlyAwareAdmin): |
| 34 | model = VRouterService |
| 35 | verbose_name = "vRouter Service" |
| 36 | verbose_name_plural = "vRouter Service" |
| 37 | list_display = ("backend_status_icon", "name", "enabled") |
| 38 | list_display_links = ('backend_status_icon', 'name', ) |
| 39 | fieldsets = [(None, {'fields': ['backend_status_text', 'name','enabled','versionNumber', 'description', "view_url", "icon_url", ], |
| 40 | 'classes':['suit-tab suit-tab-general']})] |
| 41 | readonly_fields = ('backend_status_text', ) |
| 42 | inlines = [SliceInline,ServiceAttrAsTabInline,ServicePrivilegeInline,AddressPoolInline] |
| 43 | form = VRouterServiceForm |
| 44 | |
| 45 | extracontext_registered_admins = True |
| 46 | |
| 47 | user_readonly_fields = ["name", "enabled", "versionNumber", "description"] |
| 48 | |
| 49 | suit_form_tabs =(('general', 'vRouter Service Details'), |
| 50 | ('administration', 'Administration'), |
| 51 | ('addresspools', 'Addresses'), |
| 52 | #('tools', 'Tools'), |
| 53 | ('slices','Slices'), |
| 54 | ('serviceattrs','Additional Attributes'), |
| 55 | ('serviceprivileges','Privileges'), |
| 56 | ) |
| 57 | |
| 58 | suit_form_includes = (('vrouteradmin.html', 'top', 'administration'), |
| 59 | ) #('hpctools.html', 'top', 'tools') ) |
| 60 | |
Zack Williams | bde22ab | 2016-06-27 12:27:01 -0700 | [diff] [blame] | 61 | def get_queryset(self, request): |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 62 | return VRouterService.get_service_objects_by_user(request.user) |
| 63 | |
| 64 | class VRouterTenantForm(forms.ModelForm): |
| 65 | public_ip = forms.CharField(required=True) |
| 66 | public_mac = forms.CharField(required=True) |
| 67 | gateway_ip = forms.CharField(required=False) |
| 68 | gateway_mac = forms.CharField(required=False) |
| 69 | cidr = forms.CharField(required=False) |
| 70 | address_pool = forms.ModelChoiceField(queryset=AddressPool.objects.all(),required=False) |
| 71 | |
| 72 | def __init__(self,*args,**kwargs): |
| 73 | super (VRouterTenantForm,self ).__init__(*args,**kwargs) |
| 74 | self.fields['kind'].widget.attrs['readonly'] = True |
| 75 | self.fields['provider_service'].queryset = VRouterService.get_service_objects().all() |
| 76 | if self.instance: |
| 77 | # fields for the attributes |
| 78 | self.fields['address_pool'].initial = self.instance.address_pool |
| 79 | self.fields['public_ip'].initial = self.instance.public_ip |
| 80 | self.fields['public_mac'].initial = self.instance.public_mac |
| 81 | self.fields['gateway_ip'].initial = self.instance.gateway_ip |
| 82 | self.fields['gateway_mac'].initial = self.instance.gateway_mac |
| 83 | self.fields['cidr'].initial = self.instance.cidr |
| 84 | if (not self.instance) or (not self.instance.pk): |
| 85 | # default fields for an 'add' form |
| 86 | self.fields['kind'].initial = VROUTER_KIND |
| 87 | if VRouterService.get_service_objects().exists(): |
| 88 | self.fields["provider_service"].initial = VRouterService.get_service_objects().all()[0] |
| 89 | |
| 90 | def save(self, commit=True): |
| 91 | self.instance.public_ip = self.cleaned_data.get("public_ip") |
| 92 | self.instance.public_mac = self.cleaned_data.get("public_mac") |
| 93 | self.instance.address_pool = self.cleaned_data.get("address_pool") |
| 94 | return super(VRouterTenantForm, self).save(commit=commit) |
| 95 | |
| 96 | class Meta: |
| 97 | model = VRouterTenant |
Zack Williams | edbf95f | 2016-06-27 08:31:44 -0700 | [diff] [blame] | 98 | fields = '__all__' |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 99 | |
| 100 | class VRouterTenantAdmin(ReadOnlyAwareAdmin): |
| 101 | list_display = ('backend_status_icon', 'id', 'subscriber_tenant', 'public_ip' ) |
| 102 | list_display_links = ('backend_status_icon', 'id') |
| 103 | fieldsets = [ (None, {'fields': ['backend_status_text', 'kind', 'provider_service', 'subscriber_tenant', 'subscriber_service', |
| 104 | 'address_pool', 'public_ip', 'public_mac', 'gateway_ip', 'gateway_mac', 'cidr'], |
| 105 | 'classes':['suit-tab suit-tab-general']})] |
| 106 | readonly_fields = ('backend_status_text', 'service_specific_attribute', 'gateway_ip', 'gateway_mac', 'cidr') |
| 107 | form = VRouterTenantForm |
| 108 | |
| 109 | suit_form_tabs = (('general','Details'),) |
| 110 | |
Zack Williams | bde22ab | 2016-06-27 12:27:01 -0700 | [diff] [blame] | 111 | def get_queryset(self, request): |
Scott Baker | 87eb740 | 2016-06-20 17:21:50 -0700 | [diff] [blame] | 112 | return VRouterTenant.get_tenant_objects_by_user(request.user) |
| 113 | |
| 114 | admin.site.register(VRouterService, VRouterServiceAdmin) |
| 115 | admin.site.register(VRouterTenant, VRouterTenantAdmin) |
| 116 | |